You are on page 1of 6

#include<stdio.

h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node * next;
};
//fun prototype
struct node *createnode(struct node*,int);
struct node *insertfirst(struct node*,int);
struct node *insertbet(struct node*,int,int);
struct node *insertlast(struct node*,int);
struct node *deletefirst(struct node*);
struct node *deletebet(struct node*,int);
struct node *deletelast(struct node*);
struct node *reverse(struct node*);
struct node *sortlist(struct node*);
void printlist(struct node*);
void main()
{
struct node *root=NULL;
int val,choice,position;
clrscr();
// creat the menu
while(1)
{
printf("\n\n\t menu doubly linked list");
printf("\n\n\t .1 creat node");
printf("\n\n\t .2 insert first");
printf("\n\n\t .3 insert between");
printf("\n\n\t .4 insert last");
printf("\n\n\t .5 delete first");
printf("\n\n\t .6 delete between");
printf("\n\n\t .7 delete last");
printf("\n\n\t .8 print list");
printf("\n\n\t .9 reverse list");
printf("\n\n\t .10 sortlist");
printf("\n\n\t .11 exit");
printf("\n\n\t enter the choice:-");
scanf("%d",&choice);
switch(choice)
{
case 1: //create node
printf("\n\n\t enter the number");
scanf("%d",&val);
root=createnode(root,val);
break;
case 2: // insert first
printf("\n\n\t enter the number");
scanf("%d",&val);
root= insertfirst(root,val);
break;
case 3: //ins bet
printf("\n\n\t enter the number");
scanf("%d",&val);
printf("\n\n\t enter the position");
scanf("%d",&position);
root=insertbet(root,val,position);
break;
case 4: //ins last
printf("\n\n\t enter the number");
scanf("%d",&val);
root=insertlast(root,val);
break;
case 5: //delete first
root=deletefirst(root);
break;
case 6://delete bet
printf("\n\n\t enter the position");
scanf("%d",&position);
root=deletebet(root,position);
break;
case 7: //del last
root=deletelast( root);
break;
case 8: //print list
printlist(root);
break;
case 9://reverse list
root=reverse(root);
break;
case 10://sortlist
root=sortlist(root);
break;
case 11://exit
return;
}
getch();
clrscr();
}
}
//fun definition
struct node *createnode(struct node *root,int val)
{
struct node *base=root,*newnode;
if(root==NULL)
{
//link list is empty
root=(struct node *)malloc(sizeof(struct node));
root->data=val;
root->next=NULL;
return root;
}
else
{
//traverse till the last node
while(root->next!=NULL)
root=root->next;
//allocate memomery to newnode
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;
root->next=newnode;
return base;
}
}
void printlist(struct node *root)
{
while(root!=NULL)
{
printf("\n\n\t data=%d",root->data);
root=root->next;
}
}
struct node *insertfirst(struct node *root,int val)
{
struct node *newnode;
//allocate memory to new node
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=root;
return newnode;
}
struct node *insertbet(struct node *root,int val,int position)
{
int p=1;
struct node *previous,*base=root,*newnode;
while(p!=position)
{
previous=root;
root=root->next;
p=p+1;
}
//create a new node
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=root;
previous->next=newnode;
return base;
}
struct node *insertlast(struct node *root,int val)
{
struct node *newnode,*base=root;
//traverse till the last
while(root->next!=NULL)
root=root->next;
// create newnode
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=NULL;
root->next=newnode;
return base;
}
struct node *deletefirst(struct node *root)
{
root=root->next;
return root;
}
struct node *deletebet(struct node *root,int position)
{
int p=1;
struct node *previous,*base=root;
while(p!=position)
{
previous=root;
root=root->next;
p=p+1;
}
//create a newnode
previous->next=root->next;
return base;
}
struct node *deletelast(struct node *root)
{
struct node *previous,*base=root;
//traverse till the last node
while(root->next!=NULL)
{
previous=root;
root=root->next;
}

previous->next=NULL
return base;
}
struct node *reverse(struct node *root)
{
struct node *temp1,*temp2,*ptr;
temp1=NULL;
while(root!=NULL)
{
temp2=root->next;
root->next=temp1;
temp1=root;
root=temp2;
}
return temp1;
}
struct node *sortlist(struct node *root)
{
struct node *base=root,*ptr1,*ptr;
int temp;
while(root!=NULL)
{
ptr1=root->next;
while(ptr1!=NULL)
{
if(root->data>ptr1->data)
{
temp=root->data;
root->data=ptr1->data;
ptr1->data=temp;
}
ptr1=ptr1->next;
}
root=root->next;
}
return base;
}

You might also like