Google Search

Saturday, October 24, 2015

C Program to Reverse a Linked List

‪#‎include‬<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void insert_data(int value)
{
struct node *var,*temp;
temp=head;
var=(struct node *)malloc(sizeof(struct node));
var->data=value;
if(head==NULL)
{
head=var;
head->next=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
var->next=NULL;
temp->next=var;
}
}
void reverse_list()
{
struct node *temp,*temp1,*var;
temp=head;
var=NULL;
while(temp!=NULL)
{
temp1=var;
var=temp;
temp=temp->next;
var->next=temp1;
}
head=var;
}
void display()
{
struct node *var;
var=head;
printf("\nlist of elments are \n");
while(var!=NULL)
{
printf("-> %d ",var->data);
var=var->next;
}
}
int main()
{
int i,value;
char ch='y';
head=NULL;
printf(" 1.) Insert node");
printf("\n 2.) display the list");
printf("\n 3.) reverse the nodes");
printf("\n 4.) exit");
while(ch=='y')
{
printf("\nChoose to do operation :");
scanf("%d",&i);
switch(i)
{
case 1 :
{
printf("\nEnter the data to be inserted in node ");
scanf("%d",&value);
insert_data(value);
display();
break;
}
case 2 :
{
display();
break;
}
case 3 :
{
reverse_list();
display();
break;
}
case 4 :
{
exit(0);
break;
}
}
}
getch();
}

No comments:

Post a Comment