c - PrintList function is not working properly -


i'm working on list program course , seems working correctly far, thing acting weird printlist function. let's compile program , push 1 number (e.g. 4) in first node , print list, output is: [4]->[0]->null if didn't add node 0 data. sorry in advance if stupid question, here code wrote:

#include <stdio.h> #include <stdlib.h>  struct node{     int data;     struct node *next; };  typedef struct node node;  void printlist(node *); void push(node **, int ); void insert(node *, int ); void append(node **, int ); void delete(node **, int ); void reverse(node **);  int main(){     node *head, *temp;      head=malloc(sizeof(node *));     if(head==null){         puts("cannot allocate memory");     }      int i, t;     int x;     while(1){         printf("insert choice:\n1. print list\n2. push\n3. insert\n4.      append\n5. delete\n6. reverse\n\nenter other number exit.");         scanf("%d", &x);         switch(x){             case 1:                  printlist(head);                 break;             case 2:                 printf("\ninsert data push: ");                 scanf("%d", &i);                  push(&head, i);                 printlist(head);                 break;             case 3:                 printf("\ninsert data insert: ");                 scanf("%d", &i);                  printf("\ninsert data of previous node: ");                 scanf("%d", &t);                  temp=head;                 while(temp->data!=t){                     temp=temp->next;                 }                  insert(temp, i);                 printlist(head);                 break;             case 4:                 printf("\ninsert data append: ");                 scanf("%d", &i);                  append(&head, i);                 printlist(head);                 break;             case 5:                 printf("\ninsert value of node delete: ");                 scanf("%d", &i);                  delete(&head, i);                 printlist(head);                 break;             case 6:                 reverse(&head);                 break;             default:                 printf("\nexiting program.");                 exit(1);                 break;         }     }     return 0; }  void printlist(node *n){     while(n!=null){         printf("[%d]->", n->data);         n=n->next;     }     printf("null\n\n");     return; }  void push(node **headptr, int d){     node *newnode;      newnode=malloc(sizeof(node *));     if(newnode==null){         puts("\ncannot allocate memory\n");     }      newnode->next=(*headptr);     newnode->data=d;     (*headptr)=newnode;     return; }  void insert(node *prev, int d){     node *newnode;      newnode=malloc(sizeof(node *));     if(newnode==null){         puts("\ncannot allocate memory\n");     }      newnode->next=prev->next;     newnode->data=d;     prev->next=newnode;     return; }  void append(node **headptr, int d){     node *newnode, *cursor;      newnode=malloc(sizeof(node *));     if(newnode==null){         puts("\ncannot allocate memory\n");     }      newnode->next=null;     newnode->data=d;      if((*headptr)==null){         (*headptr)=newnode;         return;     }     else{         cursor=(*headptr);         while(cursor->next!=null){             cursor=cursor->next;         }          cursor->next=newnode;         return;     } }  void delete(node **headptr, int d){     if((*headptr)==null){         puts("list empty");         return;     }     else{         node *temp=null, *cursor;         cursor=(*headptr);          while(cursor->data!=d){             temp=cursor;             cursor=cursor->next;         }          temp->next=cursor->next;         free(cursor);          return;     } }  void reverse(node **headptr){     if((*headptr)==null){         puts("list empty");         return;     }     else{         node *prev=null, *current=(*headptr), *next;          while(current!=null){             next=current->next;             current->next=prev;             prev=current;             current=next;         }          (*headptr)=prev;         return;     } } 

edit: feel free give me suggestion code.

two problems

the first 1 pointed @bluepixy, reserving space wrong size:

malloc(sizeof(node *)); /* size of pointer object */ 

should be

malloc(sizeof(node)) /* size of object */ 

and in first call push() assigning address of uninitialized pointer:

newnode->next=(*headptr); /* (*headptr) contains garbage @ point */ 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -