#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}NODE,*NODEPTR;
NODEPTR insert(NODEPTR start)
{
NODEPTR temp;
temp=(NODEPTR)malloc(sizeof(NODE));
if(temp==NULL)
{
printf("Unsuccessful\n");
return start;
}
printf("Enter the data\n");
scanf("%d",&temp->data);
temp->link=start;
start=temp;
return start;
}
NODEPTR rev(NODEPTR start)
{
NODEPTR cur=start;
NODEPTR prev=NULL;
NODEPTR next;
while(cur!=NULL)
{
next=cur->link;
cur->link=prev;
prev=cur;
cur=next;
}
return prev;
}
NODEPTR disb(NODEPTR start)
{
NODEPTR temp;
if(start==NULL)
{
printf("List is empty\n");
return;
}
printf("The contents of the list before reversing\n");
for(temp=start;temp!=NULL;temp=temp->link)
printf("%d\n",temp->data);
return start;
}
NODEPTR disa(NODEPTR prev)
{
NODEPTR temp;
if(prev==NULL)
{
printf("list is empty\n");
return;
}
printf("The contents of the list after reversing\n");
for(temp=prev;temp!=NULL;temp=temp->link)
printf("%d\n",temp->data);
return prev;
}
main()
{
int ch;
NODEPTR start=NULL;
NODEPTR prev=NULL;
while(1)
{
printf("1.Insert into the list\n2.List before reversing\n3.Reverse the list\n4.List after reversing\n");
scanf("%d",&ch);
switch(ch)
{
case 1 : start=insert(start);break;
case 2 : start=disb(start);break;
case 3 : prev=rev(start);break;
case 4 : prev=disa(prev);break;
default : return;
}
}
}
No comments:
Post a Comment