0% found this document useful (0 votes)
83 views38 pages

DS Programs

The document describes the implementation of a priority queue using a heap data structure. It includes function definitions for initializing the heap, performing percolation down after insertion, percolation up after deletion, and functions to add and delete elements from the heap. Sample code is provided to create a heap struct, define max size, and call the functions.

Uploaded by

Karen Faith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views38 pages

DS Programs

The document describes the implementation of a priority queue using a heap data structure. It includes function definitions for initializing the heap, performing percolation down after insertion, percolation up after deletion, and functions to add and delete elements from the heap. Sample code is provided to create a heap struct, define max size, and call the functions.

Uploaded by

Karen Faith
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 38

Reg no:30309205005

IMPLEMENTATION OF DEPTH FIRST SEARCH Coding:


#include<stdio.h> #include<conio.h> int q[20],top=-1,front=-1,rear=-1; int a[20][20],vis[20],stack[20]; int del(); void add(int item); void dfs(int s,int n); void push(int item); int pop(); void main() { int n,i,s,ch,j; char c,dummy; clrscr(); printf("ENTER THE NUMBER OF VERTICES:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("\n ENTER 1 IF %d HAS %d ELSE 0\n",i,j); scanf("%d",&a[i][j]); } } printf("\nthe adjacent matrix is \n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%d",a[i][j]); } printf("\n"); } for(i=1;i<=n;i++) vis[i]=0; printf("ENTER THE SOURCE VERTEX:"); scanf("%d",&s); dfs(s,n); getch(); }

Reg no:30309205005

void dfs(int s,int n) { int i,k; push(s); vis[s]=1; k=pop(); if(k!=0) printf("%d",k); while(k!=0) { for(i=1;i<=n;i++) if((a[k][i]!=0)&&(vis[i]==0)) { push(i); vis[i]=1; } k=pop(); if(k!=0) printf("%d",k); } for(i=1;i<=n;i++) if(vis[i]==0) { dfs(i,n); } } void push(int item) { if(top==19) printf("stack overflow"); else stack[++top]=item; } int pop() { int k; if(top==-1) return(0); else { k=stack[top--];

Reg no:30309205005

return(k); }}

Reg no:30309205005

OUTPUT:
ENTER 1 IF 1 HAS 1 ELSE 0 0 ENTER 1 IF 1 HAS 2 ELSE 0 1 ENTER 1 IF 1 HAS 3 ELSE 0 1 ENTER 1 IF 1 HAS 4 ELSE 0 0 ENTER 1 IF 2 HAS 1 ELSE 0 1 ENTER 1 IF 2 HAS 2 ELSE 0 0 ENTER 1 IF 2 HAS 3 ELSE 0 0 ENTER 1 IF 2 HAS 4 ELSE 0 1 ENTER 1 IF 3 HAS 1 ELSE 0 1 ENTER 1 IF 3 HAS 2 ELSE 0 0 ENTER 1 IF 3 HAS 3 ELSE 0 0 ENTER 1 IF 3 HAS 4 ELSE 0 1 ENTER 1 IF 4 HAS 1 ELSE 0 0 ENTER 1 IF 4 HAS 2 ELSE 0 1 ENTER 1 IF 4 HAS 3 ELSE 0

Reg no:30309205005

1 ENTER 1 IF 4 HAS 4 ELSE 0 0 the adjacent matrix is 0110 1001 1001 0110 ENTER THE SOURCE VERTEX:1 1342

Reg no:30309205005

IMPLEMENTATION OF BREATH FIRST SEARCH Coding:


#include<stdio.h> #include<conio.h> int q[20],top=-1,front=-1,rear=-1; int a[20][20],vis[20],stack[20]; int del(); void add(int item); void bfs(int s,int n); void main() { int n,i,s,ch,j; char c,dummy; clrscr(); printf("ENTER THE NUMBER OF VERTICES:"); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("\n ENTER 1 IF %d HAS %d ELSE 0\n",i,j); // get matrix input scanf("%d",&a[i][j]); } } printf("\nthe adjacent matrix is \n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%d",a[i][j]); // displays the adjacency matrix } printf("\n"); } for(i=1;i<=n;i++) vis[i]=0; printf("ENTER THE SOURCE VERTEX:"); scanf("%d",&s); bfs(s,n); // function call getch(); }

Reg no:30309205005

void bfs(int s,int n) { int p,i; add(s); // function call vis[s]=1; p=del(); // function call if(p!=0) printf("%d",p); while(p!=0) { for(i=1;i<=n;i++) if((a[p][i]!=0)&&(vis[i]==0)) { add(i); vis[i]=1; } p=del(); if(p!=0) printf("%d",p); } for(i=1;i<=n;i++) if(vis[i]==0) bfs(i,n); } void add(int item) { if(rear==19) printf("\n QUEUE FULL"); else { if(rear==-1) { q[++rear]=item; front++; } else q[++rear]=item; }} int del() // function definition { int k; if((front>rear)||(front==-1)) return(0); else

Reg no:30309205005

k=q[front++]; return(k);}

OUTPUT:
ENTER THE NUMBER OF VERTICES: 2

Reg no:30309205005

ENTER 1 IF 1 HAS 1 ELSE 0 1 ENTER 1 IF 1 HAS 2 ELSE 0 0 ENTER 1 IF 2 HAS 1 ELSE 0 1 ENTER 1 IF 2 HAS 2 ELSE 0 0 The adjacent matrix is 10 10 ENTER THE SOURCE VERTEX: 5 512

IMPLEMENTATION OF QUEUE USING LINKED LIST CODING:

Reg no:30309205005

#include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct sll // create the structure { int val; struct sll *next; } node; node *p,*q,*head; int r=1,f=0,size=4,n,i,m,ch,t; void enq(); // Function prototypes int deq(); void print(); void main() { clrscr(); do { printf("\n [1]enqueue \n [2] dequeue \n [3]printqueue\n [4]exit"); printf("\n enter the choice"); scanf("%d",&ch); switch(ch) { case 1: if((r==size-1)&&(f==0)) printf("\n stack is full \n"); else enq(); break; case 2: if(r<f) printf("\t the queue is empty"); else { t=deq(); printf("\nthe dequeued value is %d",t); break; } case 3: print(); break; case 4:printf("THANKS!!!BYE"); exit(0); break;

Reg no:30309205005

default:printf("\n enter btw 1-4 \t"); } }while(ch!=4); getch(); } void enq() // function definition { if(n==0) { p=(node*)malloc(sizeof(node)); printf("\nEnter the data "); canf("%d",&p->val); head=p; p->next=NULL; r=r+1; n=n+1; } else { q=(node*)malloc(sizeof(node)); printf("Enter the value "); scanf("%d",&q->val); p->next=q; p=q; f=f+1; n=n+1; }} int deq() { p=head; head=p->next; n=n-1; return (p->val); } void print() { int i; printf(" \n *********************QUEUE*********************\n"); p=head; for(i=0;i<=n-1;i++) { printf(" |%d|->",p->val); p=p->next; }}

Reg no:30309205005

OUTPUT:
[1]enqueue [2] dequeue [3]printqueue [4]exit enter the choice 1 Enter the data 10

Reg no:30309205005

[1]enqueue [2] dequeue [3]printqueue [4]exit enter the choice 1 Enter the value 20 [1]enqueue [2] dequeue [3]printqueue [4]exit enter the choice 3 *********************QUEUE********************* |10|-> |20|-> [1]enqueue [2] dequeue [3]printqueue [4]exit enter the choice 2 the dequeued value is 10 [1]enqueue [2] dequeue [3]printqueue [4]exit enter the choice 3 *********************QUEUE********************* |20|-> [1]enqueue [2] dequeue [3]printqueue [4]exit enter the choice 4

IMPLEMENTATION OF STACK USING LINKED LIST CODING:


#include<stdio.h> #include<conio.h> #include<alloc.h> typedef struct sll {

// creates the structure

Reg no:30309205005

int val; struct sll *next; } node; node *p,*q,*s,*t,*y,*head; int ch,stacksize,i,a,x=0,b,c; void print(); void push(int); void pop(); void main() { clrscr(); printf("CREATELIST\n"); printf("\nEnter the size of the stack"); scanf("%d",&stacksize); do { printf("\n1.push\n2.pop\n3.print stack\n4.exit"); printf("\nEnter the choice"); scanf("%d",&ch); switch(ch) { case 1: if(x==stacksize) { printf("The stack is full\n"); printf("STACK OVERFLOW") ; break; } else { printf("Enter the data"); scanf("%d",&b); push(b); } break;

case 2: if(x!=0) { pop(); break; }

Reg no:30309205005

else { printf("The stack is empty\n"); printf("STACK UNDERFLOW"); break; } case 3: { if(x==0) printf("Stack is empty"); else print(); } break; case 4: { printf("exit"); exit(0); } break; default: { printf("Invalid choice"); } break; } }while(ch!=4); getch(); } void push(int d) { if(x<=stacksize) { if(x==0) { p=(node*)malloc(sizeof(node)); p->val=d; p->next=NULL; head=p; x=x+1; } else { t=(node*)malloc(sizeof(node)); t->val=d;

// function definition

Reg no:30309205005

p=head; for(i=1;i<x;i++) { p=p->next; } p->next=t; t->next=NULL; x=x+1; } } else { printf("stack is full"); } } void pop() // function definition { p=head; for(i=0;i<=x-1;i++) p=p->next; printf("The element is popped"); p->next=NULL; x=x-1; } void print() // function definition { int i; printf(" \n ************PRINTING LIST*********************\n"); p=head; printf("\t\t\t") ; for(i=0;i<=x-1;i++) { printf(" |%d|->",p->val); p=p->next; } }

Reg no:30309205005

OUTPUT:
CREATELIST Enter the size of the stack 2 1.push 2.pop 3.print stack 4.exit Enter the choice 1 Enter the data 10

Reg no:30309205005

1.push 2.pop 3.print stack 4.exit Enter the choice 1 Enter the data 20 1.push 2.pop 3.print stack 4.exit Enter the choice 2 The element is popped 1.push 2.pop 3.print stack 4.exit Enter the choice 3 ************PRINTING LIST********************* |10|-> 1.push 2.pop 3.print stack 4.exit Enter the choice 4 Exit

IMPLEMENTATION OF EXPRESSION TREE CODING:


#include<stdio.h> #include<conio.h> #include<string.h> #include<ctype.h> int ch; char postfix[100]; typedef struct treenode { char val;

Reg no:30309205005

struct treenode *left; struct treenode *right; }searchtree; searchtree *p,*d,*c,*x,*stack[100];; void push(searchtree*); void postorder(searchtree* c) ; void inorder(searchtree* c) ; searchtree* pop(); int i,j,in=0,post=0; int top=-1,size=25; void main() { clrscr(); printf("\n Enter the postfix expression"); scanf("%s",postfix); c=(searchtree*)malloc(sizeof( searchtree)); c->val=postfix[in]; c->left=NULL; c->right=NULL; do { if (isalpha(c->val)) { push(c); } else { p=(searchtree*)malloc(sizeof( searchtree)); p->val=c->val; p->right=pop(); p->left=pop(); push(p); } in+=1; c=(searchtree*)malloc(sizeof( searchtree)); c->val=postfix[in]; c->left=NULL; c->right=NULL; }while(c->val!='#'); p = pop(); printf(" THE postfix expr follows ... \n"); postorder(p); printf(" THE infix expr follows ... \n");

//END OF ELSE

//END OF DO WHILE LOOP

Reg no:30309205005

inorder(p); getch(); } void push(searchtree *c) { top+=1; stack[top]=c; } searchtree* pop() { return stack[top--]; } void postorder (searchtree *c) { if(c==NULL) return; postorder(c->left); postorder(c->right); printf("%c ",c->val); } void inorder (searchtree *c) { if(c==NULL) return; inorder(c->left); printf("%c ",c->val); inorder(c->right); } // end of main function //PUSH FUNCTION DEFINTION

//POP FUNCTION DEFINITON

// POSTORDER FN Definition

// INORDER FUNTION DEFINTION

Reg no:30309205005

OUTPUT:
Enter the postfix expression abc+*# The postfix expr follows ... abc+* The infix expr follows ... a*b+c

Reg no:30309205005

IMPLEMENTATION OF PRIORITY QUEUE USING HEAPS CODING:


#include<stdio.h> #include<conio.h> #define max 15 typedef struct node { int data[max]; int count,sindex; } heap; void initialise(heap *h) { int i;

Reg no:30309205005

h->count=0; for(i=1;i<max;i++) h->data[i]=0; } void percdown(heap *h,int pos) { int i; int val; val=h->data[pos]; while(pos<=h->count/2) { i=2*pos; if(i<=h->count&&h->data[i]<h->data[i+1]) i++; if(val>=h->data[i]) break; h->data[pos]=h->data[i]; pos=i; } h->data[pos]=val; } void percup(heap *h,int pos) { int i; int val; val=h->data[pos]; while(h->data[pos/2]<=val&&pos>1) { h->data[pos]=h->data[pos/2]; pos=pos/2; } printf("%d",pos); h->data[pos]=val; } int add(heap *h,int num) { if(h->count<max) { h->count++; h->data[h->count]=num; percup(h,h->count); return 1; }

Reg no:30309205005

else return 0; } int del(heap *h) { int i; int val; val=h->data[1]; h->data[1]=h->data[h->count]; h->count--; percdown(h,1); return val; } void disp(heap *h) { int i; printf("\nthe data in the max heap:"); for(i=1;i<=h->count;i++) printf("%d\t",h->data[i]); printf("\n"); } void menu() { printf("priority q\n-------------\n1.insert a new element\n2.delete\n3.disp\n4.quit"); } void main() { heap *h; int i,ch,z,x; clrscr(); initialise(h); do { menu(); printf("enter the choice"); scanf("%d",&ch); switch(ch) { case 1: printf("\n eneter the element to be inserted:"); scanf("%d",&x); z=add(h,x); if(z==0) printf("element can't be inserted");

Reg no:30309205005

else printf("\n element inserted"); disp(h); break; case 2: z=del(h); printf("element deleted is:\n\npriority q after del:"); disp(h); break; case 3: disp(h) ; break; } } while(ch!=4); getch(); }

Output:
Priority queue -------------------1.insert a new element 2.delete 3.disp 4.quit Enter ur choice:1 Enter the elment to be inserted:4 Element inserted The data in max heap :4 Priority queue -------------------1.insert a new element 2.delete 3.disp 4.quit Enter ur choice:1 Enter the elment to be inserted:25

Reg no:30309205005

Element inserted The data in max heap :5 Priority queue -------------------1.insert a new element 2.delete 3.disp 4.quit Enter ur choice:2 Pririty queue after deletion: The data in max heap :4 Priority queue -------------------1.insert a new element 2.delete 3.disp 4.quit Enter ur choice:4

IMPLEMENTATION OF HASHING TECHNIQUES Hashing header file:


#include<stdio.h> #include<conio.h> typedef struct node { int value,index,count,*a,max; }hash; void initialise(hash *h) { int i; printf("\n enter the arry size"); scanf("%d",&h->max); h->a=(int *)malloc(sizeof(int)*h->max); for(i=0;i<h->max;i++) h->a[i]=-1; h->count=0; } void collision(int x,int y,hash *h)

Reg no:30309205005

{ int i; h->index=x; h->value=y; for(i=(h->index+1);i!=h->index;i++,i=i%h->max) { if(h->a[i]==-1) { h->a[i]=h->value; printf("\n New index found"); h->count++; break; } } } int hashcal(int x,hash *h) { h->value=x; h->index=h->value%h->max; return(h->index); } void insert(int x,hash *h) { if(h->count==h->max) printf("\n insertion not possible"); else { h->value=x; h->index=hashcal(h->value,h); printf("\n hash index %d",h->index); if(h->a[h->index]==-1) { h->a[h->index]=h->value; h->count++; } else collision(h->index,h->value,h); } } void display(hash *h) { int i; for(i=0;i<h->max;i++) printf("\n data %d",h->a[i]); }

Reg no:30309205005

void del(int x,hash *h) { int i,z=0; h->value=x; h->index=hashcal(h->value,h); if(h->a[h->index]==h->value) { h->a[h->index]=-1; printf("\n deleted element %d",x); } else { for(i=0;i<h->max;i++) { if(h->a[i]==h->value) { h->a[i]=-1; z=1; printf("\n deleted element %d",x); break; }} if(z==0) printf("\n element not found"); }} void menu() { printf("\n 1.insert\n2.deletion\n3.display\n4.exit"); }

Reg no:30309205005

Coding:
#include"hash.h" void main() { hash *h; int ch,x; clrscr(); initialise(h); do { menu(); printf("\n enter ur choice"); scanf("%d",&ch); switch(ch) { case 1: printf("\n enter the element 2 b insert="); scanf("%d",&x); insert(x,h); break; case 2: printf("\n enter the element 2 b delete="); scanf("%d",&x); del(x,h); break;

Reg no:30309205005

case 3: display(h); break; case 4: break; } }while(ch!=4); getch(); }

Output:
Enter the array size 3 1.insert 2.deletion 3.display 4.exit Enter ur choice:1 Enter the element to be inserted=4 Hash index 0 1.insert 2.deletion 3.display 4.exit Enter ur choice:1 Enter the element to be inserted=5 Hash index 1 1.insert 2.deletion 3.display 4.exit Enter ur choice:1 Enter the element to be inserted=2 Hash index 2

Reg no:30309205005

1.insert 2.deletion 3.display 4.exit Enter ur choice:3 Data 4 Data 5 Data 2 1.insert 2.deletion 3.display 4.exit Enter ur choice:2 Enter the element to be deleted=2 Deleted element=2 1.insert 2.deletion 3.display 4.exit Enter ur choice:4

Reg no:30309205005

IMPLEMENTATION OF DIJKSTRAS ALGORITHM USING PRIORITY QUEUE Coding:


#include<stdio.h> #include<conio.h> #define member 1 #define nomem 0 #define infinity 999 #define max 10 int g[max][max],h[max]; int n; void main() { int src,dest,path; void build_g(); void disk(int,int); int del_q(); int front,rear; clrscr(); printf("\n enter the no.of vertices\n"); scanf("%d",&n); build_g(); printf("\n enter the source\n"); scanf("%d",&src); printf("\n enter the dest ="); scanf("%d",&dest);

Reg no:30309205005

disk(src,dest); printf("\n the shortest path is \n"); front=1; rear=n; while(front<=rear) { path=del_q(front); if(path!=infinity) printf("%d",path); front++; } getch(); } void build_g() { int i,j,v1,v2; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("\n Enter the edge of v%d to v%d",i,j); scanf("%d",&g[i][j]); } printf("\n"); }} void ins_q(int index) { h[index]=1; } int del_q(int i) { if(h[i]==1) return i; return infinity; } void disk(int src,int dest) { int small,dist[10],current,start,new1; int temp,i; void ins_q(int); for(i=0;i<=n;i++) { h[i]=0; dist[i]=infinity;

Reg no:30309205005

} h[src]=1; dist[src]=0; current=src; while(current!=dest) { small=infinity; start=dist[current]; for(i=1;i<=n;i++) { if(h[i]==0) { new1=start+g[current][i]; if(new1<dist[i]) dist[i]=new1; if(dist[i]<small) { small=dist[i]; temp=i; }} }current=temp; ins_q(current); }}

Reg no:30309205005

Output:
shortest path algorithm using pq enter the no of vertices:4 enter the edge of v 1 to v 1:1 enter the edge of v 1 to v 2:2 enter the edge of v 1 to v 3:3 enter the edge of v 1 to v 4:4 enter the edge of v 2 to v 1:4 enter the edge of v 2 to v 2:3 enter the edge of v 2 to v 3:2 enter the edge of v 2 to v 4:1 enter the edge of v 3 to v 1:1 enter the edge of v 3 to v 2:3 enter the edge of v 3 to v 3:5 enter the edge of v 3 to v 4:7 enter the edge of v 4 to v 1:7 enter the edge of v 4 to v 2:5

Reg no:30309205005

enter the edge of v 4 to v 3:3 enter the edge of v 4 to v 4:1 enter the source 1 enter the destination 4 the shortest path is.:1234

IMPLEMENTATION OF KNAPSACK PROBLEM USING BACKTRACKING ALGORITHM CODING:


#include<stdio.h> #include<conio.h> float final_profit=-1.0; int p[9]={0,11,21,31,33,43,53,55,65}; int w[9]={0,1,11,21,23,43,45,6,78}; int m=110; int n=8; int temp[9],x[9]; float final_wt=-1.0; float bound_cal(int cp,int cw,int k) { int ub,c,i; ub=cp; c=cw; for(i=k+1;i<=n;i++) { c=c+w[i]; if(c<m) ub=ub+p[i]; else return(ub+(1-(c-m)/w[i])*p[i]); } return ub; } void bk(int k,int cp,int cw) { int new_k,new_cp,new_cw,j; if(cw+w[k]<=m)

Reg no:30309205005

{ temp[k]=1; if(k<n) { new_k=k+1; new_cp=cp+p[k]; new_cw=cw+w[k]; bk(new_k,new_cp,new_cw); } if((new_cp>final_profit)&&(k==n)) { final_profit=new_cp; final_wt=new_cw; for(j=1;j<=k;j++) x[j]=temp[j]; } } if(bound_cal(cp,cw,k)>=final_profit) { temp[k]=0; if(k<n) bk(k+1,cp,cw); if((cp>final_profit)&&(k==n)) { final_profit=cp; final_wt=cw; for(j=1;j<=n;j++) x[j]=temp[j]; } } } void main() { int i; clrscr(); printf("\n capacity of knapsack=%d",m); printf("\n profit \t weight"); for(i=1;i<=n;i++) printf("\n %d\t%d",p[i],w[i]); bk(1,0,0); printf("\n knapsack "); for(i=i;i<=n;i++) { if(x[i]==1) printf("\n item %d",i);

Reg no:30309205005

} printf("\n final wt=%0.2f",final_wt); printf("\n final profit=%0.2f",final_profit); getch(); }

Output:
Capacity of knapsack = 110 Profit 11 21 31 33 43 53 55 65 Knapsack final wt = 107.00 final profit = 204.00 weight 1 11 21 23 43 45 6 78

You might also like