0% found this document useful (0 votes)
25 views44 pages

DS Lab Record

Data structures and algorithms programs

Uploaded by

shaniyanajeem002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views44 pages

DS Lab Record

Data structures and algorithms programs

Uploaded by

shaniyanajeem002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

PROGRAM

#include<stdio.h>
void swap(int *a,int *b)
{ int temp = *b;
*b = *a;
*a = temp;
}
void heapify(int i,int size,int a[])
{ int lc=2*i;
int rc=(2*i)+1;
int larg=i;
if(lc<=size&&a[lc]>a[larg]){
larg=lc;}
if(rc<=size&&a[rc]>a[larg]) {
larg=rc; }
if(larg!=i) {
swap(&a[i],&a[larg]);
heapify(larg,size,a);
}}
void buildheap(int a[],int size)
{ for (int i=size/2;i>1;i--)
heapify(i,size,a);
}
void heapsort(int a[],int size)
{ buildheap(a,size);
for(int i=size;i>1;i--){
swap(&a[i],&a[1]);
heapify(1,i-1,a);
}}
void main()
{ int a[10],i;
printf("Enter the elements of array:");
for(int i=1;i<10;i++)
scanf("%d",&a[i]);
heapsort(a,10);
printf("The heap is :");
for(i=1;i<10;i++)
printf("\t%d",a[i]);
}

OUTPUT

Enter the elements of the array: 20 12 43 28 30 15 17 40 10 32

The heap is : 10 12 15 17 20 28 30 32 40 43
EXPERIMENT NO.08 Date:

HEAP SORT

AIM

To perform heap sort using minheap in c programming.

ALGORITHM
1.heapify(i,size,array)
1.1 set large=i,left=2*i,right=2*i +1
1.2 if left<=size and array[left] > array[large]
set large as left
1.3 if right<=size and array[right] > array[large]
set large as right
1.4 if large != i , then swap array[i] and array[large]
heapify(large,size,array)
2.buildheap(array, size)
2.1 for i=size/2 to i=2
heapify(i,size,array)
decrement i
3.heapsort(array,size)
3.1 buildheap(array, size)
3.2 for i=size to i=2
swap array[i] and array[1]
heapify(1,i-1,array)
decrement i

RESULT

The program is executed and output is verified.

20
PROGRAM
#include <stdio.h>
#define size 50
int stack[size],top=0,x;
void push(int x)
{ if(top==size)
printf("stack is full");
else
printf("enter element");
scanf("%d",&x);
stack[top]=x;
top=top+1;
}
void pop()
{ if(top==0)
printf("stack is empty");
else
top=top-1;
x=stack[top];
printf("%d is poped",x);
}
void peek()
{ if(top==0)
printf("stack is empty");
else
printf("%d",stack[top-1]);
}
void display()
{ int i;
if(top==0)
printf("stack is empty");
else
for(i=0;i<top;i++)
printf("\n%d",stack[i]);
}
void isfull()
{ if(top==size)
printf("full");
else
printf("not full");
}
void isempty()
{ if(top==0)
printf("empty");
else
printf("not empty");
}
int main()
{ int choice;
do{ printf("\n1.push\n2.pop\n3.peek\n4.display\n5.isfull\n6.isempty\n7.exit\n");
printf("\nEnter choice:");
scanf("%d",&choice);
switch(choice){ case 1: push(x);
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: display();
break;
case 5: isfull();
break;
case 6: isempty();
break;
case 7: break;
default: printf("invalid choice"); }}
while(choice!=7);
getchar(); }
EXPERIMENT NO.09 Date:

IMPLEMENTATION OF STACK

AIM

To implement stack using array in c programming.

ALGORITHM
1.push()
1.1 if top = size, then display stack is full
1.2 else do,top = top +1
stack [top] = item;
1.3 end
2.pop()
2.1 if top = 0, then display stack is empty
2.2 else do,top = top -1
item = stack[top]
2.3 return item
2.4 end
3.peek()
3.1 if top=0 ,then display stack is empty
3.2 else item=stack[top-1]
return item
3.3 end
4.display
4.1 if top=0,then display stack is empty
4.2 else For i=0 to i=top-1
Display stack[i]
Increment i
4.3 end
5.isfull()
5.1 if top=size ,then return 1
5.2 else return 0
6.isempty()
6.1 if top=0 then return 1
6.2 else return 0

22
OUTPUT

1.push 33
2.pop
3.peek 1.push
4.display 2.pop
5.isfull 3.peek
6.isempty 4.display
7.exit 5.isfull
6.isempty
Enter choice: 1 7.exit
enter element: 22
Enter choice: 2
1.push 33 is poped
2.pop
3.peek 1.push
4.display 2.pop
5.isfull 3.peek
6.isempty 4.display
7.exit 5.isfull
6.isempty
Enter choice: 1 7.exit
enter element: 33
Enter choice: 4

1.push 22
2.pop
3.peek 1.push
4.display 2.pop
5.isfull 3.peek
6.isempty 4.display
7.exit 5.isfull
6.isempty
Enter choice: 4 7.exit

22 Enter choice: 7

33
1.push
2.pop
3.peek
4.display
5.isfull
6.isempty
7.exit

Enter choice: 3
RESULT

The program is executed and output is verified.

24
PROGRAM

#include<stdio.h>
#define n 50
int q[n],f=-1,r=-1;
void insertqueue(int x)
{ if(f==(r+1)%n)
printf("The queue is full");
else if(f==-1&&r==-1){
f=r=0;
q[r]=x;}
else{
r=(r+1)%n;
q[r]=x;
}}
void deletequeue()
{ if(f==-1&&r==-1)
printf("The queue is empty");
else if(f==r)
f=r=-1;
else
printf("The deleted element is %d",q[f]);
f=(f+1)%n;
}
void isfull()
{ if(f==(r+1)%n)
printf("Queue is full");
else
printf("Queue is not full");
}
void isempty()
{ if(f==-1&&r==-1)
printf("Queue is empty");
else
printf("Queue is not empty");
}
void display()
{ int i;
if(f==-1&&r==-1)
printf("Queue is empty");
else
for(i=f;i<=r;i++) {
printf("\n%d",q[i]);
}}
void main()
{ int choice,x;
do{ printf("\n1.Insertion\n2.Deletion\n3.isfull\n4.isempty\n5.Display\n6.exit");
printf("\nEnter choice");
scanf("%d",&choice);
switch(choice){
case 1: printf("enter the element to be added");
scanf("%d",&x);
insertqueue(x);
break;
case 2: deletequeue();
break;
case 3: isfull();
break;
case 4: isempty();
break;
case 5: display();
break;
case 6: break;
default:printf("invalid choice");
}}
while(choice!=6);
getchar();
}
EXPERIMENT NO.10 Date:

IMPLEMENTATION OF CIRCULAR QUEUE

AIM
To write a c program to implement circular queue using array.

ALGORITHM
1.insertqueue(item)
1.1 if (rear+1)mod size=front
then display queue is full.
1.2 else if front=-1 and rear=-1,
then front=rear=0
Queue[rear]=item
1.3 else,set rear=(rear+1)mod size
Queue[rear]=item
2.deletequeue()
2.1 if front=-1 and rear=-1,
then display queue is empty
2.2 else if front=rear
set front= rear=-1
2.3 else display Queue[front]
set front=(front+1)mod size
3.display()
3.1 if front=-1 and rear=-1,
then display queue is empty
3.2 else for i=front to i=rear
Display Queue[i]
Increment i
3.3 end
4.isfull()
5.1 if (rear+1)mod size=front
then display queue is full.
5.2 else display queue is not full
5.isempty()
5.1 if front=-1 and rear=-1,
then display queue is empty
5.2 else display queue is not empty

26
OUTPUT
1.Insertion 1.Insertion
2.Deletion 2.Deletion
3.isfull 3.isfull
4.isempty 4.isempty
5.Display 5.Display
6.Exit 6.Exit
Enter choice: 1 Enter choice: 6
Enter the element to be added: 34

1.Insertion
2.Deletion
3.isfull
4.isempty
5.Display
6.Exit
Enter choice: 1
Enter the element to be added: 56

1.Insertion
2.Deletion
3.isfull
4.isempty
5.Display
6.Exit
Enter choice: 5
34
56

1.Insertion
2.Deletion
3.isfull
4.isempty
5.Display
6.Exit
Enter choice: 2
The deleted element is 34

1.Insertion
2.Deletion
3.isfull
4.isempty
5.Display
6.Exit
Enter choice: 5
56
RESULT

The program is executed and output is verified.

28
PROGRAM
#include<stdio.h>
#define size 10
int q[size],f=-1,r=-1;
void insertfront(int x)
{ if(f==(r+1)%size)
printf("queue is full \n");
else if(f==-1)
f=0,r=0;
else if(f==0)
f=size-1;
else
f=f-1;
q[f]=x;
}
void insertrear(int x)
{ if(f==(r+1)%size)
printf("queue is full \n");
else{
if(f==-1 && r==-1){
f=r=0;
q[r]=x;}
else{
r=(r+1)%size;
q[r]=x;
}}}
void deletefront()
{ if(f==-1 && r==-1)
printf("queue is empty \n");
else if(f==r)
f=r=-1;
else
printf("the deleted element is:%d \n",q[f]);
f=(f+1)%size;
}
int deleterear()
{ int x;
if(f==-1 && r==-1)
printf("queue is empty \n");
else if(f==r)
x=q[r];
f=r=-1;
else if(r==0)
r=size-1;
else
r=r-1;
return(x);
}
void isfull()
{ if(f==(r+1)%size)
printf("queue is full\n");
else
printf("queue is not full \n");
}
void isempty()
{ if(f==-1 && r==-1)
printf("queue is empty \n");
else
printf("queue is not empty \n");
}
void display()
{ int i;
if(f==-1 && r==-1)
printf("queue is empty \n");
else{
for(i=f;i!=r;i=(i+1)%size){
printf("%d \n",q[i]);}
if(i==r)
printf("%d \n",q[i]); }}
EXPERIMENT NO.11 Date:

IMPLEMENTATION OF DOUBLE ENDED QUEUE

AIM

To write a c program to implement double ended queue using array.

ALGORITHM

1.insertrear(item)
1.1 if isfull,then display queue is full
1.2 else if front=-1 and rear=-1,
then front=rear=0
Queue[rear]=item
1.3 else,set rear=(rear+1)mod size
Queue[rear]=item
2.insertfront(item)
2.1 if isfull(),then display queue is full
2.2 else if front=-1
then set front=rear=0
2.3 else if front=0
then set front =size-1
2.4 else
set front =front-1
2.5 Queue[front]=item
3.deletefront()
3.1 if isempty(),then display queue is empty
3.2 else if front=rear
set front= rear=-1
3.3 else display Queue[front]
set front=(front+1)mod size
4.deleterear()
4.1 if isempty(),then display queue is empty
4.2 else if front=rear
then set front=rear=-1
4.3 else if rear=0
then set rear=size-1
4.4 else set rear=rear-1

30
void main()
{ int x,ch;
do{ printf("enter choice: \n 1.insertfront \n 2.insertrear \n 3.deletefront \n 4.deleterear \n 5.isfull \n 6.isempty \n
7.display \n 8.exit \n");
scanf("%d",&ch);
switch(ch) { case 1: printf("enter element to be inserted \n");
scanf("%d",&x);
insertfront(x);
break;
case 2: printf("enter element to be inserted \n");
scanf("%d",&x);
insertrear(x);
break;
case 3: deletefront();
break;
case 4: deleterear();
break;
case 5: isfull();
break;
case 6: isempty();
break;
case 7: display();
break;
case 8: break;
default: printf("invalid choice \n");
}}
while(ch!=8); }

OUTPUT

Enter choice: 1
1.insert front
2.insert rear
3.delete front
4.delete rear
5.isfull
6.isempty
7.display
8.exit
Enter the element to be inserted: 75

Enter choice: 2
1.insert front
2.insert rear
3.delete front
4.delete rear
5.isfull
6.isempty
7.display
8.exit
Enter the element to be inserted: 49

Enter choice: 1
1.insert front
2.insert rear
3.delete front
5.display()
5.1 if front=-1 and rear=-1,
then display queue is empty
5.2 else for i=front to i=rear
Display Queue[i]
Increment i
5.3 end
6.isfull()
6.1 if (rear+1)mod size=front
then display queue is full.
6.2 else display queue is not full
7.isempty()
7.1 if front=-1 and rear=-1,
then display queue is empty
7.2 else display queue is not empty

32
4.delete rear
5.isfull
6.isempty
7.display
8.exit
Enter the element to be inserted: 8

Enter choice: 7
1.insert front
2.insert rear
3.delete front
4.delete rear
5.isfull
6.isempty
7.display
8.exit
8
75
49

Enter choice: 3
1.insert front
2.insert rear
3.delete front
4.delete rear
5.isfull
6.isempty
7.display
8.exit
Enter the element to be inserted: 49
the deleted element is 8

Enter choice: 7
1.insert front
2.insert rear
3.delete front
4.delete rear
5.isfull
6.isempty
7.display
8.exit
75
49

Enter choice: 8
1.insert front
2.insert rear
3.delete front
4.delete rear
5.isfull
6.isempty
7.display
8.exit
RESULT

The program is executed and output is verified.

34
PROGRAM
#include<stdio.h>
#include<string.h>
#define max 100
char s[max];int top=0;
int isfull()
if(top==max)
return(1);
else
return(0);
}
void push(char a)
{ if(!isfull())
s[top++]=a;
}
void displaystack()
{ int i;
printf("\t stack : ");
for(i=0;i<top;i++)
printf("%c ",s[i]);
}
void displaypost(char a[],int k)
{ int i;
printf("\t postfix : ");
for(i=0;i<k;i++)
printf("%c ",a[i]);
}
char peek()
{ return(s[top-1]);
}
char pop()
{ return(ss[--top]);}
int isempty()
{if(top==0)
return(1);
else
return(0);
}
int inprio(char c)
{ switch(c) {
case '-':
case '+': return(1);break;
case '*':
case '/': return(3);break;
case '^': return(6);break;
}}
int instack(char c)
{ switch(c) {
case '-':
case '+': return(2);break;
case '*':
case '/': return(4);break;
case '^': return(5);break;
case '(': return(0);break;
}}
void main()
{ int i,j=0;
char expr[50], post[50],token,c;
printf( "\nEnter an infix expression\n");
scanf("%[^\n]",expr);
printf("\n %s \n",expr);
for(i=0;expr[i]!='\0';i++){
token=expr[i];
EXPERIMENT NO.12 Date:

CONVERSION FROM INFIX TO POSTFIX

AIM

To write a c program to convert an infix expression to its postfix form using stack.

ALGORITHM
1.isfull()
1.1 if top=max , then return 1
1.2 else return 0
2.push(item)
2.1 if not isfull()
stack[top++]= item
2.4 end
3.displaystack()
3.1 For i=0 to i=top-1
Display stack[i]
Increment i
3.2 end
4.displaypost(item,index)
4.1 For i=0 to i=index-1
Display stack[i]
Increment i
4.3 end
5.peek()
5.1 return stack[top-1]
6.pop()
6.1 return stack[--top]
7.isempty()
6.1 if top=0 then return 1
6.2 else return 0
8.priority(character)
8.1 If the reading symbol is operator (+ , – , * , /), then Push it onto the Stack. First
pop the operators which are already on the stack that have higher or equal
precedence than the current operator and append them to the postfix . If open
parenthesis is there on top of the stack then push the operator into the stack.
8.2 If the reading symbol is left parenthesis ‘( ‘, then Push it onto the tack.

36
switch(token){ case '(': push(token);break;
case ')': while((c=pop())!='(')
post[j++]=c;
break;
case '-':
case '+':
case '*':
case '/':
case '^': while(!isempty() && (instack(peek())>inprio(token)))
post[j++]=pop();
push(token);
break;
default: post[j++]=token;
}
printf("\n Token: %c",token);
displaystack(); displaypost(post,j);
}
while(!isempty())
post[j++]=pop();
post[j]='\0';
printf("\n Postfix: %s \n",post);
}

OUTPUT

Enter an infix expr


a*(b-c)+f*g*h
a*(b-c)+f*g*h

Token: a stack : postfix : a


Token: * stack : * postfix : a
Token: ( stack : * ( postfix : a
Token: b stack : * ( postfix : a b
Token: - stack : * ( - postfix : a b
Token: c stack : * ( - postfix : a b c
Token: ) stack : * postfix : a b c -
Token: + stack : + postfix : a b c - *
Token: f stack : + postfix : a b c - * f
Token: * stack : + * postfix : a b c - * f
Token: g stack : + * postfix : a b c - * f g
Token: * stack : + * postfix : a b c - * f g *
Token: h stack : + * postfix : a b c - * f g * h
Postfix: abc-*fg*h*+
If the reading symbol is right parenthesis ‘)’, then Pop all the contents of the stack until the
respective left parenthesis is popped and append each popped symbol to Postfix Expression

8.3 If the input is over, pop all the remaining symbols from the stack and append them to the
postfix .

RESULT

The program is executed and output is verified.

38
PROGRAM

#include <stdlib.h>
#include <stdio.h>
struct node
{ int info;
struct node *link;
};
struct node *head=NULL;
void display()
{ struct node *ptr;
if(head==NULL)
printf("List is empty!!");
else{
ptr=head;
printf("\nElements are:\n");
while(ptr!=NULL){
printf("%d\t",ptr->info);
ptr=ptr->link;
}}}
void insertf(int ele)
{ struct node *new;
new=(struct node *)malloc(sizeof(struct node));
new->info=ele;
new->link=head;
head=new;
display();
}
void insertend(int ele)
{ struct node *new,*ptr;
new=(struct node *)malloc(sizeof(struct node));
new->info=ele;
new->link=NULL;
if(head==NULL)
head=new;
else{
ptr=head;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=new;}
display();
}
void insertafter(int key,int ele)
{ struct node *new,*ptr;
if(head==NULL)
printf("Insertion is not possible,empty list!!");
else{
ptr=head;
while(ptr->info!=key && ptr->link!=NULL)
ptr=ptr->link;
if(ptr->info!=key)
printf("Insertion not possible,element not found!!");
else{
new=(struct node *)malloc(sizeof(struct node));
new->info=ele;
new->link=ptr->link;
ptr->link=new; }}
display();
}
void insertbefore(int key,int ele)
{ struct node *new,*ptr,*prev=NULL;
ptr=head;
while(ptr!=NULL && ptr->info!=key ){
prev=ptr;
ptr=ptr->link;}
if(ptr==NULL)
printf("Insertion not possible,element not found!!");
else
{printf("Insert after %d %d!",key,ele);
EXPERIMENT NO.13 Date:

SINGLY LINKED LIST

AIM

To write a c program to implement singly linked list.

ALGORITHM
Set a newnode() containing info and link fields.
Declare a node pointer head and set head=NULL
1.display()
Declare node pointer *ptr.
1.1 if head=NULL ,
display the list is empty.
1.2 else,
set ptr=head
while ptr != NULL
display ptr->info
set ptr = ptr->link
2.insertfront(element)
Declare node pointer *new, new=newnode()
2.1 set new-> info=element
new->link=head
head=new
display()
3.insertend(element)
declare two node pointers *new and *ptr,new=newnode()
3.1 set new-> info=element
new->link=NULL
3.2 if head=NULL
then set head=new
3.3 else
set ptr=head
while ptr->link != NULL
set ptr= ptr->link
ptr->link=new
display()
4. insertafter(element,key)
declare two node pointers *new and *ptr,new=newnode()
4.1 if head=NULL
then display the list is empty
4.2 else
set ptr=head
4.2.1 while ptr->info != key and ptr->link != NULL
ptr=ptr->link

40
new=(struct node *)malloc(sizeof(struct node));
new->info=ele;
new->link=ptr;
if(ptr==head)
head=new;
else
prev->link=new; }
display();
}
void deletefront()
{ struct node *temp;
if(head==NULL)
printf("Deletion not possible,empty list!!");
else{
temp=head;
head=head->link;
free(temp);}
display();
}
void deleteend()
{ struct node *prev,*curr,*temp;
if(head==NULL)
printf("Deletion not possible,empty list!!");
else if(head->link==NULL) {
temp=head;
head=NULL;
free(temp); }
else{
prev=head;
curr=head->link;
while(curr->link!=NULL){
prev=curr;
curr=curr->link; }
prev->link=NULL;
free(curr); }
display();
}
void deleteany(int key)
{ struct node *prev,*curr,*temp;
if(head==NULL)
printf("Deletion not possible,empty list!!");
else if(head->info==key){
temp=head;
head=head->link;
free(temp); }
else {
prev=head;
curr=head;
while(curr->info!=key && curr->link!=NULL) {
prev=curr;
curr=curr->link;}
if(curr->info!=key)
printf("Deletion is not possible,element not found!!");
else{
prev->link=curr->link;
free(curr); }}
display();
}
void main()
{ int ele,ch,key;
do{ printf("\n1.Insert front\n2.Delete front\n3.Insert end\n4.Delete end\n5.Insert after \n6.Insert before \n7.Delete any
\n8.Display\n9.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch){ case 1: printf("Enter the element to be pushed:");
scanf("%d",&ele);
insertf(ele);
break;
case 2: deletefront();
break;
if ptr->info != key
then display element not found.
else set new->info=element
new->link=ptr->link
ptr->link=new
4.3 display()

5.insertbefore(element, key)
declare three node pointers *new ,*prev, *ptr,prev=NULL,ptr=head
5.1 while ptr != NULL and ptr->info != key
set prev=ptr
ptr=ptr->link
5.2 if ptr=NULL ,
then display element not found.
5.3 else
new=newnode()
new->info=element
new->link=ptr
5.3.1 if ptr=head
then set head=new
5.3.2 else set prev->link =new
5.4 display()
6.deletefront()
declare a node pointer *temp
6.1 if head= NULL
then display the list is empty.
6.2 else
set temp=head
head=head->link
free(temp)
6.3 display()
7.deleteend()
declare three node pointers *curr ,*prev, *temp,
7.1 if head= NULL
then display the list is empty.
7.2 else if head->link =NULL
set temp=head
head=NULL
free(temp)
7.3 else
set prev=head
curr=head->link
7.3.1 while curr ->link !=NULL
set prev=curr
curr=curr->link
7.3.2 prev ->link=NULL
free (curr)
7.4 display()

42
case 3: printf("Enter the element to be pushed:");
scanf("%d",&ele);
insertend(ele);
break;
case 4: deleteend();
break;
case 5: printf("Enter the element to be pushed:");
scanf("%d",&ele);
printf("Enter the element after which given element should be pushed:");
scanf("%d",&key);
insertafter(key,ele);
break;
case 6: printf("Enter the element to be pushed:");
scanf("%d",&ele);
printf("Enter the element before which given element should be pushed:");
scanf("%d",&key);
insertbefore(key,ele);
break;
case 7: printf("Enter the element to be deleted:");
scanf("%d",&ele);
deleteany(ele);
break;
case 8: display();
break;
case 9: printf("Program terminated.\n");
break;
default: printf("\nInvalid choice!!!\n");
}}while(ch!=9);
}

OUTPUT
1.Insert front
2.Delete front
3.Insert end
4.Delete end
5.Insert after
6.Insert before
7.Delete any
8.Display
9.Exit
Enter your choice : 1
Enter the element to be pushed : 42

1.Insert front
2.Delete front
3.Insert end
4.Delete end
5.Insert after
6.Insert before
7.Delete any
8.Display
9.Exit
Enter your choice : 3
Enter the element to be pushed : 80

1.Insert front
2.Delete front
3.Insert end
4.Delete end
8.deleteany(key)
declare three node pointers *curr ,*prev, *temp
8.1 if head= NULL
then display the list is empty.
8.2 else if head->info = key
set temp=head
head = head->link
free(temp)
8.3 else
set prev=head and curr=head
8.3.1 while curr->info != key and curr->link != NULL
set prev=curr
curr=curr->link
8.3.2 if curr->info != key,
then display element not found
else
set prev->link=curr->link
free(curr)
8.4 display()

44
5.Insert after
6.Insert before
7.Delete any
8.Display
9.Exit
Enter your choice : 5
Enter the element to be pushed : 55
Enter the element before. which given element should be pushed: 67
Element not found

1.Insert front
2.Delete front
3.Insert end
4.Delete end
5.Insert after
6.Insert before
7.Delete any
8.Display
9.Exit
Enter your choice : 8
42
80

1.Insert front
2.Delete front
3.Insert end
4.Delete end
5.Insert after
6.Insert before
7.Delete any
8.Display
9.Exit
Enter your choice : 4

1.Insert front
2.Delete front
3.Insert end
4.Delete end
5.Insert after
6.Insert before
7.Delete any
8.Display
9.Exit
Enter your choice : 8
42

1.Insert front 7.Delete any


2.Delete front 8.Display
3.Insert end 9.Exit
4.Delete end Enter your choice : 9
5.Insert after Program terminated.
6.Insert before
RESULT

The program is executed and output is verified.

46
PROGRAM

#include <stdio.h>
#include <stdlib.h>
struct node{
int info;
struct node *prev;
struct node *next;};
struct node *head,*tail;
void insertfront(int ele)
{ struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->info=ele;
p->prev=NULL;
p->next=head;
if(head==NULL){
head=tail=p;}
else{
head->prev=p;
head=p;
}}
void insertlast(int ele)
{ struct node *p,*curr;
p=(struct node*)malloc(sizeof(struct node));
p->info=ele;
p->next=NULL;
p->prev=tail;
if(tail==NULL){
head=tail=p;}
else{
tail->next=p;
tail=p;
}}
void insertafter(int val,int ele)
{ struct node *p,*curr;
curr=head;
while(curr!=NULL && curr->info!=val)
curr = curr->next;
if(curr==NULL)
printf("No such element");
else
p=(struct node*)malloc(sizeof(struct node));
p->info=ele;
p->prev=curr;
p->next=curr->next;
if(curr->next==NULL)
tail=p;
else
curr->next->prev=p;
curr->next=p;
}}
void insertbefore(int val,int ele)
{ struct node *p,*curr;
curr=head;
while(curr!=NULL && curr->info!=val)
curr = curr->next;
if(curr==NULL){
printf("No such element");}
else{
p=(struct node*)malloc(sizeof(struct node));
p->info=ele;
p->prev=curr->prev;
p->next=curr;
if(curr->prev==NULL)
head=p;
else
curr->prev->next=p;
curr->prev=p;
}}
EXPERIMENT NO.14 Date:

DOUBLY LINKED LIST

AIM

To write a c program to implement doubly linked list.

ALGORITHM
Set a newnode() containing info and link fields.
Declare a node pointer head and set head=NULL
1.display()
Declare node pointer *curr.
1.1 if head=NULL ,display the list is empty.
1.2 else, set curr=head
while curr != NULL
display curr->info
set curr = curr->next
2.insertfront(element)
Declare node pointer *new, new=newnode()
2.1 set new-> info=element
new->prev= new->next=NULL
2.2 if head =NULL then set head=new
2.3 else set new->next=head
head->prev=new
head=new
2.4 display()
3.insertend(element)
declare two node pointers *new and *ptr,new=newnode()
3.1 set new-> info=element
new->prev= new->next=NULL
3.2 if head=NULL then set head=new
3.3 else set ptr=head
while ptr->next != NULL
set ptr= ptr->next
ptr->next=new
new->prev=ptr
3.4 display()
4. insertafter(element,key)
declare two node pointers *new and *ptr,new=newnode()
4.1 if head=NULL then display the list is empty
4.2 else set ptr=head
4.2.1 while ptr->info != key and ptr->next != NULL
ptr=ptr->next
4.2.2 if ptr->info != key ,then display element not found.
4.2.3 else set new->info=element
new->prev=ptr
new->next=ptr->next
if new->next!=NULL,new->next->prev=new
48
void deletenode(int val)
{ struct node *curr;
curr=head;
while(curr!=NULL && curr->info!=val)
curr=curr->next;
if(curr==NULL)
printf("No such node");
else{
if(curr->prev==NULL)
head=curr->next;
else
curr->prev->next=curr->next;
if(curr->next==NULL)
tail=curr->prev;
else
curr->next->prev=curr->prev;
free(curr);
}}
void display()
{ struct node *curr;
if(head==NULL)
printf("List is empty");
else{
curr=head;
printf("Elements are;\n");
while(curr!=NULL){
printf("%d\t",curr->info);
curr=curr->next;}
}}
void main()
{ int x,val,ele,choice;
do{printf("\n1. Insertfirst \n2. Insertlast \n3. Insertafter \n4. Insertbefore \n5. Delete Node \n6. Display linkedlist \n7. To
Exit\nEnter Choice : ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the Element : ");
scanf("%d",&ele);
insertfront(ele);
break;
case 2: printf("Enter the Element : ");
scanf("%d",&ele);
insertlast(ele);
break;
case 3: printf("Enter the Element after which you want to insert : ");
scanf("%d",&val);
printf("Enter the Element : ");
scanf("%d",&ele);
insertafter(val,ele);
break;
case 4: printf("Enter the Element before which you want to insert : ");
scanf("%d",&val);
printf("Enter the Element : ");
scanf("%d",&ele);
insertbefore(val,ele);
break;
case 5: printf("Enter the Element to Delete : ");
scanf("%d",&val);
deletenode(val);
break;
case 6: printf("\n");
display();
break;
default:printf("\nExited!!!\n\n");
}}
while(choice!=7);
getchar();
}
ptr->next=new
4.3 display()
5.insertbefore(element, key)
declare two node pointers *new,*ptr,ptr=head
5.1 while ptr != NULL and ptr->info != key
set ptr=ptr->next
5.2 if ptr=NULL ,then display element not found.
5.3 else new=newnode()
new->info=element
new->prev=ptr->prev
new->next=ptr
5.4 if ptr->prev=NULL then set head=new
5.5 else set ptr->prev->next=new
ptr->prev=new
5.6 display()
6.deletenode(element)
declare a node pointer *curr,curr=head
6.1 while curr !=NULL and curr->info!= element
curr=curr->next
6.2 if curr= NULL then display no such node.
6.3 else
6.3.1 if curr->prev=NULL,set head=curr->next
6.3.2 else, set curr->prev->next=curr->next
if curr->next =NULL then tail=curr->prev
6.3.3 else curr->next->prev=curr->prev
6.3.4 free(temp)

50
OUTPUT

1. Insertfirst
2. Insertlast
3. Insertafter
4. Insertbefore
5. Delete Node
6. Display linkedlist
7. To Exit
Enter Choice : 1
Enter the Element : 23

1. Insertfirst
2. Insertlast
3. Insertafter
4. Insertbefore
5. Delete Node
6. Display linkedlist
7. To Exit
Enter Choice : 2
Enter the Element : 78

1. Insertfirst
2. Insertlast
3. Insertafter
4. Insertbefore
5. Delete Node
6. Display linkedlist
7. To Exit
Enter Choice : 6

Elements are;
23 78
1. Insertfirst
2. Insertlast
3. Insertafter
4. Insertbefore
5. Delete Node
6. Display linkedlist
7. To Exit
Enter Choice : 7

Exited!!!
RESULT

The program is executed and output is verified.

52
PROGRAM
#include <stdlib.h>
#include <stdio.h>
struct node
{
int info;
struct node *link;
};
struct node *head=NULL;
void display()
{ struct node *ptr;
if(head==NULL)
printf("List is empty!!");
else{
ptr=head;
printf("\nElements are:\n");
while(ptr!=NULL){
printf("%d\t",ptr->info);
ptr=ptr->link;
}}}
void push(int ele)
{ struct node *new,*ptr;
new=(struct node *)malloc(sizeof(struct node));
new->info=ele;
new->link=NULL;
if(head==NULL)
head=new;
else{
ptr=head;
while(ptr->link!=NULL)
ptr=ptr->link;
ptr->link=new;}
display();
}
void pop()
{ struct node *prev,*curr,*temp;
if(head==NULL)
printf("Deletion not possible,empty list!!");
else if(head->link==NULL){
temp=head;
head=NULL;
free(temp);}
else{
prev=head;
curr=head->link;
while(curr->link!=NULL){
prev=curr;
curr=curr->link;}
prev->link=NULL;
free(curr);}
display();
}
void main()
{ int ele,ch,key;
do{ printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch){case 1: printf("Enter the element to be pushed:");
scanf("%d",&ele);
push(ele);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("Program terminated.\n");
break;
default: printf("\nInvalid choice!!!\n"); }
}while(ch!=4); }
EXPERIMENT NO.15 Date:

STACK USING LINKED LIST

AIM

To write a c program to implement stack using linked list.

ALGORITHM
Set a newnode() containing info and link fields.
1. display()
1.1 if head=NULL, display the list is empty
1.2 else,set ptr=head
while ptr != NULL,display ptr->info
ptr= ptr->link
2. push(element)
declare node pointers *new and *ptr,new=newnode()
2.1 set new->info=element
new->link=NULL
2.2 if head= NULL, set, head=new
2.3 else, set ptr=head
while ptr->link!= NULL,
ptr=ptr->link
ptr->link=new
2.4 display()
3. pop()
declare node pointers *prev,*curr,*temp
3.1 if head=NULL, display the list is empty
3.2 else if head->link =NULL
temp=head
head=NULL
free(temp)
3.3 else set prev=head and curr=head->link
while curr->link != NULL,
prev=curr
curr=curr->link
prev->link=NULL
free(curr)
3.4 display()

54
OUTPUT

1.push
2.pop
3.display
4.exit

Enter choice: 1
enter element: 22

1.push
2.pop
3.display
4.exit

Enter choice: 1
enter element: 67

1.push
2.pop
3.display
4.exit

Enter choice: 1
enter element: 59

1.push
2.pop
3.display
4.exit

Enter choice: 3
22
67
59

1.push
2.pop
3.display
4.exit

Enter choice: 2

1.push
2.pop
3.display
4.exit

Enter choice: 4
Program terminated.
RESULT

The program is executed and output is verified.

56
PROGRAM

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;};
struct node * front = NULL;
struct node * rear = NULL;
void enqueue(int value) {
struct node * ptr;
ptr = (struct node * ) malloc(sizeof(struct node));
ptr - > data = value;
ptr - > next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear - > next = ptr;
rear = ptr;}
printf("Node is Inserted\n");}
int dequeue() {
if (front == NULL) {
printf("\nQueue is empty.");
return -1;
} else {
struct node * temp = front;
int temp_data = front - > data;
front = front - > next;
free(temp);
return temp_data;
}}
void display() {
struct node * temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d", temp - > data);
temp = temp - > next; }
printf("NULL\n\n");
}}
void main() {
int choice, value;
do { printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", & choice);
switch (choice) {
case 1: printf("\nEnter the value to insert: ");
scanf("%d", & value);
enqueue(value);
break;
case 2: printf("Popped element is :%d\n", dequeue());
break;
case 3: display();
break;
case 4: exit(0);
break;
default: printf("\nWrong Choice\n");
}}
while (choice!=4);}
EXPERIMENT NO.16

QUEUE USING LINKED LIST

AIM

To write a c program to implement queue using linked list.

ALGORITHM
Set a newnode() containing data and link fields.
Declare node pointers *front and *rear , set front=rear=NULL.
1. enqueue(element)
declare node pointer *ptr, ptr=newnode()
1.1 ptr->data=element
ptr->next=NULL
1.1 if front=NULL and rear=NULL,
then front =rear= ptr
1.2 else,set rear->next=ptr
rear=ptr
1.3 display node is inserted.
2.dequeue()
2.1 if front=NULL,then display queue is empty.
2.2 temp=front
temp->data=front->data
front=front->next
free(temp)
return temp->data
3. display()
declare node pointer *temp.
3.1 if front=NULL and rear=NULL,
display,queue is empty.
3.2 else ,set temp=front
while(temp),display temp->data
temp=temp->next

58
OUTPUT
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Enter the value to insert : 61

1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Enter the value to insert : 94

1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 1
Enter the value to insert : 85

1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
61
94
85

1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 2

1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 3
94
85

1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice : 4
RESULT

The program is executed and output is verified.

60
CSL 201
DATA STRUCTURES LAB

You might also like