Consider the attached C source file (sltcll.c) containing part of the implementation of a dynamic (can grow and shrink d
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Consider the attached C source file (sltcll.c) containing part of the implementation of a dynamic (can grow and shrink d
Consider the attached C source file (sltcll.c) containing part of the implementation of a dynamic (can grow and shrink during run-time) linked list. Compose your answers to following questions (based on sltcll.c) and submit as a single PDF document. a) Explain the working (logic) of the insert (struct node* insert(int d, struct node* h)) function in your words. b) Explain the working (logic) of the display (insertdisplayLL(struct node* h)) function in your words.
18:57 { | ... 100% #include //this pre-processor directive is required for malloc //create the node user-defined data structure struct node { int data; }; //function prototpe for inserting a new node to the linked list struct node* insert(int d, struct node* h); //function prototpe for displaying the linked list void displayLL (struct node* h); int main() { struct node* link;: 11/ linked list struct node* head=NULL; } //call insert to add a node to the linked list head=insert(12, head); head=insert(23, head); head-insert(21, head); insert (-3, head); insert (123, head); insert(-23, head); //create the head pointer of the return 0; struct node* insert(int d, struct node* h) { //display the linked list displayLL (head); //create a pointer variable to loop through the linked list struct node* loop=NULL; //create a pointer variable to keep the end node linked list struct node* prev=NULL; //create new pointer to point to a "new block" of memory that can hold a struct node struct node* new-malloc(sizeof(struct node)); } %d\n", (*new).data); (*new).link=NULL; //printf("\nnew's link: %p\n", (*new).link); //set data and the link (*new).data=d; //printf("\nnew's data: //check for head node(=if there are no nodes in the list) } data: %d\n", (*head).data); printf("\nhead's link: %p\n", (*head).link); if (h==NULL) { }else{ list to find the tail end loop=h; } h=new; //printf("\nhead's //step to next node } //copy the new node's memory address to end (=tail) node's link (*prev).link=new; //loop through the linked while(loop!=NULL) { prev=loop; loop=(*loop).link; void displayLL(struct node* h) { //create a pointer variable to loop through the linked list struct node* loop=NULL; loop=h; } return h; printf("\n"); printf(" %p |-->", (*loop).link); loop=(*loop) .link; while(loop!=NULL) { printf(" %d ", (*loop).data);