c - View item of linked list -
i want view items of linked list.
i've created 3 items list, , when use below "show_items" function, show first element, , other items can't showed becuase segmentation fault error given compiler.
#include <stdio.h> #include <stdlib.h> struct list{ int age; struct list *next; }; void create_item(int *total_items, struct list *where_is_first_item, struct list *where_is_last_item) { struct list *generic_item; generic_item = malloc(sizeof(struct list)); printf("\nage of item %d: ", (*total_items)+1); scanf("%d", &generic_item->age); if(*total_items == 0){ where_is_first_item->next=generic_item; where_is_last_item->next=generic_item; printf("\nitem created\n"); } else{ where_is_last_item->next=generic_item; printf("\nitem created\n"); } void show_items(int *total_items, struct list *where_is_first_item, struct list *temp){ temp=where_is_first_item->next; int i; for(i=0;i<*total_items;i++){ printf("age of element %d: %d\n", i+1, temp->age); temp=temp->next; } } int main (void){ int total_items=0; struct list *where_is_first_item; where_is_first_item=malloc(sizeof(struct list)); struct list *temp; temp=malloc(sizeof(struct list)); printf("\n\n\tcreate new item\n"); create_item(&total_items, where_is_first_item, where_is_last_item); total_items++; show_items(&total_items, where_is_first_item, temp); return 0; }
when iterating through list, should checking value of temp
make sure isn't null
before trying access it. @ least, should use following:
for ( = 0; < *total_items && temp != null; i++ ) ...
although it's more common iterate through list like
while ( temp != null ) // or while ( temp ) { ... temp = temp->next; }
your usage of temp
seems confused; serve purpose in main
after you've called show_items
? if not, should make local show_items
, not pass argument:
void show_items(int *total_items, struct list *where_is_first_item) { struct list *temp = where_is_first_item->next; ... }
in code you've posted, total_items
initialized 0; written, code shouldn't attempt output anything. make life easier if either post actual code you're having problems with, or if it's big, reduce representative sample of problem.
edit
duh. see problem. you're never setting next
item in generic_item
struct.
so have 2 pointers keep track of head , tail of list, where_is_first_item
, where_is_last_item
. when add first item list, create generic_item
, set head , tail pointers point through respective next
members, giving (using head
, tail
instead of where_is_first_item
, where_is_last_item
brevity):
head generic_item tail +--+--+ +--+--+ +--+--+ | | -+------->| | -+-??? | | -+---+ +--+--+ +--+--+ +--+--+ | ^ | | | +------------------------+
so far good. however, when add second item, update list tail pointer; don't create explicit link first item second, wind following:
head generic_item tail +--+--+ +--+--+ +--+--+ +--+--+ | | -+------->| | -+-??? | | -+-??? | | -+--+ +--+--+ +--+--+ +--+--+ +--+--+ | ^ | | | +------------------------+
hopefully can see problem. next
pointer in first list element never initialized, contains indeterminate value doesn't correspond valid address.
you need step when append new element list:
struct list *pre = where_is_last_item->next; pre->next = generic_item; where_is_last_item->next = generic_item;
that give following:
head pre generic_item tail +--+--+ +--+--+ +--+--+ +--+--+ | | -+------->| | -+------->| | -+-??? | | -+--+ +--+--+ +--+--+ +--+--+ +--+--+ | ^ | | | +------------------------+
as rule, should initialize next
member null
, rather leave indeterminate. it's easy test against null
; it's harder determine validity of non-null
pointer value.
Comments
Post a Comment