1
votes

I've created a structure and function for a doubly linked list. It works flawlessly for integers but now I have to convert it to use characters. I've always had a problem when it comes to characters, also when I put in a character I get a constant loop.

So far I have :

 struct node
{
      struct node *previous;
      char data;
      struct node *next;
}*head, *last; 

 void begin(char value)
 {
     struct node *temp;
 char *var=(char *)malloc(sizeof(char)*100);
 var->data=value;
 if(head==NULL)
 {
     head=var;
     head->previous=NULL;
     head->next=NULL;
     last=head;
 }
 else
 {
     temp=var;
     temp->previous=NULL;
     temp->next=head;
     head->previous=temp;
     head=temp;
 }
} 

I used some examples from my previous push/pop function which used characters but I'm not sure what I'm doing wrong.

Edit: Forgot to put errors> null.c:14: error: request for member `data' in something not a structure or union null.c:17: warning: assignment from incompatible pointer type

3
Do you want the size of the data to be 1 character, or are you trying to make the data Strings of length 100? - burtmacklin16
My menu is made so basically it's supposed to be add 'a' at beginning, add 'b', add 'c' at end, insert before 'c' as 'd', and display. So just 1 char, but look at the post below it fixed errors but didn't return 'a' - Hamas4

3 Answers

3
votes
char *var=(char *)malloc(sizeof(char)*100);

This should be,

struct node *var= malloc( sizeof( struct node ) );
0
votes

That's correct...the type of var is not 'struct node' so you can't do var->data.

0
votes

Just an small question: Are you trying to make a list of single char data or the idea is to make a list of strings (more than one char in every element)?

I`m asking ´cause the "malloc(sizeof(char)*100);" give me the impression you´re trying to save an string with lenght of 100 bytes, but, the function receives only one char. (-: