How to reverse singly Linklist..

Here we  write a program of a linklist in which we create some function for  insert the element from the starting of the list , insert the elements at the end of the list , delete the element from the starting of the list , delete the elements from the end of the list, display the list of the elements . and the most important reverse function for reverse the linklist.

#include<stdio.h>
#include”stdlib.h
struct node
{
int data;
struct node *next;
};

struct node *start=NULL;

// function for createNode

struct node * createNode()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
return temp;
}

// function for insert the at the first

void insert_first()
{
struct node *temp;
temp=createNode();
printf(“Enter the data to be insertedn”);
scanf(“%d”,&temp->data);
temp->next=start;
start=temp;
}

// function for inset the element in the end of the list;
void insert_end()
{
struct node *temp;
temp=createNode();
printf(“Enter the element to be insertedn”);
scanf(“%d”,&temp->data);
if(start==NULL)
{
temp->next=start;
start=temp;
}
else
{
struct node * t;
t=start;
while(t->next!=NULL)
{
t=t->next;
}
temp->next=NULL;
t->next=temp;
}
}

// function for display the elements of the list
void display()
{
struct node* temp;
temp=start;
if(start==NULL)
printf(“List is emptyn”);
else
{
while(temp!=NULL)
{
printf(“%d “,temp->data);
temp=temp->next;
}
}
}

// function for delete the element from the starting
void delete_start()
{
struct node *temp;
if(start==NULL)
printf(“list is emptyn”);
else
{
temp=start;
start=temp->next;
free(temp);
}
}

// function for delete elements from the end of the list
void delete_end()
{
struct node *temp,*t;
if(start==NULL)
printf(“List is emptyn”);
else
{
t=start;
while(t->next->next!=NULL)
{
t=t->next;
}
temp=t->next;
t->next=NULL;
free(temp);
}
}

//function for reverse the linklist

void reverse()
{
struct node *pre,*curr,*nxt;
pre=NULL;
curr=nxt=start;
while(curr!=NULL)
{
nxt=curr->next;
curr->next=pre;
pre=curr;
curr=nxt;
}
start=pre;
}

// Driver program
int main()
{
int ch;
printf(“1. Insert at the starting of the listn”);
printf(“2. insert the element at the end of the listn”);
printf(“3. display the elementsn”);
printf(“4. delete element from the startingn”);
printf(“5. delete elements from the end of the listn”);
printf(“6. reverse the listn”);
printf(“7. Exitn”);
while(1)
{
printf(“Enter your choicen”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: insert_first();
break;
case 2: insert_end();
break;
case 3: display();
break;
case 4: delete_start();
break;
case 5: delete_end();
break;
case 6: reverse();
break;
case 7: exit(0);
default: printf(“Invalid input”);
}
}
return 0;
}
Output:-
1. Insert at the starting of the list
2. insert the element at the end of the list
3. display the elements
4. delete element from the starting
5. delete elements from the end of the list
6. reverse the list
7. Exit
Enter your choice
1
Enter the data to be inserted
12
Enter your choice
1
Enter the data to be inserted
23
Enter your choice
1
Enter the data to be inserted
34
Enter your choice
3
34 23 12 Enter your choice
6
Enter your choice

Recommended Post:

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

Key points:-

 MCQs:-

Leave a Reply

Your email address will not be published.