Sei sulla pagina 1di 72

141352

AIM:

DATA STRUCTURES LAB

0032

To develop programming skills in design and implementation of data structures and their applications. 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 (de queue) where insertion and deletion operations are possible at both the ends. 5. Implement an expression tree. Produce its pre-order, in-order, and post order 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 LINUX 3. COMPUTERS REQUIRED 30 Nos. (Minimum Requirement: Pentium III or Pentium IV with 256 RAM and 40 GB hard disk)

Ex.No: 1(A)

Singly Linked List

// INSERTION IN SINGLY LINKED LIST #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node *next; }*head,*temp,*temp1,*p; void create(); void insert(); void display(); void find(); int ch,n,i,flag; void main() { clrscr(); do { printf("\n1.create \n2.insert \n3.display \n4.Find \n5.exit"); printf("\nEnter the choice : "); scanf("%d",&ch); switch(ch) { case 1: create(); break;

case 2: insert(); break; case 3: display(); break; case 4: find(); break; case 5: exit(0); } }while(ch<6); getch(); } void create() { head=NULL; printf("\nEnter the number of node to be created : "); scanf("%d",&n); temp=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data :"); scanf("%d",&temp->data); temp->next=NULL; head=temp; for(i=2;i<=n;i++) { temp1=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data :"); scanf("%d",&temp1->data); temp1->next=NULL; temp->next=temp1; temp=temp1;

} } void display() { temp=head; while(temp!=NULL) { printf("\t%d ->",temp->data); temp=temp->next; } } void find() { i=0,flag=0; printf("\nEnter the data to be found :"); scanf("%d",&n); for(temp=head;temp!=NULL;temp=temp->next) { i=i+1; if(n==temp->data) { flag=1; printf("\nThe data is found at position %d ",i); } } if(flag==0) { printf("\n Data Not Found"); } } void insert() {

temp1=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the node to be inserted : "); scanf("%d",&temp1->data); printf("\n1. Insert at the beginning \n2. Insert in the Middle \n3. Insert at the end"); printf("\nEnter your choice :"); scanf("%d",&ch); switch(ch) { case 1: temp1->next=head; head=temp1; break; case 2: i=1; printf("\nEnter the position :"); scanf("%d",&n); for(temp=head;temp!=NULL && i!=n;p=temp,temp=temp->next,i++); p->next=temp1; temp1->next=temp; //temp->next=temp1; break; case 3: for(temp=head;temp->next!=NULL;temp=temp->next); temp->next=temp1; temp1->next=NULL; break; } }

OUTPUT : 1.create 2.insert 3.display 4.Find 5.exit Enter the choice : 1 Enter the number of node to be created : 2 Enter the data :10 Enter the data :20 1.create 2.insert 3.display 4.Find 5.exit Enter the choice : 3 10 -> 20 -> 1.create 2.insert 3.display 4.Find 5.exit Enter the choice : 2 Enter the node to be inserted : 30 1. Insert at the beginning 2. Insert in the Middle 3. Insert at the end

Enter your choice :1 1.create 2.insert 3.display 4.Find 5.exit Enter the choice : 3 30 -> 10 -> 20 -> 1.create 2.insert 3.display 4.Find 5.exit Enter the choice : 2 Enter the node to be inserted : 40 1. Insert at the beginning 2. Insert in the Middle 3. Insert at the end Enter your choice :2 Enter the position :3 1.create 2.insert 3.display 4.Find 5.exit Enter the choice : 3 30 -> 10 -> 40 -> 20 -> 1.create

2.insert 3.display 4.Find 5.exit Enter the choice : 2 Enter the node to be inserted : 50 1. Insert at the beginning 2. Insert in the Middle 3. Insert at the end Enter your choice :3 1.create 2.insert 3.display 4.Find 5.exit Enter the choice : 3 30 -> 10 -> 40 -> 20 -> 50 -> 1.create 2.insert 3.display 4.Find 5.exit Enter the choice :

Ex.No: 1(B)

Doubly Linked List

//DOUBLY LINKED LIST #include<stdio.h> #include<conio.h> struct node { int data; struct node *next,*prev; }*head=NULL,*temp,*temp1; void create(); void insert(); void del(); void display(); int ch,n,i,f; void main() { clrscr(); do { printf("\n1. create\n2.insert\n3.delete\n4.display\n5.exit"); printf("\nenter the choice : "); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: insert();

break; case 3: del(); break; case 4: display(); break; case 5: exit(0); } }while(ch<6); getch(); } void create() { temp=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the no. of nodes to be created : "); scanf("%d",&n); printf("\nEnter the data : "); scanf("%d",&temp->data); temp->next=NULL; temp->prev=NULL; head=temp; for(i=2;i<=n;i++) { temp1=(struct node*)malloc(sizeof(struct node)); printf("\n Enter the data : "); scanf("%d",&temp1->data); temp->next=temp1; temp1->prev=temp; temp1->next=NULL; temp=temp1; }

} void display() { for(temp=head;temp!=NULL;temp=temp->next) printf("%d ->",temp->data); } void insert() { temp1=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data to be inserted : "); scanf("%d",&temp1->data); printf("\n1.insert at beginning\n2.insert in the middle\n3.insert at the end"); printf("\nEnter the choice : "); scanf("%d",&ch); switch(ch) { case 1: temp=head; temp1->next=temp; temp->prev=temp1; head=temp1; break; case 2: printf("\nEnter the position : "); scanf("%d",&n); i=1; for(temp=head;temp!=NULL && i!=n;temp=temp->next,i++); temp1->next=temp; temp1->prev=temp->prev; temp->prev->next=temp1; temp->prev=temp1; break; case 3:

for(temp=head;temp->next!=NULL;temp=temp->next); temp->next=temp1; temp1->prev=temp; temp1->next=NULL; break; } } void del() { printf("\n1.Delete at first\n2.Delete in the middle\n3.Delete at the end:"); printf("\nEnter the choice :"); scanf("%d",&ch); switch(ch) { case 1: temp=head; head=temp->next; head->prev=NULL; temp->next=NULL; free(temp); break; case 2: printf("\nEnter the data to be deleted : "); scanf("%d",&n); f=0; for(temp=head;temp!=NULL;temp=temp->next) { if(temp->data==n) { f=1; temp->prev->next=temp->next; temp->next->prev=temp->prev;

free(temp); } } if(f==0) printf("\n Data not Found"); break; case 3: for(temp=head;temp->next!=NULL;temp=temp->next); temp->prev->next=NULL; free(temp); break; } } OUTPUT: 1. create 2.insert 3.delete 4.display 5.exit enter the choice : 1 Enter the no. of nodes to be created : 2 Enter the data : 10 Enter the data : 20 1. create 2.insert 3.delete 4.display 5.exit enter the choice : 4

10 ->20 -> 1. create 2.insert 3.delete 4.display 5.exit enter the choice : 2 Enter the data to be inserted : 30 1.insert at beginning 2.insert in the middle 3.insert at the end Enter the choice : 1 1. create 2.insert 3.delete 4.display 5.exit enter the choice : 4 30 ->10 ->20 -> 1. create 2.insert 3.delete 4.display 5.exit enter the choice : 2 Enter the data to be inserted : 40 1.insert at beginning 2.insert in the middle 3.insert at the end Enter the choice : 3

Ex.No: 2

Polynomial Addition

// POLYNOMIAL ADDITION #include<stdio.h> #include<conio.h> struct node { int coef,pow; struct node *next; }; void create(struct node*); void show(struct node*); void add(struct node*,struct node*,struct node*); struct node*p1,*p2,*pr,*tmp1; void main() { char ch; clrscr(); do { p1=(struct node*)malloc(sizeof(struct node)); p2=(struct node*)malloc(sizeof(struct node)); pr=(struct node*)malloc(sizeof(struct node)); printf("\n--------------POLYNOMIAL ADDITION------------------\n"); printf("\n Enter the 1st polynomial : \n"); create(p1); show(p1); printf("\n Enter the 2nd polynomial : \n"); create(p2); show(p2); printf("\n************RESULT OF ADDITION*********************\n");

add(p1,p2,pr); show(pr); printf("\nDo you want to add any more polynomial? (y/n) :"); ch=getch(); }while(ch=='y' || ch=='Y'); } void create(struct node *tmp) { char ch; do { printf("\n Enter the co-ef and power values :"); scanf("%d %d",&tmp->coef,&tmp->pow); tmp1=(struct node*)malloc(sizeof(struct node)); tmp->next=tmp1; tmp1->next=NULL; tmp=tmp1; printf("\nDo you want to continue (y/n) :"); ch=getch(); }while(ch=='y' ||ch=='Y'); } void show(struct node *tmp) { while(tmp->next!=NULL) { if(tmp->pow!=0) { printf("%dX^%d",tmp->coef,tmp->pow); if(tmp->next->next!=NULL) { if(tmp->next->coef>0) printf("+");

} } else printf("%d",tmp->coef); tmp=tmp->next; } } void add(struct node *p1,struct node *p2,struct node *pr) { while(p1->next && p2->next) { if(p1->pow > p2->pow) { pr->pow=p1->pow; pr->coef=p1->coef; p1=p1->next; } else if(p1->pow < p2->pow) { pr->pow=p2->pow; pr->coef=p1->coef; p2=p2->next; } else { pr->pow=p1->pow; pr->coef=p1->coef+p2->coef; p1=p1->next; p2=p2->next; } tmp1=(struct node*)malloc(sizeof(struct node));

tmp1->next=NULL; pr->next=tmp1; pr=tmp1; } while(p1->next || p2->next) { if(p1->next) { pr->coef=p1->coef; pr->pow=p1->pow; p1=p1->next; } if(p2->next) { pr->coef=p2->coef; pr->pow=p2->pow; p2=p2->next; } tmp1=(struct node*)malloc(sizeof(struct node)); tmp1->next=NULL; pr->next=tmp1; pr=tmp1; } }

OUTPUT: Enter the 1st polynomial : Enter the co-ef and power values :4 3 Do you want to continue (y/n) : Enter the co-ef and power values :5 2 Do you want to continue (y/n) : Enter the co-ef and power values :6 1 Do you want to continue (y/n) : Enter the co-ef and power values :7 0 Do you want to continue (y/n) :4X^3+5X^2+6X^1+7 Enter the 2nd polynomial : Enter the co-ef and power values :3 2 Do you want to continue (y/n) : Enter the co-ef and power values :2 1 Do you want to continue (y/n) : Enter the co-ef and power values :1 0 Do you want to continue (y/n) :3X^2+2X^1+1 ************RESULT OF ADDITION********************* 4X^3+8X^2+8X^1+8 Do you want to add any more polynomial? (y/n) :

Ex.No:3 //INFIX to POSTFIX #include<stdio.h> #include<conio.h> char inf[40],post[40]; int top=0,st[20]; void postfix(); void push(int); char pop(); void main() { clrscr();

INFIX TO POSTFIX CONVERSTION

printf("\nEnter the Infix Expression :"); scanf("%s",&inf); postfix(); getch(); } void postfix() { int i,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;

case ')': while(st[top]!=0) post[j++]=pop(); top--; break; default: post[j++]=inf[i]; } } while(top>0) post[j++]=pop(); printf("\n\tEvaluated Postfix Expression is = \t%s",post); } void push(int ele) { top++; st[top]=ele; } char pop() { int el; char e; el=st[top]; top--; switch(el) { case 1: e='+'; break; case 2: e='-';

break; case 3: e='*'; break; case 4: e='/'; break; case 5: e='^'; break; } return(e); }

OUTPUT:

Enter the Infix Expression: A+B*C+(D*E+F)*G Evaluated Postfix Expression is = ABC*+DE*F+G*+

Ex.No :4 IMPLEMENTATION OF DOUBLE ENDED QUEUE PROGRAM: //DOUBLE ENDED QUEUE #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int data; struct node *next; }*front=NULL,*rear=NULL,*t,*temp; void enquehead(); void enquetail(); void dequehead(); void dequetail(); void display(); void main() { int ch,op,opt; clrscr(); do { printf("\n\tMENU\n1.enqueue\n2.dequeue\n3.display\n4.exit"); printf("\nEnter your choice :"); scanf("%d",&ch); switch(ch) {

case 1: printf("\n1. Enque head\n2. Enque tail"); printf("\nENter your choice :"); scanf("%d",&op); switch(op) { case 1: enquehead(); display(); break; case 2: enquetail(); display(); break; } break; case 2: printf("\n1. Deque head\n2. Deque tail"); printf("\nENter your choice :"); scanf("%d",&opt); switch(opt) { case 1: dequehead(); display(); break; case 2: dequetail(); display(); break; } break; case 3: display(); break; case 4: exit(0); } }while(ch<5); getch(); } void enquehead() {

t=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data :"); scanf("%d",&t->data); t->next=NULL; if(front==NULL) { fornt=rear=t; } else { t->next=front; fornt=t; } } void enquetail() { t=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data :"); scanf("%d",&t->data); t->next=NULL; if(rear==NULL) { fornt=rear=t; } else { rear->next=t; rear=t; } } void dequehead()

{ if(front==NULL) printf("\nQueue is empty"); else { temp=front; front=front->next; printf("\nDequeued Element => %d",temp->data); free(temp); } } void dequetail() { if(rear==NULL) printf("\nQueue is empty"); if(front==rear) { printf("\nDequeued Element => %d",rear->data); front=rear=NULL; } else { for(temp=front;temp->next->next!=NULL;temp=temp->next); printf("\nDequeued Element => %d",rear->data); rear=temp; temp->next=NULL; } } void display() { if(front==NULL) printf("\nQueue is empty"); else

{ printf("\nElements in the Queue :"); for(temp=front;temp!=NULL;temp=temp->next) printf("%d\t",temp->data); }}

OUTPUT: MENU 1.enqueue 2.dequeue 3.display 4.exit Enter your choice :1 1. Enque head 2. Enque tail ENter your choice :1 Enter the data :1 Elements in the Queue :1 MENU 1.enqueue 2.dequeue 3.display 4.exit Enter your choice :1 1. Enque head 2. Enque tail ENter your choice :1 Enter the data :2 Elements in the Queue :2 1

MENU 1.enqueue 2.dequeue 3.display 4.exit Enter your choice :1 1. Enque head 2. Enque tail ENter your choice :2

Ex.No :5 1. PROGRAM:

IMPLEMENTATION OF EXPRESSION TREE

// EXPRESSION TREE #include<stdio.h> #include<conio.h> struct node { char data; struct node *left,*right; }*root=NULL,*tmp,*tmp1,*r,*l; struct node *stack[15]; int top=-1; char exp[15]; void push(struct node*); struct node *pop(); void process(); void inorder(struct node*); void preorder(struct node*); void postorder(struct node*); void traverse(); void display(struct node*,int,int); void main() { int x=35,y=12; clrscr(); printf("\nEnter the postfix expression :\n"); scanf("%s",exp); process(); //display(root,x,y);

getch(); } struct node *makenode(char x) { tmp1=(struct node*)malloc(sizeof(struct node)); tmp1->data=x; tmp1->left=tmp1->right=NULL; return tmp1; } void process() { int i; for(i=0;i<=strlen(exp);i++) { if(isalpha(exp[i])) { tmp=makenode(exp[i]); push(tmp); } else if(exp[i]=='+' || exp[i]=='-' || exp[i]=='*' || exp[i]=='/') { tmp=makenode(exp[i]); r=pop(); l=pop(); if(l==NULL ) goto lbl; tmp->left=l; tmp->right=r; push(tmp); } else if(exp[i]!='\0')

{ lbl: printf("\n Invalid Expression \n"); break; } else { root=pop(); if(top==-1) { printf("\n Tree Constructed\n"); traverse(); } else printf("\n Invalid Expression\n"); } } } void push(struct node *tmp1) { top++; stack[top]=tmp1; } struct node *pop() { return stack[top--]; } void display(struct node *root,int x1,int y1) { if(root!=NULL) { gotoxy(x1,y1); cprintf("%c",root->data); display(root->left,x1-5,y1+5);

display(root->right,x1+2,y1+5); } } void traverse() { printf("\n\n"); printf("In-Order Traversal : "); inorder(root); printf("\n"); printf("Pre-Order Traversal : "); preorder(root); printf("\n"); printf("Post-Order Traversal: "); postorder(root); } void inorder(struct node *root) { if(root!=NULL) { inorder(root->left); printf("%c ",root->data); inorder(root->right); } } void preorder(struct node *root) { if(root!=NULL) { printf("%c ",root->data); preorder(root->left); preorder(root->right); } }

void postorder(struct node *root) { if(root!=NULL) { postorder(root->left); postorder(root->right); printf("%c ",root->data); } } OUTPUT: Enter the postfix expression : AB+CDE+** Tree Constructed In-Order Traversal : A + B * C * D + E Pre-Order Traversal : * + A B * C + D E Post-Order Traversal: A B + C D E + * *

Ex.No :6 PROGRAM:

IMPLEMENTATION OF BINARY SEARCH TREE

// IMPLEMENTATION OF BINARY SEARCH TREE #include<stdio.h> #include<conio.h> struct node { int data; struct node *left,*right; }*root=NULL,*tmp,*tmp1; int in[15]; void create(); struct node *maketree(int); void display(struct node*); struct node *find(struct node*,int); struct node *findmin(struct node*); struct node *findmax(struct node*); void insert(int,struct node*); struct node *del(int s,struct node*); void main() { int ch,x; clrscr(); printf("\n\t\t********* Binary Search Tree *************\n"); do { printf("\nBST oprations :");

printf("\n\t1. Create\n\t2. Find\n\t3. FindMin\n\t4. FindMax\n\t"); printf("5. display\n\t6. Insert\n\t7. Delete\n\t8. Exit\n"); printf("\nEnter the choice : "); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: printf("\nEnter the element to be searched : "); scanf("%d",&x); tmp=find(root,x); if(tmp!=NULL) printf("\n Data %d found ",tmp->data); else printf("\n Data not found\n"); break; case 3: tmp=findmin(root); printf("\n Smallest element in the tree : %d",tmp->data); break; case 4: tmp=findmax(root); printf("\n largest element in the tree : %d",tmp->data); break; case 5: display(root); break; case 6: printf("\n Enter the key value to be inserted : "); scanf("%d",&x); insert(x,root); break; case 7:

printf("\nEnter the key value to be deleted : "); scanf("%d",&x); tmp1=del(x,root); printf("\n The given key is deleted; root is : %d\n",root->data); break; case 8: exit(0); } }while(ch<8); getch(); } struct node *maketree(int x) { tmp=(struct node*)malloc(sizeof(struct node)); tmp->data=x; tmp->left=tmp->right=NULL; return tmp; } void create() { struct node *p,*q; int i,y,n; printf("\n Enter the no. of elements : "); scanf("%d",&n); printf("\nenter the elements one by one : "); for(i=0;i<n;i++) scanf("%d",&in[i]); root=maketree(in[0]); for(i=1;i<n;i++) { y=in[i]; p=root; while(p!=NULL)

{ q=p; if(y<p->data) p=p->left; else if(y>p->data) p=p->right; else if(y==p->data) { q=NULL; break; } } if(y<q->data) q->left=maketree(y); else q->right=maketree(y); } printf("\n Binary Search Tree created!..\n"); } struct node *find(struct node *r,int x) { if(r==NULL) return NULL; if(x<r->data) return find(r->left,x); else if(x>r->data) return find(r->right,x); else return r; }

struct node *findmin(struct node *root) { if(root==NULL) return NULL; else if(root->left==NULL) return root; else return findmin(root->left); } struct node *findmax(struct node *root) { if(root==NULL) return NULL; else if(root->right==NULL) return root; else return findmax(root->right); } void display(struct node *root) { if(root!=NULL) { display(root->left); printf("%d ",root->data); display(root->right); } } struct node *del(int x,struct node *tmp)

{ if(tmp==NULL) printf("\nElement not found!!!\n"); else if(x<tmp->data) tmp->left=del(x,tmp->left); else if(x>tmp->data) tmp->right=del(x,tmp->right); else if(tmp->left && tmp->right) { tmp1=findmin(tmp->right); tmp->data=tmp1->data; tmp->right=del(tmp->data,tmp->right); } else { tmp1=tmp; if(tmp->left==NULL) tmp=tmp->right; else if(tmp->right==NULL) tmp=tmp->left; free(tmp1); } return tmp; } void insert(int x,struct node *tmp) { while(tmp!=NULL) { tmp1=tmp;

if(x<tmp->data) tmp=tmp->left; else if(x>tmp->data) tmp=tmp->right; else if(x==tmp->data) { printf("\n sorry! key value already exist \n"); tmp1=NULL; break; } } if(x<tmp1->data && tmp1!=NULL) { tmp1->left=maketree(x); printf("\n Key Inserted \n"); } else if(x>tmp1->data && tmp1!=NULL) { tmp1->right=maketree(x); printf("\n Key Inserted \n"); } }

OUTPUT: ********* Binary Search Tree ************* BST oprations : 1. Create 2. Find 3. FindMin 4. FindMax 5. display 6. Insert 7. Delete 8. Exit Enter the choice : 1 Enter the no. of elements : 4 Enter the elements one by one : 78 89 90 12 Binary Search Tree created!.. BST oprations : 1. Create 2. Find 3. FindMin 4. FindMax 5. display 6. Insert 7. Delete 8. Exit Enter the choice : 2 Enter the element to be searched : 12 Data 12 found BST oprations :

1. Create 2. Find 3. FindMin 4. FindMax 5. display 6. Insert 7. Delete 8. Exit Enter the choice : 3 Smallest element in the tree : 12

Ex.No :7

IMPLEMENTATION OF AVL TREE

ROTATIONS
PROGRAM: AVL Tree Rotations #include <iostream.h> #include <stdlib.h> #include<constream.h> #define FALSE 0 #define TRUE 1 struct AVLNode { int data ; int balfact ; AVLNode *left ; AVLNode *right ; }; class avltree { private : AVLNode *root ; public : avltree( ) ; AVLNode* insert ( int data, int *h ) ; static AVLNode* buildtree ( AVLNode *root, int data, int *h ) ;

void display( AVLNode *root ) ; AVLNode* deldata ( AVLNode* root, int data, int *h ) ; static AVLNode* del ( AVLNode *node, AVLNode* root, int *h ) ; static AVLNode* balright ( AVLNode *root, int *h ) ; static AVLNode* balleft ( AVLNode* root, int *h ) ; void setroot ( AVLNode *avl ) ; ~avltree( ) ; static void deltree ( AVLNode *root ) ; }; avltree :: avltree( ) { root = NULL ; } AVLNode* avltree :: insert ( int data, int *h ) { root = buildtree ( root, data, h ) ; return root ; } AVLNode* avltree :: buildtree ( AVLNode *root, int data, int *h ) { AVLNode *node1, *node2 ; if ( root == NULL ) { root = new AVLNode ; root -> data = data ;

root -> left = NULL ; root -> right = NULL ; root -> balfact = 0 ; *h = TRUE ; return ( root ) ; } if ( data < root -> data ) { root -> left = buildtree ( root -> left, data, h ) ; // If left subtree is higher if ( *h ) { switch ( root -> balfact ) { case 1 : node1 = root -> left ; if ( node1 -> balfact == 1 ) { cout << "\nRight rotation." ; root -> left = node1 -> right ; node1 -> right = root ; root -> balfact = 0 ; root = node1 ; }

else { cout << "\nDouble rotation, left then right." ; node2 = node1 -> right ; node1 -> right = node2 -> left ; node2 -> left = node1 ; root -> left = node2 -> right ; node2 -> right = root ; if ( node2 -> balfact == 1 ) root -> balfact = -1 ; else root -> balfact = 0 ; if ( node2 -> balfact == -1 ) node1 -> balfact = 1 ; else node1 -> balfact = 0 ; root = node2 ; } root -> balfact = 0 ; *h = FALSE ; break ; case 0 : root -> balfact = 1 ; break ;

case -1 : root -> balfact = 0 ; *h = FALSE ; }}} if ( data > root -> data ) { root -> right = buildtree ( root -> right, data, h ) ; if ( *h ) { switch ( root -> balfact ) { case 1 : root -> balfact = 0 ; *h = FALSE ; break ; case 0 : root -> balfact = -1 ; break ; case -1 : node1 = root -> right ; if ( node1 -> balfact == -1 ) { cout << "\nLeft rotation." ; root -> right = node1 -> left ; node1 -> left = root ;

root -> balfact = 0 ; root = node1 ; } else { cout << "\nDouble rotation, right then left." ; node2 = node1 -> left ; node1 -> left = node2 -> right ; node2 -> right = node1 ; root -> right = node2 -> left ; node2 -> left = root ; if ( node2 -> balfact == -1 ) root -> balfact = 1 ; else root -> balfact = 0 ; if ( node2 -> balfact == 1 ) node1 -> balfact = -1 ; else node1 -> balfact = 0 ; root = node2 ; } root -> balfact = 0 ; *h = FALSE ; }}} return ( root ) ;

} void avltree :: display ( AVLNode* root ) { if ( root != NULL ) { display ( root -> left ) ; cout << root -> data << "\t" ; display ( root -> right ) ; }} AVLNode* avltree :: deldata ( AVLNode *root, int data, int *h ) { AVLNode *node ; if ( root -> data == 13 ) cout << root -> data ; if ( root == NULL ) { cout << "\nNo such data." ; return ( root ) ; } else { if ( data < root -> data ) { root -> left = deldata ( root -> left, data, h ) ;

if ( *h ) root = balright ( root, h ) ; } else { if ( data > root -> data ) { root -> right = deldata ( root -> right, data, h ) ; if ( *h ) root = balleft ( root, h ) ; } else { node = root ; if ( node -> right == NULL ) { root = node -> left ; *h = TRUE ; delete ( node ) ; } else { if ( node -> left == NULL ) { root = node -> right ;

*h = TRUE ; delete ( node ) ; } else { node -> right = del ( node -> right, node, h ) ; if ( *h ) root = balleft ( root, h ) ; }}}}} return ( root ) ; } AVLNode* avltree :: del ( AVLNode *succ, AVLNode *node, int *h ) { AVLNode *temp = succ ; if ( succ -> left != NULL ) { succ -> left = del ( succ -> left, node, h ) ; if ( *h ) succ = balright ( succ, h ) ; } else { temp = succ ; node -> data = succ -> data ;

succ = succ -> right ; delete ( temp ) ; *h = TRUE ; } return ( succ ) ; } AVLNode* avltree :: balright ( AVLNode *root, int *h ) { AVLNode *temp1, *temp2 ; switch ( root -> balfact ) { case 1 : root -> balfact = 0 ; break ; case 0 : root -> balfact = -1 ; *h = FALSE ; break ; case -1 : temp1 = root -> right ; if ( temp1 -> balfact <= 0 ) { cout << "\nLeft rotation." ; root -> right = temp1 -> left ;

temp1 -> left = root ; if ( temp1 -> balfact == 0 ) { root -> balfact = -1 ; temp1 -> balfact = 1 ; *h = FALSE ; } else { root -> balfact = temp1 -> balfact = 0 ; } root = temp1 ; } else { cout << "\nDouble rotation, right then left." ; temp2 = temp1 -> left ; temp1 -> left = temp2 -> right ; temp2 -> right = temp1 ; root -> right = temp2 -> left ; temp2 -> left = root ; if ( temp2 -> balfact == -1 ) root -> balfact = 1 ; else root -> balfact = 0 ;

if ( temp2 -> balfact == 1 ) temp1 -> balfact = -1 ; else temp1 -> balfact = 0 ; root = temp2 ; temp2 -> balfact = 0 ; } } return ( root ) ; } AVLNode* avltree :: balleft ( AVLNode *root, int *h ) { AVLNode *temp1, *temp2 ; switch ( root -> balfact ) { case -1 : root -> balfact = 0 ; break ;

case 0 : root -> balfact = 1 ; *h = FALSE ; break ; case 1 :

temp1 = root -> left ; if ( temp1 -> balfact >= 0 ) { cout << "\nRight rotation." ; root -> left = temp1 -> right ; temp1 -> right = root ; if ( temp1 -> balfact == 0 ) { root -> balfact = 1 ; temp1 -> balfact = -1 ; *h = FALSE ; } else { root -> balfact = temp1 -> balfact = 0 ; } root = temp1 ; } else { cout << "\nDouble rotation, left then right." ; temp2 = temp1 -> right ; temp1 -> right = temp2 -> left ; temp2 -> left = temp1 ; root -> left = temp2 -> right ;

temp2 -> right = root ; if ( temp2 -> balfact == 1 ) root -> balfact = -1 ; else root -> balfact = 0 ; if ( temp2-> balfact == -1 ) temp1 -> balfact = 1 ; else temp1 -> balfact = 0 ; root = temp2 ; temp2 -> balfact = 0 ; }} return ( root ) ; } void avltree :: setroot ( AVLNode *avl ) { root = avl ; } avltree :: ~avltree( ) { deltree ( root ) ; } void avltree :: deltree ( AVLNode *root ) {

if ( root != NULL ) { deltree ( root -> left ) ; deltree ( root -> right ) ; } delete ( root ) ; } void main( ) { avltree at ; AVLNode *avl = NULL ; int h ; clrscr(); avl = at.insert ( 20, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 6, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 29, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 5, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 12, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 25, &h ) ; at.setroot ( avl ) ;

avl = at.insert ( 32, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 10, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 15, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 27, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 13, &h ) ; at.setroot ( avl ) ; cout << endl << "AVL tree:\n" ; at.display ( avl ) ; avl = at.deldata ( avl, 20, &h ) ; at.setroot ( avl ) ; avl = at.deldata ( avl, 12, &h ) ; at.setroot ( avl ) ; cout << endl << "AVL tree after deletion of a node:\n" ; at.display ( avl ) ; getch(); }

OUTPUT:

EX.NO:8

IMPLEMENTATION OF PRIORITY QUEUE USING HEAPS

PROGRAM // PRIORITY QUEUE USING HEAPS // #include<stdio.h> #include<conio.h> int size; void insert(int,int[]); int delmin(int[]); void display(int[]); void main() { int ch,i,n,x; int a[20],heap[20]; clrscr(); printf("\n\n************ PRIORITY QUEUE USING HEAPS *************** \n\n"); do { printf("\n\tHeap Operation\n\n"); printf("1. Create\n2. Insert\n3. Delete\n4. Display\n5. Exit\n"); printf("\nEnter the choice : "); scanf("%d",&ch); switch(ch) { case 1: size=0; printf("\nEnter the no. of elements to inserted : "); scanf("%d",&n); printf("\nEnter the elements : "); for(i=0;i<n;i++) { scanf("%d",&a[i]); insert(a[i],heap);

} break; case 2: printf("\nEnter the element to be inserted : "); scanf("%d",&x); insert(x,heap); printf("\n Element Inserted \n"); break; case 3: x=delmin(heap); printf("\n The element deleted is : %d",x); break; case 4: display(heap); break; case 5: exit(0); } }while(ch<6); getch(); } void insert(int x,int heap[]) { int i; for(i=++size;heap[i/2]>x;i=i/2) heap[i]=heap[i/2]; heap[i]=x; } int delmin(int heap[]) { int i,child; int min,last;

min=heap[1]; last=heap[size--]; for(i=1;(i*2)<=size;i=child) { child=i*2; if(child!=size && heap[child+1] < heap[child]) child++; if(last > heap[child]) heap[i]=heap[child]; else break; } heap[i]=last; return min; } void display(int heap[]) { int i; for(i=1;i<=size;i++) printf("%d\t",heap[i]); }

OUTPUT: ************ PRIORITY QUEUE USING HEAPS ***************

Heap Operation 1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter the choice : 1 Enter the no. of elements to inserted : 4 Enter the elements : 3 2 6 7 Heap Operation 1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter the choice : 4 2 3 6 7 Heap Operation

1. Create 2. Insert 3. Delete 4. Display 5. Exit Enter the choice : 2 Enter the element to be inserted : 7 Element Inserted

EX. No: 9 PROGRAM

IMPLEMENTATION OF HASHING WITH OPEN ADDRESSING

// Hashing with open addressing #include<stdio.h> #include<conio.h> #include<stdlib.h> # define max 10 void display(int []); void main() { int a[max],num,key,i; char ans; int create(int); void lin_prob(int [],int,int); clrscr(); printf("\n Collision Handling by Linear Probing\n"); for(i=0;i<=max;i++) a[i]=-1; do { printf("\nEnter the number : "); scanf("%d",&num); key=create(num); lin_prob(a,key,num); printf("\nDo you want to continue? (y/n) :"); ans=getch(); }while(ans=='y' || ans=='Y'); display(a); getch(); }

int create(int num) { int key=num % max; return key; } void lin_prob(int a[max],int key,int num) { int flag,i,count=0; 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; } for(i=0;i<key && flag==0;i++) if(a[i]==-1) { a[i]=num; flag=1; break; } } } void display(int a[max]) { int i; printf("\nThe hash table is : \n"); for(i=0;i<max;i++) printf("\n a[%d]\t%d",i,a[i]); }

OUTPUT : Collision Handling by Linear Probing Enter the number : 34 Do you want to continue? (y/n) : Enter the number : 56 Do you want to continue? (y/n) : Enter the number : 22 Do you want to continue? (y/n) : The hash table is : a[0] -1 a[1] -1 a[2] 22 a[3] -1 a[4] 34 a[5] -1 a[6] 56 a[7] -1 a[8] -1 a[9] -1

EX.NO: 10

PRIMS ALGORITHM FOR MINIMAL SPANNING TREE

.PROGRAM: //PRIMS ALGORITHM FOR MINIMAL SPANNING TREE #include<stdio.h> #include<conio.h> # define size 10 # define infinity 32767 void prim(int [][size],int); void main() { int G[size][size],nodes; int v1,v2,length,i,j,n; clrscr(); printf("\n\t*******************PRIMS ALGORITHM**********************\n"); printf("\nEnter the number of nodes : "); scanf("%d",&nodes); printf("\nEnter the number of edges : "); scanf("%d",&n); for(i=0;i<nodes;i++) for(j=0;j<nodes;j++) G[i][j]=0; printf("\nEnter the edges and Weights : "); for(i=0;i<n;i++) { printf("\nEnter the edges by V1 and V2 :"); scanf("%d %d",&v1,&v2); printf("\nEnter the corresponding weight :");

scanf("%d",&length); G[v1][v2]=G[v2][v1]=length; } getch(); printf("\n"); clrscr(); prim(G,nodes); getch(); } void prim(int G[][size],int nodes) { int Q[size],i,j,k; int min_dist,v1,v2,total=0; for(i=0;i<nodes;i++) Q[i]=0; printf("\n\nMinimal Spanning Tree is \n"); Q[0]=1; for(k=1;k<nodes;k++) { min_dist=infinity; for(i=0;i<nodes;i++) { for(j=0;j<nodes;j++) { if(G[i][j] && ((Q[i] && !Q[j]) || (!Q[i] && Q[i]))) { if(G[i][j]<min_dist) { min_dist=G[i][j]; v1=i; v2=j; }

} } } printf("\nEdges (%d %d) and weight = %d",v1,v2,min_dist); Q[v1]=Q[v2]=1; total=total+min_dist; } printf("\n\n\t Total Path length is = %d",total); } OUTPUT: *******************PRIMS ALGORITHM**********************

Enter the number of nodes : 3 Enter the number of edges : 3 Enter the edges and Weights : Enter the edges by V1 and V2 :0 1 Enter the corresponding weight :1 Enter the edges by V1 and V2 :0 2 Enter the corresponding weight :2 Enter the edges by V1 and V2 :2 1 Enter the corresponding weight :3 Minimal Spanning Tree is Edges (0 1) and weight = 1 Edges (0 2) and weight = 2 Total Path length is = 3

Potrebbero piacerti anche