C Exercises: Create a singly linked list and print it in reverse orderLast update on December 20 2021 09:10:21 (UTC/GMT +8 hours)Reverse a linked listGiven pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing the links between nodes. Show Examples:
Required knowledgeBasic C programming, Functions, Singly Linked List, Dynamic memory allocation Algorithm to reverse a Singly Linked ListAlgorithm to reverse a Singly Linked List
%%Input : head node of the linked list
Begin:
If (head != NULL) then
prevNode ← head
head ← head.next
curNode ← head
prevNode.next ← NULL
While (head != NULL) do
head ← head.next
curNode.next ← prevNode
prevNode ← curNode
curNode ← head
End while
head ← prevNode
End if
End C program to display numbers in reverse order using single linked listCServer Side ProgrammingProgramming Linked lists use dynamic memory allocation and are collection of nodes. Nodes have two parts which are data and link. C program to display a Linked List in ReverseDisplay a linked list in reverse: Here, we are going to implement a C program to display the linked list in reverse using recursion. Problem statement: Write a program to display the linked list in reverse order. Note that the original linked list will not change. Solution
Displaying in reverse order can be done using recursive function. Function reverse_display(node) IF (node!= NULL) reverse_display(node->next) display node value END IFExample with Explanation: Let's check how the program runs... Let the input linked list to be: 1->2->3->4->NULL with head at 1 In Main() it calls reverse_display(1) ---------------------------------------------------------------- reverse_display(1): node is not null reverse_display(1->next) thus it calls reverse_display(2) ---------------------------------------------------------------- reverse_display(2): node is not null reverse_display(2->next) thus it calls reverse_display(3) ---------------------------------------------------------------- reverse_display(3): node is not null reverse_display(3->next) thus it calls reverse_display(4) ---------------------------------------------------------------- reverse_display(4): node is not null reverse_display(4->next) thus it calls reverse_display(NULL) ---------------------------------------------------------------- reverse_display(NULL): node is null no further call, control returned to reverse_display(4) ---------------------------------------------------------------- At reverse_display(4) Control returned from reverse_display(4->next) So it prints the node value that is 4 Control returns to reverse_display(3) ---------------------------------------------------------------- At reverse_display(3) Control returned from reverse_display(3->next) So it prints the node value that is 3 Control returns to reverse_display(2) ---------------------------------------------------------------- At reverse_display(2) Control returned from reverse_display(2->next) So it prints the node value that is 2 Control returns to reverse_display(1) ---------------------------------------------------------------- At reverse_display(1) Control returned from reverse_display(1->next) So it prints the node value that is 1 Control returns to Main function Thus it displays 4 3 2 1ADVERTISEMENT |