cs2208 lab manua
TRANSCRIPT
PRINCE SHRI VENKATESHWARA ENGINEERING COLLEGE
PONMAR, CHENNAI-48
Department of Computer Science and Engineering
Lab Manual
CS2208 - Data Structures Lab(III Semester CSE)
Prepared by:
Mrs. Subhasini. M (Lecturer / CSE)
CS2208-DATA STRUCTURES LAB 1
CS 2208 DATA STRUCTURES LAB 0 0 3 2AIM:
To develop programming skills in design and implementation of data structures and theirapplications.
1. Implement singly and doubly linked lists.
2. Represent a polynomial as a linked list and write functions for polynomial addition.
3. Implement stack and use it to convert infix to postfix expression
4. Implement a double-ended queue (dequeue) where insertion and deletion operations are possible at both the ends.
5. Implement an expression tree. Produce its pre-order, in-order, and postorder traversals.
6. Implement binary search tree.
7. Implement insertion in AVL trees.
8. Implement priority queue using binary heaps
9. Implement hashing with open addressing.
10. Implement Prim's algorithm using priority queues to find MST of an undirected graph. Total: 45
List of Equipments and components for A Batch of 30 students (1 per batch)
1. SOFTWARE REQUIRED – TURBOC version 3 or GCC version 3.3.4.2. OPERATING SYSTEM – WINDOWS 2000 / XP / NT OR LINUX3. COMPUTERS REQUIRED – 30 Nos. (Minimum Requirement : Pentium III or
4.Pentium IV with 256 RAM and 40 GB harddisk)
CS2208-DATA STRUCTURES LAB 2
Ex.no:1(a) IMPLEMENTATION OF SINGLY LINKED LIST
AIM:
To write a ‘C’ program to create a singly linked list and to insert, delete, modify,
display and count the elements in the list.
ALGORITHM:
Algorithm for main program:
Step 0: Start the program
Step 1: Initialize head pointer as NULL.
Step 2: Get the choice from the user.
Step 2.1: If choice is 0 then call displaymenu() function
Step 2.2: If choice is 1 then ,
Step 2.2.0: open a do loop
Step 2.2.1: Allocate the memory space for the new node.
Step 2.2.2: Get the data for the new node.
Step 2.2.3: if head==NULL then
Assign head<-newnode
Assign last<-head
Step 2.2.4: if head!=NULL then
Assign last->link=newnode
Assign last=newnode
Step 2.2.5: loop continues till we give ‘N’ to ch
Step 2.3: If choice is 1 then
Step 2.3.0: if head==NULL then
Print SLL is empty
Step 2.3.1: if head!=NULL then
Get the roll no for modification
Set the for loop
Check the rollno with the rollno in the list
CS2208-DATA STRUCTURES LAB 3
If yes, get the new rollno to be modified
If no,then print rollno doesn’t exist
Step 2.4: If choice is 2 then
Step 2.4.0: if head==NULL then
Print SLL is empty
Step 2.4.1: if head!=NULL then
Using for loop print the no of elements and rollno in the list.
Step 2.5: if choice is 3 then
Step 2.5.0: Assign memory for newnode and get data for it.
Step 2.5.1: if head==NULL then
Assign head=newnode
Assign last=head
Step 2.5.2 if head!=NULL then
Assign newnode->link=head;
Assign head=newnode;
Step 2.6: if choice is 5 then
Step 2.6.0: Assign memory for newnode and get data for it.
Step 2.6.1: if head==NULL then
Assign head=newnode
Step 2.6.2: if head !=NULL then
Using the for loop traverse the list till the last node
If lastnode link field is NULL,then
Assign newnode->link=NULL
Assign last->link=newnode
Step 2.7: if choice is 6 then
Step 2.7.0: if head==NULL then
Print SLL is empty
Step 2.7.1: if head !=NULL then
Get the rollno after which insertion to be made
Using for loop check the given rollno is matching with rollno in the list
If yes, get the newnode value then
CS2208-DATA STRUCTURES LAB 4
Assign newnode->link=curr->link
Assign curr->link=newnode
If no, then print rollno not found
Step 2.8: if choice is 7 then
Step 2.8.0: if head==NULL then
Print SLL is empty
Step 2.8.1: if head!=NULL then
Assign delnode=head and head=head->link
print the deleted rollno from the list
step 2.9: if choice is 8 then
Step 2.9.0: if head==NULL then
Print SLL is empty
Step 2.9.1: if head!=NULL then
Traverse the list till the last node using for loop
Check whether the head->link==NULL
If yes, assign delnode=head and head=NULL
If no, assign last=head
Using while loop check whether all nodes linkfield!=NULL in
the list
if yes, assign prev=last and last=last->link
if no, assign delnode=last and prev->link=NULL
print the rollno deleted from the list
step 2.10: if choice is 9 then
Step 2.10.0: if head==NULL then
Print SLL is empty
Step 2.10.1: assign curr=head
Step 2.10.2: read the rollno for deletion
Step 2.10.3: using for loop check whether the given rollno matching with the
rollno
in the list
if yes, assign delnode=curr then
CS2208-DATA STRUCTURES LAB 5
check if curr==head
if yes, assign head=head->link
if no, assign prev->link=curr->link
print the rollno deleted from the list
if no, print rollno not found
step 2.11: print end of the program
step 2.12: stop the program.
Algorithm for displaymenu() function:
Step 1: displays all the menus as create list,modify etc.,
Algorithm for getnode():
Step 1: allocate the memory for the newnode
Algorithm for readnode() function:
Step 1: get the value of rollno
Step 2: assign newnode->link=NULL
PROGRAM CODING:–#include<stdio.h>#include<conio.h>#include<stdlib.h>typedefstruct list{ int roll; struct list *link;}node;voiddisplaymenu();node *getnode();voidreadnode(node*);voidreleasenode(node*);void main() { node *head=NULL; node *newnode,*delnode,*prev,*curr,*last; int i,n,ch,count; int data,deldata,moddata;
CS2208-DATA STRUCTURES LAB 6
clrscr(); displaymenu(); while(1) { fflush(stdin); printf("\n\n?"); scanf("%d",&ch); switch(ch) { case 0:displaymenu(); break; case 1:head=NULL; do { newnode=getnode(); readnode(newnode); if(head==NULL) { head=newnode; last=head; } else { last->link=newnode; last=newnode; } printf("\nDo you wish to continue:"); ch=getch(); } while((ch=='Y')||(ch=='y')); break; case 2:if(head==NULL) { printf("\nSLL is empty"); break; } printf("\nEnter the roll no for modification:"); scanf("%d",&moddata); for(curr=head;curr!=NULL;curr=curr->link) if(curr->roll==moddata) { printf("\nEnter the new roll:"); scanf("%d",&curr->roll); break; } if(curr==NULL)
CS2208-DATA STRUCTURES LAB 7
printf("\nRoll not found"); break; case 3: if(head==NULL) { printf("\nSLL is empty"); break; } for(count=0,curr=head;curr!=NULL;curr=curr->link) printf("\n%d-->Roll No:%d",++count,curr->roll); printf("\nNo of elements:%d",count); break; case 4:newnode=getnode(); readnode(newnode); newnode->link=head; head=newnode; break; case 5:newnode=getnode(); readnode(newnode); if(head==NULL) { head=newnode; break; } for(last=head;last->link!=NULL;last=last->link); newnode->link=NULL; last->link=newnode; break; case 6:if(head==NULL) { printf("\nSLL is empty"); break; } printf("\nEnter the roll no after which the insertion is to be made:"); scanf("%d",&data); for(curr=head;curr!=NULL;curr=curr->link) if(curr->roll==data) { newnode=getnode(); readnode(newnode); newnode->link=curr->link; curr->link=newnode; break; } if(curr==NULL) printf("\nRoll No not found"); break;
CS2208-DATA STRUCTURES LAB 8
case 7:if(head==NULL) { printf("\nSLL is empty"); break; } delnode=head; head=head->link; printf("\nDeleted data is..."); printf("\nRoll No:%d",delnode->roll); releasenode(delnode); break; case 8:if(head==NULL) { printf("\nSLL is empty"); break; } for(curr=head;curr->link!=NULL;curr=curr->link); if(head->link==NULL) { delnode=head; head=NULL; } else { last=head; while(last->link!=NULL) { prev=last; last=last->link; } delnode=last; prev->link=NULL; } printf("\nDeleted data is...."); printf("\nRoll No:%d",delnode->roll); releasenode(delnode); break; case 9:if(head==NULL) { printf("\nSLL is empty"); break; } curr=head; printf("\nEnter the roll no for deletion:"); scanf("%d",&deldata); for(;curr!=NULL;prev=curr,curr=curr->link)
CS2208-DATA STRUCTURES LAB 9
{ if(curr->roll==deldata) { delnode=curr; if(curr==head) head=head->link; else prev->link=curr->link; printf("\nDeleted data is...."); printf("\nRoll No:%d",curr->roll); releasenode(delnode); break; } } if(curr==NULL) printf("\nRoll No not found"); break; default:releasenode(head); printf("\nEnd of the program"); exit(0);}}}void displaymenu() { printf("\nBasic operation in Singly Linked List"); printf("\n1.Create List"); printf("\n2.Modify List"); printf("\n3.View List"); printf("\n4.Insert First"); printf("\n5.Insert Last"); printf("\n6.Insert Middle"); printf("\n7.Delete First"); printf("\n8.Delete Last"); printf("\n9.Delete Middle"); printf("\n0.Show Menu"); printf("\n10.Exit"); }node *getnode() { return(node*)malloc(sizeof(node)); }void readnode(node *newnode) { printf("\nEnter the roll no:"); scanf("%d",&newnode->roll); newnode->link=NULL; }void releasenode(node *newnode)
CS2208-DATA STRUCTURES LAB 10
{ free(newnode); }
Ex.no:1(b) IMPLEMENTATION OF DOUBLY LINKED LIST
AIM:
To write a ‘C’ program to create a singly linked list and to insert, delete, modify,
display and count the elements in the list.
ALGORITHM:
Algorithm for main program:
Step 0: Start the program
Step 1: Initialize head pointer as NULL.
Step 2: Get the choice from the user.
Step 2.1: If choice is 0 then call displaymenu() function
Step 2.2: If choice is 1 then ,
Step 2.2.0: using for loop get the input from user
Step 2.2.1: if head==NULL then
Assign head=newnode
Assign last=head
Step 2.2.2: if head!=NULL then
Assign newnode->blink=last
Assign last->flink=newnode
Assign last=newnode
Step 2.3: If choice is 2 then
Step 2.3.0: if head==NULL then
Print DLL is empty
Step 2.3.1: if head!=NULL then
Get the roll no for modification
Set the for loop
Check the rollno with the rollno in the list
If yes, get the new rollno to be modified
CS2208-DATA STRUCTURES LAB 11
If no,then print rollno doesn’t exist
Step 2.4: If choice is 3 then
Step 2.4.0: if head==NULL then
Print DLL is empty
Step 2.4.1: if head!=NULL then
Using for loop print the no of elements and rollno in the list.
Step 2.5: if choice is 4 then
Step 2.5.0: Assign memory for newnode and get data for it.
Step 2.5.1: assign newnode->flink=head
Step 2.5.2 if head!=NULL then
Assign newnode->blink=head
Assign head=newnode
Step 2.6: if choice is 4 then
Step 2.6.0: Assign memory for newnode and get data for it.
Step 2.6.1: if head==NULL then
Assign head=newnode
Assign last=newnode
Step 2.6.2: if head !=NULL then
Using the for loop traverse the list till the last node
If lastnode link field is NULL,then
Assign newnode->blink=last
Assign last->flink=newnode
Step 2.7: if choice is 6 then
Step 2.7.0: if head==NULL then
Print DLL is empty
Step 2.7.1: if head !=NULL then
Get the rollno after which insertion to be made
Using for loop check the given rollno is matching with rollno in the list
If yes, get the newnode value then
Assign next=curr->flink
Assign newnode->flink=next
CS2208-DATA STRUCTURES LAB 12
Assign newnode->blink=curr
Assign curr->link=newnode
If next!=NULL then
Assign next->blink=newnode
If no, then print rollno not found
Step 2.8: if choice is 7 then
Step 2.8.0: if head==NULL then
Print DLL is empty
Step 2.8.1: Assign delnode=head and head=head->flink
Step 2.8.2: if head!=NULL
Assign head->blink=NULL
Step 2.8.3: print the deleted rollno from the list
Step 2.9: if choice is 8 then
Step 2.9.0: if head==NULL then
Print DLL is empty
Step 2.9.1: if head!=NULL then
Traverse the list till the last node using for loop
Assign delnode=last
Step 2.9.2: check whether last=head
If yes, Assign head=NULL
If no, assign prev=last->blink
If yes, assign delnode=head and head=NULL
If no, assign last=head
Step 2.9.3: assign prev->flink=NULL
Step 2.9.4: print the rollno deleted from the list
Step 2.10: if choice is 9 then
Step 2.10.0: if head==NULL then
Print DLL is empty
Step 2.10.1: read the rollno for deletion
CS2208-DATA STRUCTURES LAB 13
Step 2.10.2: using for loop check whether the given rollno matching with the
rollno
in the list
if yes, assign delnode=curr then
check if curr==head
if yes, assign head=head->flink
check if head!=NULL
if yes, assign head->blink=NULL
if no, assign prev=curr->blink
assign next=curr->flink
assign prev->flink=next
print the rollno deleted from the list
if next!=NULL then
assign next->blink=prev
print the deleted value
step 2.11: print end of the program
step 2.12: stop the program.
Algorithm for displaymenu() function:
Step 1: displays all the menus as create list,modify etc.,
Algorithm for getnode():
Step 1: allocate the memory for the newnode
Algorithm for readnode() function:
Step 1: get the value of rollno and name
Step 2: assign newnode->flink=NULL
Step 3: assign newnode->blink=NULL
PROGRAM CODING:
#include<stdio.h>
CS2208-DATA STRUCTURES LAB 14
#include<conio.h>#include<stdlib.h>typedef struct list{int roll;char name[20];struct list *flink,*blink;}node;void displaymenu();node *getnode();void readnode(node*);void releasenode(node*);void main() { node *head=NULL; node *newnode,*delnode,*prev,*next,*curr,*last; int i,n,ch,count; int data,deldata,moddata; clrscr(); displaymenu(); while(1) { fflush(stdin); printf("\n\n ?"); scanf("%d",&ch); switch(ch) { case 0:displaymenu(); break; case 1:head=NULL; printf("\nEnter the limit:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter the [%d] details:",(i+1)); newnode=getnode(); readnode(newnode); if(head==NULL) { head=newnode; last=head; } else { newnode->blink=last; last->flink=newnode;
CS2208-DATA STRUCTURES LAB 15
last=newnode; } } break; case 2:if(head==NULL) { printf("\nDLL is empty"); break; } printf("\nEnter the roll no for modification:"); scanf("%d",&moddata); for(curr=head;curr!=NULL;curr=curr->flink) if(curr->roll==moddata) { printf("\nEnter the new roll:"); scanf("%d",&curr->roll); printf(“\nEnter the new name:”); scanf(“%s”,curr->name); break; } if(curr==NULL) printf("\nRoll not found"); break; case 3:if(head==NULL) { printf("\nDLL is empty"); break; } for(count=0,curr=head;curr!=NULL;curr=curr->flink) printf("\n%d-->Roll No:%d\tName:%s",++count,curr->roll,curr->name); printf("\nNo of elements:%d",count); break; case 4:newnode=getnode(); readnode(newnode); newnode->flink=head; if(head!=NULL) head->blink=newnode; head=newnode; break; case 5:newnode=getnode(); readnode(newnode); if(head==NULL) { head=newnode; last=newnode; break;
CS2208-DATA STRUCTURES LAB 16
} for(last=head;last->flink!=NULL;last=last->flink) newnode->blink=last; last->flink=newnode; break; case 6:if(head==NULL) { printf("\nDLL is empty"); break; } printf("\nEnter the roll no after which the insertion is to be made:"); scanf("%d",&data); for(curr=head;curr!=NULL;curr=curr->flink) if(curr->roll==data) { newnode=getnode(); readnode(newnode); next=curr->flink; newnode->flink=next; newnode->blink=curr; curr->flink=newnode; } if(next!=NULL) next->blink=newnode; break; } if(curr==NULL) printf("\nRoll No not found"); break; case 7:if(head==NULL) { printf("\nDLL is empty"); break; } delnode=head; head=head->flink; if(head!=NULL) head->blink=NULL; printf("\nDeleted data is..."); printf("\nRoll No:%d\tName:%s”,delnode->roll,delnode->name); releasenode(delnode); break; case 8:if(head==NULL) { printf("\nDLL is empty"); break;
CS2208-DATA STRUCTURES LAB 17
} for(last=head;last->flink!=NULL;last=last->flink); delnode=last; if(last==head) head=NULL; else prev=last->blink; prev->flink=NULL; printf("\nDeleted data is...."); printf("\nRoll No:%d\tName:%s",delnode->roll,delnode->name); releasenode(delnode); break; case 9:if(head==NULL) { printf("\nDLL is empty"); break; } printf("\nEnter the roll no for deletion:"); scanf("%d",&deldata); for(curr=head;curr!=NULL;curr=curr->flink) { if(curr->roll==deldata) { delnode=curr; if(curr==head) { head=head->flink; if(head!=NULL) head->blink=NULL; } else { prev=curr->blink; next=curr->flink; prev->flink=next; } if(next!=NULL) next->blink=prev; } printf("\nDeleted data is...."); printf("\nRoll No:%d",delnode->roll); releasenode(delnode); break; } } if(curr==NULL)
CS2208-DATA STRUCTURES LAB 18
printf("\nRoll No not found"); break; default:releasenode(head); printf("\nEnd of the program"); exit(0); }}}void displaymenu() { printf("\nBasic operation in Doubly Linked List"); printf("\n1.Create List"); printf("\n2.Modify List"); printf("\n3.View List"); printf("\n4.Insert First"); printf("\n5.Insert Last"); printf("\n6.Insert Middle"); printf("\n7.Delete First"); printf("\n8.Delete Last"); printf("\n9.Delete Middle"); printf("\n0.Show Menu"); printf("\n10.Exit"); }node *getnode(){ return(node*)malloc(sizeof(node));}void readnode(node *newnode) { printf("\nEnter the roll no:"); scanf("%d",&newnode->roll); printf("\nEnter the Name:"); scanf("%d",newnode->name); newnode->flink=NULL; newnode->blink=NULL; }void releasenode(node *newnode) { free(newnode);}
CS2208-DATA STRUCTURES LAB 19
Ex.no:2 POLYNOMIAL ADDITION USING SINGLY LINKED LIST
AIM:-To write a ‘C’ program to perform polynomial addition using singly linked list.
ALGORITHM:
Algorithm for main() function:
Step 1: Initialize poly1, poly2 and poly3 as NULL
Step 2: get the choice of the user
Step 2.1: if choice is 1 then
Step 2.1.1: call createpoly() function to get the first polynomial
Step 2.1.2: call viewpoly() function to view the first polynomial
Step 2.2: : if choice is 2 then
Step 2.2.1: call createpoly() function to get the second polynomial
Step 2.2.2: call viewpoly() function to view the second polynomial
Step 2.3: if choice is 3 then
Step 2.3.1: call viewpoly() function to view the first polynomial equation
Step 2.3.2: call viewpoly() function to view the second polynomial equation
Step 2.4: if choice is 4 then
Step 2.4.1: call addpoly() function to add two polynomial equation
Step 2.4.2: call again the viewpoly() function to see the resultant polynomial equation
Step 2.5: if choice is 0 then
Step 2.5.1: call viewmenu() function to view the menu
Step 2.6: print end of program
Step 3: stop the program.
Algorithm for createpoly() function:
CS2208-DATA STRUCTURES LAB 20
Step 1:Initialize head=NULL
Step 2: check whether (p->exp!=0)
If yes, Get the exponent and coefficient values for the polynomial equation
If no, return head
Algorithm for getnode() function:
Step 1: Allocate memory for every node of a polynomial equation
Algorithm for readnode() function:
Step 1: Get the coefficient from user
Step 2: Get the exponent value
Step 3: Assign newnode->coef=coef
Step 4: Assign newnode->exp=exp
Step 5: Assign newnode->next=NULL
Algorithm for insertnode() function:
Step 1: if p->coef==0.0
Return head
Step 2: if head==NULL
Return p
Step 3: else if(p->exp>head->exp) then
Assign p->next=head
Return p
Step 4: else if (p->exp<head->exp) then
Assign head->next=insertnode(head->next,p)
Step 5: return head.
Algorithm for viewpoly() function:
Step 1: if(ply==NULL)
Print the value is null
Step 2: print the polynomial equation when ply!=NULL
Algorithm for addpoly() function:
Step 1: Initialize head=NULL
Step 2: while poly1!=null
Assign head=insertnode(head,copynode(poly1,positive))
CS2208-DATA STRUCTURES LAB 21
Assign poly1=poly1->next
Step 3: while poly2!=null
Assign head=insertnode(head,copynode(poly2,positive))
Assign poly2=poly2->next
Step 4: return the value of head pointer
Algorithm for copynode() function:
Step 1: create newnode for copying
Step 2: assign newnode->coef=sign*p->coef
Step 3: assign newnode->exp=p->exp
Step 4: assign newnode->next=NULL
Step 5: assign newnode
Algorithm for viewmenu() function
Step 1: display all the menus like create first polynomial,create second polynomial etc.,
PROGRAM CODING:–#include<stdio.h>#include<conio.h>#include<stdlib.h>#define positive 1typedef struct node { float coef; int exp; struct node *next;}poly; poly *getnode(); poly *createpoly(); poly *addpoly(poly*,poly*); void viewmenu(); void readnode(poly*); void viewpoly(poly*); poly *insertnode(poly*,poly*); poly *copynode(poly*,int); main() { poly *poly1=NULL,*poly2=NULL,*poly3=NULL; intch; clrscr(); viewmenu();
CS2208-DATA STRUCTURES LAB 22
while(1) { printf("\n ?"); fflush(stdin); scanf("%d",&ch); switch(ch) { case 1:printf("\nEnter the first polynomial:"); poly1=createpoly(); printf("\nFirst polynomial is..."); viewpoly(poly1); break; case 2:printf("\nEnter the second polynomial:"); poly2=createpoly(); printf("\nSecond polynomial is:"); viewpoly(poly2); break; case 3:printf("\nThe first polynomial is:"); viewpoly(poly1); printf("\nSecond polynomial is:"); viewpoly(poly2); break; case 4:poly3=addpoly(poly1,poly2); printf("\nResultant polynomial is:"); viewpoly(poly3); break; case 0: clrscr(); viewmenu (); break; default:printf("\nEnd of program:"); exit(0);}}}poly *createpoly(){ poly *p,*head=NULL; do { p=getnode(); readnode(p); head=insertnode(head,p); } while(p->exp!=0); return head;}poly *getnode(){
CS2208-DATA STRUCTURES LAB 23
return(poly*)malloc(sizeof(poly));}void readnode(poly *newnode) { int exp; float coef; printf("\nEnter the coefficient:"); scanf("%f",&coef); printf("\nEnter the exponent:"); scanf("%d",&exp); newnode->coef=coef; newnode->exp=exp; newnode->next=NULL; }poly *insertnode(poly *head,poly *p){ if(p->coef==0.0f) return head; if(head==NULL) return p; else if(p->exp>head->exp) { p->next=head; return p; } else if(p->exp<head->exp) head->next=insertnode(head->next,p) ; else if((head->coef=head->coef+p->coef)==0.0f) return head->next; return head;}void viewpoly(poly *ply) { if(ply==NULL) printf("\nNULL"); while(ply!=NULL) { printf("%.2fx^%d",ply->coef,ply->exp); printf("%s",(ply->next==NULL)?"=0\n":"+"); ply=ply->next; }}poly *addpoly(poly *poly1,poly *poly2){ poly *head=NULL; while(poly1!=NULL) {
CS2208-DATA STRUCTURES LAB 24
head=insertnode(head,copynode(poly1,positive)); poly1=poly1->next; } while(poly2!=NULL) { head=insertnode(head,copynode(poly2,positive)); poly2=poly2->next; } return head; }poly *copynode(poly *p,int sign) { poly *newnode; newnode=getnode(); newnode->coef=sign*p->coef; newnode->exp=p->exp; newnode->next=NULL; return newnode;}void viewmenu(){ printf("\nPolynomial Manipulation using Singly Linked List"); printf("\n0.Show Menu"); printf("\n1.Create First polynomial"); printf("\n2.Create Second polynomial"); printf("\n3.View First and second polynomial"); printf("\n4.Polynomial Addition"); printf("\n5.Exit"); }
CS2208-DATA STRUCTURES LAB 25
Ex.no:3 CONVERT INFIX TO POSTFIX EXPRESSION USING STACK
AIM:-To write a ‘C’ program to implement stack and use it to convert infix to postfix
expression.
ALGORITHM:-
1. Start the program
2. Scan the Infix string from left to right.
3. Initialise an empty stack.
4. If the scannned character is an operand, add it to the Postfix string. If the
scanned character is an operator and if the stack is empty Push the character to
stack.
If the scanned character is an Operand and the stack is not empty,
compare the precedence of the character with the element on top of the
stack (topStack). If topStack has higher precedence over the scanned
character Pop the stack else Push the scanned character to stack. Repeat
this step as long as stack is not empty and topStack has precedence over
the character.
Repeat this step till all the characters are scanned.
5. (After all characters are scanned, we have to add any character that the stack
may have to the Postfix string.) If stack is not empty add topStack to Postfix
string and Pop the stack. Repeat this step as long as stack is not empty.
6. Return the Postfix string.
7. Terminate the program.
CS2208-DATA STRUCTURES LAB 26
PROGRAM CODING:-#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>charinf[40],post[40];int top=0,st[20];void postfix();void push(int);char pop();voidmain(){clrscr();printf("\nEnter the infix expression:");scanf("%s",inf);postfix();getch();}void postfix(){inti,j=0;for(i=0;inf[i]!='\0';i++){switch(inf[i]){case '+':while(st[top]>=1)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;
CS2208-DATA STRUCTURES LAB 27
case ')':while(st[top]!=0)post[j++]=pop();top--;break;default:post[j++]=inf[i];}}while(top>0)post[j++]=pop();printf("\nPostfixexp:%s",post);}void push(intele){top++;st[top]=ele;}char pop(){int e1;char e;e1=st[top];top--;switch(e1){case 1:e='+';break;case 2:e='-';break;case 3:e='*';break;case 4:e='/';break;case 5:e='^';break;}return e;}
CS2208-DATA STRUCTURES LAB 28
Ex.no:4 DOUBLE ENDED QUEUE USING ARRAY
AIM:
To write a ‘C’ program to implement linear double ended queue (deque) using
arrays.
ALGORITHM:
Algorithm for main() function:
Step 1: Initialize front=-1, rear=1
Step 2: Get the choice from the user
Step 2.1: If choice is 0, call displaymenu() function to display the menus.
Step 2.2: If choice is 1, then
Step 2.2.1: get the data from the user
Step 2.2.2: check if front==0
If yes, print deque overflow on enqueue at front
Step 2.2.3: check if (front==-1 && rear==-1)
If yes, assign front=rear=0
Step 2.2.4: If above two condition are false , then front—
Step 2.2.5: assign queue[front]=data
Step 2.3: If choice is 2, then
Step 2.3.1: Get the data from the user
Step 2.3.2: if(rear=(qsize-1)), then
Print dequeue overflow on enqueue at rear
Step 2.3.3: if(front==-1 && rear==-1), then
Assign front=rear=0
CS2208-DATA STRUCTURES LAB 29
Step 2.3.4: if the above two conditions are false, then assign rear=rear+1
Step 2.3.5: Assign queue[rear]=data
Step 2.4: if choice is 3, then
Step 2.4.1: if(front==-1&&rear==-1), then
Print deque is empty
Step 2.4.2: if(front==0) ,then
Print deque full at front
Step 2.4.3: if(rear==(qsize-1))
Print deque is full at rear
Step 2.4.4: print the elements the queue using for loop
Step 2.5: if choice is 4, then
Step 2.5.1: if(front==-1&&rear==-1), then
Print deque underflow on deque at front
Step 2.5.2:else print the deleted value from front of the deque
If(front==rear),then
Assign front=rear=-1
Else
Assign front=front+1
Step 2.6: if choice is 5, then
Step 2.6.1: if(front==-1&&rear==-1), then
Print deque underflow on deque at rear
Step 2.5.2:else print the deleted value from rear end of the deque
If(front==rear),then
Assign front=rear=-1
Else
Assign rear=rear-1
Step 2.7: if choice is 6, then
Step 2.7.1: print the size of the deque
Step 2.7.2: check whether the front and rear equals -1
If yes, print current queue element is 0
Step 2.7.3: if front and rear not equals -1,then
CS2208-DATA STRUCTURES LAB 30
Print the no of elements in the deque
Step 2.8: if choice is 7 ,then
Step 2.8.1: if(front==-1&&rear==-1), then
Print deque is empty
Step 2.8.1: if front and rear not equals -1, then
Print the value at the front end
Step 2.9: if choice is 8 ,then
Step 2.9.1: if(front==-1&&rear==-1), then
Print deque is empty
Step 2.9.1: if front and rear not equals -1, then
Print the value at the front end
Step 2.10: print end of the program
Step 3: Stop the program
PROGRAM CODING:-#include<stdio.h>#include<conio.h>#include<stdlib.h>#define qsize 5voiddisplaymenu();voidmain(){int front=-1,rear=-1,queue[qsize];intf,data,ch;displaymenu();while(1){printf("\n\n?");scanf("%d",&ch);switch(ch){case 0:displaymenu();break;case 1:fflush(stdin);printf("\nEnter the data:");scanf("%d",&data);if(front==0){
CS2208-DATA STRUCTURES LAB 31
printf("\nDeque overflow on enqueue at front");break;}else if(front==-1&&rear==-1)front=rear=0;else front--;queue[front]=data;break;case 2:fflush(stdin);printf("\nEnter the data:");scanf("%d",&data);if(rear==(qsize-1)){printf("\nDeque overflow on enqueue at rear");break;}else if(front==-1&&rear==-1)front=rear=0;else rear++;queue[rear]=data;break;case 3:if(front==-1&&rear==-1){printf("\nDeque is empty");break;}if(front==0)printf("\nDeque full at front");else if(rear==(qsize-1))printf("\nTheDeque is full at rear");printf("\nThe content of deque is:\nFRONT");for(f=front;f!=rear+1;f++)printf("\n%d",queue[f]);printf("\nREAR");break;case 4:if(front==-1&&rear==-1)printf("\nDeque Underflows on deque at front");else{printf("\nThedequed value is %d",queue[front]);if(front==rear)front=rear=-1;else front++;}break;case 5:if(front==-1&&rear==-1)printf("\nDeque Underflows on deque at rear");
CS2208-DATA STRUCTURES LAB 32
else{printf("\nThedequed value is %d",queue[rear]);if(front==rear)front=rear=-1;else rear--;}break;case 6:printf("\nThedeque size is:%d",qsize);if(front==-1&&rear==-1)printf("\nCurren t Deque element:%d",0);elseprintf("\nThe current dequed value is %d",rear-front+1);break;case 7:if(front==-1&&rear==-1)printf("\nDeque is empty");elseprintf("\nThe value at front: %d",queue[front]);break;case 8:if(front==-1&&rear==-1)printf("\nDeque is empty");elseprintf("\nThe value at rear: %d",queue[rear]);break;default:printf(“\nEnd of the program”);exit(0);}}}void displaymenu(){printf("\nBasic operation in Deque");printf("\n1.Enque at front");printf("\n2.Enqueat rear");printf("\n3.View");printf("\n4.Deque at Front");printf("\n5.Deque at rear");printf("\n6.Size");printf("\n7.Value at front");printf("\n8.Value at rear");printf("\n9.exit");printf("\n0.Show Menu");}
CS2208-DATA STRUCTURES LAB 33
Ex.no:5 EXPRESSION TREE TRAVERSAL
AIM:-
To write a ‘C’ program to implement an expression tree. Produce its pre-order, in-
order, and post-order traversals.
ALGORITHM:
Algorithm for main() program:
Step 1: Start the process.
Step 2: Initialize and declare variables.
Step 3: Get the expression from the user as postfix expression.
Step 4: call create() function to create expression tree.
Step 5: call display() function to display the tree.
Step 6: call inorder() function to find the inorder traversal path
Step 7: call preorder() function to find the preorder traversal path
Step 8: call postorder() function to find the postorder traversal path
Step 9: stop the program
Algorithm for inorder() function:
Step 1: If root is not equal to NULL, then
o Traverse the left subtree in inorder.o Process the root node.o Traverse the right subtree in inorder.
CS2208-DATA STRUCTURES LAB 34
Algorithm for preorder() function:
Step 1: If root is not equal to NULL, then
o Process the root node.o Traverse the left subtree in preorder.o Traverse the right subtree in preorder.
Algorithm for postorder() function:
Step 1: If root is not equal to NULL, then
o Traverse the left subtree in postorder.o Traverse the right subtree in postorder.o Process the root node.
Algorithm for create() function:
Step 1: Get the postfix string as character by character from left to right using while loop.
Step 1.1: For each character(operator/operand) node will be created.
Step 1.2: If the character is alphabetical letter(operand) , then push into stack.
Step 1.3: If the character is operator then pop two operator out of the stack.
Step 1.4: If the character is not an operator/operand then print invalid expression
Step 2: If no more character is available for checking, then pop all the characters out of the
stack.
Algorithm for push() function:
Step 1: Check whether (top+1)>=stack size,
Step 1.1: If yes, then print stack overflow.
Step 1.2: If no, then increment the top value of stack and push the new value into
the stack.
Algorithm for pop() function:
Step 1: Check whether top==-1
Step 1.1: If yes, then print the stack is empty.
Step 1.2: If no, then decrement the top value of stack and pop the symbol
out of the stack.
Algorithm for display() function:
Step 1: check whether the roo is equal to NULL
CS2208-DATA STRUCTURES LAB 35
Step 1.1: If yes, return the NULL value.
Step 1.2: If no, display the expression tree as
o Traverse the right subtree.
o Process the root node.
o Traverse the left subtree.
PROGRAM CODING:-#include<stdio.h>#include<conio.h>#include<ctype.h>#include<stdlib.h>#define size 30typedefstruct node{char data;struct node *left,*right;}btree;void display(btree*,int);void push(btree*);btree *create(char exp[20]);btree *pop();btree *stack[size];int top=-1;void main(){btree *root=NULL;charexp[20];btree *create(char expr[20]);void preorder(btree *root);voidpostorder(btree *root);voidinorder(btree *root);clrscr();printf("\nEnter the expression:");scanf("%s",exp);root=create(exp);printf("\nTree created");display(root,0);printf("\nInorder:");inorder(root);printf("\nPreorder:");preorder(root);printf("\nPostorder:");postorder(root);getch();}
CS2208-DATA STRUCTURES LAB 36
void preorder(btree *root){if(root!=NULL){printf("%c",rootdata);preorder(rootleft);preorder(rootright);}}voidinorder(btree *root){if(root!=NULL){inorder(rootleft);printf("%c",rootdata);inorder(rootright);}}voidpostorder(btree *root){if(root!=NULL){postorder(rootleft);postorder(rootright);printf("%c",rootdata);}}btree *create(char exp[20]){btree *temp;int i=0;charch;ch=exp[i];while((ch=exp[i++])!='\0'){temp=(btree*)malloc(sizeof(btree));templeft=tempright=NULL;tempdata=ch;if(isalpha(ch))push(temp);else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){tempright=pop();templeft=pop();push(temp);}
CS2208-DATA STRUCTURES LAB 37
elseprintf("\nInvalid expression");}temp=pop();return(temp);}void push(btree *newnode){if((top+1)>=size)printf("\nStack Overflow");else stack[++top]=newnode;}btree *pop(){btree *temp;if(top==-1)printf("\nStack is empty");elsetemp=stack[top--];return temp;}void display(btree *root,int level){int k;if(root==NULL)return;display(rootright,level+1);printf("\n\n");for(k=0;k<level;k++)printf(" ");printf("%c",rootdata);display(rootleft,level+1);}
CS2208-DATA STRUCTURES LAB 38
Ex.no:6 BINARY SEARCH TREE
AIM:-To write a ‘C’ program to implement binary search tree.
ALGORITHM:-
Algorithm for main() function:
Step 1: Start the program.
Step 2: Initialize and declare variables.
Step 3: Get the choice from the user:
Step 3.1: If choice is 0, then call displaymenu() function
Step 3.2: If choice is 1, then call create() function.
Step 3.3: if choice is 2, then call insert() function.
Step 3.4: If choice is 3, then call find() function
Step 3.5: if choice is 4, then call findmin() function
Step 3.6: If choice is 5, then call findmax() function
Step 3.7: If choice is 6, then call delete1() function
Step 3.8: If choice is 7, then call display() function
Step 3.9: If choice is 8, then exit
Step 3.10: if choice is other than 0-8,then print invalid choice
Step 4: Stop the program.
Algorithm for create() function:
Step 1: Get the number of elements
Step 2: Using for loop get the elements one by one
CS2208-DATA STRUCTURES LAB 39
Step 3:call insert() function to insert data into the tree.
Algorithm for insert() function:
Step 1: Check whether t==NULL
If yes, Allocate the memory space for t
Step 1.1: Check whether t==NULL
If yes, then print out of space
If no, then assign t->no=x and t->left=t->right=NULL.
Step 2: Check whether x<t->no
Step 2.2: If yes, then
Assign t->left=insert(x,t->left) // call insert() function
Step 3: Check whether x>t->no
Step 3.3: If yes, then
Assign t->right=insert(x,t->right)
Step 4: Return the value of t.
Algorithm for delete1() function:
Step 1: Check whether t==NULL, then
Step 1.1: If yes, then print element is not found
Step 2: Check whether x<t->no, then
Step 2.1: If yes, then Assign t->left=delete1(x,t->left)
Step 3: Check whether x>t->no, then
Step 3.1: If yes, then Assign t->right=delete1(x,t->right)
Step 4: Check whether t->left and t->right has node(i.e., node with two child)
Step 4.1: If yes, then
CS2208-DATA STRUCTURES LAB 40
Assign k=findmin(t->right)
Assign temp->no=k
Assign t->no=temp->no
Assign t->right=delete1(t->no,t->right)
Step 5: Assign temp=t
Step 5.1: check whether t->left==NULL
If yes, then assign t=t->right
Step 5.2: check whether t->right==NULL
If yes, then assign t=t->left
Step 6: Return the value of t
Algorithm for findmin() function:
Step 1: When root not equal to NULL, traverse in the left subtree to find which node left
Link field is NULL. That node is considered to be the minimum value node.
Algorithm for findmax() function:
Step 1: When root not equal to NULL, traverse in the Right subtree to find which node
Right Link field is NULL. That node is considered to be the maximum value node.
Algorithm for find() function:
Step 1: Check whether t==NULL, then
Step 1.1: If yes, then return NULL
Step 2: Check whether x<t->no, then
Step 2.1: If yes, then return find(x,t->left)
Step 3: Check whether x>t->no, then
Step 3.1: If yes, then return find(x,t->right)
CS2208-DATA STRUCTURES LAB 41
Step 4: Return the value of t
Algorithm for display() function:
Step 1: check whether the roo is equal to NULL
Step 1.1: If yes, return the NULL value.
Step 1.2: If no, display the expression tree as
o Traverse the right subtree.
o Process the root node.
o Traverse the left subtree.
PROGRAM CODING:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
typedef struct tree
{
int no;
struct tree *right,*left;
}*node;
node insert(intx,node t);
node find(intx,node t);
int findmin(node t);
int findmax(node t);
void create();
void displaymenu();
node delete1(intx,node t);
void display(node t,int level);
node t=NULL;
intn,i,data,x;
void main()
{
CS2208-DATA STRUCTURES LAB 42
inta,min,max,n;
node val;
clrscr();
displaymenu();
while(1)
{
printf("\nEnter your choice:");
scanf("%d",&a);
switch(a)
{
case 0:displaymenu();
break;
case 1:create();
break;
case 2:printf("\nEnter the element for insertion:");
scanf("%d",&x);
t=insert(x,t);
break;
case 3:printf("\nEnter the searching element:");
scanf("%d",&x);
val=find(x,t);
if(val!=NULL)
printf("\nElement found");
elseprintf("\Element not found");
break;
case 4:min=findmin(t);
printf("\nMinimum value:%d",min);
break;
case 5:max=findmax(t);
printf("\nMinimum value:%d",max);
break;
CS2208-DATA STRUCTURES LAB 43
case 6:printf("\nEnter the element to be deleted:");
scanf("%d",&x);
delete1(x,t);
printf("Elements after deletion");
display(t,1);
break;
case 7:display(t,1);
break;
case 8:exit(0);
break;
default:printf("\nInvalid choice");
break;
}}}
void create()
{
printf("\nEnter the no of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&data);
t=insert(data,t);
}
}
node insert(intx,node t)
{
if(t==NULL)
{
t=(tree*)malloc(sizeof(struct tree));
if(t==NULL)
printf("\nOut of space");
CS2208-DATA STRUCTURES LAB 44
else{
tno=x;
tleft=tright=NULL;
}}
else if(x<tno)
tleft=insert(x,tleft);
else if(x>tno)
tright=insert(x,tright);
return t;
}
node find(intx,node t)
{
if(t==NULL)
return NULL;
else if(x<tno)
return find(x,tleft);
else if(x>tno)
return find(x,tright);
else return t;
}
int findmin(node t)
{
if(t!=NULL)
{
if(tleft==NULL)
return tno;
else return findmin(tleft);
}
return 0;}
int findmax(node t)
{
CS2208-DATA STRUCTURES LAB 45
if(t!=NULL)
{
if(tright==NULL)
return tno;
else return findmax(tright);
}
return 0;}
node delete1(intx,node t)
{
node temp;
int k;
if(t==NULL)
printf("\nElement is not found");
else if(x<tno)
tleft=delete1(x,tleft);
else if(x>tno)
tright=delete1(x,tright);
else if(tleft&&tright)
{
k=findmin(tright);
tempno=k;
tno=tempno;
tright=delete1(tno,tright);
}
else
{
temp=t;
if(tleft==NULL)
t=tright;
else if(tright==NULL)
t=tleft;
CS2208-DATA STRUCTURES LAB 46
free(temp);
}
return t; }
void display(node t,int level)
{
int k;
if(t==NULL)
return;
display(tright,level+1);
printf("\n\n");
for(k=0;k<level;k++)
printf(" ");
printf("%d",tno);
display(tleft,level+1); }
void displaymenu()
{
printf("\nBinary search tree");
printf("\n0.show menu”);
printf(“\n1.Create”);
printf(“\n2.Insert”);
printf(“\n3.Find”);
printf(“\n4.Find Minimum”);
printf(“\n5.Find Maximum”);
printf(“\n6.Delete”);
printf(“\n7.Display”);
printf(“\n8.Exit");
}
CS2208-DATA STRUCTURES LAB 47
Ex.no:7: INSERTION IN AN AVL TREE
AIM:
To write a ‘C’ program to implement insertion in an AVL tree.
ALGORITHM:
Algorithm for height() function:
Step 1: Check whether p==NULL
Step1.1: If yes, then return -1
Step1.2: If no, then return p->height
Algorithm for max() function:
Step 1: Check whether h1>h2
Step 1.1: If yes, then return h1
Step 1.2: If no, then return h2
Algorithm for singlerotatewithleft() function:
Step 1: Assign k1=k2->left
Step 2: Assign k2->left=k1->right
Step 3: Assign k1->right=k2
Step 4: Assign k2->height=maximum height of left and right subtree
Step 5: Assign k1->height=max(height(k1->left),k2->height)+1
Step 6: Return k1
Algorithm for singlerotatewithright() function:
Step 1: Assign k2=k1->left
Step 2: Assign k1->right=k1->left
Step 3: Assign k2->left=k1
CS2208-DATA STRUCTURES LAB 48
Step 4: Assign k1->height=maximum height of left and right subtree
Step 5: Assign k2->height=max(height(k2->right),k1->height)+1
Step 6: Return k2
Algorithm for doublerotatewithleft() function:
Step 1: Assign k3->left=singlerotatewithright(k3->left)
Step 2: Return singlerotatewithleft(k3)
Algorithm for doublerotatewithright() function:
Step 1: Assign k3->right=singlerotatewithleft(k3->right)
Step 2: Return singlerotatewithright(k3)
Algorithm for insertnode() function:
Step 1: Check whether t==NULL
If yes, Allocate the memory space for t
Step 1.1: Check whether t==NULL
If yes, then print out of space
If no, then assign t->element=x , t->left=t->right=NULL and t->height=0.
Step 2: Check whether x<t->element
Step 2.1: If yes, then
Assign t->left=insertnode(x,t->left) // call insert() function
Step2.1.1: Check whether (height(t->left)-height(t->right)==2)
If yes, then check x<t->left->element
If true, then call singlerotatewithleft() function
If false, then call doublerotatewithleft() function
CS2208-DATA STRUCTURES LAB 49
Step 3: Check whether x>t->element
Step 2.1: If yes, then
Assign t->right=insertnode(x,t->right) // call insert() function
Step2.1.1: Check whether (height(t->right)-height(t->left)==2)
If yes, then check x>t->right->element
If true, then call singlerotatewithright() function
If false, then call doublerotatewithright() function
Step 4: print x is already in tree
Step 5: calculate the height of the resultant tree
Step 6: Return the height of the root
Step 3: Check whether x>t->no
Step 3.3: If yes, then
Assign t->right=insert(x,t->right)
Step 4: Return the value of t.
Algorithm for view() function:
Step 1: check whether the root is equal to NULL
Step 1.1: If yes, return the NULL value.
Step 1.2: If no, display the expression tree as
o Traverse the right subtree.
o Process the root node.
o Traverse the left subtree.
Algorithm for main() function:
Step 1: Get the choice from the user
CS2208-DATA STRUCTURES LAB 50
Step 1.1: If choice is 1, then
Get the value from the user
call insertnode() function to insert the values in the tree
Step 1.2: If choice is 2, then
If t is true, then call view() function to display the AVL tree
If t is not true, then print tree is empty
Step 1.3: If choice is 3, then
Exit from the menu
PROGRAM CODING:-
#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>structtreenode;typedef structtreenode *position;typedef structtreenode *avltree;typedef structtreenodeptrtonode;typedef int elementtype;avltree insert node(elementtypeX,avltree t);void view(avltreet,int level);structtreenode{elementtype element;avltree left;avltree right;int height;};int height(position p){if(p==NULL)return -1;elsereturn pheight;}int max(int h1,int h2){if(h1>h2)return h1;elsereturn h2;
CS2208-DATA STRUCTURES LAB 51
}positionsinglerotatewithleft(position k2){position k1;k1=k2left;k2left=k1right;k1right=k2;k2height=max(height(k2left),height(k2right))+1;k1height=max(height(k1left),k2height)+1;return k1;}positionsinglerotatewithright(position k1){position k2;k2=k1right;k1right=k2left;k2left=k1;k1height=max(height(k1left),height(k1right))+1;k2height=max(height(k2right),k1height)+1;return k2;}positiondoublerotatewithleft(position k3){k3left=singlerotatewithright(k3left);returnsinglerotatewithleft(k3);}positiondoublerotatewithright(position k3){k3right=singlerotatewithleft(k3right);returnsinglerotatewithright(k3);}avltreeinsertnode(elementtypeX,avltree t){if(t==NULL){t=(ptrtonode*)malloc(sizeof(structtreenode));if(t==NULL)printf("\nOut of space");else{telement=X;tleft=tright=NULL;theight=0;}}else if(X<telement)
CS2208-DATA STRUCTURES LAB 52
{tleft=insertnode(X,tleft);if(height(tleft)-height(tright)==2)if(X<tleftelement)t=singlerotatewithleft(t);elset=doublerotatewithleft(t);}else if(X>telement){t->right=insertnode(X,tright);if(height(tright)-height(tleft)==2)if(X>trightelement)t=singlerotatewithright(t);elset=doublerotatewithright(t);}elseprintf("\nX is already in tree");theight=max(height(tleft),height(tright))+1;return t;}void view(avltreet,int level){int k;if(t==NULL)return;view(tright,level+1);printf("\n\n");for(k=0;k<level;k++)printf(" ");printf("%d",tlement);view(tleft,level+1);}int main(){intch,item;position p;avltree t;t=NULL;clrscr();do{printf("\n1.Insertion\n2.View\n3.Exit");printf("\n\n?");scanf("%d",&ch);
CS2208-DATA STRUCTURES LAB 53
switch(ch){case 1:printf("\nEnter the item to be inserted:");scanf("%d",&item);t=insertnode(item,t);printf("\nitem inserted");break;case 2:if(t){printf("\AVL tree is…");view(t,1);}elseprintf("\nTree is empty");break;default:exit(0);}}while(1);}
CS2208-DATA STRUCTURES LAB 54
Ex.no:8 IMPLEMENTATION OF PRIORITY QUEUE USING BINARY HEAP
AIM:
To write a ‘C program to implement priority queue using binary heap.
ALGORITHM:
Algorithm for main() function:
Step 1: Get the choice from the user
Step 1.1: If the choice is 1, then
Get the data from the user
Call insert() function to insert the datas into the heap
Step 1.2: If the choice is 2, then
Delete the data from the heap
Step 1.3: If the choice is 3, then
Display the heap using view() function
Step 1.4: print end of program
Algorithm for insert() function:
Step 1: Check whether n==maxsize
If yes, then heap is full
Step 2: If n!=maxsize, then
Step 2.1: using for loop construct the heap for the given values
Algorithm for delete1() function:
Step 1: If n==0, then print binary heap is empty
CS2208-DATA STRUCTURES LAB 55
Step 2: If n!=0, then
Step 2.1: Assign del=heap[0]
Step 2.2: Assign t=heap[n-1]
Step 2.3: Decrement the value n
Step 2.4: using for loop reconstruct the heap excluding the deleted values
Step 3: Return the deleted value
Algorithm for view() function:
Step 1:Using for loop display the values in the heap.
PROGRAM CODING:-#include<stdio.h>#include<conio.h>#include<stdlib.h>#define maxsize 30int *heap;int n=0;void insert(int data);void view();int delete1();void main(){intch,data;clrscr();printf("\nMENU");printf("\n1.Insert\n2.Delete\n3.View\n4.Exit");while(1){printf("\n\n?");scanf("%d",&ch);switch(ch){case 1:printf("\nEnter the data");scanf("%d",&data);insert(data);break;case 2:data=delete1();printf("\nThe deleted data is:%d",data);break;case 3:view();
CS2208-DATA STRUCTURES LAB 56
break;default:printf("\nEnd of program");exit(0);}}}void insert(int data){int i;if(n==maxsize){printf("\nHeap is full");return;}for(i=n;1;){if(i==0)break;if(data<=heap[i/2])break;heap[i]=heap[i/2];i=i/2;}heap[i]=data;n++;}int delete1(){intdel,t,i=0,j=1;if(n==0){printf("\nBinary Heap is empty");return 0;}del=heap[0];t=heap[n-1];n--;for(;j<=n;){if(j<n)if(heap[j]<heap[j+1])j++;if(t>heap[i])break;heap[i]=heap[j];i=j;
CS2208-DATA STRUCTURES LAB 57
j=j*2;}heap[i]=t;return del;}void view(){int i=0;for(;i<n;i++)printf("\t%d",heap[i]);}
Ex.no:9 HASHING WITH OPEN ADDRESSING
CS2208-DATA STRUCTURES LAB 58
AIM:
To write a ‘C’ program to create hash table and handle the collision using open
addressing.
ALGORITHM:
Algorithm for main() function:
Step 0: Start the program.
Step 1: Initialize the hash table with -1,using for loop till the maxsize
Step 2: Open the do-while loop
Step 2.1:Get the element to be placed in the hash table.
Step 2.2: Generate the key for the elements using create() function
Step 2.3: call linear_prob() function to place the value in the hah table
And handle the collision
Step 2.4: continue the loop till user press ‘n’
Step 3: display the hash table
Step 4: stop the program
Algorithm for create() function:
Step 1: The hash key is generated using the formula key=num%10
Step 2: Return the hash key
Algorithm for linear_prob() function:
Step 1: Check whether a[key]==-1
If yes, then assign a[key]=num
Step 2: Check whether i<max using while loop
Step 2.1: If yes, check a[i]!=-1
CS2208-DATA STRUCTURES LAB 59
Assign count=count+1
Increment the value of i
Step 2.2: If count==max, then
Print hash table is full
Display the hash table
Step 2.3: for(i=key;i<max;i++
Step 2.3.1: check whether a[i]==-1
If yes, then assign a[i]=num and flag=1
Step 2.4: for (i=0;i<key&&flag==0;i++)
Step 2.4.1: check whether a[i]==-1
If yes, then assign a[i]=num and flag=1
Step 3: close the while loop.
Algorithm for display() function:
Step 1: Using for loop, print the values of hash table
PROGRAM CODING:-#include<stdio.h>#include<conio.h>#include<stdlib.h>#define max 10voidlinear_prob(int a[max],intkey,intnum);void display(int a[max]);int create(intnum);void main(){int a[max],num,key,i;charans;clrscr();printf("\nCollision Handling technique by linear probing");for(i=0;i<max;i++)a[i]=-1;do{printf("\nEnter the element:");
CS2208-DATA STRUCTURES LAB 60
scanf("%d",&num);key=create(num);linear_prob(a,key,num);printf("\nDo u wish to continue:");scanf(“%c”,&ans);}while(ans=='y'||ans=='Y');display(a);getch();}int create(intnum){int key;key=num%10;return key;}voidlinear_prob(int a[max],intkey,intnum){intflag,i,count=0;void display(int a[]);flag=0;if(a[key]==-1)a[key]=num;else{i=0;while(i<max){if(a[i]!=-1)count++;i++;}if(count==max){printf("\nHash table is full");display(a);getch();exit(1);}for(i=key+1;i<max;i++)if(a[i]==-1){a[i]=num;flag=1;break;}
CS2208-DATA STRUCTURES LAB 61
for(i=0;i<key&&flag==0;i++)if(a[i]==-1){a[i]=num;flag=1;break;}}}void display(int a[max]){printf("\nThe Hash table is:");for(int i=0;i<max;i++)printf("\n[%d]---->%d\n",i,a[i]);}
Ex.no:10 MINIMUM SPANNING TREE USING PRIM’S ALGORITHM
CS2208-DATA STRUCTURES LAB 62
AIM:
To write a ‘C’ program to find the Minimum Spanning Tree (MST) using
PRIM’S algorithm.
ALGORITHM:
Algorithm for main() function:
Step 1: start the program
Step 2: Get the number of vertices
Step 3: print the adjacency matrix using the weight of the edge of the graph
Step 4: Call prims() function to find the minimum spanning tree
Step 5: stop the program
Algorithm for prims() function:
Step 1: Initialize all vertices variables known=0, dist=infinity, path=0
Step 2: Consider the any node as first node and assign known=1, path=0, dist=0
Step 3: Consider the other adjacent nodes of first node and add the edge to the tree which
Has least cost among the adjacent nodes
Step 4: Again the next adjacent node will be added with the second least value
Step 5: Step 4,5 will be repeated till we get the minimum spanning tree.
Algorithm for visitedall() function:
Step 1: Check whether t[i].known==0
If yes, then return 0
If no, then return 1
PROGRAM CODING:-
CS2208-DATA STRUCTURES LAB 63
#include<stdio.h>#include<conio.h>#include<stdlib.h>#define max 10#define infinity 999typedefstruct{ints,e;}spantree;typedefstruct{int known,dist,path;}table;int adjmat[max][max],n;void prims();int visitedall(table[max],int);void main(){int src,dest,i,j,n;clrscr();printf("\nEnter the number of vertices:");scanf("%d",&n);for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(i==j) adjmat[i][j]=0; else { printf("\n%d to %d:",i,j); scanf("%d",&adjmat[i][j]); }}printf("\nTheadjecency matrix of the given graph is...");for(i=1;i<=n;i++){printf("\n\n");for(j=1;j<=n;j++)printf("\t%d",adjmat[i][j]);}prims();getch();}void prims(){int s,e;
CS2208-DATA STRUCTURES LAB 64
int i,j,newlen,current,small,minlen=0,tot=0;table t[10];spantreesptree[10];for(i=1;i<=n;i++){t[i].known=0;t[i].dist=infinity;t[i].path=0;}t[1].known=1;t[1].dist=0;t[1].path=0;current=1;while(visitedall(t,n)!=1){for(i=1;i<=n;i++){if(t[i].known==0&&adjmat[current][i]>0){if(adjmat[current][i]<t[i].dist){t[i].dist=adjmat[current][i];t[i].path=current;}}}small=infinity;current=0;for(j=1;j<=n;j++)if(t[j].known==0&&t[j].dist<small){small=t[j].dist;current=j;}t[current].known=1;s=t[current].path=0;e=current;tot++;sptree[tot].s=s;sptree[tot].e=e;minlen=minlen+adjmat[s][e];}printf("\nNo of edges:%d",tot);getch();printf("\nThe MST edges are:");for(i=1;i<=tot;i++)printf("\n%d%d\t%d",sptree[i].s,sptree[i].e,adjmat[sptree[i].s][sptree[i].e]);
CS2208-DATA STRUCTURES LAB 65
printf("\nthe spanning tree minimum length is= %d",minlen);}int visitedall(table t[10],int n){for(int i=1;i<=n;i++)if(t[i].known==0)return 0;return 1;}
CS2208-DATA STRUCTURES LAB 66