Vous êtes sur la page 1sur 43

http://www.vidyarthiplus.

com

DATA STRUCTURE
Lab Manual
(CS 2208)

LIST OF EXPERIMENTS
1. SINGLY LINKED LIST
2. DOUBLY LINKED LIST
3. REPRESENT A POLYNOMIAL AS A LINKED LIST
4. ARRAY IMPLEMENTATION OF STACK ADT
5. LINKED LIST IMPLEMENTATION OF STACK ADT
6. CONVERSION OF INFIX EXPRESSION TO POSTFIX NOTATION
7. IMPLEMENT A DOUBLE-ENDED QUEUE
8. IMPLEMENT AN EXPRESSION TREE
9. IMPLEMENT A BINARY SEARCH TREE
10. IMPLEMENT AN INSERTION IN AVL TREES
11. IMPLEMENT A PRIORITY QUEUE USING BINARY HEAPS
12. IMPLEMENT A PRIMS ALGORITHM TO FIND THE MST

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:1
SINGLY LINKED LIST
AIM:
Aim to create a singly linked list and perform insertion and deletion operation any where
in the list
ALGORITHM:
STEP 1: Get option from the user.
STEP 2: If the option is 1 perform creation operation and goto step3.
If the option is 2 perform insertion operation and goto step7.
If the option is 3 perform deletion operation and goto step9.
If the option is 4 perform exit operation and goto step10.
STEP 3: Initialize pointer to NULL.
STEP 4: Get data from user assign the links to new node.
STEP 5: Get the next data from user and make proper links.
STEP 6: Repeat step5 until user stop the input elements.
STEP 7: Get the element and position of insertion create a node and assign its
user inputted.

data as

STEP 8: Make the new node is properly linked with existing list.
STEP 9: Get the element to be deleted. Choose the data field at each node if element to be
deleted is found. Separate the node from existing list by changing the links.
STEP 10: Stop the execution.
PROGRAM:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define null 0
struct element_list
{
int info;
struct element_list *next;
};
typedef struct element_list node;
node *create(node*);
node *insert(node*);
node *delet(node*);
void display(node*);

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
void main()
{
node *start;
int key,choice,newinfo,delinfo,position,quit;
char c;
clrscr();
start=NULL;
quit=0;
do
{
printf("Program for singly Linked List\n\n");
printf("\n Options \t\t choices");
printf("\n c \t\t\t Create");
printf("\n i \t\t\t Insert");
printf("\n d \t\t\t Delete");
printf("\n q \t\t\t Quit");
do
c=getchar();
while(strchr("CcIiDdQq",c)==NULL);
switch(c)
{
case 'c':
case 'C':
start=create(start);
printf("\nLINKED LIST");
display(start);
break;
case 'i':
case 'I':
start=insert(start);
printf("\nLINKED LIST");
display(start);
break;
case 'd':
case 'D':
start=delet(start);
printf("\nLINKED LIST");
display(start);
break;
case 'q':
case 'Q':
quit=1;
}
}while(!quit);
printf("\n");
return;
}
node *create(node *start)
{
node *temp,*prev;
int element;
prev=start=NULL;
printf("\n Enter the integer -99 to exit:");
scanf("%d",&element);
while(element!=-99)
{
temp=(node*)malloc(sizeof(node));
temp->info=element;
temp->next=NULL;
if(start==NULL)

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
start=temp;
else
prev->next=temp;
prev=temp;
printf("\n Enter the integer -99 to exit:");
scanf("%d",&element);
}
return start;
}
void display(node *start)
{
printf("\n The root-->");
while(start!=NULL)
{
printf("%d-> ",start->info);
start=start->next;
}
printf("NULL\n\n");
}
node *insert(node *start)
{
node *newnode,*temp;
int i,newinfo,position;
printf("Enter the element to be inserted:");
scanf("%d",&newinfo);
do
{
printf("\n Enter position of insertion:");
scanf("%d",&position);
}
while(position<1);
newnode=(node*)malloc(sizeof(node));
newnode->info=newinfo;
if((position==1)||(start==NULL))
{
newnode->next=start;
start=newnode;
}
else
{
temp=start;
i=2;
while((i<position)&&(temp->next!=NULL))
{
temp=temp->next;
++i;
}
newnode->next=temp->next;
temp->next=newnode;
}
return start;
}
node *delet(node *start)
{
node *temp,*prev;
int i,delinfo;
printf("Enter the element to be deleted:");
scanf("%d",&delinfo);

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
if(start==NULL)
printf("\n Can't Delete -- List Empty");
else
prev=NULL;
temp=start;
while((temp!=NULL)&&(temp->info!=delinfo))
{
prev=temp;
temp=temp->next;
}
if(temp==NULL)
printf("Element not found \n");
else
{
if(prev==NULL)
start=start->next;
else
{
prev->next=temp->next;
free(temp);
printf("\n LINKED LIST\n");
}
}
return start;
}

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:2
DOUBLY LINKED LIST
AIM:
Aim is to create a doubly linked list using structure and perform insertion and deletion.
ALGORITHM:
STEP 1: Create a structure node and initialize two pointers next and previous as variables
and the info to assign the element field.
STEP 2: Initially head=NULL.
STEP 3: Get the choice from the user. Choices are insert, delete, show and exit.
If the choice is 1 perform insertion operation and goto step4.
If the choice is 2 perform deletion operation and goto step5.
If the choice is 3 perform display operation and goto step6.
If the choice is 4 perform exit operation and goto step7.
STEP 4: Insertion option , Initially head=NULL, if a new list is created starts from empty
head there nodeinfo=element and new nodenext= new node previous
=NULL. Now head is assigned new node, if insertion within an existing list means,
new node is created using malloc. New node info=element and new node
next=new nodeprevious=NULL. Then traverse upto the end of the existing list.
There joining the end of the existing list with the new node by traverse next.
New node previous = traverse.
STEP 5: Deletion, Initially head is assigned as traverse, find the node to be deleted, there
the node to be deleted is assigned as traverse then proceed with the following
instruction traverse nextprevious= traverse previous traverseprevnext=
traversenext.
.
STEP 6: Starts from the first node of the linked list upto the end of the list show the
element of each and every node.
STEP 7: Stop the execution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
char info;
struct node *next,*prev;
}*head=NULL,*newnode ,*Traverse;
insertdata(char chh1)
{
if(head==NULL)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->info=chh1;
newnode->next=newnode->prev=NULL;
head=newnode;
}
else
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->info=chh1;
newnode->next=newnode->prev=NULL;
Traverse=head;
while(Traverse->next!=NULL)
Traverse=Traverse->next;
Traverse->next=newnode;
newnode->prev=Traverse;
}
return;
}
deletedata(char chh1)
{
Traverse=head;
if(Traverse->info==chh1)
{
head=head->next;
free(Traverse);
}
else
{
while(Traverse->info!=chh1)
Traverse=Traverse->next;
Traverse->next->prev=Traverse->prev;
Traverse->prev->next=Traverse->next;
free(Traverse);
}
return;
}
showdata()
{
Traverse=head;
while(Traverse->info!=NULL)
{
printf("<-%c->",Traverse->info);
Traverse=Traverse->next;
}
return;
}
void main(void)
{
char chh;
char i;

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
clrscr();
while(1)
{
printf("\n1.insert 2.Delete ");
printf("3.show 4.exit \n ");
printf("\n\nEnter any one of the choice :--");
scanf("\n%c",&i);
switch(i)
{
case '1' :
printf("\nEnter a CHAR--");
scanf ("\n%c",&chh);
insertdata(chh);
break;
case '2' :
printf("\nEnter the CHAR U want to delete :: ");
scanf ("\n%c", &chh);
deletedata(chh);
break;

}
}

case '3' :
showdata();
break;
case '4' :
exit(0);
break ;
default :
printf("\n invalid choice\n" );
break;

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:3
REPRESENT A POLYNOMIAL AS A LINKED LIST
AIM:
Aim is to write a program in C to convert given infix expression in to postfix notation
ALGORITHM:
STEP1: Get the two polynomials. First polynomial is P1 and second polynomial is P2
STEP2: For addition of two polynomials if exponents of both the polynomials are same
then we ad the coefficients. For storing the result we will create the third linked
lists say P3.
STEP3: If Exponent of P2 is greater than exponent of P1 then keep the P3 as P2.
STEP4: If Exponent of P2 is greater than exponent of P1 then keep the P3 as P1
STEP5: If Exponent of P2 is equal to the exponent of P1 then add the coefficient of P1
and coefficient of P2 as coefficient of P3.
STEP6: Continue the above step from 3 to 5 until end o the two polynomials.
STEP7: If any of the polynomial is ended keep P3 as the remaining polynomial.
STEP8: Stop the execution.

PROGRAM:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL, *poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
printf("\n Enter coefficient:");
scanf("%d",&node->coeff);
printf("\n Enter power:");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\nContinue(y/n):");
ch=getch();

}
while(ch=='y'||ch=='Y');

void show(struct link *node)


{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next||poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;

}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;

}
void main()
{
char ch;
do
{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\n Enter 1st number:");
create(poly1);
printf("\n Enter 2nd number:");
create(poly2);
printf("\n 1st Number:");
show(poly1);
printf("\n 2nd number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\n Added polynomial:");
show(poly);
printf("\n Add two more numbers:");
ch=getch();
}
while(ch=='y'||ch=='Y');
}

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:4
ARRAY IMPLEMENTATION OF STACK ADT
AIM:
Aim is to write a program in C to implement the stack ADT using array concept that
performs all the operations of stack.
ALGORITHM:
STEP 1: Define an array to store the element.
STEP 2: Get the users choice.
STEP 3: If the option is 1 perform creation operation and goto step4.
If the option is 2 perform insertion operation and goto step5.
If the option is 3 perform deletion operation and goto step6.
If the option is 4 perform display operation and goto step7.
STEP 4: Create the stack. Initially get the limit of stack and the get the items. If the limit
of stack is exceeds print the message unable to create the stack.
STEP 5: Get the element to be pushed. If top pointer exceeds stack capacity. Print Error
message that the stack overflow. If not, increment the top pointer by one and store
the element in the position which is denoted by top pointer.
STEP 6: If the stack is empty, then print error message that stack is empty. If not fetch the
element from the position which is denoted by top pointer and decrement the top
pointer by one
.
STEP 7: If the top value is not less than the 0 the stack is display otherwise print the
message stack is empty.
STEP 8: Stop the execution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define max 20
int opt, a[20],i,top=0,n;
void main()
{
void create(),push(),pop(),disp();
int wish;
do
{

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
clrscr();
printf("\nMENU");
printf("\n1.Create\n2.Push\n3.pop\n4.Display\n5.Exit\n");
printf("\nEnter your option");
scanf("%d",&opt);
switch(opt)
{
case 1:create();break;
case 2:push();break;
case 3:pop();break;
case 4:disp();break;
case 5:exit(0);
}
printf("\nDo u want to cintinue(1/0):");
scanf("%d",&wish);
}while(wish==1);
}
void create()
{
printf("\n Enter the limit of stack");
scanf("%d",&n);
if(n<max)
{
printf("\nEnter the items");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
top=n-1;
}
else
printf("\nUnable to create the stack");
}
void push()
{
int x;
if(top<max)
{
printf("\nEnter the element to be pushed:");
scanf("%d",&x);
top=top+1;
a[top]=x;
n=top;
}
else
printf("\n Stack is full");
}
void pop()
{
if(top<0)
printf("\n Stack is empty");
else
{
printf("\nThe element popped is %d",a[top]);
top=top-1;
n=top;
}
}
void disp()

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
{

if(top<0)
printf("\n Stack is empty");
else
{
printf("\n The elements in the stack are:");
for(i=top;i>=0;i--)
printf("\n%d",a[i]);
}

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:5
LINKED LIST IMPLEMENTATION OF STACK ADT
AIM:
Aim is to write a stack which is implemented using linked list concept and perform the
push and pop operation..
ALGORITHM:
STEP 1: Start the program defined a structure of linked list.
STEP 2: Set the options for performing stack operations.
STEP 3: If the option is 1, call the function create.
Create a pointer structure and given details on it.
Create a pointer to point the structure. While creating another structure C.
After creating enough nodes design the last pointer of the node to the first
node.
STEP 4: If the option is 2, call the push operation. The element is pushed and it is pointed
at top.
STEP 5: If the option is 3, Call the pop function the element is popped out and the pointer
points the previous node.
STEP 6: If the option is 4, call view function. Print the entire element until the last node
print to null.
STEP 7: If the option is 5, exit from the program.
STEP 8: Stop the execution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*top,*new1,*first;
void main()
{
int wish,opt;
void create(),push(),pop(),view();
do

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
{

clrscr();
printf("Stack using linked list menu");
printf("\n1.Create\n2.Push\n3.Pop\n4.View\n5.Exit\n");
printf("\nEnter your option(1,2,3,4,5):");
scanf("%d",&wish);
switch(wish)
{
case 1: create(); break;
case 2: push(); break;
case 3: pop(); break;
case 4: view(); break;
case 5: exit(0);
}
printf("\nDo you wnat to continue(0/1):");
scanf("%d",&opt);
}while(opt==1);

void create()
{
int ch;
top=(struct node*)malloc(sizeof(struct node));
top->next=NULL;
do
{
clrscr();
printf("Enter the data:\n");
scanf("%d",&top->data);
printf("Do you want to insert another(1/0)\n");
scanf("%d",&ch);
if(ch==1)
{
new1=(struct node*)malloc(sizeof(struct node));
new1->next=top;
top=new1;
first=top;
}
else
break;
}while(ch==1);
}
void push()
{
top=first;
new1=(struct node*)malloc(sizeof(struct node));
printf("Enter the element to be pushed:");
scanf("%d",&new1->data);
new1->next=top;
top=new1;
first=top;
}
void pop()
{
clrscr();
top=first;
if(top==NULL)
printf("\n Stack is empty");
else

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
{

printf("\nThe element popped out from stack is %d",top->data);


top=top->next;
first=top;

void view()
{
printf("\nStack contents\n");
while(top->next!=NULL)
{
printf("%d->",top->data);
top=top->next;
}
printf("%d\n",top->data);
getch();
}

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:6
CONVERSION OF INFIX EXPRESSION TO POSTFIX NOTATION
AIM:
Aim is to write program in C to convert given infix expression to postfix notation
ALGORITHM:
STEP 1: Get an infix expression.
STEP 2: Scan the expression from left to right.
STEP 3: If any operands come display it.
STEP 4: If the incoming symbol in a operator and has more priority then the symbol into
the stack.
STEP 5: If the incoming operator has less priority than the stack symbol then copy the
symbol at the top of the stack and then print until the condition becomes false and
push the following operator on the stack.
STEP 6: If the symbol is ) then copy operators from top of the stack. Deletion opening
parenthesis is from top of the stack.
STEP 7: Stop the process.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
char in[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
void main()
{
clrscr();
printf("enter the infix expression");
scanf("%s",in);
postfix();
getch();
}
void postfix()
{
int i,j=0;
for(i=0;in[i]!='\0';i++)
{

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
switch(in[i])
{
case'+':
while(st[top]>=2)
post[j++]=pop();
push(1);
break;
case'-':
while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case'*':
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case'/':
while(st[top]>=3)
post[j++]=pop();
push(4);
break;
case'^':
while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case'(':
push(0);
break;
case')':
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=in[i];
}
}
while(top>0)
post[j++]=pop();
printf("postfix expression is %s",post);
}
void push(int element)
{
top++;
st[top]=element;
}
char pop()
{
int e1;
char e;
e1=st[top];
top--;
switch(e1)
{
case 1:
e='+';

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
break;
case 2:
e='-';
break;
case 3:
e='*';
break;
case 4:
e='/';
break;
case 5:
e='^';
break;

}
return(e);

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:7
IMPLEMENT A DOBLE-ENDED QUEUE
AIM:
Aim is to write a program in C to implement a double ended queue to perform the
restricted insertion and restricted deletion.
ALGORITHM:
STEP 1: Get the choice from the user; if the choice is 1 go to step2.
If the choice is 2 go to step 3. If the choice is 3 go to step 4
STEP 2: Restricted insertion: insertion is performed at the right side and deletion is
performed at both left and right.
STEP 3: Restricted deletion: deletion is performed at the left side and insertion is
performed at both left and right.
STEP 4: Main operations:

Insert right step 5


Insert left step 6
Delete right step 7
Delete left step 8
Display step 9

STEP 5: Insert right:


If left=0 & right=max-1 or left=right+1
Queue overflow.
If left==-1. queue empty
Left=0&right=0
Else if
right=max-1 then right=0
Else
right=right+1;
Get the element to be inserted . place the element at the value of right in the
array.
STEP 6: Insert left:
If Left==0
Queue overflow
Queue empty
Else if
Left==0 then left=max-1
Else
Left=left-1
Get the element to be inserted value of left in the array.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
STEP 7 : Delete right:
If left==-1
Queue underflow
Element deleted is the right
If Left=right
Only one element
Else if
right==0 then right=max-1
Else
right=right-1
STEP 8: Delete left:
Queue underflow
Element deleted is the left pointed
If Left=right
Queue only one element
Else if
Left==max-1;left=0
Else
Left=left+1
STEP 9: Display
Starts from left to right of the array print the values.
STEP10:Stop the execution.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 5
int deque_arr[MAX];
int left = -1;
int right = -1;
void main()
{
int choice;
clrscr();
printf("1.Input restricted dequeue\n");
printf("2.Output restricted dequeue\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
input_que();
break;
case 2:
output_que();
break;
default:
printf("Wrong choice\n");
}/*End of switch*/

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
}/*End of main()*/
input_que()
{
int choice;
while(1)
{
printf("1.Insert at right\n");
printf("2.Delete from left\n");
printf("3.Delete from right\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
delete_left();
break;
case 3:
delete_right();
break;
case 4:
display_queue();
break;
case 5:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of input_que() */
output_que()
{
int choice;
while(1)
{
printf("1.Insert at right\n");
printf("2.Insert at left\n");
printf("3.Delete from left\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
display_queue();
break;
case 5:
exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of output_que() */
insert_right()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{
printf("Queue Overflow\n");
return;
}
if (left == -1) /* if queue is initially empty */
{
left = 0;
right = 0;
}
else
if(right == MAX-1) /*right is at last position of queue */
right = 0;
else
right = right+1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[right] = added_item ;
}/*End of insert_right()*/
insert_left()
{
int added_item;
if((left == 0 && right == MAX-1) || (left == right+1))
{
printf("Queue Overflow \n");
return;
}
if (left == -1)/*If queue is initially empty*/
{
left = 0;
right = 0;
}
else
if(left== 0)
left=MAX-1;
else
left=left-1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item);
deque_arr[left] = added_item ;
}/*End of insert_left()*/
delete_left()
{
if (left == -1)
{

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",deque_arr[left]);
if(left == right) /*Queue has only one element */
{
left = -1;
right=-1;
}
else
if(left == MAX-1)
left = 0;
else
left = left+1;
}/*End of delete_left()*/
delete_right()
{
if (left == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",deque_arr[right]);
if(left == right) /*queue has only one element*/
{
left = -1;
right=-1;
}
else
if(right == 0)
right=MAX-1;
else
right=right-1;
}/*End of delete_right() */
display_queue()
{
int front_pos = left,rear_pos = right;
if(left == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
{
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
front_pos = 0;

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
while(front_pos <= rear_pos)
{
printf("%d ",deque_arr[front_pos]);
front_pos++;
}
}/*End of else */
printf("\n");
}/*End of display_queue() */

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:8
IMPLEMENT AN EXPRESSION TREE
AIM:
Aim is to write a program in C to implement the expression tree and perform the in order,
preorder and post order traversals.
ALGORITHM:
STEP 1: Get the postfix expression from the user.
STEP 2: Read the expression char by char
STEP 3: If the read char is the operand store it in the stack,
STEP 4: If the read char is the symbol, perform pop operation in the stack and keep it as
the right and left nodes of the symbol, now a new node is created with the left and
right nodes.
STEP 5: continue this up to the end of the post fix notation.
STEP 6: Traversals: Inorder go to step 7
Preorder go to step 8
Postorder go to step 9
STEP 7: Visit the left node first
Root node next
Right node at the last
STEP 8: Visit the Root node first
Left node next
Right node at the last
STEP 9: Visit the Left node first
Right node Next
Root node at the last
STEP 10: Stop execution
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<ctype.h>
#define size 20

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
typedef struct node
{
char data;
struct node *left;
struct node *right;
}btree;
/*stack stores the operand nodes of the tree*/
btree *stack[size];
int top;
void main()
{
btree *root;
char exp[80]; //exp stores postfix expression
btree *create(char exp[80]);
void inorder(btree *root);
void preorder(btree *root);
void postorder(btree *root);
clrscr();
printf("Enter the postfix expression\n");
scanf("%s",exp);
top=-1; //Initialise the stack
root=create(exp);
printf("\n Tree is created...\n");
printf("\n The inorder traversal is... \n");
inorder(root);
printf("\n The preorder traversal is ... \n");
preorder(root);
printf("\n The postorder traversal is ... \n");
postorder(root);
getch();
}
btree* create(char exp[])
{
btree *temp;
int pos;
char ch;
void push(btree *);
btree *pop();
pos=0;
ch=exp[pos];
while(ch!='\0')
{
/* Create anew node */
temp=(btree *)malloc(sizeof(btree));
temp->left=temp->right=NULL;
temp->data=ch;
if(isalpha(ch))
//is it a operand
push(temp); //push operand
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
{
/* it is operator, so pop two nodes from stack
set first node as right child and
set second as left child and push the operator
node on to the stack
*/
temp->right=pop();
temp->left=pop();

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
push(temp);
}
else
printf("Invalid character expression\n");
pos++;
ch=exp[pos];//read next character
}
temp=pop();
return(temp);
}
void push(btree *node)
{
if(top+1>=size)
printf("Error: Stack is Full\n");
top++;
stack[top]=node;
}
btree* pop()
{
btree *node;
if(top==-1)
printf("Error:Stack is Empty\n");
node=stack[top];
top--;
return(node);
}
void inorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
inorder(temp->left);
printf("%c",temp->data);
inorder(temp->right);
}
}
void preorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
printf("%c",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
void postorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
postorder(temp->left);

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
postorder(temp->right);
printf("%c",temp->data);
}

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:9
IMPLEMENT BINARY SEARCH TREE
AIM:
Aim is to construct a binary search tree and perform the search and deletion operation.
ALGORITHM:
CONSTRUCTION
STEP 1: Get the total number of elements
STEP 2: Store them in an array.
STEP 3: Read first element from array and form first node of tree.(root node)
STEP 4: Read the next element and compare it with the previous node.
STEP 5: If the node is less than previous node then store it as left child. If it is
greater store it as right child.
STEP 6: Repeat step 4.
STEP 7: Stop the process.
SEARCHING A NODE IN BST
STEP 1: Obtain the search element.
STEP 2: Start to search the element by scanning the tree from root node.
STEP 3: compare the search element with the root node. If the search element is less than
the root then move it to left branch of tree from current node. Else move in to the
right branch of the tree.
STEP 4: Repeat the step3.
STEP 5: Stop the process.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct treenode;
typedef struct treenode* position;

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
typedef struct treenode* searchtree;
searchtree insert(int x,searchtree t);
searchtree delet(int x,searchtree t);
searchtree makeempty(searchtree t);
position find(int x, searchtree t);
position findmin(searchtree t);
void inorder(searchtree t);
struct treenode
{
int element;
searchtree left;
searchtree right;
};
searchtree makeempty(searchtree t)
{
if(t!=NULL)
{
makeempty(t->left);
makeempty(t->right);
free(t);
}
return NULL;
}
position find(int x,searchtree t)
{
if(t==NULL)
return NULL;
if(x<t->element)
return find(x,t->left);
else
return t;
}
position findmin(searchtree t)
{
if(t==NULL)
return NULL;
else if(t->left==NULL)
return t;
else if(t->left==NULL)
return t;
else
return findmin(t->left);
}
searchtree insert(int x,searchtree t)
{
if(t==NULL)
{
t=malloc(sizeof(struct treenode));
if(t==NULL)
printf("Out of space!!!!!");
else
{
t->element=x;
t->left=t->right=NULL;
}
}
else if(x<t->element)
t->left=insert(x,t->left);
else if(x>t->element)
t->right=insert(x,t->right);
return t;

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
}
searchtree delete(int x, searchtree t)
{
position tempcell;
if(t==NULL)
printf("\n Element not found");
else if(x<t->element)
t->left=delete(x,t->left);
else if(x>t->element)
t->right=delete(x,t->right);
else if(t->left && t->right)
{
tempcell=findmin(t->right);
t->element=tempcell->element;
t->right=delete(t->element,t->right);
}
else
{
tempcell=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
free(tempcell);
}
return t;
};
void inorder(searchtree t)
{
if(t!=NULL)
{
inorder(t->left);
printf("%d\t",t->element);
inorder(t->right);
}
}
void main()
{
searchtree t;
position p;
int i,opt,n,a;
t=makeempty(NULL);
clrscr();
do
{
printf("\n Enter the option");
printf("1.Create \n 2.insert \n3.Delete \n 4.Exit");
scanf("%d", &opt);
switch(opt)
{
case 1:printf("enter the no. of elements");
scanf("%d",&n);
printf("enter the elements to insert");
for(i=0;i<n;i++)
{
scanf("%d",&a);
t=insert(a,t);
}
printf("binary search tree");

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
inorder(t);
break;
case 2:printf("enter the element to insert");
scanf("%d",&a);
t=insert(a,t);
printf("binary search tree");
inorder(t);
break;
case 3:printf("enter the element to delete");
scanf("%d",&a);
t=delete(a,t);
printf("binary search tree");
inorder(t);
break;
case 4:exit(0);
}
fflush(stdin);
}
while(getchar()!=0);
}

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:10
IMPLEMENT INSERTION IN AVL TREES
AIM:
Aim is to write a program in C to perform insertion operation in AVL trees
ALGORITHM:
STEP 1: AVL tree is a balancing tree
STEP 2: Create a new node and insert the element in that node.
STEP 3: The left and right node of the new node is NULL
STEP 4: left node values are smaller that root and the right node values are greater than
the root
STEP 5: Compare the value of the root with the node to be inserted, if the node value is
smaller go to the left side, if node value is grater go to the right side
STEP 6: Continue this step until we find the proper position of the node to be inserted,
after insertion its left and right sub tree are NULL
STEP 7: If the height is not balanced at the left with the value smaller than the root
perform single rotation with left else perform double rotation with left.
STEP 8: If the height is not balanced at the right with the value greater than the root,
perform single rotation with right else perform double rotation with right.
STEP 9: Stop execution
PROGRAM:
#include<stdio.h>
#include<conio.h>
typedef enum { FALSE ,TRUE } bool;
struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};
struct node *insert (int , struct node *, int *);
struct node* search(struct node *,int);

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
main()
{
bool ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;
while(1)
{
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
if( search(root,info) == NULL )
root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
struct node* search(struct node *ptr,int info)
{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}/*End of search()*/
struct node *insert (int info, struct node *pptr, int *ht_inc)
{
struct node *aptr;
struct node *bptr;

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
if(pptr==NULL)
{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}
if(info < pptr->info)
{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1: /* Right heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = 1;
break;
case 1: /* Left heavy */
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf("Left to Left Rotation\n");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;
if(bptr->balance == 1 )
pptr->balance = -1;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}/*End of switch */
}/*End of if */
}/*End of if*/

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
if(info > pptr->info)
{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1: /* Left heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = -1;
break;
case -1: /* Right heavy */
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf("Right to Right Rotation\n");
pptr->rchild= aptr->lchild;
aptr->lchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Right to Left Rotation\n");
bptr = aptr->lchild;
aptr->lchild = bptr->rchild;
bptr->rchild = aptr;
pptr->rchild = bptr->lchild;
bptr->lchild = pptr;
if(bptr->balance == -1)
pptr->balance = 1;
else
pptr->balance = 0;
if(bptr->balance == 1)
aptr->balance = -1;
else
aptr->balance = 0;
bptr->balance=0;
pptr = bptr;
}/*End of else*/
*ht_inc = FALSE;
}/*End of switch */
}/*End of if*/
}/*End of if*/
return(pptr);
}/*End of insert()*/
display(struct node *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/
inorder(struct node *ptr)
{
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:11
IMPLEMENT PRIORITY QUEUE USING BINARY HEAPS
AIM:
Aim is to write a program in C to implement an Ascending priority queue with insertion is
at proper position and deletion is at the front
ALGORITHM:
STEP 1: Get the choice from the user, if choice is 1 go to step 2
If choice is 2 go to step 3
If choice is 3 go to step 4
STEP 2: Insertion: Insertion is in ascending order.
Compare the element to be inserted with all the elements in the queue and insert
the node at the correct position.
After insertion rearrange the elements in the array
STEP 3: Deletion: deletion is at the front
Smaller element is deleted; the element to be deleted is pointed by the front
variable.
STEP 4: Display:
Starts from the first to the last element of the queue, print the values of the array
STEP 5: Stop
PROGRAM:
# include<stdio.h>
# include<conio.h>
struct node
{
int priority;
int info;
struct node *link;
}*front = NULL;
main()
{
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
insert()
{
struct node *tmp,*q;
int added_item,item_priority;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the item value to be added in the queue : ");
scanf("%d",&added_item);
printf("Enter its priority : ");
scanf("%d",&item_priority);
tmp->info = added_item;
tmp->priority = item_priority;
/*Queue is empty or item to be added has priority more than first
item*/
if( front == NULL || item_priority < front->priority )
{
tmp->link = front;
front = tmp;
}
else
{
q = front;
while( q->link != NULL && q->link->priority <= item_priority )
q=q->link;
tmp->link = q->link;
q->link = tmp;
}/*End of else*/
}/*End of insert()*/
del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp = front;
printf("Deleted item is %d\n",tmp->info);
front = front->link;
free(tmp);

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com
}
}/*End of del()*/
display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{ printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{
printf("%5d %5d\n",ptr->priority,ptr->info);
ptr = ptr->link;
}
}/*End of else */
}/*End of display() */

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

http://www.vidyarthiplus.com

EX.NO:12
IMPLEMENT PRIMS ALGORITHM
AIM:
Aim is to write a program in C to generate the minimum spanning tree of the given graph.
ALGORITHM:
STEP1: Get the number of nodes from the user
STEP2: Get the number of edges from the user
STEP3: Get all the nodes and its weights from the user.
STEP4: Initialize the selected vertices list
STEP5: Select an edge such that one vertex is selected and other is not and the edge has
the least weight.
STEP6: Obtain the edge with minimum weight
STEP7: Picking up those vertices and print them.
STEP8: Stop

RESULT:
Thus the program has been executed successfully.

http://www.vidyarthiplus.com

Vous aimerez peut-être aussi