
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















And the second is:
head->data = 1; // setup first node head->next = second; // note: pointer assignment rule second->data = 2; // setup second node second->next = third; third->data = 3; // setup third link third->next = NULL; // At this point, the linked list referenced by "head" // matches the list in the drawing. return head; } This function demonstrates how calls to malloc() and pointer assignments (=) work to build a pointer structure in the heap.
3-Step Link In Drawing
Wrong Push() function void WrongPush(struct node* head, int data) { struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = head; head = newNode; // NO this line does not work! } void WrongPushTest() { List head = BuildTwoThree(); WrongPush(head, 1); }
void Push(struct node** headRef, int data) { struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = headRef; // The '' to dereferences back to the real head headRef = newNode; // ditto } void PushTest() { struct node head = BuildTwoThree();// suppose this returns the list {2, 3} Push(&head, 1); // note the & Push(&head, 13); // head is now the list {13, 1, 2, 3} }
/* Given a linked list head pointer, compute and return the number of nodes in the list. / int Length(struct node head) { struct node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; }
Pass The List By Passing The Head Pointer