DS LAB MANUAL(B.E)
-
Upload
pitchrks19841 -
Category
Documents
-
view
27 -
download
1
description
Transcript of DS LAB MANUAL(B.E)
DS LAB
om sakthi
ADHIPARASAKTHI ENGINEERING COLLEGE
MELMARUVATHUR
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
LAB MANUALFOR
141352 Data Structures Lab
By, Balamurugan.R
Assistant Professor
Department of Computer Science and Engineering
1
DS LAB
CONTENT
EX. NO EXPERIMENT NAME PAGE NO.
1A IMPLEMENT SINGLY LINKED LIST 3
1B IMPLEMENT DOUBLY LINKED LIST 9
2 POLYNOMIAL ADDITION 15
3 CONVERT INFIX TO POSTFIX EXPRESSION 19
4 DOUBLE-ENDED QUEUE (DEQUEUE) 22
5 EXPRESSION TREE 27
6 BINARY SEARCH TREE 31
7 AVL TREES 37
8 PRIORITY QUEUE USING BINARY HEAPS 43
9 HASHING WITH OPEN ADDRESSING 47
10 PRIM'S ALGORITHM 50
11 DIJKSTRA’S ALGORITHM 54
12 BUBBLE SORT 58
2
DS LAB
1 a. SINGLY LINKED LIST
Aim:
To write a C program to perform a singly linked list
Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be
performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. In the insert beginning function set y->link=head. For the insert middle check if c<pos
if so set z=x; and x=x->link;and increment c
5. In the delete beginning function check the above function but perform delete
operation
6. In the display function display the array elements
7. Stop the program
Program:
#include<stdio.h>#include<conio.h>#include<stdlib.h>struct node{int data;struct node *link;};struct node *head,*x,*y,*z;void main(){void create();void insbeg();void insmid();void insend();void delbeg();
3
DS LAB
void delmid();void delend();void display();int ch;clrscr();while(1){printf("\n 1.Creation");printf("\n 2.Insertion at beginning");printf("\n 3.Insertion at middle");printf("\n 4.Insertion at end");printf("\n 5.Deletion at beginning");printf("\n 6.Deletion at middle");printf("\n 7.Deletion at end");printf("\n 8.Display");printf("\n 9.Exit");printf("\n Enter ur choice:");scanf("%d",&ch);switch(ch){case 1:create();break;case 2:insbeg();break;case 3:insmid();break;case 4:insend();break;case 5:delbeg();break;case 6:delmid();break;case 7:delend();break;case 8:display();break;default:exit(0);}}}void create()
4
DS LAB
{int c;head=NULL;x=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data:");scanf("%d",&x->data);x->link=NULL;head=x;printf("\n Do u wish to continue press 1 otherwise 0:");scanf("%d",&c);while(c!=0){y=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data:");scanf("%d",&y->data);y->link=NULL;x->link=y;x=y;printf("\n Do u wish to continue press 1 otherwise 0:");scanf("%d",&c);}}void insbeg(){y=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data:");scanf("%d",&y->data);y->link=head;head=y;}void insmid(){int pos,c=1;y=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data:");scanf("%d",&y->data);printf("\n Enter the position to be inserted:");scanf("%d",&pos);x=head;while(c<pos){z=x;x=x->link;c++;}y->link=x;z->link=y;}void insend(){
5
DS LAB
y=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data:");scanf("%d",&y->data);y->link=NULL;x=head;while(x->link!=NULL){x=x->link;}x->link=y;}void delbeg(){if(head==NULL)printf("\n List is empty");else{x=head;head=x->link;free(x);}}
void delmid(){int pos,c=1;if(head==NULL)printf("\n List is empty");else{printf("\n Enter the position to be deleted:");scanf("%d",&pos);x=head;while(c<pos){z=x;x=x->link;c++;}z->link=x->link;free(x);}}void delend(){if(head==NULL)printf("\n List is empty");else{x=head;
6
DS LAB
while(x->link!=NULL){y=x;x=x->link;}y->link=NULL;free(x);}}void display(){if(head==NULL)printf("\n List is empty");else{x=head;printf("\n List elements are \n");while(x->link!=NULL){printf("%d->",x->data);x=x->link;}printf("%d",x->data);}}
7
DS LAB
Output:
8
DS LAB
Result:
Thus the lists operations are performed using singly linked list in C perform.
1 b. DOUBLY LINKED LIST
Aim:
To a write a C program to implement a doubly linked list
Algorithm:
1. Start the program
2. Declare all the required functions and select the operation to be performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. Perform insertion, deletion and display operation to insert, delete and display the lists.
5. Stop the program
Program:
#include<stdio.h>#include<conio.h>#include<stdlib.h>struct node{int data;struct node *llink;struct node *rlink;};struct node *head,*x,*y,*z;void main(){void create();void insbeg();void insmid();void insend();void delbeg();void delmid();void delend();void display();int ch;clrscr();while(1){
9
DS LAB
printf("\n 1.Creation");printf("\n 2.Insertion at beginning");printf("\n 3.Insertion at middle");printf("\n 4.Insertion at end");printf("\n 5.Deletion at beginning");printf("\n 6.Deletion at middle");printf("\n 7.Deletion at end");printf("\n 8.Display");printf("\n 9.Exit");printf("\n Enter your choice: ");scanf("%d",&ch);switch(ch){case 1:create();break;case 2:insbeg();break;case 3:insmid();break;case 4:insend();break;case 5:delbeg();break;case 6:delmid();break;case 7:delend();break;case 8:display();break;default:exit(0);}}}void create(){int c;head=NULL;x=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data: ");scanf("%d",&x->data);x->llink=NULL;
10
DS LAB
x->rlink=NULL;head=x;printf("\n Do you wish to continue press 1 or 0:");scanf("%d",&c);while(c!=0){y=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data: ");scanf("%d",&y->data);x->rlink=y;y->llink=x;y->rlink=NULL;x=y;printf("\n Do you wish to continue press 1 or 0:");scanf("%d",&c);}}void insbeg(){y=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data: ");scanf("%d",&y->data);y->llink=NULL;y->rlink=head;head->llink=y;head=y;}void insmid(){int c=1,pos;y=(struct node*)malloc(sizeof(struct node));printf("\n Enter the data: ");scanf("%d",&y->data);printf("\n Enter the position to be inserted:");scanf("%d",&pos);x=head;while(c<pos){z=x;x=x->rlink;c++;}y->rlink=x;y->llink=z;x->llink=y;z->rlink=y;}void insend(){y=(struct node*)malloc(sizeof(struct node));
11
DS LAB
printf("\n Enter the data: ");scanf("%d",&y->data);y->rlink=NULL;x=head;while(x->rlink!=NULL){x=x->rlink;}x->rlink=y;y->llink=x;}void delbeg(){if(head==NULL)printf("\n the list is empty");else{x=head;head=x->rlink;head->llink=NULL;free(x);}}void delmid(){int pos,c=1;if(head==NULL)printf("\n the list is empty");else{x=head;printf("\n Enter the position to be deleted:");scanf("%d",&pos);while(c<pos){z=x;x=x->rlink;c++;}z->rlink=x->rlink;x->rlink->llink=z;free(x);}}void delend(){if(head==NULL)printf("\n the list is empty");else{
12
DS LAB
x=head;while(x->rlink!=NULL){y=x;x =x->rlink;}y->rlink=NULL;free(x);}}void display(){if(head==NULL)printf("\n the list is empty");else{x=head;printf("\nThe list elements are");while(x->rlink!=NULL){printf("%d->",x->data);x=x->rlink;}printf("%d",x->data);}}
13
DS LAB
Output:
14
DS LAB
Result:Thus the list operations are executed using doubly linked list in C program.
2. POLYNOMIAL ADDITION
Aim:
To write a C program to perform polynomial addition
Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be
performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. Using the while loop check if node->next!=NULL if so print the values of
node->coeff,node->pow
5. Perform the addition according to the conditions of polynomial addition.
6. print the result
7. Stop the program
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{printf("\n enter coeff:");scanf("%d",&node->coeff);printf("\n enter power:");
15
DS LAB
scanf("%d",&node->pow);node->next=(struct link*)malloc(sizeof(struct link));node=node->next;node->next=NULL;printf("\n continue(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){
16
DS LAB
poly->pow=poly1->pow;poly->coeff=poly1->coeff;poly1=poly1->next;}if(poly2->next){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;}}main(){char ch;clrscr();do{poly1=(struct link *)malloc(sizeof(struct link));poly2=(struct link *)malloc(sizeof(struct link));poly=(struct link *)malloc(sizeof(struct link));printf("\nenter 1st number:");create(poly1);printf("\nenter 2nd number:");create(poly2);printf("\n1st Number:");show(poly1);printf("\n2nd Number:");show(poly2);polyadd(poly1,poly2,poly);printf("\nAdded polynomial:");show(poly);printf("\n add two more numbers:");ch=getch();}while(ch=='y' || ch=='Y');return 0;}
17
DS LAB
Output:
Result:
Thus the program for polynomial addition using linked list is executed and verified.
18
DS LAB
3. INFIX TO POSTFIX CONVERSION
Aim:
To write a C program to convert infix to postfix expression
Algorithm:
1. Start the program
2. Using the strlen function determine the length of the string
3. Declare three arrays; the decremented value of one array is assigned to the
incremented value. This process is repeated until the parenthesis matches.
4. Until t==0 assign the values to sack1
5. Check if (top2==-1) if the condition is satisfied then assign stack2 [++top2]=str[i];
6. Print the result.
7. Stop the program
Program:
#include<stdio.h>#include<conio.h>#include<string.h>char str[50],stack1[50],stack2[50];int i=0,j,n,top1=-1,top2=-1;int prec(char);void main(){int t=0;clrscr();printf("\nEnter the infix expression:");scanf("%s",str);printf("%s",str);n=strlen(str);for(i=0;i<n;i++){t=0;if(str[i]==')'){stack1[++top1]=stack2[top2--];top2--;t=1;}if(str[i]=='('){stack2[++top2]='(';t=1;}
19
DS LAB
if(t==0){if(((str[i]>='a')&&(str[i]<='z'))||(( str[i]>='A')&&(str[i]<='Z')))stack1[++top1]=str[i];else{if(top2==-1)stack2[++top2]=str[i];else{if(prec(str[i])>prec(stack2[top2])){stack2[++top2]=str[i];}else{while(prec(str[i])<=prec(stack2[top2]))stack1[++top1]=stack2[top2--];stack2[++top2]=str[i];}}}}}printf("\nANSWER\n");while(top2>=0)stack1[++top1]=stack2[top2--];for(i=0;i<=top1;i++)printf("%c",stack1[i]);getch();}int prec(char ch){int tt;if((ch=='+')||(ch=='-'))tt=2;if((ch=='*')||(ch=='/'))tt=3;if(ch=='(')tt=1;return tt;}
20
DS LAB
Output:
Result:
Thus an infix expression is converted to postfix expression using stack.
21
DS LAB
4. LINKED LIST IMPLEMENTATION OF DEQUEAim:
To perform the linked list implementation of Deque using C program
Algorithm:
1. Start the program
2. Declare all the required functions and select the operation to be performed
3. Allocate the memory location using malloc function and if the queue is empty then
the front and the rear element are the same
4. For deletion operation check if the queue has only one element if so perform deletion.
if the queue contains more elements then perform deletion accordingly.
5. For the display operation use the while loop and check if x->link!=NULL if so print
the elements.
6. For the display operation if rear== null then print that the queue is empty
7. Stop the program
Program:
#include<stdio.h>#include<conio.h>#include<stdlib.h>struct node{int data;struct node *link;};struct node *front=NULL,*rear=NULL,*x;void main(){int a;void enqfro();void deqrea();void enqrea();void deqfro();void display1();void display2();clrscr();while(1){printf("\n1.insertion at front");printf("\n2.insertion at rear ");printf("\nenter choice");
22
DS LAB
scanf("%d",&a);while(a==1){int ch;printf("\n1.insertion at front");printf("\n2.deletion at rear ");printf("\n3.display1");printf("\n4.exit");printf("\nenter ur choice");scanf("%d",&ch);switch(ch){ case 1: enqfro(); break; case 2: deqrea(); break; case 3: display1(); break; default: exit(0);}}while(a==2){int ch;printf("\n1.insertion at rear");printf("\n2.deletion at front");printf("\n3.display2");printf("\n4.exit");printf("\nenter ur choice");scanf("%d",&ch);switch(ch){ case 1: enqrea(); break; case 2: deqfro(); break; case 3: display2(); break; default: exit(0); }}}
23
DS LAB
}void enqfro(){if(front==NULL){x=(struct node*)malloc(sizeof(struct node));printf("\nenter the data");scanf("%d",&x->data);x->link=NULL;rear=x;front=x;}else{x=(struct node*)malloc(sizeof(struct node));printf("\nenter the data");scanf("%d",&x->data);x->link=NULL;front->link=x;front=x;}}void deqrea(){if(rear==NULL)printf("\nQueue is empty");else{x=rear;printf("\ndeleted elementis %d",x->data);rear=x->link;free(x);}}void enqrea(){if(rear==NULL){x=(struct node*)malloc(sizeof(struct node));printf("\nenter the data");scanf("%d",&x->data);x->link=NULL;rear=x;front=x;}else{x=(struct node*)malloc(sizeof(struct node));printf("\nenter the data");scanf("%d",&x->data);
24
DS LAB
x->link=NULL;rear->link=x;rear=x;}}void deqfro(){if(front==NULL)printf("\nQueue is empty");else{x=front;printf("\ndeleted elementis %d",x->data);front=x->link;free(x);}}void display2(){if(front==NULL)printf("\nqueue is empty");else{x=front;printf("\nqueue elements are");while(x->link!=NULL){printf("%d\t",x->data);x=x->link;}printf("%d",x->data);}}void display1(){if(rear==NULL)printf("\nqueue is empty");else{x=rear;printf("\nqueue elements are");while(x->link!=NULL){printf("%d\t",x->data);x=x->link;}printf("%d",x->data);}}
25
DS LAB
Output:
Result:
Thus the C program to implement the Deque is verified successfully.
26
DS LAB
5. EXPRESSION TREE
Aim:
To construct an expression tree using C program
Algorithm:
1. Start the program
2. Declare all the required functions and select the operation to be performed
3. Allocate the memory location using malloc function
4. In the post function check if p!=NULL if call the recursive function post with
parameters p->rlink and p->llink
5. Using the switch case construct the expression tree
6. Using the push function insert the elements into the tree if topt==max then the tree is
full
7. Print the values
8. Stop the program
Program:
#include<stdio.h>#include<conio.h>#include<process.h>#include<string.h>#include<stdlib.h>struct node {char c;struct node *rlink;struct node *llink;}*stc[30];struct node *temp,*root;char prefix[20],ch;int topt=-1,max=50,len; //global declarationvoid pusht(struct node *p);struct node *popt();void tredis(struct node *ptr,int level);void exptree();void post(struct node *p);void main(){ clrscr(); printf("Enter a prefix expression:"); scanf("%s",prefix);
27
DS LAB
exptree(); tredis(root,1); printf("\nThe postfix expression is:"); post(root); getch();}void post(struct node *p){ if(p!=NULL) { post(p->llink); post(p->rlink); printf("%c",p->c); }}void exptree(){ int i; len=strlen(prefix); i=len-1; while(i>=0) { switch(prefix[i]) { case '+': case '-': case '*': case '/': case '^':
temp=(struct node*)malloc(sizeof(struct node)); temp->c=prefix[i]; temp->llink=popt(); temp->rlink=popt(); pusht(temp); break;
default : temp=(struct node*)malloc(sizeof(struct node)); temp->c=prefix[i]; temp->rlink=NULL; temp->llink=NULL; pusht(temp);
} i--;} root=stc[topt];}
void pusht(struct node * p){ if(topt==max)
28
DS LAB
{ printf("/**Beyond Capacity**/"); } else { stc[++topt]=p; }}struct node *popt(){ if(topt==-1) printf("/**No Expression**/"); else return(stc[topt--]);}void tredis(struct node *ptr,int level){ int i; if ( ptr!=NULL ) { tredis(ptr->rlink, level+1); printf(" "); for (i = 0; i < level; i++) printf(" "); printf("%c", ptr->c); tredis(ptr->llink, level+1); }}
29
DS LAB
Output:
Result:
Thus the C program to construct an expression tree is execute
30
DS LAB
6. BINARY SEARCH TREE
Aim:
To construct a binary search tree and perform various operation
Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be
performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. In the insert function assign the memory and assign n->llink=NULL
n->rlink=NULL
5. In the deletion function perform deletion of node that has no child,having two
children
6. In the inorder function perform recursion by calling inorder(head->rlink).
7. In the display function display the values
8. Stop the program
Program:
#include<conio.h>#include<stdio.h>#include<malloc.h>#include<process.h>struct node{ struct node *llink; struct node *rlink; int data;};void main(){ struct node *head,*t; int s,d; struct node* finsert(); struct node* delenode(struct node*,int); void insert(struct node *); void inorder(struct node *); clrscr(); head=NULL; do {
31
DS LAB
printf("\n1-Insertion"); printf("\n2-Deletion"); printf("\n3-Inorder"); printf("\n4-Exit"); printf("\nEnter Choice:"); scanf("%d",&s); switch(s) { case 1://insertion if(head==NULL) {
head=finsert(); } else
insert(head); break;
case 2://Deletion if(head==NULL) printf("\nBinary Tree Empty......."); else { printf("\nEnter Data to delete:"); scanf("%d",&d); if(head->llink==NULL && head->rlink==NULL && head->data==d)
{ t=head; head=NULL; free(t);}
else head = delenode(head,d);
} break; case 3://to display printf("\nIN-ORDER:"); if(head==NULL) printf("\nBinary Tree Empty...."); else inorder(head); break; case 4://exit exit(0); } }while(s<5 ||s>0); getch();}struct node* finsert(){ struct node * head;
32
DS LAB
head=(struct node*)malloc(sizeof(struct node)); printf("\nEnter Data:"); scanf("%d",&head->data); head->llink=NULL; head->rlink=NULL; return head;}void insert(struct node *head){ struct node *t,*n; t=head; n=(struct node *)malloc(sizeof(struct node)); printf("\nEnter Data:"); scanf("%d",&n->data); n->llink=NULL; n->rlink=NULL; while(t->llink!=NULL || t->rlink!=NULL) { if(t->llink!=NULL) if(n->data < t->data) t=t->llink; if(t->rlink!=NULL) if(n->data>=t->data) t=t->rlink; if((t->llink==NULL) && (n->data < t->data) && (n->data <t->rlink->data)) break; if((t->rlink==NULL) && (n->data >= t->data) && (n->data >t->llink->data)) break; } if((n->data < t->data) && (t->llink==NULL)) t->llink=n; if((n->data > t->data) && (t->rlink==NULL)) t->rlink=n;}void inorder(struct node * head){ if(head!=NULL) { inorder(head->llink); printf("%d\t",head->data); inorder(head->rlink); }}struct node * delenode(struct node *head,int d){ int f=0,f1=0; struct node *p,*t,*t1,*x; t=head; //to search found or not while(t!=NULL)
33
DS LAB
{ if(t->data==d) { f=1; x=t; break; } if(t->data > d) { p=t; t=t->llink; } else if(t->data <= d) { p=t; t=t->rlink; } } if(f==0) { printf("\nGiven element not found......."); return head; } //Deleted node has no child if(x->llink==NULL && x->rlink==NULL) { if(p->rlink==x) p->rlink=NULL; else p->llink=NULL; free(x); return head; } //deleted node has 2 children if(x->llink!=NULL && x->rlink!=NULL) { p=x; t1=x->rlink; while(t1->llink!=NULL) { p=t1; f1=1; t1=t1->llink; } if(t1->llink==NULL && t1->rlink==NULL) { x->data=t1->data; if(f1==1) p->llink=t1->llink; if(f1==0) x->rlink=t1->rlink;
34
DS LAB
free(t1); return head; } if(t1->rlink!=NULL) { x->data=t1->data; if(f1==1) p->llink=t1->rlink; if(f1==0) p->rlink=t1->rlink; free(t1); return head; } } //Deleted node has oniy right child if(x->llink==NULL && x->rlink!=NULL && x->data!=head->data) { if(p->llink==x) p->llink=x->rlink; else p->rlink=x->rlink; free(x); return head; } //Deleted node has oniy left child if(x->llink!=NULL && x->rlink==NULL && x->data!=head->data) { if(p->llink==x) p->llink=x->llink; else p->rlink=x->llink; free(x); return head; } if(x->llink!=NULL && x->rlink==NULL && x->data==head->data) { head=x->llink; free(p); return head; } if(x->llink==NULL && x->rlink!=NULL && x->data==head->data) { head=x->rlink; free(p); return head; }return 0;}
35
DS LAB
Output:
Result:
Thus the binary search tree is constructed and various operations are performed using C program.
36
DS LAB
7. AVL TREE
Aim:
To write a C program to perform insertion on an AVL tree
Algorithm:
1. Start the program
2. Declare all the functions and using the switch case select the operation to be
performed
3. Declare a structure with all the required variables and allocate the memory using the
malloc function
4. Check if (search (root, info) == NULL ), if so assign the value to the variable root
5. Insert an element into the tree.
6. After insertion check if the tree is balanced or not. If not balance the tree.
7. Stop the program.
Program:
#include<stdio.h>#include<conio.h>#include<stdlib.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);main(){bool ht_inc;int info ;int choice;struct node *root = (struct node *)malloc(sizeof(struct node));root = NULL;clrscr();while(1)
{printf("1.Insert\n");
37
DS LAB
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);elseprintf("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;if(pptr==NULL){pptr = (struct node *) malloc(sizeof(struct node));
38
DS LAB
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;elsepptr->balance = 0;if(bptr->balance == -1)aptr->balance = 1;elseaptr->balance = 0;bptr->balance=0;pptr=bptr;
39
DS LAB
}*ht_inc = FALSE;}/*End of switch */}/*End of if */}/*End of if*/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;elsepptr->balance = 0;if(bptr->balance == 1)aptr->balance = -1;elseaptr->balance = 0;bptr->balance=0;pptr = bptr;}/*End of else*/*ht_inc = FALSE;
40
DS LAB
}/*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++)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()*/
41
DS LAB
Output:
Result:
Thus the C program to perform insertion on an AVL tree is executed and verified
42
DS LAB
8. PRIORITY QUEUE USING BINARY HEAPS
Aim:
To write a C program to perform Priority queue using binary heaps.
Algorithm:
1. Start the program
2. Declare all the functions and the operation to be performed
3. Declare a structure with all the required variables and define the Max Size of packet
4. Exchange to maintain heap property
5. Find the correct place to insert the element
6. After insertion check if the tree is balanced or not
7. Stop the program.
Program:
#include <stdio.h>#define MAX_SIZE 10
typedef struct{
unsigned char priority;unsigned char packetType;unsigned int destinationAddress;unsigned int sourceAddress;char payload[26];
} Packet;
typedef struct{
Packet* packets[MAX_SIZE];unsigned int size;
} PacketHeap;
void heap_init(PacketHeap* h){
h->size=0;}
void heap_heapify(PacketHeap* h,int i){
int l,r,smallest;Packet* tmp;l=2*i; /*left child*/
43
DS LAB
r=2*i+1; /*right child*/
if ((l < h->size)&&(h->packets[l]->priority < h->packets[i]->priority))smallest=l;
elsesmallest=i;
if ((r < h->size)&&(h->packets[r]->priority < h->packets[smallest]->priority))smallest=r;
if (smallest!=i) {/*exchange to maintain heap property*/tmp=h->packets[smallest];h->packets[smallest]=h->packets[i];h->packets[i]=tmp;heap_heapify(h,smallest);
}}
void heap_addItem(PacketHeap* h,Packet* packet){
unsigned int i,parent;h->size=h->size+1;i=h->size-1;parent=i/2;/*find the correct place to insert*/while ((i > 0)&&(h->packets[parent]->priority > packet->priority)){
h->packets[i]=h->packets[parent];i=parent;parent=i/2;
}h->packets[i]=packet;
}
Packet* heap_extractMin(PacketHeap* h){
Packet* max;if (heap_isEmpty(h))
return 0;max=h->packets[0];h->packets[0]=h->packets[h->size-1];h->size=h->size-1;heap_heapify(h,0);return max;
}
int heap_isEmpty(PacketHeap *h){
return h->size==0;}
44
DS LAB
int heap_isFull(PacketHeap *h){
return h->size>=MAX_SIZE;}
int main(){
PacketHeap heap;unsigned char i;Packet p[10];p[0].priority=123;p[1].priority=23;p[2].priority=0;p[3].priority=22;p[4].priority=255;p[5].priority=1;p[6].priority=10;p[7].priority=3;p[8].priority=101;p[9].priority=102;clrscr();
heap_init(&heap);for (i=0;i<10;i++){
printf("Add %i\n",p[i].priority);heap_addItem(&heap,&(p[i]));
}while (!heap_isEmpty(&heap)){
i=heap_extractMin(&heap)->priority;printf("%i\n",i);
}getch();return 0;
}
45
DS LAB
Output:
Result:
Thus the C program to perform Priority queue using binary heaps is executed and verified
46
DS LAB
9. HASHING WITH OPEN ADDRESSING
Aim:
To write a C program to perform Hashing with open addressing
Algorithm:
1. Start the program
2. Declare all the required variables
3. Get the size of the hash table
4. Get the hash value from user
5. Find the correct place to store the element
6. Stop the program.
Program:
#include<STDIO.H>#include<conio.h>void main(){int a[10]={0,0,0,0,0,0,0,0,0,0};int n,value,temp,hashvalue;clrscr();printf("\nEnter the value of n(table size):");scanf("%d",&n);do{printf("\nEnter the hash value");scanf("%d",&value);hashvalue=value%n;if(a[hashvalue]==0){a[hashvalue]=value;printf("\na[%d]the value %d is stored",hashvalue,value);}else{for(hashvalue++;hashvalue<n;hashvalue++){if(a[hashvalue]==0){printf("Space is allocated give other value");a[hashvalue]=value;printf("\n a[%d]the value %d is stored",hashvalue,value);goto menu;}
47
DS LAB
}hashvalue=0;for(hashvalue;hashvalue<n;hashvalue++){if(a[hashvalue]==0){printf("Space is allocated give other value");a[hashvalue]=value;printf("\n a[%d]the value %d is stored",hashvalue,value);goto menu;}}printf("ERROR");printf("\nEnter '0' and press 'Enter key' twice to exit");
}
menu:
printf("\n Do u want enter more ENTER 1 here:");
scanf("%d",&temp);
}
while(temp==1);
getch();
}
48
DS LAB
Output:
Result:
Thus the C program to perform Hashing with open addressing is executed and verified.
49
DS LAB
10. PRIM'S ALGORITHM
Aim:
To write a C program to find MST of an undirected graph using Prim’s algorithm.
Algorithm:
1. Start the program
2. Declare all the functions and the operation to be performed
3. Declare a structure with all the required variables
4. Get the number of nodes and edges of undirected graph
5. Find the minimum shortest path use the corresponding edge cost.
6. Check all the nodes are visited.
7. Stop the program.
Program:
#include<stdio.h>#include<conio.h>
int b[10][10],a[10][10],v,v1,v2,n,e,visited[10],edge,cost,i,j;
void initialize(){printf("\nEnter the number of nodes\n");scanf("%d",&n);printf("\Enter the number of edges\n");scanf("%d",&e);for(i=1;i<=n;i++)for(j=1;j<=n;j++)a[i][j]=0;for(i=1;i<=n;i++)visited[i]=0;for(i=1;i<=e;i++){printf("\nEnter the two vertices v1 & v2 \n");scanf("%d%d",&v1,&v2);printf("\nEnter the corresponding cost\n");scanf("%d",&edge);a[v1][v2]=edge;}}
int prim(){for(i=1;i<=n;i++)
50
DS LAB
for(j=1;j<=n;j++)if(i==j)b[i][j]=0;elseb[i][j]=999;
for(i=1;i<=n-1;i++){findmin(&v1,&v2);edge=a[v1][v2];b[v1][v2]=b[v2][v1]=edge;visited[v1]=visited[v2]=1;cost=cost+edge;}return cost;}
int findmin(int *v1,int *v2){int edge=0,i;for(i=1;i<=n;i++){for(j=i+1;j<=n;j++){if((visited[i]==1&&visited[j]!=1)||(visited[i]!=1&&visited[j]==1)){if((edge>a[i][j]||edge==0)&&a[i][j]>0){edge=a[i][j];*v1=i;*v2=j;}}}}return;}
void main(){int val;clrscr();initialize();val=prim();
printf("\nThe cost is %d\n",val);printf("\nThe matrix is\n");
for(i=1;i<=n;i++){
51
DS LAB
for(j=1;j<=n;j++){printf("%d\t",b[i][j]);}printf("\n");}getch();}
52
DS LAB
Output:
Result:
Thus the C program to find MST of an undirected graph using Prim’s algorithm is executed and verified
53