/*Program of doubly linked list*/
#include<stdio.h>
#include<malloc.h>
typedef struct nodetype
{
int data;
struct nodetype *prev,*next;
}node;
void add_beg(node **start,int ele)
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->data=ele;
temp->next=NULL;
temp->prev=NULL;
if(*start==NULL)
{
(*start)=temp;
}
else
{
(*start)->prev=temp;
temp->next=(*start);
(*start)=temp;
}
}
void add_end(node **start,int ele)
{
node *temp,*p=(*start);
temp=(node *)malloc(sizeof(node));
temp->data=ele;
temp->next=NULL;
if((*start)==NULL)
(*start)=temp;
else
{
while(p->next!=NULL)
p=p->next;
p->next=temp;
temp->prev=p;
}
}
void add_pos(node **start,int ele,int pos)
{
int c=1;
node *temp,*p=(*start),*q;
temp=(node *)malloc(sizeof(node));
temp->data=ele;
temp->prev=NULL;
temp->next=NULL;
if(pos<1)
printf("\nInvalid Position\n");
else if(pos==1)
{
temp->next=(*start);
(*start)->prev=temp;
(*start)=temp;
}
else
{
while(c<pos && p!=NULL)
{
c++;
q=p;
p=p->next;
}
if(c==pos)
{
temp->next=p;
p->prev=temp;
q->next=temp;
temp->prev=q;
}
else
printf("\nInvalid Position\n");
}
}
void del_beg(node **start)
{
node *p=(*start);
(*start)=(*start)->next;
(*start)->prev=NULL;
free(p);
}
void del_end(node **start)
{
node *p=(*start),*q;
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
free(p);
}
void del_pos(node **start,int pos)
{
node *p=(*start),*q;
int c=1;
if(pos<1)
{
printf("\nInvalid position\n");
return;
}
if(pos==1)
{
(*start)=(*start)->next;
(*start)->prev=NULL;
free(p);
}
else
{
while(c<pos && p->next!=NULL)
{
c++;
q=p;
p=p->next;
}
if(c==pos)
{
q->next=p->next;
(p->next)->prev=q;
free(p);
}
}
}
void search(node *start,int ele)
{
node *p=start;
int f=0;
while(p!=NULL)
{
if(p->data==ele)
{
f=1;
break;
}
p=p->next;
}
if(f==0)
printf("\nElement not present\n");
else
printf("\nElement present\n");
}
void show(node *start)
{
if(start==NULL)
printf("\nNo list\n");
else
{
while(start!=NULL)
{
printf("%d ",start->data);
start=start->next;
}
printf("\n");
}
}
void sort(node **start)
{
node *p=(*start),*q;
int temp;
while(p!=NULL)
{
q=q->next;
while(q!=NULL)
{
if(p->data > q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
q=q->next;
}
p=p->next;
}
}
void reverse(node **start)
{
node *p=(*start),*q;
while(p->next!=NULL)
{
q=p->next;
p->next=q->next;
q->next=(*start);
(*start)=q;
}
}
main()
{
node *start=NULL;
int ele,n,i,st=1,ch,pos;
do
{
printf("\n 0. Exit");
printf("\n 1. Display");
printf("\n 2. Insert first");
printf("\n 3. Insert last");
printf("\n 4. Insert at given position");
printf("\n 5. Delete from first");
printf("\n 6. Delete from last");
printf("\n 7. Delete from the given postion");
printf("\n 8. Search");
printf("\n 9. Sort");
printf("\n10. Reverse");
printf("\n\nEnter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 0:
st=0;
break;
case 1:
show(start);
break;
case 2:
printf("\nEnter the element=");
scanf("%d",&ele);
add_beg(&start,ele);
break;
case 3:
printf("\nEnter the element=");
scanf("%d",&ele);
add_end(&start,ele);
break;
case 4:
printf("\nEnter the element=");
scanf("%d",&ele);
printf("\nEnter the position=");
scanf("%d",&pos);
add_pos(&start,ele,pos);
break;
case 5:
del_beg(&start);
break;
case 6:
del_end(&start);
break;
case 7:
printf("\nEnter the position=");
scanf("%d",&pos);
del_pos(&start,pos);
break;
case 8:
printf("\nEnter the element=");
scanf("%d",&ele);
search(start,ele);
break;
case 9:
sort(&start);
show(start);
break;
case 10:
reverse(&start);
break;
default:
printf("\nWrong choice");
}
}
while(st==1);
}
#include<stdio.h>
#include<malloc.h>
typedef struct nodetype
{
int data;
struct nodetype *prev,*next;
}node;
void add_beg(node **start,int ele)
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->data=ele;
temp->next=NULL;
temp->prev=NULL;
if(*start==NULL)
{
(*start)=temp;
}
else
{
(*start)->prev=temp;
temp->next=(*start);
(*start)=temp;
}
}
void add_end(node **start,int ele)
{
node *temp,*p=(*start);
temp=(node *)malloc(sizeof(node));
temp->data=ele;
temp->next=NULL;
if((*start)==NULL)
(*start)=temp;
else
{
while(p->next!=NULL)
p=p->next;
p->next=temp;
temp->prev=p;
}
}
void add_pos(node **start,int ele,int pos)
{
int c=1;
node *temp,*p=(*start),*q;
temp=(node *)malloc(sizeof(node));
temp->data=ele;
temp->prev=NULL;
temp->next=NULL;
if(pos<1)
printf("\nInvalid Position\n");
else if(pos==1)
{
temp->next=(*start);
(*start)->prev=temp;
(*start)=temp;
}
else
{
while(c<pos && p!=NULL)
{
c++;
q=p;
p=p->next;
}
if(c==pos)
{
temp->next=p;
p->prev=temp;
q->next=temp;
temp->prev=q;
}
else
printf("\nInvalid Position\n");
}
}
void del_beg(node **start)
{
node *p=(*start);
(*start)=(*start)->next;
(*start)->prev=NULL;
free(p);
}
void del_end(node **start)
{
node *p=(*start),*q;
while(p->next!=NULL)
{
q=p;
p=p->next;
}
q->next=NULL;
free(p);
}
void del_pos(node **start,int pos)
{
node *p=(*start),*q;
int c=1;
if(pos<1)
{
printf("\nInvalid position\n");
return;
}
if(pos==1)
{
(*start)=(*start)->next;
(*start)->prev=NULL;
free(p);
}
else
{
while(c<pos && p->next!=NULL)
{
c++;
q=p;
p=p->next;
}
if(c==pos)
{
q->next=p->next;
(p->next)->prev=q;
free(p);
}
}
}
void search(node *start,int ele)
{
node *p=start;
int f=0;
while(p!=NULL)
{
if(p->data==ele)
{
f=1;
break;
}
p=p->next;
}
if(f==0)
printf("\nElement not present\n");
else
printf("\nElement present\n");
}
void show(node *start)
{
if(start==NULL)
printf("\nNo list\n");
else
{
while(start!=NULL)
{
printf("%d ",start->data);
start=start->next;
}
printf("\n");
}
}
void sort(node **start)
{
node *p=(*start),*q;
int temp;
while(p!=NULL)
{
q=q->next;
while(q!=NULL)
{
if(p->data > q->data)
{
temp=q->data;
q->data=p->data;
p->data=temp;
}
q=q->next;
}
p=p->next;
}
}
void reverse(node **start)
{
node *p=(*start),*q;
while(p->next!=NULL)
{
q=p->next;
p->next=q->next;
q->next=(*start);
(*start)=q;
}
}
main()
{
node *start=NULL;
int ele,n,i,st=1,ch,pos;
do
{
printf("\n 0. Exit");
printf("\n 1. Display");
printf("\n 2. Insert first");
printf("\n 3. Insert last");
printf("\n 4. Insert at given position");
printf("\n 5. Delete from first");
printf("\n 6. Delete from last");
printf("\n 7. Delete from the given postion");
printf("\n 8. Search");
printf("\n 9. Sort");
printf("\n10. Reverse");
printf("\n\nEnter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 0:
st=0;
break;
case 1:
show(start);
break;
case 2:
printf("\nEnter the element=");
scanf("%d",&ele);
add_beg(&start,ele);
break;
case 3:
printf("\nEnter the element=");
scanf("%d",&ele);
add_end(&start,ele);
break;
case 4:
printf("\nEnter the element=");
scanf("%d",&ele);
printf("\nEnter the position=");
scanf("%d",&pos);
add_pos(&start,ele,pos);
break;
case 5:
del_beg(&start);
break;
case 6:
del_end(&start);
break;
case 7:
printf("\nEnter the position=");
scanf("%d",&pos);
del_pos(&start,pos);
break;
case 8:
printf("\nEnter the element=");
scanf("%d",&ele);
search(start,ele);
break;
case 9:
sort(&start);
show(start);
break;
case 10:
reverse(&start);
break;
default:
printf("\nWrong choice");
}
}
while(st==1);
}
No comments:
Post a Comment