C Programming - Insert value into singly linked list after specific node -
i have program have been sweating on , keep getting segmentation fault on function insertafter. given base code , asked create serveral functions. have gotten of them work, cannot insertafter insert value after specified node. haven't worked on insertbefore, assuming have same problem
so, here code: (i have included header node , function creates new node well)
struct lnode { int data; struct lnode *next; }; struct lheader { struct lnode *start; int len; }; struct lnode *makenode( int val ) { struct lnode *box; box = malloc( sizeof( struct lnode ) ); box->data = val; box->next = null; return box; }
here function:
void insertafter( struct lheader *l, struct lnode *p ) { int pos, value; struct lnode *nn; struct lnode *temp; temp = p; printf( "what number want insert? " ); scanf( "%d", &value ); printf( "insert after value: " ); scanf( "%d", &pos ); nn = makenode(value); if ( l->start == null ) { l->start = nn; } else { temp = l->start; while( temp->next != null && temp->data != pos ) { temp = temp->next; } if ( temp->data == pos ) { nn->next = temp->next; temp->next = nn; printf("value %d: ", nn->data); } else { printf( "value %d not in list\n", pos ); } } }
i added in wrong spot, think!
thanks input. had pick kids , not program.
here the main function, print functions main calls. commented out of other functions.
void printlist( struct lnode *front ) { struct lnode *mov; mov = front; while (mov != null) { printf("%d ", mov->data); mov = mov->next; } printf("\n"); } void printer( struct lheader *alist ) { struct lnode *mov; printf("--------------------------\n"); printf("list print, len %d\n", alist->len); printlist( alist->start ); printf("--------------------------\n"); } int main() { struct lheader *l; struct lnode *head, *tmp; struct lnode *mark; int i, x; l = makelist(); (i = 1; <= 5; ++i) { x = rand() % 25 + 1; printf("-- adding -- %d\n", x); //insertfront( l, x ); insertback( l, x, ); printer( l ); } printf(">>>>value search: "); scanf("%d", &x); = isinlist(l, x); printf("i %d\n", i); tmp = findnode(l, x); if (tmp == null) { printf("nope\n"); { else { printf("found node %d\n", tmp->data); { insertafter( l, mark ); // printer( l ); // insertbefore( l, mark ); // printer( l ); return 0; }
i tried debugger (first time) , said segmentation fault @ temp = temp->next in below snippet of code:
else { temp = l->start; while( temp->next != null && temp->data != pos ) { temp = temp->next; } if ( temp->data == pos ) { nn->next = temp->next; temp->next = nn; printf("value %d: ", nn->data); } else { printf( "value %d not in list\n", pos ); } }
}
you can check above conditions this:
- value: data inserted.
- loc: location after data inserted.
void insertafter(int value,int loc) { struct node* newnode; newnode=(struct node*)malloc(sizeof(struct node)); newnode->data=value; if(head==null) { newnode->next=null; head=newnode; } else { struct node* temp=head; while(temp->next!=null) { if(temp->data==loc) { newnode->next=temp->next; temp->next=newnode; break; } else { temp=temp->next; } } if(temp->next==null) printf("location not found"); } printf("new node inserted after %d",loc); }
Comments
Post a Comment