Linked List Problems
Linked List Problems Linked List Problems
32three to get the next node.*/struct node* middle = *headRef;struct node* front = middle->next;struct node* back = NULL;// the main pointer// the two other pointers (NULL ok)while (1) {middle->next = back;if (front == NULL) break;// fix the middle node// test if done}back = middle;middle = front;front = front->next;// advance the three pointers}}*headRef = middle;// fix the head pointer to point to the new front18 — RecursiveReverse() SolutionProbably the hardest part is accepting the concept that theRecursiveReverse(&rest) does in fact reverse the rest. Then then there's a trickto getting the one front node all the way to the end of the list. Make a drwaing to see howthe trick works.void RecursiveReverse(struct node** headRef) {struct node* first;struct node* rest;if (*headRef == NULL) return;// empty list base casefirst = *headRef; // suppose first = {1, 2, 3}rest = first->next; // rest = {2, 3}if (rest == NULL) return;RecursiveReverse(&rest);// empty rest base case// Recursively reverse the smaller {2, 3} case// after: rest = {3, 2}first->next->next = first; // put the first elem on the end of the listfirst->next = NULL; // (tricky step -- make a drawing)}*headRef = rest;// fix the head pointer
33AppendixBasic Utility Function ImplementationsHere is the source code for the basic utility functions.Length()// Return the number of nodes in a listint Length(struct node* head) {int count = 0;struct node* current = head;while (current != NULL) {count++;current=current->next;}}return(count);Push()// Given a reference (pointer to pointer) to the head// of a list and an int, push a new node on the front of the list.// Creates a new node with the int, links the list off the .next of the// new node, and finally changes the head to point to the new node.void Push(struct node** headRef, int newData) {struct node* newNode = malloc(sizeof(struct node)); // allocate nodenewNode->data = newData;// put in the datanewNode->next = (*headRef); // link the old list off the new node(*headRef) = newNode;// move the head to point to the new node}BuildOneTwoThree()// Build and return the list {1, 2, 3}struct node* BuildOneTwoThree() {struct node* head = NULL; // Start with the empty listPush(&head, 3); // Use Push() to add all the dataPush(&head, 2);Push(&head, 1);}return(head);
- Page 6 and 7: 6pointers. See problem #8 and its s
- Page 8 and 9: 87) Build — Local ReferencesFinal
- Page 10 and 11: 10void GetNthTest() {List myList =
- Page 12 and 13: 12Pop()/*The opposite of Push(). Ta
- Page 14 and 15: 14// Append 'b' onto the end of 'a'
- Page 16 and 17: 16SortedMerge() should return the n
- Page 18 and 19: 1818 — RecursiveReverse()(This pr
- Page 20 and 21: 203 — DeleteList() SolutionDelete
- Page 22 and 23: 22// Local references strategy for
- Page 24: 24*backRef = NULL;}else {int hopCou
- Page 27 and 28: 27}return(dummy.next);SuffleMerge()
- Page 29 and 30: 29}}tail = tail->next;}return(dummy
- Page 31: 31while (a!=NULL && b!=NULL) {if (a
33AppendixBasic Utility Function ImplementationsHere is the source code for the basic utility functions.Length()// Return the number of nodes in a listint Length(struct node* head) {int count = 0;struct node* current = head;while (current != NULL) {count++;current=current->next;}}return(count);Push()// Given a reference (pointer to pointer) to the head// of a list and an int, push a new node on the front of the list.// Creates a new node with the int, links the list off the .next of the// new node, and finally changes the head to point to the new node.void Push(struct node** headRef, int newData) {struct node* newNode = malloc(sizeof(struct node)); // allocate nodenewNode->data = newData;// put in the datanewNode->next = (*headRef); // link the old list off the new node(*headRef) = newNode;// move the head to point to the new node}BuildOneTwoThree()// Build and return the list {1, 2, 3}struct node* BuildOneTwoThree() {struct node* head = NULL; // Start with the empty listPush(&head, 3); // Use Push() to add all the dataPush(&head, 2);Push(&head, 1);}return(head);