ds cse lab

55
1.a) Single linked list #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct node { int element; struct node*next; }*list=NULL,*p; struct node*find(int); struct node*findpre(int); void insert(int f); void delet(int g); void display(); void main() { int data,ch; clrscr(); printf("singly linked list"); printf("\n menu:\n\t1.insert\n\t2.delet\n\t3.find\n\t4.display\n\ t5.exit"); do { printf("\n\t enter your choice:"); scanf("%d",&ch); switch(ch) { case 1:printf("\n\tenter the element to be insert:"); scanf("%d",&data); insert(data); break; case 2:printf("\n\t enter the element to be delete:"); scanf("%d",&data); delet(data); break; case 3:printf("\n\t enter the element to be find:"); scanf("%d",&data); find(data);

Transcript of ds cse lab

Page 1: ds cse lab

1.a) Single linked list

#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>struct node{int element;struct node*next;}*list=NULL,*p;struct node*find(int);struct node*findpre(int);void insert(int f);void delet(int g);void display();void main(){int data,ch;clrscr();printf("singly linked list");printf("\n menu:\n\t1.insert\n\t2.delet\n\t3.find\n\t4.display\n\t5.exit");do{printf("\n\t enter your choice:");scanf("%d",&ch);switch(ch){case 1:printf("\n\tenter the element to be insert:");scanf("%d",&data);insert(data);break;case 2:printf("\n\t enter the element to be delete:");scanf("%d",&data);delet(data);break;case 3:printf("\n\t enter the element to be find:");scanf("%d",&data);find(data);printf("\n\t the element is present in the position:%d",p);break;case 4:display(); break;case 5: exit(0);}}

Page 2: ds cse lab

while(ch<5);getch();}void insert(int x){ struct node *newnode; int pos; newnode=malloc(sizeof(struct node)); newnode->element=x; if(list->next==NULL) { list->next=newnode; newnode->next=NULL; } else { printf("\n\t enter the values of the element after which is to be inserted:"); scanf("%d",&pos); p=find(pos); newnode->next=p->next; p->next=newnode; } } struct node*find(int s) { p=list->next; while(p!=NULL&&p->element!=s) p=p->next; return p; } void delet(int x) { struct node*temp; temp=malloc(sizeof(struct node)); p=findpre(x); if(p->next!=NULL) { temp=p->next; p->next=temp->next; printf("\n\t the deleted element is:%d",temp->element); free(temp); } else printf("\n\t Elements are not found:"); } struct node*findpre(int s)

Page 3: ds cse lab

{ p=list; while (p->next!=NULL&&p->next->element!=s) p=p->next; return p; } void display() { if(list->next==NULL) printf("\n\tlist is empty:"); else { p=list->next; printf("\n\t elements are:\n\t"); while(p!=NULL) { printf("%d--->",p->element); p=p->next; } printf("NULL\n"); } }

Page 4: ds cse lab

Output:

singly linked list menu: 1.insert 2.delet 3.find 4.display 5.exit enter your choice:1

enter the element to be insert:10

enter your choice:1

enter the element to be insert:20

enter the values of the element after which is to be inserted:10

enter your choice:1

enter the element to be insert:30

enter the values of the element after which is to be inserted:10

enter your choice:4

elements are: 10--->30--->20--->NULL

enter your choice:2

enter the element to be delete:30

the deleted element is:30 enter your choice:4

elements are: 10--->20--->NULL

enter your choice:3

enter the element to be find:20

the element is present in the position:2264 enter your choice:5

Page 5: ds cse lab

1.b)Double linked list

#include<stdio.h>#include<conio.h>#include<alloc.h>struct node{int data;struct node *lptr,*rptr;}*head,*p;struct node *ins_beg(int x, struct node *first);struct node *ins_end(int x,struct node *first);struct node *find(int x);void display(struct node *first);struct node *dele(struct node *first,int del);void main(){int choice,x,del;head=NULL;clrscr();printf(“DOUBLY LINKED LIST”);printf("\n menu:\n");printf(" 1.insert-beg\n 2.insert-end\n 3.delete\n 4.find\n 5.display\n 6.exit\n");while(1){printf("\n enter ur choice");scanf("%d",&choice);switch(choice){case 1:printf("\n enter the data to be inserted:");scanf("%d",&x);head=ins_beg(x,head);break;case 2:printf("\n enter the data to be inserted:");scanf("%d",&x);head=ins_end(x,head);break;case 3:printf("\n enter the data to be deleted:");scanf("%d",&del);head=dele(head,del);break;case 4:printf("\n enter the element to be searched:");scanf("%d",&x);

Page 6: ds cse lab

find(x);printf("the element is present in the location:%d",p);break;case 5:display(head);break;case 6:exit(0);default:printf("\n invalid entry!!!!");getch();}}}struct node *ins_beg(int x,struct node *first){struct node *new,*cur,*prev;new=malloc(sizeof(struct node));if(first==NULL){new->data=x;new->lptr=NULL;new->rptr=NULL;return new;}else{new->data=x;new->lptr=NULL;new->rptr=first;return new;}}struct node *ins_end(int x,struct node *first){struct node*new,*cur,*prev;new=malloc(sizeof(struct node));if(first==NULL){new->data=x;new->lptr=NULL;new->rptr=NULL;return new;}else{cur=first;

Page 7: ds cse lab

while(cur->rptr!=NULL){prev=cur;cur=cur->rptr;}cur->rptr=new;new->data=x;new->lptr=cur;new-> rptr=NULL;return first;}}struct node *find(int x){p=head->rptr;while(p!=NULL&&p->data!=x)p=p->rptr;return p;}struct node *dele(struct node *first,int del){struct node *prev,*cur;cur=first;if(first==NULL){printf("\n no data present!!!!");getch();}else if(first->data==del){printf("\n data %dis deleted \n",first->data);first=first->rptr;getch();return first;}else{while(cur->rptr!=NULL&&cur->data!=del){prev=cur;cur=cur->rptr;}if(cur->rptr==NULL&&cur->data!=del)printf("\n data not present!!!!");else if(cur->rptr!=NULL&&cur->data==del){

Page 8: ds cse lab

prev->rptr=cur->rptr;(cur->rptr)->lptr=prev;printf("\n data %d is deleted\n",cur->data);}else if(cur->rptr==NULL&&cur->data==del){prev->rptr=NULL;printf("\n data %d is deleted\n",cur->data);}getch();return first;}}void display(struct node *first){struct node *temp;temp=first;if(temp==NULL)printf("\n no data present!!!!");while(temp!=NULL){printf("%d\t",temp->data);temp=temp->rptr;}getch();}

Page 9: ds cse lab

Output:

Double linked list menu:

1. insertbeg2. insertend3. delete4. find5. display6. exit

enter your choice:1enter the data to be inserted:10enter your choice:1enter the data to be inserted:20enter your choice:1enter the data to be inserted:30enter your choice:530 20 10enter your choice:2enter the data to be inserted:40enter your choice:530 20 10 40enter your choice:3enter the data to be deleted:10Data 10 is deletedenter your choice:530 20 40enter your choice:4enter the element to be searched:20the element is present in the location:2260enter your choice:4enter the element to be searched:40the element is present in the location:2280enter your choice:6

Page 10: ds cse lab

2. Polynomial addition#include<malloc.h>#include<stdio.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("\nEnter the coefficient:");scanf("%d",&node->coeff);printf("\nenter the power");scanf("%d",&node->pow);node->next=(struct link*)malloc(sizeof (struct link));node=node->next;node->next=NULL;printf("\ndo u wt to continue(y/n):");ch=getch();printf("%c",ch);}while(ch=='y'||ch=='y');}void display (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){

Page 11: ds cse lab

while(poly1->next&&poly2->next){if(poly1->pow>poly2->pow){poly->pow=poly1->pow;poly->coeff=poly1->coeff;poly1=poly1->next;}else if(poly1->pow < poly2->pow){poly ->pow=poly2->pow;poly ->coeff=poly2->coeff;poly2=poly2->next;}else{poly ->pow=poly1->pow;poly->coeff=poly1->coeff+poly2->coeff;poly1=poly1->next;poly2=poly2->next;}poly->next=(struct link*)malloc(sizeof(struct link));poly =poly->next;poly->next=NULL;}while(poly1->next||poly2->next){if(poly1->next){poly->pow=poly1->pow;poly->coeff=poly1->coeff;poly1=poly1->next;}if(poly2->next){poly->pow=poly2->pow;poly->coeff=poly->coeff;poly2=poly->next;}poly->next=(struct link*)malloc(sizeof (struct link));poly=poly ->next;

Page 12: ds cse lab

poly->next=NULL;}}void main(){poly1=(struct link*)malloc(sizeof(struct link));poly2=(struct link*)malloc(sizeof(struct link));poly=(struct link*)malloc(sizeof(struct link));clrscr();printf("\nEnter the first polynominal");create(poly1);printf("\nFirst polynominal");display(poly1);printf("\nEnter the second polynominal");create(poly2);printf("\nsecond polynominal");display(poly2);polyadd(poly1,poly2,poly);printf("Addition of two polynominal");display(poly);getch();}

Output:

Page 13: ds cse lab

Enter the first polynomialEnter the coefficient:2enter the power3do u wt to continue(y/n):yEnter the coefficient:4enter the power2do u wt to continue(y/n):nFirst polynominal2x^3+4x^2Enter the second polynominalEnter the coefficient:4enter the power3do u wt to continue(y/n):yEnter the coefficient:2enter the power1do u wt to continue(y/n):nSecond polynomial:4x^3+2x^1Addition of two polynomial: 2x^3+4x^2+2x^1

3.Conversion of infix to postfix expression

Page 14: ds cse lab

#include<stdio.h>#include<conio.h>#include<alloc.h>char inf[40],post[40];int top=0,st[20];void postfix();void push(int);char pop();void main(){clrscr();printf(“TO CONVERT INFIX TO POSTFIX”);printf("\n Enter the infix expression\n");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]>=4)post[j++]=pop();push(4);break;

case '^':while(st[top]>=4)

Page 15: ds cse lab

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\tPostfix expression is=>\n\t\t%s",post);}

void push(int ele){top++;st[top]=ele;}

char pop(){int ele;char e;ele=st[top];top--;switch(ele) { case 1:

e='+';break;

case 2:e='-';break;

case 3:e='*';break;

case 4:e='/';break;

Page 16: ds cse lab

case 5:e='^';break;

}return(e);}

OutputEnter the infix expressiona+b-c*e+fPostfix expression is=>ab+ce*-f+

4. Double ended queue:

Page 17: ds cse lab

#include<stdio.h>#include<alloc.h>#include<math.h>#include<conio.h>struct node{int number;struct node *next;}*head,*current;void main(){int x,p,n;void create();void display();void insertfront();void insertrear();void deletefront();void deleterear();void exit();clrscr();printf("deque");printf("\n 1.create");printf("\n 2.display");printf("\n 3.insert at front");printf("\n 4.insert at rear");printf("\n 5.delete at front");printf("\n 6.delete at rear");printf("\n 7.exit");while(1){printf("\n enter ur choice:");scanf("%d",&n);switch(n){case 1:create();break;case 2:display();break;case 3:insertfront();break;case 4:insertrear();break;case 5:deletefront();break;case 6:deleterear();break;

Page 18: ds cse lab

case 7:exit();break;}}getch();}void create(){struct node*front;front=malloc(sizeof(struct node));printf("\n enter the value:");scanf("%d",&front->number);front->next=NULL;if(head==NULL){head=front;current=head;}else{current->next=front;current=current->next;}}void display(){struct node *front;front=head;printf("\n the elements are:\n");while(front!=NULL){printf("%d\t",front->number);front=front->next;}}void insertfront(){struct node *front;front=malloc(sizeof(struct node));printf("\n enter the value to insert in front position:");scanf("%d",&front->number);front->next=NULL;if(head==NULL){head=front;}

Page 19: ds cse lab

else{front->next=head;head=front;}}void insertrear(){struct node*rear;rear=malloc(sizeof (struct node));printf("\n enter the value to insert in rear posistion:");scanf("%d",&rear->number);rear->next=NULL;if(head==NULL){head=rear;}else{current->next=rear;current =current->next;}}void deletefront(){struct node *front;if(head==NULL){printf("\n empty");}front=head;head=front->next;printf("\n first element is deleted \n");free(front);}void deleterear(){struct node *rear,*prev;if(head==NULL){printf("\n empty");}else if(head->next==NULL){rear=head;free(rear);

Page 20: ds cse lab

}else{current=head;while(current->next!=NULL){prev=current;current=current->next;}rear=current;prev->next=NULL;printf("\n last element is deleted");free(rear);}}

Page 21: ds cse lab

Output:

deque 1.create 2.display 3.insert at front 4.insert at rear 5.delete at front 6.delete at rear 7.exit enter ur choice:1 enter the value:30 enter ur choice:1 enter the value:40 enter ur choice:2 the elements are:30 40enter ur choice:3enter the value to insert in front position:20enter ur choice:3enter the value to insert in front position:10enter ur choice:2the elements are:10 20 30 40enter ur choice:4enter the value to insert in rear posistion:50enter ur choice:4enter the value to insert in rear posistion:60enter ur choice:2the elements are:10 20 30 40 50 60enter ur choice:5first element is deletedenter ur choice:2the elements are:20 30 40 50 60enter ur choice:6last element is deletedenter ur choice:2the elements are:20 30 40 50enter ur choice:7

5. Expression tree traversals

Page 22: ds cse lab

#include<stdio.h>#include<conio.h>#include<alloc.h>#include<ctype.h>#define size 20typedef struct node{ char data; struct node *left; struct node *right;}btree;btree *stack[size];int top;void main(){ btree *root; char exp[80]; btree *create(char exp[80]); void inorder(btree *root); void preorder(btree *root); void postorder(btree *root); clrscr(); printf("\n Enter the postfix expression\n"); scanf("%s",exp); top=-1; root=create(exp); printf("\n The tree is created:"); printf("\n The inorder traversal is .... \n"); inorder(root); printf("\n The preorder traversal is .... \n"); preorder(root); printf("\n The postorder traversal is ...\n"); postorder(root); getch();}btree* create(char exp[]){ btree *temp; int pos; char ch; void push(btree *); btree *pop(); pos=0; ch=exp[pos]; while(ch!='\0')

Page 23: ds cse lab

{ temp=(btree *) malloc(sizeof(btree)); temp->left=temp->right=NULL; temp->data=ch; if(isalpha(ch))

push(temp); else if(ch=='+' || ch=='*' || ch=='-' || ch=='/') {

temp->right=pop(); temp->left=pop(); push(temp);

} else

printf("Invalid character in expression\n"); pos++; ch=exp[pos]; } temp=pop(); return(temp);}void push(btree *Node){ if(top+1 >= size) printf("Error: stack is full\n"); top++; stack[top]=Node;}btree* pop(){ btree *Node; if(top==-1) printf("Error: Stack is Empty\n"); Node=stack[top]; top--; return(Node);}void inorder(btree *root){ btree *temp; temp=root; if(temp!=NULL) { inorder(temp->left); printf("%c",temp->data); inorder(temp->right); }

Page 24: ds cse lab

}void preorder(btree *root){ btree *temp; temp=root; if(temp!=NULL) { printf("%c",temp->data); preorder(temp->left); preorder(temp->right); }}void postorder(btree *root){ btree *temp; temp=root; if(temp!=NULL) { postorder(temp->left); postorder(temp->right); printf("%c",temp->data); }}

OutputEnter the Postfix Expressionab+cd+*The tree is created.The inorder traversal is …a+b*c+dThe preorder traversal is …*+ab+cdThe postorder traversal is …ab+cd+*

6.Binary search tree

Page 25: ds cse lab

#include<stdio.h>#include<stdlib.h>#include<conio.h>typedef struct tree *node;node insert (int ,node T);node findmin(node T);node del(int ,node T);void display(node T);struct tree{int data;struct tree *right ,*left;}*root;void main(){node T=NULL;int data ,n,i=0;char c;clrscr();printf("\nEnter the element no.of element in the tree");scanf("%d",&n);printf("\nEnter the element to insert ");while(i<n){scanf("%d",&data);T=insert(data,T);i++;}printf("\nElement display inorder formate are");diaplay(T);printf("Enter the element to delete\n");scanf("%d",&data);T=del(data,T);printf("\nthe Content of the tree after deletion\n ");display(T);getch();}node insert(int x,node T){struct tree *newnode;newnode=malloc(sizeof(struct tree));

Page 26: ds cse lab

if(newnode==NULL)printf("\nout of space");else{if(T==NULL){newnode->data=x;newnode->left=NULL;newnode->right=NULL;T=newnode;}else{if(x<T->data)T->left=insert(x,T->left);elseT->right=insert(x,T->right);}}return T;}node del(int x,node T){node tmpcell;if(T==NULL)printf("\nElement not found");else if(x<T->data)T->left=del(x,T->left);else if(x>T->data)T->right=del(x,T->right);else if(T->left&&T->right){tmpcell=findmin(T->right);T->data=tmpcell->data;T->right=del(T->data,T->right);}else{tmpcell=T;if(T->left==NULL)T=T->right;

Page 27: ds cse lab

else if(T->right==NULL)T=T->left;free(tmpcell);}return T;}node findmin(node T){if(T!=NULL){if(T->left==NULL)return T;elsereturn findmin(T->left);}return(0);}void display(node T){if(T!=NULL){display(T->left);printf("%d",T->data);display(T->right);}}

Output:Enter the number of elements in the tree:5Enter the elements to insert:35

Page 28: ds cse lab

23104530Elements displayed in in-order format are:10 23 30 35 45Enter the element to delete:23The contents of tree after deletion:10 30 35 45

7.AVL Tree #include<stdio.h>#include<conio.h>#include<stdlib.h>

Page 29: ds cse lab

#include<alloc.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();printf("\n1.Insert\n");printf("2.Display");printf("\n3.Quit");while(1){printf("Enter your choice:");scanf("%d",&choice);switch(choice){case 1: printf("Enter the value to be inserted:"); scanf("%d",&info); if(search(root,info)==NULL) root=insert(info,root,&ht_inc); else printf("\n Duplicate value ignored"); break;case 2: if(root==NULL) { printf("Tree is empty\n"); continue;

Page 30: ds cse lab

} printf("Tree is:\n"); display(root,1); printf("\n\n"); break;case 3: exit(1);default: printf("Wrong Choice\n");}}getch();}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);}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));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){

Page 31: ds cse lab

switch(pptr->balance){case -1: pptr->balance=0; *ht_inc=FALSE; break;case 0: pptr->balance=1; break;case 1: 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 rototation\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;}*ht_inc=FALSE;}

Page 32: ds cse lab

}}if(info>pptr->info){pptr->rchild=insert(info,pptr->rchild,ht_inc);if(*ht_inc==TRUE){switch(pptr->balance){case 1: pptr->balance=0; *ht_inc=FALSE; break;case 0: pptr->balance=-1; break;case -1: aptr=pptr->rchild; if(aptr->balance==-1) { printf("Right toRight 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;

Page 33: ds cse lab

elseaptr->balance=0;bptr->balance=0;pptr=bptr;}*ht_inc=FALSE;}}}return(pptr);}display(struct node*ptr,int level){int i;if(ptr!=NULL){display(ptr->lchild,level+1);printf("\n");for(i=0;i<level;i++)printf(" ");printf("%d",ptr->info);display(ptr->rchild,level+1);}}

Output:Left to left rotation 1.Insert2.Display3.QuitEnter your choice:1Enter the value to be inserted:8 Enter your choice:1Enter the value to be inserted:5Enter your choice:1

Page 34: ds cse lab

Enter the value to be inserted:10Enter your choice:1Enter the value to be inserted:3Enter your choice:1Enter the value to be inserted:7Enter your choice:1Enter the value to be inserted:1Left to Left rotationEnter your choice:2Tree is: 1 3 5 7 8 10Enter your choice:3Right to right rotation1.Insert2.Display3.QuitEnter your choice:1Enter the value to be inserted:3Enter your choice:1Enter the value to be inserted:1Enter your choice:1Enter the value to be inserted:7Enter your choice:1Enter the value to be inserted:5Enter your choice:1Enter the value to be inserted:8Enter your choice:1Enter the value to be inserted:10Right to right rotationEnter your choice:2Tree is: 1 3 5 7 8

Page 35: ds cse lab

10Enter your choice:3Right to left rotation1.Insert2.Display3.QuitEnter your choice:1Enter the value to be inserted:20Enter your choice:1Enter the value to be inserted:10Enter your choice:1Enter the value to be inserted:30Enter your choice:1Enter the value to be inserted:5Enter your choice:1Enter the value to be inserted:15Enter your choice:1Enter the value to be inserted:12Left to right rotationEnter the value to be inserted:18Enter your choice:2Tree is: 5 10 12 15 18 20 30Enter your choice:3

Left to left rotation1.Insert2.Display3.QuitEnter your choice:1Enter the value to be inserted:10Enter your choice:1Enter the value to be inserted:8Enter your choice:1Enter the value to be inserted:15

Page 36: ds cse lab

Enter your choice:1Enter the value to be inserted:12Enter your choice:1Enter the value to be inserted:17Enter your choice:1Enter the value to be inserted:11Left to right rotationEnter the value to be inserted:14Enter your choice:2Tree is:

8 10 11 12 14 15 17Enter your choice:3

8.Priority queue#include<stdio.h>#include<conio.h>#include<stdlib.h>#define MAX 1000typedef int ElementType;struct HeapStruct;typedef struct HeapStruct *PriorityQueue;priorityQueueInitialize(int MaxElement);void insert(ElementType X, PriorityQueue H);ElementType DeleteMin(PriorityQueue H);

Page 37: ds cse lab

ElementType FindMin(PriorityQueue H);int IsEmpty(PriorityQueue H);int IsFull(PriorityQueue H);void display(PriorityQueue H);#define MINPQSIZE 10#define MINDATA-32767struct HeapStruct{int Capacity;int Size;ElementType *Element;};PriorityQueue Initialize(int MaxElements){PriorityQueue H;if(MaxElements<MINPQSIZE)printf("priority queue Size is too Small");H=(struct HeapStruct*)malloc(sizeof(struct HeapStruct));if(H==NULL)printf("Out of space!!!");H->Element=(int*)malloc(sizeof(struct HeapStruct));if(H==NULL)printf("out of space");H->Capacity=MaxElements;H->Size=0;H->Element[0]=MINDATA;return H;}void Insert(ElementType X,PriorityQueue H){int i;if(IsFull(H)){printf("\nPriority queue is full");return;}for(i=++H->Size;H->Element[i/2]>X;i/=2)H->Element[i]=H->Element[i/2];H->Element[i]=X;}ElementType DeleteMin(PriorityQueue H)

Page 38: ds cse lab

{int i,child;ElementType MinElement,LastElement;if(IsEmpty(H)){printf("\nPriority Queue is empty");return H->Element[0];}MinElement=H->Element[i];LastElement=H->Element[H->Size--];for(i=1;i*2<=H->Size;i=child){child=i*2;if(child !=H->Size&&H->Element[child+1]<H->Element[child]);child++;if(LastElement>H->Element[child])H->Element[i]=H->Element[child];elsebreak;}H->Element[i]=LastElement;return MinElement;}ElementType FindMin(PriorityQueue H){if(!IsEmpty(H))return H->Element[1];printf("Priority Queue is empty");return H->Element[0];}int IsEmpty(PriorityQueue H){return H->Size==0;}int IsFull(PriorityQueue H){return H->Size==H->Capacity;}void display(PriorityQueue H){int i=1;

Page 39: ds cse lab

printf("\nthe element in the prinoity Queue are");while(i<=H->Size){printf("%d\t",H->Element[i]);i++;}}main(){PriorityQueue H;int b,a,j,n;clrscr();H=Initialize(MAX);printf("\n1.Insert\n2.Delete min\n3.Find min\n4.Display\n5.exit");while(1){printf("\nenter ur choice");scanf("%d",&b);switch(b){case 1:printf("\nEnter the num");scanf("%d",&j);Insert(j,H);break;case 2:n=DeleteMin(H);printf("the delete element is %d \n",n);break;case 3:a=FindMin(H);printf("\nthe mini element is %d \n",a);break;case 4:display(H);break;case 5:exit(0);break;}}

Page 40: ds cse lab

}Output:1.Insert2.Delete min3.Find min4.Display5.exitenter ur choice1Enter the num45enter ur choice1Enter the num56enter ur choice1Enter the num12enter ur choice1Enter the num4enter ur choice1Enter the num13enter ur choice4the element in the prinoity Queue are4 12 45 56 13enter ur choice2the delete element is 4enter ur choice4the element in the prinoity Queue are 12 13 45 56enter ur choice3the mini element is 12enter ur choice5 9.Hashing Technique #include<stdio.h>#include<conio.h>#include<stdlib.h>#define MAX 10void main(){ int a[MAX],num,key,i; char ans; int create(int); void linear_prob(int [],int,int),display(int []); /*clrscr();*/ printf("\n Collision Handling By Linear Probing"); for(i=0;i<MAX;i++) a[i]=-1; do {

Page 41: ds cse lab

printf("\n Enter the Number: "); scanf("%d",&num); key=create(num); linear_prob(a,key,num); printf("\n Do u wish to continue?(y/n)"); ans=getch(); }while(ans=='y'); display(a); getch();}int create(int num){ int key; key=num%10; return key;}void linear_prob(int a[MAX],int key,int num){ int flag,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("\n Hash 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;

}

Page 42: ds cse lab

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("\n The Hash Table is .... \n"); for(i=0;i<MAX;i++)

printf("\n %d %d",i,a[i]); }

OutputCollision Handling By Linear ProbingEnter the Number 131Do u wish to continue?(y/n)yEnter the Number 21Do u wish to continue?(y/n)yEnter the Number 3Do u wish to continue?(y/n)yEnter the Number 4Do u wish to continue?(y/n)yEnter the Number 5Do u wish to continue?(y/n)yEnter the Number 8Do u wish to continue?(y/n)yEnter the Number 9Do u wish to continue?(y/n)yEnter the Number 18Do u wish to continue?(y/n)nThe Hash Table is…

0 181 1312 213 34 45 56 -17 -18 8

Page 43: ds cse lab

9 9

10.Prims Algorithm Minimal Spanning Tree#include<stdio.h>#include<conio.h># define SIZE 20# define INFINITY 32767void prim(int G[][SIZE],int nodes){ int select[SIZE],i,j,k; int min_dist,v1,v2,total=0; for(i=0;i<nodes;i++)

select[i]=0; printf("\n\n The Minimal Spanning Tree is :\n"); select[0]=1; for(k=1;k<nodes;k++) { min_dist=INFINITY; for(i=0;i<nodes;i++)

Page 44: ds cse lab

{for(j=0;j<nodes;j++){ if(G[i][j] && ((select[i] && !select[j]) || (!select[i] && select[j]))) { if(G[i][j]<min_dist) {

min_dist=G[i][j]; v1=i; v2=j;

} } }}printf("\n Edge (%d,%d) and weight = %d",v1,v2,min_dist);select[v1]=select[v2]=1;total=total+min_dist;

} printf("\n\n\t Total Path Length is = %d",total);}void main(){

int G[SIZE][SIZE],nodes;int v1,v2,length,i,j,n;clrscr();printf("\n\t Enter Number of Nodes in the Graph ");scanf("%d",&nodes);printf("\n Enter Number of Edges in the Graph ");scanf("%d",&n);for(i=0;i<nodes;i++)for(j=0;j<nodes;j++) G[i][j]=0;printf("\n Enter edges and weights \n");for(i=0;i<n;i++){ printf("\n Enter Edge by v1 and v2 :"); scanf("%d %d",&v1,&v2); printf("\n Enter corresponding weight :"); scanf("%d",&length); G[v1][v2]=G[v2][v1]=length;}

getch(); printf("\n\t"); clrscr(); prim(G,nodes); getch();

Page 45: ds cse lab

}

OUTPUTEnter the number of nodes in the graph: 7Enter the number of edges in the graph: 9Enter edges and weights:Enter edge by V1&V2:0 1Enter the corresponding weight: 27Enter edge by V1&V2:1 2Enter the corresponding weight: 16Enter edge by V1&V2:2 3Enter the corresponding weight: 12Enter edge by V1&V2:3 4Enter the corresponding weight: 22Enter edge by V1&V2:4 5Enter the corresponding weight: 25Enter edge by V1&V2:0 5Enter the corresponding weight: 06Enter edge by V1&V2:1 6

Page 46: ds cse lab

Enter the corresponding weight: 14Enter edge by V1&V2:4 6Enter the corresponding weight: 24Enter edge by V1&V2:3 6Enter the corresponding weight: 18The minimum spanning tree is:Edge (0 5) and weight = 6Edge (4 5) and weight = 25Edge (3 4) and weight =22Edge (2 3) and weight = 12Edge (1 2) and weight =16Edge (1 6) and weight =14Total path length is = 95