Up to now, we have only seen one-directional (or singly) linked lists. That is t
ID: 3884517 • Letter: U
Question
Up to now, we have only seen one-directional (or singly) linked lists. That is to say, each node only points to the next one. It is possible for nodes to point to the previous element as well.
In addition to storing data, each node has a previous pointer and a next pointer. The first element of the list has a NULL previous pointer, and the last item (as before) has a NULL next pointer.
Download the files reverse_linked_lists.cpp and RevNode.h, and study the program carefully.
This time Node is a doubly linked node, meaning that it points to the previous as well as the next node.
The append function is also different. It now returns the node it appended. That way we can keep track of the last element. It is simply the one that was appended last
The print method that is provided, printReverse, does exactly that, it starts from the last element and prints all elements in reverse order
Your task is to complete the RevNode.h file, as in the previous exercise, you need to provide a definition for the Node struct, and implement the append function, so that the code compiles and runs without any errors.
How should I write the append function so that it returns a node pointer from the append function correctly?
lirk ist reverse Microsoft Visual C2010 Exoress File Edit vew Praject Debug Taals winda selution Lcalorer link list reversecpp X mainfint argc, enst char ang Global Sape) #1nclude include #include Solutian 'link list reverse projec "stdafx.h" ioslrean > "Revhode.h" Hcoder Filcs us ing nanespace std; targetver.h Resource Hiles 1,Sourco Files oid printRpversP(Node tai1)0 Node" cur - tail stdafxcpp ReadMetxt while (curr-NULL coutExplanation / Answer
Please find my implementation of append method that returns append Node.
Node* append(Node **head, int value) {
// creating new node
Node *newNode = new Node();
// setting up data and link
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
Node *last = *head;
// if list is empty, then newNode is new node
if(*head == NULL) {
*head = newNode;
return *head;
}
// moving 'last' to at last
while(last->next != NULL)
last = last->next;
// appending newNode at last
last->next = newNode;
newNode->prev = last;
return newNode;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.