#include<stdio.h>
#include<conio.h>
struct slinklist
{
int data;
struct slinklist *next;
};
typedef struct slinklist node;
node *start=NULL;
void createlist(int);
void insert_node_at_begin();
Publish Post
void insert_node_at_mid();
void insert_node_at_end();
void delete_node_at_begin();
void delete_node_at_mid();
void delete_node_at_end();
void display_left_to_right();
void display_right_to_left(node*);
node *getnode();
int countnode(node*);
int menu();
void main()
{
int ch,n=5;
clrscr();
do
{
ch=menu();
switch(ch)
{
case 1:createlist(n);
break;
case 2:insert_node_at_begin();
break;
case 3:insert_node_at_mid();
break;
case 4:insert_node_at_end();
break;
case 5:delete_node_at_begin();
break;
case 6:delete_node_at_mid();
break;
case 7:delete_node_at_end();
break;
case 8:display_left_to_right();
break;
case 9:display_right_to_left(start);
break;
case 10:exit(1);
default:printf("wrong choice");
break;
}
}while(1);
getch();
}
int menu()
{
int ch;
printf("menu\n");
printf("1.create list \n");
printf("2.insert node at begin\n");
printf("3.insert node at mid\n");
printf("4.insert node at end\n");
printf("5.delete node at begin\n");
printf("6.delete node at mid\n");
printf("7.delete node at end\n");
printf("8.display list from left to right\n");
printf("9.display list from right to left\n");
printf("10.exit\n");
printf("enter your choice");
scanf("%d",&ch);
return(ch);
}
void createlist(int n)
{
int i;
node *newnode,*temp;
for(i=0;i<n;i++)
{
newnode=getnode();
if(start==NULL)
{
start=newnode;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
}
void insert_node_at_begin()
{
node *newnode;
newnode=getnode();
if(start==NULL)
{
start=newnode;
}
else
{
newnode->next=start;
start=newnode;
}
}
void insert_node_at_mid()
{
node *newnode,*post,*prev;
int ctr=1,p,count;
newnode=getnode();
if(start==NULL)
{
start=newnode;
}
else
{
printf("enter the position");
scanf("%d",&p);
count=countnode(start);
if(p>1&&p<count)
{
prev=start;
post=start;
while(ctr<p)
{
prev=post;
post=post->next;
ctr++;
}
prev->next=newnode;
newnode->next=post;
}
else
printf("invalid choice");
}
}
void insert_node_at_end()
{
node *newnode,*temp;
newnode=getnode();
if(start==NULL)
start=newnode;
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void delete_node_at_begin()
{
node *temp;
if(start==NULL)
printf("list is empty");
else
{
temp=start;
start=start->next;
free(temp);
printf("element is deleted");
}
}
void delete_node_at_mid()
{
node *temp,*prev;
int ctr=1,p,count=0;
if(start==NULL)
printf("list is empty");
else
{
printf("enter position");
scanf("%d",&p);
count=countnode(start);
if(p>1&&p<count)
{
prev=start;
temp=start;
while(ctr<p)
{
prev=temp;
temp=temp->next;
ctr++;
}
prev->next=temp->next;
free(temp);
}
else
printf("invalid choice");
}
}
void delete_node_at_end()
{
node *temp,*prev;
if(start==NULL)
printf("list is empty");
else
{
temp=start;
prev=start;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
free(temp);
}
}
void display_left_to_right()
{
node *temp;
if(start==NULL)
{
printf("list is empty");
}
else
{
temp=start;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
}
void display_right_to_left(node *st)
{
if(st==NULL)
return 0;
else
{
display_right_to_left(st->next);
printf("%d",st->data);
}
}
int countnode(node *st)
{
int count=0;
node *temp;
if(st==NULL)
return 0;
else
return(1+countnode(st->next));
}
node *getnode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("enter data");
scanf("%d",&newnode->data);
newnode->next=NULL;
return(newnode);
}