Hello,
I'am having trouble with my read_list function in my header file. I know every thing else works, because when I don't use it the program runs fine. What is wrong with it!!!
This is how I'am calling it:
#include <stdio.h>
#include "functionlist.h"
main()
{
list List_a;
printf("Enter a list 1: "
List_a = read_list();
List_a = cons('X',List_a);
printf("%c\n", car(cdr(List_a)));
printf("%c\n", car(cdr(cdr(List_a))));
return 0;
}
/*Here is the header file*/
#include <stdlib.h>
typedef char data;
typedef struct node{
data info;
struct node *next;
}list_node;
typedef list_node list_head;
typedef list_head *list;
data car(list L)
{
return L->next->info;
}
list nullist(void)
{
list L;
L=(list_head*)malloc(sizeof(list_head));
L->next=NULL;
return L;
}
list cdr(list L)
{
list_head *cdr_head;
cdr_head=(list_head*) malloc(sizeof(list_head));
cdr_head->next=L->next->next;
return (cdr_head);
}
list cons(data X, list L)
{
list_node *new_node;
list_head *cons_head;
new_node=(list_node*)malloc(sizeof(list_node));
cons_head=(list_head*)malloc(sizeof(list_node));
new_node->info=X;
cons_head->next=new_node;
new_node->next=L->next;
return (cons_head);
}
list read_list(void) /* here is the problem area */
{
char x;
list L;
x = getchar();
if(x == ')');
return nullist();
else if(isalpha() == 0)
return read_list();
else
return (cons(x,read_list()));
}
/* Thanks!!! */
I'am having trouble with my read_list function in my header file. I know every thing else works, because when I don't use it the program runs fine. What is wrong with it!!!
This is how I'am calling it:
#include <stdio.h>
#include "functionlist.h"
main()
{
list List_a;
printf("Enter a list 1: "
List_a = read_list();
List_a = cons('X',List_a);
printf("%c\n", car(cdr(List_a)));
printf("%c\n", car(cdr(cdr(List_a))));
return 0;
}
/*Here is the header file*/
#include <stdlib.h>
typedef char data;
typedef struct node{
data info;
struct node *next;
}list_node;
typedef list_node list_head;
typedef list_head *list;
data car(list L)
{
return L->next->info;
}
list nullist(void)
{
list L;
L=(list_head*)malloc(sizeof(list_head));
L->next=NULL;
return L;
}
list cdr(list L)
{
list_head *cdr_head;
cdr_head=(list_head*) malloc(sizeof(list_head));
cdr_head->next=L->next->next;
return (cdr_head);
}
list cons(data X, list L)
{
list_node *new_node;
list_head *cons_head;
new_node=(list_node*)malloc(sizeof(list_node));
cons_head=(list_head*)malloc(sizeof(list_node));
new_node->info=X;
cons_head->next=new_node;
new_node->next=L->next;
return (cons_head);
}
list read_list(void) /* here is the problem area */
{
char x;
list L;
x = getchar();
if(x == ')');
return nullist();
else if(isalpha() == 0)
return read_list();
else
return (cons(x,read_list()));
}
/* Thanks!!! */