C Program to Reverse a Linked List

«
»

Linked List is a linear data structure in which nodes are connected with each other in a sequential manner. Each node contains the data and the address of the next node. The last node points to the NULL to terminate the list.

Example: 2 → 4 → 5 → NULL

Reverse Linked List is a linked list created to form a linked list by inverting the links within the list. The first node in the linked list is the last node in the linked list, and the last node is the first node.

Example:
Linked List before reversed:
1 → 2 → 3 → 4 → 5 → NULL

Reversed Linked List:
5 → 4 → 3 → 2 → 1 → NULL

Problem Description

Write a C program to reverse the linked list and also display the reversed linked list.

Problem Solution

To solve this problem, we need to follow the given steps:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

1. Given the head pointer of the linked list.
2. Reverse the order of the linked list by updating the pointer of the node.
3. Update the head of the list.
4. Print the linked list before and after reversing it.
5. Exit.

This problem can be solved by many approaches, but here we are discussing the following approaches:

Method 1: Iterative Approach by Creating a New List

The idea of this concept is to create a new list in reverse order. Create a node and insert it to the beginning of the list and update the head pointer.

Example:
Input:
Given the head of the linked list.
99 → 78 → 31 → 90 → 10 → NULL

Output:
Print the linked list before and after reversing it.

Linked List before reversed:
99 → 78 → 31 → 90 → 10 → NULL

Linked List after reversed:
10 → 90 → 31 → 78 → 99 → NULL

Program/Source Code

Here is a source code of the C Program to reverse a linked list using iterative approach. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1. /*
  2.  * C Program to Reverse a Linked List using iterative
  3.  */
  4. #include<stdio.h>
  5. #include<malloc.h>
  6. /*
  7. * A linked list node
  8. */
  9. struct node
  10. {
  11.     int data;
  12.     struct node* next;
  13. };
  14.  
  15. //Globally initialized head pointer
  16. struct node* head = NULL;
  17.  
  18. //function prototyping
  19. struct node* create_node(int);
  20. void insert_begin(int);
  21. void reverse_list();
  22. void print();
  23. int main()
  24. {
  25.     /* Create some nodes and insert data into them */
  26.     insert_begin(10);
  27.     insert_begin(90);
  28.     insert_begin(31);
  29.     insert_begin(78);
  30.     insert_begin(99);
  31.     printf("Linked List before reversed: \n");
  32.     print();
  33.     reverse_list();
  34.     printf("\nLinked List after reversed: \n");
  35.     print();
  36.     return 0;
  37. }
  38.  
  39. /*
  40. * Creates a new node using the malloc function
  41. */
  42. struct node* create_node(int data)
  43. {
  44.     struct node* new_node = (struct node*) malloc (sizeof(struct node));
  45.     if (new_node == NULL)
  46.     {
  47.         printf("Memory can't be allocated for new node");
  48.         return NULL;
  49.     } 
  50.     else
  51.     {
  52.         new_node -> data = data;
  53.         new_node -> next = NULL;
  54.         return new_node;
  55.     }
  56. }
  57.  
  58. /*
  59. * insert a new node at the beginning of the list
  60. */
  61. void insert_begin(int data)
  62. {
  63.     struct node* new_node = create_node(data);
  64.     if (new_node != NULL)
  65.     {
  66.         new_node -> next = head;
  67.         head = new_node;
  68.     }
  69. }
  70.  
  71. /*
  72. * reverse the linked list
  73. */
  74. void reverse_list()
  75. {
  76.     if (head == NULL)
  77.     {
  78.         return;
  79.     }
  80.     struct node* temp = head;
  81.     struct node* new_head = NULL;
  82.  
  83.     // create new nodes and insert them beginning
  84.     while (temp != NULL)
  85.     {
  86.         struct node* new_node = create_node(temp->data);
  87.         new_node->next = new_head;
  88.         new_head = new_node;
  89.         temp = temp->next;
  90.     }
  91.  
  92.     // update the head with the new head
  93.     head = new_head;
  94. }
  95.  
  96. /*
  97. * prints the linked list
  98. */
  99. void print()
  100. {
  101.     struct node* temp = head;
  102.     while (temp != NULL)
  103.     {
  104.         printf("%d --> ", temp->data);
  105.         temp = temp->next;
  106.     }
  107.     printf("NULL \n");
  108. }
Program Explanation

1. Creating some nodes with data and appending them at the start of the linked list.
2. Print the list before reversing the original list.
3. Iterate through the list from start to end node.
4. On every iteration, create a new node and store the data of the current iterating node.
5. Insert this node at the beginning of the list.
6. In the end, Update the head pointer to the new head pointer.
7. Print the reversed list.

Time Complexity: O(n)
Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.

Space Complexity: O(n)
Another list of the same size is created so the space complexity becomes the factor of n, which is the size of the list.

Program Output
 
Linked List before reversed:
99 → 78 → 31 → 90 → 10 → NULL
 
Linked List after reversed:
10 → 90 → 31 → 78 → 99 → NULL

advertisement
Method 2: Reverse the Linked List using Stack

In this method, the stack is implemented using the array. Stack is a linear data structure that stores the element in LIFO (Last In First Out) manner. Here the main concept is to store the all nodes of the list in Stack and then remove them one by one and insert it into the end of the list.

Program/Source Code

Here is a source code of the C Program to reverse the linked list using stack. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Reverse a Linked List using Stack
  3.  */
  4. #include<stdio.h>
  5. #include<malloc.h>
  6. /*
  7. * A linked list node
  8. */
  9. struct node
  10. {
  11.     int data;
  12.     struct node* next;
  13. };
  14. //Globally initialized head pointer
  15. struct node* head = NULL;
  16.  
  17. //function prototyping
  18. struct node* create_node(int);
  19. void insert_begin(int);
  20. void reverse_list();
  21. void print();
  22. int main()
  23. {
  24.     /* Create some nodes and insert data into them */
  25.     insert_begin(10);
  26.     insert_begin(90);
  27.     insert_begin(31);
  28.     insert_begin(78);
  29.     insert_begin(99);
  30.     printf("Linked List before reversed: \n");
  31.     print();
  32.     reverse_list();
  33.     printf("\nLinked List after reversed: \n");
  34.     print();
  35.     return 0;
  36. }
  37.  
  38. /*
  39. * Creates a new node using the malloc function
  40. */
  41. struct node* create_node(int data)
  42. {
  43.     struct node* new_node = (struct node*) malloc (sizeof(struct node));
  44.     if (new_node == NULL)
  45.     {
  46.         printf("Memory can't be allocated for new node");
  47.         return NULL;
  48.     }
  49.     else
  50.     {
  51.         new_node -> data = data;
  52.         new_node -> next = NULL;
  53.         return new_node;
  54.     }
  55. }
  56.  
  57. /*
  58. * insert a new node at the beginning of the list
  59. */
  60. void insert_begin(int data)
  61. {
  62.     struct node* new_node = create_node(data);
  63.     if (new_node != NULL)
  64.     {
  65.         new_node -> next = head;
  66.         head = new_node;
  67.     }
  68. }
  69.  
  70. /*
  71. * reverse the linked list
  72. */
  73. void reverse_list()
  74. {
  75.     if (head == NULL)
  76.     {
  77.         return;
  78.     }
  79.  
  80.     //create a stack of size of 100
  81.     struct node* stack[100];
  82.     int top = -1;
  83.     struct node* temp = head;
  84.  
  85.     // push list node into the stack
  86.     while (temp != NULL)
  87.     {
  88.         top += 1;
  89.         stack[top] = temp;
  90.         temp = temp->next;
  91.     }
  92.  
  93.     // make a new head node
  94.     head = stack[top];
  95.     temp = stack[top];
  96.  
  97.     // pop the nodes from the stack and insert them at the end of the last node
  98.     while (--top >= 0)
  99.     {
  100.         temp->next = stack[top];
  101.         temp = stack[top];
  102.     }
  103.     // terminate the list
  104.     temp->next = NULL;
  105. }
  106.  
  107. /*
  108. * prints the linked list
  109. */
  110. void print()
  111. {
  112.     struct node* temp = head;
  113.     while (temp != NULL)
  114.     {
  115.         printf("%d --> ", temp->data);
  116.         temp = temp->next;
  117.     }
  118.     printf("NULL \n");
  119. }
Program Explanation

1. Create some list nodes to perform the operations.
2. Print the list before reversing it.
3. Created a stack using the array.
4. Push the list nodes in Stack.
5. Update the head by the node which is at the top of the stack.
6. Remove one by one node from the stack and insert them at the end of the list.
7. Print the reversed list.

Time Complexity: O(n)
Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.

Space Complexity: O(n)
Since we have pushed all the nodes of the list in the stack so here n size of memory is used to store the list.

Program Output
 
Linked List before reversed:
99 → 78 → 31 → 90 → 10 → NULL
 
Linked List after reversed:
10 → 90 → 31 → 78 → 99 → NULL

Method 3: Reverse a Linked List using Three Pointer

The main concept of the iterative approach is to use the three different pointers. These pointers will point to the particular nodes on every iteration and change the address of the nodes.

Iterative Algorithm
Step 1: Take three pointers and initialize them as prev = NULL, next = NULL and curr = head.
Step 2: Iterate through the end of the list and follow the next steps.
Step 3: Store the address of the next node in the next pointer.
Step 4: Update the curr next to the prev pointer.
Step 5: Update the next pointer by curr pointer.
Step 6: Move the curr pointer to the next node.

Program/Source Code

Here is a source code of the C Program to reverse the linked list using three pointer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Reverse a Linked List using three pointer
  3.  */
  4. #include<stdio.h>
  5. #include<malloc.h>
  6. /*
  7. * A linked list node
  8. */
  9. struct node
  10. {
  11.     int data;
  12.     struct node* next;
  13. };
  14. //Globally initialized head pointer
  15. struct node* head = NULL;
  16.  
  17. //function prototyping
  18. struct node*create_node(int);
  19. void insert_begin(int);
  20. void reverse_list();
  21. void print();
  22. int main()
  23. {
  24.     /* Create some nodes and insert data into them */
  25.     insert_begin(10);
  26.     insert_begin(90);
  27.     insert_begin(31);
  28.     insert_begin(78);
  29.     insert_begin(99);
  30.     printf("Linked List before reversed: \n");
  31.     print();
  32.     reverse_list();
  33.     printf("\nLinked List after reversed: \n");
  34.     print();
  35.     return 0;
  36. }
  37.  
  38. /*
  39. * Creates a new node using the malloc function
  40. */
  41. struct node* create_node(int data)
  42. {
  43.     struct node* new_node = (struct node*) malloc (sizeof(struct node));
  44.     if (new_node == NULL)
  45.     {
  46.         printf("Memory can't be allocated for new node");
  47.         return NULL;
  48.     }
  49.     else
  50.     {
  51.         new_node -> data = data;
  52.         new_node -> next = NULL;
  53.         return new_node;
  54.     }
  55. }
  56.  
  57. /*
  58. * insert a new node at the beginning of the list
  59. */
  60. void insert_begin(int data)
  61. {
  62.     struct node* new_node = create_node(data);
  63.     if (new_node != NULL)
  64.     {
  65.         new_node -> next = head;
  66.         head = new_node;
  67.     }
  68. }
  69.  
  70. /*
  71. * reverse the linked list
  72. */
  73. void reverse_list()
  74. {
  75.     struct node* prev = NULL, *curr = head, *next = NULL;
  76.     while (curr != NULL)
  77.     {
  78.         // store the next node
  79.         next = curr -> next;
  80.         // reverse the pointer of the current node
  81.         curr ->next = prev;
  82.         // move prev pointer to the current node
  83.         prev = curr;
  84.         // move current to its next node
  85.         curr = next;
  86.     }
  87.     //update the head pointer by prev pointer
  88.     head = prev;
  89. }
  90.  
  91. /*
  92. * prints the linked list
  93. */
  94. void print()
  95. {
  96.     struct node* temp = head;
  97.     while (temp != NULL)
  98.     {
  99.         printf("%d → ", temp->data);
  100.         temp = temp->next;
  101.     }
  102.     printf("NULL \n");
  103. }
Program Explanation

1. Creating some nodes with data and appending them at the start of the linked list.
2. Print the list before reversing the original list.
3. Take three pointers to update the node’s address.
4. Iterate through the list from start to end node.
5. On every iteration, store the next node of the current node.
6. Point the current node to its previous node.
7. Move the previous and current pointers to their next node.
8. In the end, Update the head pointer to the start node of the reversed list.
9. Print the reversed list.

Time Complexity: O(n)
Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.

Space Complexity: O(1)
There are only three pointers used to reverse the list which is a constant space and it is not depending on the size of the list.

Program Output
 
Linked List before reversed:
99 → 78 → 31 → 90 → 10 → NULL
 
Linked List after reversed:
10 → 90 → 31 → 78 → 99 → NULL

Method 4: Reverse a Linked List using Recursion

In this method, it uses recursive function & reverses the nodes in a linked list and displays the list. A linked list is an ordered set of data elements, each containing a link to its successor. This program makes each data element to link to its predecessor.

Program/Source Code

Here is the source code of the C program to reverse the nodes and display the linked list. The C Program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * Recursive C program to reverse nodes of a linked list and display 
  3.  * them
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int data;
  11.     struct node *next;
  12. };
  13.  
  14. void print_reverse_recursive (struct node *);
  15. void print (struct node *);
  16. void create_new_node (struct node *, int );
  17.  
  18. //Driver Function
  19. int main ()
  20. {
  21.     struct node *head = NULL;
  22.     insert_new_node (&head, 1);
  23.     insert_new_node (&head, 2);
  24.     insert_new_node (&head, 3);
  25.     insert_new_node (&head, 4);
  26.     printf ("LinkedList : ");
  27.     print (head);
  28.     printf ("\nLinkedList in reverse order : ");
  29.     print_reverse_recursive (head);
  30.     printf ("\n");
  31.     return 0;
  32. }
  33.  
  34. //Recursive Reverse
  35. void print_reverse_recursive (struct node *head)
  36. {
  37.     if (head == NULL)
  38.     {
  39.         return;
  40.     }
  41.  
  42.     //Recursive call first
  43.     print_reverse_recursive (head -> next);
  44.     //Print later
  45.     printf ("%d ", head -> data);
  46. }
  47.  
  48. //Print the linkedlist normal
  49. void print (struct node *head)
  50. {
  51.     if (head == NULL)
  52.     {
  53.         return;
  54.     }
  55.     printf ("%d ", head -> data);
  56.     print (head -> next);
  57. }
  58.  
  59. //New data added in the start
  60. void insert_new_node (struct node ** head_ref, int new_data)
  61. {
  62.     struct node * new_node = (struct node *) malloc (sizeof (struct node));
  63.     new_node -> data = new_data;
  64.     new_node -> next = (*head_ref);
  65.     (*head_ref) = new_node;
  66. }

Time Complexity: O(n)
The function is calling itself n times, which is the size of the linked list.

Space Complexity: O(n)
The maximum size of the function call is n, which stores each node of the list on every call onto the stack.

Program Output
LinkedList : 4 3 2 1 
LinkedList in reverse order : 1 2 3 4

Method 5: Head Recursive Approach

Recursive functions use a stack to store the function calls. To solve this problem we recursively iterate the function calls to each node at the end of the list and then start the address to the previous node.

Recursive Algorithm
Step 1: Divide the linked list into two parts, the first part contains the first node, and the second part the remaining list.
Step 2: Call again the reverse_list_recursion() function by passing the head of the remaining list.
Step 3: Merge the remaining list to the first node and change the head pointer of the reversed list.
Step 4: Repeat the above steps until the entire list doesn’t get reversed.

Program/Source Code

Here is a source code of the C Program to reverse the linked list using recursion. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Reverse a Linked List using head recursive approach
  3.  */
  4. #include<stdio.h>
  5. #include<malloc.h>
  6. struct node
  7. {
  8.     int data;
  9.     struct node* next;
  10. };
  11. //Global head pointer
  12. struct node* head = NULL;
  13. //function prototyping
  14. struct node*create_node(int);
  15. void insert_begin(int);
  16. struct node* reverse_recursion(struct node*);
  17. void print();
  18. int main()
  19. {
  20.     /* Create some nodes and insert data into them */
  21.     insert_begin(10);
  22.     insert_begin(90);
  23.     insert_begin(31);
  24.     insert_begin(78);
  25.     insert_begin(99);
  26.     printf("Linked List before reversed: \n");
  27.     print();
  28.     head = reverse_recursion(head);
  29.     printf("\nLinked List after reversed: \n");
  30.     print();
  31.     return 0;
  32. }
  33. struct node* create_node( int data )
  34. {
  35.     struct node* new_node = ( struct node* ) malloc (sizeof(struct node));
  36.     if (new_node == NULL)
  37.     {
  38.         printf("Memory can't be allocated for new node");
  39.         return NULL;
  40.     }
  41.     else
  42.     {
  43.         new_node -> data = data;
  44.         new_node -> next = NULL;
  45.         return new_node;
  46.     }
  47. }
  48. void insert_begin(int data)
  49. {
  50.     struct node* new_node = create_node(data);
  51.     if (new_node != NULL)
  52.     {
  53.         new_node -> next = head;
  54.         head = new_node;
  55.     }
  56. }
  57. struct node* reverse_recursion(struct node* head)
  58. {
  59.     // if only one node or the last node of the list
  60.     if (head == NULL || head->next == NULL)
  61.     {
  62.         return head;
  63.     }
  64.     struct node* new_head = reverse_recursion(head->next);
  65.     //update the next pointer of the current node
  66.     head -> next -> next = head;
  67.     //make the current node the last node
  68.     head -> next = NULL;
  69.     //return the new head node of the reversed list
  70.     return new_head;
  71. }
  72. void print()
  73. {
  74.     struct node* temp = head;
  75.     while (temp != NULL)
  76.     {
  77.         printf("%d → ", temp->data);
  78.         temp = temp->next;
  79.     }
  80.     printf("NULL\n”);
  81. }
Program Explanation

1. Creating some nodes with data and inserting them at the start of the linked list.
2. Print the list before reversing the original list.
3. Call the function passing with the head node of the list.
4. The function checks whether the node is NULL or the last node of the list.
5. Pass the head of the remaining list on the next function call, which is the next node of the current node and stores the address of the returned new head node.
6. Update the next node by pointing it back to the current node.
7. Make the current node the last node.
8. Return new head of the list.
9. Print the reversed list.

Time Complexity: O(n)
The function is calling itself n times, which is the size of the linked list.

Space Complexity: O(n)
The maximum size of the function call is n, which stores each node of the list on every call onto the stack.

Program Output
 
Linked List before reversed:
99 → 78 → 31 → 90 → 10 → NULL
 
Linked List after reversed:
10 → 90 → 31 → 78 → 99 → NULL

Method 6: Tail Recursive Approach

In tail recursive solutions, we have to do operations before calling the recursive function. To reverse the list, call the function each time passing with the next node of the current node and the last node. Point the current node by the last node.

Program/Source Code

Here is a source code of the C Program to reverse the linked list using tail recursive approach. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Reverse a Linked List using tail recursion
  3.  */
  4. #include<stdio.h>
  5. #include<malloc.h>
  6. /*
  7. * A linked list node
  8. */
  9. struct node
  10. {
  11.     int data;
  12.     struct node* next;
  13. };
  14. //Globally initialized head pointer
  15. struct node* head = NULL;
  16. struct node* last = NULL;
  17. //function prototyping
  18. struct node* create_node(int);
  19. void insert_begin(int);
  20. void reverse_list();
  21. void print();
  22. int main()
  23. {
  24.     /* Create some nodes and insert data into them */
  25.     insert_begin(10);
  26.     insert_begin(90);
  27.     insert_begin(31);
  28.     insert_begin(78);
  29.     insert_begin(99);
  30.     printf("Linked List before reversed: \n");
  31.     print();
  32.     reverse_list(head, last);
  33.     printf("\nLinked List after reversed: \n");
  34.     print();
  35.     return 0;
  36. }
  37.  
  38. /*
  39. * Creates a new node using the malloc function
  40. */
  41. struct node* create_node(int data)
  42. {
  43.     struct node* new_node = (struct node*) malloc (sizeof(struct node));
  44.     if (new_node == NULL)
  45.     {
  46.         printf("Memory can't be allocated for new node");
  47.         return NULL;
  48.     }
  49.     else
  50.     {
  51.         new_node -> data = data;
  52.         new_node -> next = NULL;
  53.         return new_node;
  54.     }
  55. }
  56.  
  57. /*
  58. * insert a new node at the beginning of the list
  59. */
  60. void insert_begin(int data)
  61. {
  62.     struct node* new_node = create_node(data);
  63.     if (new_node != NULL)
  64.     {
  65.         new_node -> next = head;
  66.         head = new_node;
  67.     }
  68. }
  69.  
  70. /*
  71. * reverse the linked list
  72. */
  73. void reverse_list(struct node* curr, struct node* last)
  74. {
  75.     if (curr == NULL)
  76.     {
  77.         return;
  78.     }
  79.     struct node* next_node = curr->next;
  80.     curr->next = last;
  81.     last = curr;
  82.  
  83.     //updating the head node
  84.     head = curr;
  85.     reverse_list(next_node, last);
  86. }
  87.  
  88. /*
  89. * prints the linked list
  90. */
  91. void print()
  92. {
  93.     struct node* temp = head;
  94.     while (temp != NULL)
  95.     {
  96.         printf("%d --> ", temp->data);
  97.         temp = temp->next;
  98.     }
  99.     printf("NULL \n");
  100. }
Program Explanation

1. Create some nodes and insert them at the beginning of the list.
2. Print the list before recursion.
3. Store the next node of the current node.
4. Update the next pointer of the current node by the last pointer.
5. Change the last pointer to the current pointer.
6. Update the head of the list by the current pointer.
7. Call again the same function by passing the next node of the current node and the last node.
8. Print the reversed list.

Time Complexity: O(n)
Since the size of the list is n, where is the number of nodes. The traversing of the list is from the start node to the end node which is the size of the list.

Space Complexity: O(n)
We are recursively iterating the list from start to end, which is the size of the list

Program Output
 
Linked List before reversed:
99 → 78 → 31 → 90 → 10 → NULL
 
Linked List after reversed:
10 → 90 → 31 → 78 → 99 → NULL

Method 7: Another Linked List Example using Iterative Approach
Program/Source Code

Here is a source code of the C Program to reverse a linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Reverse a Linked List 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int num;
  10.     struct node *next;
  11. };
  12.  
  13. void create(struct node **);
  14. void reverse(struct node **);
  15. void release(struct node **);
  16. void display(struct node *);
  17.  
  18. int main()
  19. {
  20.     struct node *p = NULL;
  21.     int n;
  22.  
  23.     printf("Enter data into the list\n");
  24.     create(&p);
  25.     printf("Displaying the nodes in the list:\n");
  26.     display(p);
  27.     printf("Reversing the list...\n");
  28.     reverse(&p);
  29.     printf("Displaying the reversed list:\n");
  30.     display(p);
  31.     release(&p);
  32.  
  33.     return 0;
  34. }
  35.  
  36. void reverse(struct node **head)
  37. {
  38.     struct node *p, *q, *r;
  39.  
  40.     p = q = r = *head;
  41.     p = p->next->next;
  42.     q = q->next;
  43.     r->next = NULL;
  44.     q->next = r;
  45.  
  46.     while (p != NULL)
  47.     {
  48.         r = q;
  49.         q = p;
  50.         p = p->next;
  51.         q->next = r;
  52.     }
  53.     *head = q;
  54. }
  55.  
  56. void create(struct node **head)
  57. {
  58.     int c, ch;
  59.     struct node *temp, *rear;
  60.  
  61.     do
  62.     {
  63.         printf("Enter number: ");
  64.         scanf("%d", &c);
  65.         temp = (struct node *)malloc(sizeof(struct node));
  66.         temp->num = c;
  67.         temp->next = NULL;
  68.         if (*head == NULL)
  69.         {
  70.             *head = temp;
  71.         }
  72.         else
  73.         {
  74.             rear->next = temp;
  75.         }
  76.         rear = temp;
  77.         printf("Do you wish to continue [1/0]: ");
  78.         scanf("%d", &ch);
  79.     } while (ch != 0);
  80.     printf("\n");
  81. }
  82.  
  83. void display(struct node *p)
  84. {
  85.     while (p != NULL)
  86.     {
  87.         printf("%d\t", p->num);
  88.         p = p->next;
  89.     }
  90.     printf("\n");
  91. }
  92.  
  93. void release(struct node **head)
  94. {
  95.     struct node *temp = *head;
  96.     *head = (*head)->next;
  97.     while ((*head) != NULL)
  98.     {
  99.         free(temp);
  100.         temp = *head;
  101.         (*head) = (*head)->next;
  102.     }
  103. }
Runtime Test Cases
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 0
 
Displaying the nodes in the list:
1	2	3	4	5	
Reversing the list...
Displaying the reversed list:
5	4	3	2	1

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.