In this tutorial, you will learn how to write C program to reverse a singly linked list.
Below are the approach which we will be follow to write our program:
- To reverse any singly linked list first of all we should have some data in linked list.
- So first we will take some inputs from the users and then we will reverse the linked list.
- To reverse the linked list the logic of program is very simple. Just swap the address of head of linked list with the last node.
- Now after swapping the nodes, last node become head node and head node will become last node. So just traverse it and print it. you will get result in reverse order.
How our program will behave?
Our program will take one node as an input each time and this node will create a linked list.
After taking a node, program will give options to select options what you want to perform.
This program has 4 operations. You can select any one as per your requirement.
This 4 operation are:
- Add value to the list at end, it is to insert node in the list.
- Traverse/View list. It is for showing complete list.
- Reverse the list. This is our main operation which we want to perform in this program. You can select 3 to reverse the linked list.
- to exit the program.
Program to reverse of a singly linked list in C
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
struct node* createNode(){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return (newNode);
}
void insertNodeAtEnd(){
struct node *temp,*ptr;
temp=createNode();
printf("enter the data you want to insert:");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
head=temp;
else{
ptr=head;
while(ptr->next!=NULL){
ptr=ptr->next;
}
ptr->next=temp;
}
}
void viewList(){
struct node* temp=head;
if(temp==NULL){
printf("List is empty. Please insert some data in list n");
}
else{
printf("Our list is : n");
while(temp->next!=NULL)
{
printf("%dt",temp->data);
temp=temp->next;
}
printf("%d t",temp->data);
}
}
void reverseList(){
struct node *prev, *current;
if(head!=NULL){
prev = head;
current = head->next;
head = head->next;
prev->next=NULL;
while(head!=NULL)
{
head=head->next;
current->next=prev;
prev = current;
current=head;
}
head=prev;
}
if(head!=NULL){
printf("after reverse the list is :n");
while(head->next!=NULL)
{
printf("%dt",head->data);
head=head->next;
}
printf("%d t",head->data);
}else{
printf("List is empty. Please insert some data in list n");
}
}
int menu(){
int choice;
printf("n 1.Add value to the list at end");
printf("n 2.Travesre/View List");
printf("n 3.Reverse the list ");
printf("n 4.exit");
printf("n Please enter your choice: t");
scanf("%d",&choice);
return(choice);
}
void main(){
while(1){
switch(menu()){
case 1:
insertNodeAtEnd();
break;
case 2:
viewList();
break;
case 3:
reverseList();
break;
case 4:
exit(0);
default:
printf("invalid choice");
}
getch();
}
}
Output:
Explanation of above program to reverse of a singly linked list in c
- In the above program we have menu() function to print the options of operations each times, and inside main() function we have one switch case statement with 4 cases and one default.
- Also we have 3 functions to perform operations on linked list. insertNodeAtEnd() function is responsible to insert the node in a linked list if already created, otherwise create new Linked List.
- viewList() function is responsible to print each elements of a linked list.
- reverseList() function is responsible to reverse the each elements of a linked list and then print the elements.