c - Struggling to form a linked list -
i working on problem involves creating inventory of supermarket stock, , i'm having considerable difficulties. program supposed prompt user input (item name, quantity, weight, , price) , add item sorted (by increasing alphabetical order) database.
so idea create linked list. i've started defining following:
typedef struct item_t item; struct item_t{ char name; int weight; int price; int quantity; item *next; };
so idea i'm going every item has details stored in structure type, , *next point next structure in linked list.
next came this:
void add_new_node_at_end(char *user_input){ new_node = (*item_t)malloc(sizeof(item_t)) if (new_node == null){ printf("memory failure"); exit(exit_failure); } }
is correct far?
now, i'm not sure pointer *next when create new node, nor know how change fields of structure. can use new_node.item = 'string'
or need malloc field names well?
sorry, might silly question need point me in right direction.
you need 1 global root pointer (item_t* root
) , initialize null
start of list. single-linked list, point next pointer of new element element pointed @ root, , change root point newly created element.
by doing this, create daisy-chain of elements. end reached when item.next == null
.
Comments
Post a Comment