segmentation fault - linked list segfault error. c -
i have write code makes linked list. following code below. problem segfaulting somewhere in code , can't figure out where. if me find , understand (that's important part) segfault coming from, appreciated.
#include "linkedlist.h" #include <stdio.h> #include <stdlib.h> /* alloc new node given data. */ struct listnode* createnode(int data) { struct listnode *new_node = (struct listnode *)malloc(sizeof(struct listnode)); new_node->data = data; new_node->next = null; return new_node; } /* insert data @ appropriate place in sorted list, return new list head. */ struct listnode* insertsorted(struct listnode* head, int inputdata) { struct listnode * nextis = null; struct listnode * newnodeis = null; struct listnode * curris = head; struct listnode * listheadis = curris; if (curris == null) { listheadis = createnode(inputdata); return listheadis; } while (curris->next != null) { nextis = curris->next; if (curris->data < inputdata) { if (nextis->data >= inputdata) { nextis->next = createnode(inputdata); newnodeis = nextis->next; if (newnodeis->data > listheadis->data) { listheadis = newnodeis; } } } curris = curris->next; } return listheadis; } /* remove data list pointed headref, changing head if necessary. * make no assumptions whether list sorted. * memory removed node should freed. * return 1 if data present, 0 if not found. */ int removeitem(struct listnode** headref, int data) { while (*headref && (*headref)->data != data) headref = &(*headref)->next; if (*headref) { struct listnode *tmp = *headref; free(tmp); *headref = tmp->next; return 1; } return 0; } /* insert data @ head of list, return new list head. */ struct listnode* pushstack(struct listnode* head, int data) { int datapush = data; struct listnode * temppush = (struct listnode*)malloc(sizeof(struct listnode)); temppush->data = datapush; temppush->next = head; *head = *temppush; return temppush; } /* remove , return data head of non-empty list, changing head. * memory removed node should freed. */ int popstack(struct listnode** headref) { struct listnode * temppop = *headref; int tempdata; free(temppop); tempdata = temppop->data; temppop = temppop->next; return tempdata; } /* return length of list. */ int listlength(struct listnode* head) { int count = 0; struct listnode* current = head; while (current != null) { count++; current=current->next; } return(count); } /* print list data on single line, separated spaces , ending * newline. */ void printlist(struct listnode* head) { if (head != null) { while (head->next != null) { printf("%d\n", head->data); head = head->next; } } } /* free memory used list. */ void freelist(struct listnode* head) { struct listnode* tmp; while (head != null) { tmp = head; head = head->next; free(tmp); } } /* reverse order of elements in list */ void reverselist(struct listnode** headref) { struct listnode * origrl = *headref; struct listnode * nextrl = null; struct listnode * prevrl = null; while (origrl->next != null); { nextrl = origrl->next; prevrl = origrl; origrl = nextrl; origrl->next = prevrl; } }
the file used test (linkedlist.c) included below comment.
#include <stdio.h> #include <stdlib.h> #include "linkedlist.h" int main(int argc, char** argv) { int i, n; struct listnode* head = null; struct listnode* stack = null; printf("empty list: "); printlist(head); for(i = 0; < 23; ++i) { n = (i*17+11) % 23; head = insertsorted(head, n); stack = pushstack(stack, n); } printf("filled list: "); printlist(head); printf("list length = %d\n", listlength(head)); printf("filled stack: "); printlist(stack); printf("stack size = %d\n", listlength(stack)); for(i = -4; < 25; i+=4) { n = removeitem(&head, i); if(!n) printf("remove did not find %d\n", i); } printf("list after removes: "); printlist(head); printf("list length = %d\n", listlength(head)); for(i = 0; < 5; ++i) { printf("pop: %d\n", popstack(&stack)); } printf("stack after pops: "); printlist(stack); printf("stack size = %d\n", listlength(stack)); reverselist(&head); printf("list after reverse: "); printlist(head); freelist(head); head = null; freelist(stack); stack = null; return 0; }
the expected output is
empty list: filled list: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 list length = 23 filled stack: 17 0 6 12 18 1 7 13 19 2 8 14 20 3 9 15 21 4 10 16 22 5 11 stack size = 23 remove did not find -4 remove did not find 24 list after removes: 1 2 3 5 6 7 9 10 11 13 14 15 17 18 19 21 22 list length = 17 pop: 17 pop: 0 pop: 6 pop: 12 pop: 18 stack after pops: 1 7 13 19 2 8 14 20 3 9 15 21 4 10 16 22 5 11 stack size = 18 list after reverse: 22 21 19 18 17 15 14 13 11 10 9 7 6 5 3 2 1
your segfault happening in pushstack()
function. line *head = *temppush
makes no sense here. when pass in null
pointer head
, attempts dereference it, leading segfault. can remove line, , remove unnecessary variable datapush
. also, there no reason cast result of malloc()
, , better use pointer assigning argument sizeof()
. if type later changed, avoiding using explicit type in sizeof()
means fewer thing remember update. means new pushstack()
function is:
struct listnode * pushstack(struct listnode *head, int data) { struct listnode *temppush = malloc(sizeof(*temppush)); temppush->data = data; temppush->next = head; return temppush; }
your insertsorted()
function contains error. not sure is; function more complicated needs be, wrote new 1 you. similarly, printlist()
function can streamlined. modified printlist()
function match expected output example provided:
struct listnode * insertsorted(struct listnode *head, int inputdata) { struct listnode *prev = null; struct listnode *curr = head; struct listnode *new_node = createnode(inputdata); while (curr) { if (inputdata < curr->data) { new_node->next = curr; if (prev) prev->next = new_node; return (prev ? head : new_node); } prev = curr; curr = curr->next; } if (head) { new_node->next = null; prev->next = new_node; } else { head = new_node; } return head; } void printlist(struct listnode *head) { struct listnode *curr = head; while (curr) { printf("%d ", curr->data); curr = curr->next; } putchar('\n'); }
with these changes, output starting match expectations. there problem in listlength()
function, , there problem in popstack()
function. there may further problems well. let wrestle these issues; suggestions should started. if continue have problems, send me comment , try further.
Comments
Post a Comment