#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct node* root = NULL ;
void create(void);
void reverse(void);
void display(void);
int count=0;
void main()
{
int ch , len ;
while(1)
{
printf("\n/**Single linked list Operations**/\n");
printf("1.Create \n");
printf("2.Reverse List \n");
printf("3.Display \n");
printf("4.Quit \n");
printf("Enter Choice : ");
scanf("%d", &ch);
switch(ch)
{
case 1 : create();
break;
case 2 : reverse();
break;
case 3 : display();
break;
case 4 : exit(0);
default : printf("Invalid choice....\n\n");
}
}
}
void create()
{
struct node *temp,*p;
temp = root ;
while(1)
{
p=(struct node *)malloc(sizeof(struct node));
printf("Enter data :");
scanf("%d", &p->data);
count++;
if(root==NULL)
{
root = p;
p->link = NULL;
temp = p;
}
else
{
temp->link=p;
temp=p;
}
printf("Do you want to stop(y/n) : ");
if(getch()=='y')
{
break;
}
}
p->link=NULL;
}
void reverse()
{
struct node *p , *q;
int i, j, k, temp;
i = 0 ;
j = count-1;
p = root ;
while(i<j)
{
k = 0 ;
q = root ;
while(k<j)
{
q = q->link ;
k++ ;
}
temp = p->data ;
p->data = q->data ;
q->data = temp ;
p = p->link ;
i++ ;
j-- ;
}
}
void display()
{
if(root == NULL)
{
printf("List is empty\n\n");
}
else
{
struct node* temp = root ;
while(temp != NULL)
{
printf("%d \n", temp->data);
temp = temp->link ;
}
}
}
No Comments