Interview Content
- C Programming Coding Questions
- C Pattern Programming Questions
- C Programming Interview Questions
- Java Programming Coding Questions
- Java Pattern Programming Questions
- Java Programming Interview Questions
- Python Programming Coding Questions
- Python Pattern Programming Questions
- Python Programming Interview Questions
- SQL Interview Questions
C program to reverse a linked list with explanation
Ans:
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("%d\t",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("%d\t",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.
Also Prepare C interview programs given below
C Questions on number
- Write a program to reverse an integer in C.
- Write a program in C to check whether an integer is Armstrong number or not.
- Write a program in C to print the fibonacci series using recursive method.
- Write a program in C to check whether a number is palindrome or not using recursive method.
- Write a program in C to add two integer without using arithmetic + operator.
C Questions on String
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program