1. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
/* Doubly Linked List Acts Like Stack */
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *llink;
int info;
struct node *rlink;
};
typedef struct node NODE;
void ins_first(NODE**, int);
int del_first(NODE**);
void display(NODE*);
int main()
{
NODE *first=NULL;
int choice, item;
for(;;)
{
printf("Operation Resarch On Doubly Linked Listn");
printf("Program Acts Like Stackn");
printf("1.Push OR Insert Frontn");
printf("2.Pop OR Delete Frontn");
printf("3.Displayn");
printf("4.Exitn");
printf("Enter U R Choicen");
scanf("%d", &choice);
printf("nn");
switch(choice)
{
case 1 : printf("Enter The Item To Insertn");
scanf("%d", &item);
printf("nn");
ins_first(&first, item);
break;
case 2 : item=del_first(&first);
if(item!='0')
printf("Deleted Element Is %dnn", item);
break;
case 3 : printf("Contents Of Stack Aren");
display(first);
break;
default : exit(0);
}
}
return 1;
}
P a g e |1
2. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
void ins_first(NODE **first, int item)
{
NODE *newn;
newn=(NODE*)malloc(sizeof(NODE));
newn->llink=NULL;
newn->info=item;
if(*first==NULL)
{
newn->rlink=NULL;
*first=newn;
}
else
{
newn->rlink=*first;
newn->rlink->llink=newn; /*Hihgly IMP */
*first=newn;
}
}
int del_first(NODE **first)
{
int item;
NODE *temp;
if(*first==NULL)
{
printf("Stack IS Underflownn");
return('0');
}
temp=*first;
item=temp->info;
*first=temp->rlink; /* *first->llink=NULL Is An Error. */
free(temp);
return item;
}
void display(NODE *first)
{
NODE *temp;
temp=first;
if(first==NULL)
{
printf("Stack Is Emptynn");
}
else
{
while(temp->rlink!=NULL)
{
printf("%dn", temp->info);
temp=temp->rlink;
}
printf("%dnn", temp->info);
}
}
P a g e |2
3. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
/* Doubly Linked List */
Insert Last
void ins_last(NODE **first, int item)
{
NODE *newn, *temp;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=item;
newn->rlink=NULL;
if(*first==NULL)
{
newn->llink=NULL;
*first=newn;
}
else
{
temp=*first;
while(temp->rlink!=NULL)
temp=temp->rlink;
newn->llink=temp;
temp->rlink=newn;
}
}
P a g e |3
4. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
/* Doubly Linked List */
Delete Last
int del_last(NODE **first)
{
int item;
NODE *temp;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
else
{
temp=*first;
if(temp->rlink==NULL)
{
item=temp->info;
*first=NULL;
free(temp);
return item;
}
else
{
while(temp->rlink!=NULL)
temp=temp->rlink;
item=temp->info;
temp->llink->rlink=NULL;
free(temp);
return item;
}
}
}
P a g e |4
5. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
Write A C Module To Find The Frequency Of Given Element x In A DLL.
P a g e |5
6. P a g e |6
nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
WAP To Search A Node With A Given Data With A DLL, If It Is Found Delete It, Otherwise Display Appropriate
Message.
int del_k(NODE **first, int k)
{
int item;
NODE *temp;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
temp=*first;
st
if(temp->rlink==NULL && temp->info==k) //Element Found In 1 Position No Further Element Present Right To It.
{
item=temp->info;
*first=NULL;
free(temp);
return item;
}
st
if(temp->rlink!=NULL && temp->info==k) //Element Found In 1 Position Further Element Present Right To It.
{
item=temp->info;
*first=temp->rlink;
temp->rlink->llink=NULL;
free(temp);
return item;
}
}
while(temp!=NULL)
{
if(temp->info==k)
break;
temp=temp->rlink;
}
if(temp!=NULL)
{
item=temp->info;
temp->llink->rlink=temp->rlink;
if(temp->rlink!=NULL) /* If Element Found In Middle & Further Element Present Right To It */
temp->rlink->llink=temp->llink;
free(temp);
return item;
}
else
{
printf("k=%d Not Foundn", k);
return('0');
}
7. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
Dec 05
WAP To Perform The Following Operations On Doubly Linked List.
1)To Insert A Node To The Right Of A Given Node.
2)To Delete A Node On The Left Of A Given Node. 12 Marks.
1)To Insert A Node To The Right Of A Given Node.
void ins_x_right_to_y(NODE **first, int x, int y)
{
NODE *newn, *temp;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=x;
if(*first==NULL)
printf("List Is Emptyn");
else
{
temp=*first;
if(temp->info==y && temp->rlink==NULL)
{
newn->rlink=NULL;
newn->llink=temp;
temp->rlink=newn;
}
else
{
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL && temp->rlink!=NULL)
{
newn->llink=temp->rlink->llink;
newn->rlink=temp->rlink;
temp->rlink->llink=newn;
temp->rlink=newn;
}
else if(temp!=NULL && temp->rlink==NULL)
{
newn->rlink=NULL;
newn->llink=temp;
temp->rlink=newn;
}
else
printf("y=%d Not Found In The Listn", y);
}
}
}
P a g e |7
8. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
2)To Delete A Node On The Left Of A Given Node.
int del_x_left_to_y(NODE **first, int y)
{
int item;
NODE *temp, *arc;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
temp=*first;
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL && temp->llink!=NULL)
{
arc=temp->llink;
item=arc->info;
if(arc->llink!=NULL)
{
arc->llink->rlink=arc->rlink;
temp->llink=arc->llink;
}
else /* 12 13 14 if y=13, To Delete 12 */
{
temp->llink=NULL;
*first=arc->rlink;
}
free(arc);
return item;
}
else if(temp!=NULL && temp->llink==NULL)
{
printf("No Item Present Left To It So Deletion Not Possible");
return('0');
}
else
{
printf("y=%d Not Foundn");
return('0');
}
}
P a g e |8
9. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
To Insert A Node On The Left Of A Given Node.
void ins_x_left_to_y(NODE **first, int x, int y)
{
NODE *newn, *temp;
newn=(NODE*)malloc(sizeof(NODE));
newn->info=x;
if(*first==NULL)
printf("List Is Emptyn");
else
{
temp=*first;
if(temp->info==y)
{
newn->llink=NULL;
newn->rlink=*first;
temp->llink=newn;
*first=newn;
}
else
{
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL)
{
newn->llink=temp->llink;
newn->rlink=temp;
temp->llink->rlink=newn;
temp->llink=newn;
}
else
printf("y=%d Not Found In The Listn", y);
}
}
}
P a g e |9
10. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
To Delete A Node On The Right Of A Given Node.
int del_x_right_to_y(NODE **first, int y)
{
int item;
NODE *temp, *arc;
if(*first==NULL)
{
printf("DLL IS Emptynn");
return('0');
}
temp=*first;
while(temp!=NULL)
{
if(temp->info==y)
break;
temp=temp->rlink;
}
if(temp!=NULL && temp->rlink!=NULL)
{
arc=temp->rlink;
item=arc->info;
temp->rlink=arc->rlink;
if(arc->rlink!=NULL) /* 12,6 13, 14 if y=13 To Delete 14 It's Required */
arc->rlink->llink=arc->llink;
free(arc);
return item;
}
else if(temp!=NULL && temp->rlink==NULL)
{
printf("No Item Present Right To It, So Deletion Not Possiblen");
return('0');
}
else
{
printf("y=%d Not Foundn", y);
return('0');
}
}
P a g e | 10
11. nd
Ashoka R, 2 Sem, Maharaja Institute Of Technology Mysore.
Dec 06/Jan 07
WAP To Interchange The Mth And Nth Elements Of A DLL. 10Marks
P a g e | 11