51
List of Experiments 1. Implement Singly linked list to create, insert, delete a nd disp lay the node. 2. Write an d execut e a prog ram for Dou bly link ed list and crea te a nod e for the following elements 2,4,6 8,10 using malloc() function 3. Create Polynomia l as a linked list a nd write function s for p olynomia l addit ion 4. Impl emen t stack an d use it to con vert In fix to Pos tfix ex pres sion 5. Represen t a Bin ary tre e Traver sal with its In- order an d Post order traversa ls. 6. Impl emen t a Pr ogra m for Bina ry Se arc h tre e. 7. Imp le ment Has hi ng Tech ni que s 8. Writ e a pr ogra m for Prod ucer Consumer Prob lem 9. Impl ement AVL Trees 10. Implement Dijistra’s Algorithm 11. Implement Prim’s Algorithm 12. Implement Kruscal’ s Algori thm 13. Knapsack Pro blem Using Backtrac king Techniq ue 14. Travellin g Salesman Prob lem Using Branc h And Bound 15. Stack Implement ation.

List of Experiments of oops

Embed Size (px)

Citation preview

Page 1: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 1/51

List of Experiments

1. Implement Singly linked list to create, insert, delete and display the node.

2. Write and execute a program for Doubly linked list and create a node for the

following elements 2,4,6 8,10 using malloc() function

3. Create Polynomial as a linked list and write functions for polynomial addition

4. Implement stack and use it to convert Infix to Postfix expression

5. Represent a Binary tree Traversal with its In-order and Post order traversals.

6. Implement a Program for Binary Search tree.

7. Implement Hashing Techniques

8. Write a program for Producer Consumer Problem

9. Implement AVL Trees

10. Implement Dijistra’s Algorithm

11. Implement Prim’s Algorithm

12. Implement Kruscal’s Algorithm

13. Knapsack Problem Using Backtracking Technique

14. Travelling Salesman Problem Using Branch And Bound

15. Stack Implementation.

Page 2: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 2/51

1. Singly Linked ListAim:

To implement a program linked list using C

Algorithm:

struct Node{

int Element;struct Node *Link;

}

Using switch case ,read a operation to perform on linked list .• If choice is 1, call insert function to perform insertion operation.

Routine to insert an element in the List

void Insert (int X,List L,Position P){

/* Insert after the position p */position Newnode;

Newnode=malloc(sizeof(struct Node));if(Newnode!=NULL)

{Newnode->Element=X;Newnode->Next=P->Next;

P->Next=Newnode;}

}• If choice is 2,call deletion function to perform deletion operation

Routine to delete an element from the list

void Delete(int X,List L){ /* Delete the first occurrence of X from the List */position P,Temp;P=Findprevious(X,L);if(!IsLast(P,L))

{Temp=P->Next;

P->Next=Temp->Next;

Page 3: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 3/51

Free(Temp);

}}

• If choice is 3,call a display function

Routine to display an element from the list

Display(List L){

while(p!=NULL){print(p->element);p=p->next;}

}PROGRAM

#include<stdio.h>#include<conio.h>#include<malloc.h>#include<stdlib.h>struct node* find(int);struct node* findprevious(int);struct node{int element;struct node* next;}* list=NULL,*p;void insert(int x);void deletion(int x);void display();void main(){int data,ch;clrscr();printf("1.Insert \t 2.Deletion \t 3.Display \t 4.Exit");do{printf("\n Enter your choice:");scanf("%d",&ch);switch(ch){case 1:printf("Enter the elelment to insert:");

Page 4: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 4/51

scanf("%d",&data);insert(data);break;case 2:printf("Enter the elelment to delete:");

scanf("%d",&data);deletion(data);break;case 3:display();break;case 4:exit(0);}}while(ch<4);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 Enter the value after which the element to be inserted:\n");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 deletion(int x){

Page 5: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 5/51

struct node* temp;temp=malloc(sizeof(struct node));p=findprevious(x);if(p->next!=NULL){

temp=p->next;p->next=temp->next;printf("\n The deleted element is %d",temp->element);free(temp);}else{printf("\n The element is not in the list:");}}struct node* findprevious(int s)

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

Page 6: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 6/51

OUTPUT:

1.Insert 2.Deletion 3.Display 4.ExitEnter your choice:1Enter the elelment to insert:10

Enter your choice:1Enter the elelment to insert:20

Enter the value after which the element to be inserted:10

Enter your choice:3

The content of the list are:10->20->NULL

Enter your choice:2

Enter the elelment to delete:20

The deleted element is 20Enter your choice:

Enter your choice:3

The content of the liet are:10->NULLEnter your choice:4

exit

Page 7: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 7/51

2.Doubly linked listAim:

To implement a program linked list using C

Algorithm:

struct Node{

int Element;struct Node *FLink;struct Node *Blink;

}

• Using switch case ,read a operation to perform on linked list .• If choice is 1, call insert function to perform insertion operation.

Routine to insert an element in the List

void insert (int X, List L,position P){

struct Node *Newnode;Newnode=malloc(sizeof(struct Node));

if(Newnode!=NULL){Newnode->Element=X;Newnode->Flink=P->Flink;P->Flink->Blink=Newnode;P->Flink=Newnode;

Newnode->Blink=P;}

}

• If choice is 2,call deletion function to perform deletion operation

Routine to delete an element from the listvoid Delete(int X,List L){

position P;P=Find(X,L);if(IsLast(P,L)){

Page 8: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 8/51

Temp=P;P->Blink->Flink=NULL;free(Temp);

}else

{ Temp=P;P->Blink->Flink=P->Flink;P->Flink->Blink=P->Blink;free(Temp);

}}

• If choice is 3,call a display function

Routine to display an element from the list

Display(List L){

while(p!=NULL){

print(p->element);p=p->next;}

}PROGRAM:

#include<stdio.h>#include<conio.h>struct node{int data;struct node *lptr,*rptr;}*head;struct node *ins_beg(int x,struct node *first);struct node *ins_end(int x,struct node *first);void display(struct node *first);struct node *dele(struct node *first,int del);void main(){int choice,x,del,l;head=NULL;clrscr();printf("\n 1.Insert_begin \n 2.Insert_end \n 3.Delete");printf("\n 4.Display \n 5.Exit");while(l)

Page 9: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 9/51

{printf("\nEnter your 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:display(head);break;case 5:exit(0);default:printf("\n Invalid entry, try again");getch();}}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;

Page 10: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 10/51

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;while(cur->rptr!=NULL){prev=cur;cur=cur->rptr;}cur->rptr=New;New->data=x;New->lptr=cur;New->rptr=NULL;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();}struct node *dele(struct node *first,int del){

Page 11: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 11/51

struct node*prev,*cur;cur=first;if(first==NULL){printf("\n No data present!!");

getch();}else if(first->data==del){printf("\n Data %d is deleted",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){prev->rptr=cur->rptr;(cur->rptr)->lptr=prev;printf("\n Data %d is deleted",cur->data);}else if(cur->rptr==NULL&&cur->data==del){prev->rptr=NULL;printf("\n Data %d is deleted",cur->data);}getch();return first;}

Page 12: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 12/51

OUTPUT: 1.Insert_begin2.Insert_end3.Delete4.Display5>Exit

Enter your choice:1

Enter the data to be inserted:10

Enter your choice:2

Enter the data to be inserted:20

Enter your choice:410 20Enter your choice:3

Enter the data to be deleted:20

Data 20 is deletedEnter your choice:410Enter your choice:5

Exit

Page 13: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 13/51

3. Polynomial Addition.Aim: To Represent a polynomial as a linked list and write functions for polynomialaddition

Algorithm:

• First read a two polynomial equation using create function.• Then call polyadd function to perform addition operation.

Struct poly{

int coeff;int power;struct poly *next;

}*list1,*list2,*list3;

Routine for Create:

poly create(poly *head,poly *newmodel){

poly *ptr;if(head==NULL)

{head=newnode;return(head);

}Else{

ptr=head;while(ptr->next!=NULL)ptr=ptr->next;ptr->next=newnode;

}return(head);

}

Page 14: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 14/51

Routine for Addtion:

void add(){poly *ptr1,*ptr2,*newnode;ptr1=list1;ptr2=list2;while(ptr1!=NULL && ptr2!=NULL){

newnode=malloc(sizeof(struct poly));if(ptr1->power==ptr2->power)

{newnode->coeff=ptr1->coeff+ptr2->coeff;

newnode->power=ptr1->power;newnode->next=NULL;list3=create(list3,newnode);ptr1=ptr1->next;ptr2=ptr2->next;

}else

{if(ptr1->power>ptr2->power){

newnode->coeff=ptr1->coeff;newnode->power=ptr1->power;

newnode->next=NULL;list3=create(list3,newnode);

ptr1=ptr1->next;}

else{

newnode->coeff=ptr2->coeff;newnode->power=ptr2->power;newnode->next=NULL;list3=create(list3.newnode);ptr2=ptr2->next;

}}

}

Page 15: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 15/51

PROGRAM:

#include<stdio.h>#include<malloc.h>#include<conio.h>struct link{int coeff;int pow;struct lint *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:\n");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 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){while(poly1->next&&poly2->next){

Page 16: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 16/51

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=poly2->coeff;poly2=poly2->next;}poly->next=(struct link *)malloc(sizeof(struct link));poly=poly->next;poly->next=NULL;}}void main(){poly1=(struct link *)malloc(sizeof(struct link));poly2=(struct link *)malloc(sizeof(struct link));

Page 17: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 17/51

poly=(struct link *)malloc(sizeof(struct link));clrscr();printf("\n Enter the first polynomial");create(poly1);printf("\n First polynomial:");

display(poly1);printf("\n Enter the second polynomial");create(poly2);printf("\n Second polynomial:");display(poly2);polyadd(poly1,poly2,poly);printf("\n Addition of two polynomial:");display(poly);getch();}

OUTPUT:

Enter the first polynomialEnter coeff:4

Enter pow:2

continue (y/n):Enter coeff:3

Enter power:1

continue (y/n):First polynomial:4x^2+3x^1Enter the second polynomialEnter coeff:5

Enter power:3

continue (y/n):Enter coeff:3

Enter power:2

continue (y/n):Enter coeff:1

Enter power:1

continue (y/n):Second polynomial:5x^3+3x^2+1x^1

Page 18: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 18/51

Addition of two polynomial:5x^3+7x^2+4x^1

4. Infix to Postfix ExpressionAim: To implement stack and use it to convert infix to postfix expression

Algorithm:

create an integer stack while (not end of input) {

read next character into symbolif (symbol is a digit)

push the integer value of symbol into stack else {

pop an integer out as operand2pop an integer out as operand1switch(symbol) {

case '+': calculate operand1 + operand2case '-': calculate operand1 - operand2case '*': calculate operand1 * operand2case '/': calculate operand1 / operand2

} push the calculated value into the stack

}} /* end while */ pop out the result

Program:#include<stdio.h>#include<ctype.h>#include<string.h>#define MAX 10typedef struct node{char data;struct node *next;}stack;int push(char);int pop(char*);void intoPost(char p[],char[]);int indexPriority(char p[][2],char data);stack *topstack=NULL;char iP[MAX][2]={

Page 19: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 19/51

{'(',MAX},{')',0},{'\0',0},{'+',1},{'-',1},{'*',2},{'/',2},{'%',2},{' '̂,3}};char sP[MAX][2]={{'(',0},{')',-1},{'\0',0},{'+',1},{'-',1},{'*',2},{'/',2},{'%',2},{'^',3}};void main(){

char inStr[20],postStr[20];clrscr();printf("ENTER THE INFIX EXPRESSION: \n");scanf("%s",inStr);intoPost(inStr,postStr);printf("THE POSTFIX EXPRESSION IS:\n%s",postStr);getch();}int push(char value){stack *newnode;

newnode=(stack *)malloc(sizeof(stack));if(newnode==NULL)return -1;newnode->data=value;newnode->next=topstack;topstack=newnode;return 0;}int pop(char *value){stack *temp;if(topstack==NULL)return -1;temp=topstack;topstack=topstack->next;*value=temp->data;free(temp);return 0;}void intoPost(char inStr[],char postStr[]){char ch,item;int i=0,st=0,spr,ipr;push('\0');while((ch=inStr[st++])!=NULL){if(tolower(ch)>='a'&&tolower(ch)<='z')postStr[i++]=ch;else if(ch=='c')push(ch);

Page 20: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 20/51

else if(ch==')'){pop(&item);while(item!='c'){

postStr[i++]=item;pop(&item);}}else{pop(&item);spr=indexPriority(sP,item);ipr=indexPriority(iP,ch);while(sP[spr][1]>=iP[ipr][1]){

postStr[i++]=item;pop(&item);spr=indexPriority(sP,item);}push(item);push(ch);}}while(!pop(&item))postStr[i++]=item;}int indexPriority(char p[][2],char data){int index;for(index=0;index<MAX;index++)if(p[index][0]==data)return index;}

OUTPUT:

ENTER THE INFIX EXPRESSION:

A+B*C/DTHE POSTFIX EXPRESSION IS:ABC*D/+

Page 21: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 21/51

6.Binary Tree TraversalsAim:

To implement an expression tree. Produce its pre-order, in-order, and post-ordertraversals .

Algorithm:

• Using insert function creates an expression tree.Insert Function:

Read a elementGet the position to insertIf user press l, then call insert(root->left)If user press r, then call insert(root->right)

• Routine for InorderVoid inorder(tree_pointer ptr){

If(ptr){inorder(ptr->left_child);printf(“%d”, ptr->data);

inorder(ptr->right_child);}

}• Routine for Preorder

Void preorder(tree_pointer ptr){If(ptr){

printf(“%d”, ptr->data);preorder(ptr->left_child);preorder(ptr->right_child);

}}

• Routine for postorderVoid inorder(tree_pointer ptr){

If(ptr){postorder(ptr->left_child);postorder(ptr->right_child);printf(“%d”, ptr->data);

Page 22: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 22/51

}}

Program:#include<stdio.h>#include<conio.h>typedef struct bin{char data;struct bin *left;struct bin *right;}node;

void insert(node *,node *);void inorder(node *);void preorder(node *);void postorder(node *);node *get_node();void main(){int choice;char ans;node *New,*root;root=NULL;

clrscr();do{printf("\n1.Create \n2.Inorder \n3.Preorder \n4.Postorder \n5.Exit");printf("\n Enter ur choice:");scanf("%d",&choice);switch(choice){case 1:root=NULL;do

{New=get_node();printf("\n Enter the element:");scanf("%s",&New->data);if(root==NULL)root=New;elseinsert(root,New);

Page 23: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 23/51

printf("\n More element(y/n):");ans=getch();}while(ans=='y'||ans=='Y');clrscr();break;

case 2:if(root==NULL)printf("\n Tree is not created");elseinorder(root);break;case 3:if(root==NULL)printf("\n Tree is not created");elsepreorder(root);

break;case 4:if(root==NULL)printf("Tree is not created");elsepostorder(root);break;}}while(choice!=5);}node *get_node(){node *temp;temp=(node *)malloc(sizeof(node));temp->left=NULL;temp->right=NULL;return temp;}void insert(node *root,node *New){char ch;printf("\n Left/Right of %c",root->data);ch=getch();if((ch=='r')||(ch=='R')){if(root->right==NULL){root->right=New;}else

Page 24: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 24/51

insert(root->right,New);}else{if(root->left==NULL)

{root->left=New;}else{insert(root->left,New);}}}void inorder(node *temp){

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

OUTPUT:

1.Create2.Inorder

Page 25: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 25/51

3.Preorder4.Postorder5.Exit

Enter ur choice:1

Enter the element:*More element(y/n):yEnter the element:+Left/Right of *:LMore element(y/n):yEnter the element:aLeft/Right of *:LLeft/Right of +:LMore element(y/n):yEnter the element:bLeft/Right of *:L

Left/Right of +:RMore element(y/n):yEnter the element:-Left/Right of *:RMore elemen(y/n):yEnter the elemen:cLeft/Right of *:RLeft/Right of -:LMore element(y/n):yEnter the element:/Left/Right of *:RLeft/Right of -:RMore element(y/n):yEnter the element:dLeft/Right of *:RLeft/Right of-:RLeft/Right of /:LMore element(y/n):yEnter the elemen:eLeft/Right of *:RLeft/Right of -:RLeft/Right of /:RMore element(y/n):nEnter ur choice:2A+b*c-d/eEnter ur choice:3*+ab-c/deEnter ur choice:4Ab+cde/-*Enter ur choice:5

Page 26: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 26/51

Exit

7. Binary Search TreeAim:

To implement binary search tree.

Algorithm :

Routine for InsertionSearchTree Insert(int X,searchTree T){

if( T == NULL )T = malloc ( sizeof (struct tree_node) );if( T == NULL )fatal_error("Out of space!!!");

else{

T->element = x;T->left = T->right = NULL;

}

elseif( x < T->element )

T->left = insert( x, T->left );else

if( x > T->element )T->right = insert( x, T->right );

return T;}

Routine for Deletion

delete( element_type x, SEARCH_TREE T ){

tree_ptr tmp_cell,if( T == NULL )error("Element not found");elseif( x < T->element ) /* Go left */

T->left = delete( x, T->left );else

Page 27: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 27/51

if( x > T->element ) /* Go right */T->right = delete( x, T->right );

else /* Found element to be deleted */if( T->left && T->right ) /* Two children */

{

/* Replace with smallest in right subtree */tmp_cell = find_min( T->right );T->element = tmp_cell->element;T->right = delete( T->element, T->right );}

else /* One child */}else /* One child */{

tmp_cell = T;

if( T->left == NULL ) /* Only a right child */T = T->right;if( T->right == NULL ) /* Only a left child */

T = T->left;free( tmp_cell );}

return T;}

Program:

#include<stdio.h>#include<conio.h>#include<stdlib.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;

Page 28: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 28/51

clrscr();printf("\nEnter total no of elements in the Tree:");scanf("%d",&n);printf("\nEnter the elements:\n");while(i<n)

{scanf("%d",&data);T=Insert(data,T);i++;}printf("\nElements displayed in Inorder format:\n");display(T);printf("\nEnter the elements to delete:");scanf("%d",&data);T=del(data,T);printf("\nContents of the tree after deletion:\n");

display(T);getch();}node Insert(int x,node T){struct tree *newnode;newnode=malloc(sizeof(struct tree));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){

Page 29: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 29/51

node Tempcell;if(T==NULL){printf("\nElement not found");exit(0);

}elseif(x<T->data)T->left=del(x,T->left);elseif(x>T->data)T->right=del(x,T->right);elseif(T->left&&T->right){Tempcell=FindMin(T->right);

T->data=Tempcell->data;T->right=del(T->data,T->right);}else{Tempcell=T;if(T->left==NULL)T=T->right;else if(T->right==NULL)T=T->left;free(Tempcell);}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);

Page 30: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 30/51

printf("%d\t",T->data);display(T->right);}}

OUTPUT:

Enter total no of elements in the Tree:5

Enter the elements:4 2 6 8 9

Elements displayed in Inorder format:2 4 6 8 9Enter the elements to delete:6

Contents of the tree after deletion:2 4 8 9

Page 31: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 31/51

8. Hashing-Liner ProbingAim:

To implement hashing techniques using array .

Algorithm:

• Read the hash table size• Read the value to be inserted into hash table.• Using modular hash function (value%n) to find the position to be

inserted.• If the position is free, then insert into position• If it is not free, then placed in next position• The above steps are repeated until the

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 value of n(table size):");scanf("%d",&n);do{printf("\n Enter the hash value:");scanf("%d",&value);

hashvalue=value%n;if(a[hashvalue]==0){a[hashvalue]=value;printf("\n a[%d] the value %d is stored",hashvalue,value);}else{

Page 32: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 32/51

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 ll;}}hashvalue=0;for(hashvalue++;hashvalue<n;hashvalue++){if(a[hashvalue]==0){printf("\n Space is allocated give other value");

a[hashvalue]=value;printf("\n a[%d] the value %d is stored",hashvalue,value);goto ll;}}}ll:printf("\n\n Do u want to enter more");scanf("%d",&temp);}while(temp==1);getch();}

Page 33: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 33/51

OUTPUT: Enter the value of n(table size): 10

Enter the hash value:11

a[1] the value 11 is stored

Do u want to enter more1

Enter the hash value:59

a[9] the value 59 is stored

Do u want to enter more1

Enter the hash value:38

a[8] the value 38 is stored

Do u want to enter more1

Enter the hash value:68

Space is allocated give other valuea[0] the value 68 is stored

Do u want to enter more6

Page 34: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 34/51

5. producer-consumer problem.Aim:

To implement array-based queue and use it to simulate a producer-consumer problem.

Algorithm:

Producer: can produce an itemIf the buffer is full then produce can’t produce an item

Else the producer produce an itemConsumer: can consume an itemIf the buffer is empty then consumer can’t consume an itemElse the consumer consume an item

Program:

#include<stdio.h>#include<conio.h>#define MAX 5void producer();

void consumer();void display();int q[MAX],rear=1,front=1;void main(){int choice;clrscr();do{printf("\n1.Producer \n2.Consumer \n3.Display \n4.Exit");printf("\n Enter your choice:");

scanf("%d",&choice);switch(choice){case 1:producer();break;case 2:consumer();

Page 35: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 35/51

break;case 3:display();break;case 4:

getch();default:printf("\n Enter valid choice:");}}while(choice!=4);}void producer(){int value;if(rear>MAX){

printf("\n Buffer is full");}else{printf("\n Enter the piece of data :");scanf("%d",&value);q[rear]=value;rear++;}}void consumer(){int x,i;if(rear==1){printf("\n Buffer is empty");getch();}else{x=q[front];for(i=front;i<rear;i++)q[i]=q[i+1];printf("\n The consumed data is=%d",x);rear--;getch();}}void display(){

Page 36: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 36/51

int count;if(rear==1){printf("\nThere are no data");}

else{printf("\n The datas are");for(count=front=front;count<rear;count++){printf("\n %d",q[count]);}}getch();}

OUTPUT:1.Producer2.Consumer3.Display4.Exit

Enter your choice:1Enter the piece of data:3

Enter your choice:1

Enter the piece of data:5

Enter your choice:3

The data are35Enter your choice:2

The consumed data is=3

Enter your choice:3

The data are5Enter your choice:4Exit

Page 37: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 37/51

9. Dijkstra's algorithm

Aim :

To find the shortest path using Dijkstra's algorithm

Algorithm:

Dijkstra(w,s,t,pd,precede)

This algorithm in addition to calculating distances, if finds the shortest path itself by

maintaining an array precede such that preced[i] is the node that precedes node i on the

shortest path found thus far. An array perm is used to keep track of the corresponding

set. Perm[i] is 1 if i is a member of the set and 0 if not. The routine accepts a weight

matrix and two nodes, s and t, and calculates the minimum distance pd from s to t as well

as the array precede to define the path.

1. For i = 0 to Maxnodes – 1 do :a. perm[i] := 0.

b. distance[i] := INFINITY.

[End of for loop.]

2. Set perm[s] := 1

3. Set distance[s] := 0

4. Set current := s

5. Repeat Steps 6 to 16 while current ≠ t :

6. Set smalldist := INFINITY.

7. Set dc := distance[current]’

8. For i = 0 to Maxnodes – 1 do :

9. If perm[i] = 0 then :

10. Set newdist := dc + weight[current][i]

Page 38: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 38/51

11. If newdist < distance[i] then :

[distance from s to i through current is smaller than distance[i]

]

12. Set distance[i] := newdist and precede[i] := current.

[End of Step 11 If structure]

13. If distance[i] < smalldist then :

[determine the smallest distance]

14. Set smalldist := distance[i] and k := i.

[End of Step 13 if structure.]

[End of Step 9 If structure.]

[End of Step 8 for loop.]

15. Set current := k.16. Set perm[current] := 1

[End of Step 5 while loop.]

17. Set *pd := distance[t];

18. Return.

Program:

#include<stdio.h>#include<conio.h>#define infinity 999int v,cost[10][10],n,distance[10],status[10];void getmatrix(){int i,j;printf("\nEnter the no of nodes:");scanf("%d",&n);printf("\nEnter the cost matrix\n");

for(i=0;i<n;i++){printf("\nEnter the cost of row %d:\n",i+1);for(j=0;j<n;j++){scanf("%d",&cost[i][j]);}}

Page 39: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 39/51

printf("\nEnter the source vertex:");scanf("%d",&v);}void dispmatrix(){

int i,j;printf("\nThe given cost matrix:\n");for(i=0;i<n;i++){for(j=0;j<n;j++){//if(cost[i][j]==infinity)//printf("--\t");//elseprintf("\t%d",cost[i][j]);}

printf("\n");}}void dijkstra(){int selected[10];int i,j,u,w,newcost;for(i=0;i<n;i++){status[i]=0;distance[i]=cost[v][i];}status[v]=1;distance[v]=0;selected[0]=v;for(i=1;i<n;i++){u=minimum();status[u]=1;selected[i]=u;for(w=0;w<n;w++){if(cost[u][w]!=infinity&&status[w]==0){newcost=distance[u]+cost[u][w];if(distance[w]>newcost)distance[w]=newcost;}}}

Page 40: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 40/51

printf("\nThe shortest path for the given graph from the source vertex v",v);for(i=0;i<n;i++){printf("%d",selected[i]);printf("\nThe final cost is \n");

for(i=0;i<n;i++){printf("\nFrom the node v");printf("%d",v);printf("->V");printf("%d",i);printf("\t%d",distance[i]);}}}int minimum()

{int i,mincost,u=0;mincost=distance[0];for(i=1;i<n;i++){if(distance[i]!=0&&distance[i]!=10000&&status[i]!=1){if(mincost>distance[i]){mincost=distance[i];u=i;}}}return u;}void main(){clrscr();getmatrix();dispmatrix();dijkstra();getch();}

Page 41: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 41/51

Output:

Enter the total no. of nodes : 5

Enter the Cost Matrix :

Enter the cost of Row :110002571000

Enter the cost of Row :2210001000

10004Enter the cost of Row :351000100010006

Enter the cost of Row :47

1000100010003

Enter the cost of Row :510004

Page 42: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 42/51

631000

Enter the Source vertex : 1

The given Cost Matrix is :1000 2 5 7 1000

2 1000 1000 1000 4

5 1000 1000 1000 67 1000 1000 1000 3

1000 4 6 3 1000

The Shortest path for the given graph from the source vertex V1 is :1 0 0 0 0

The final cost is :

From the node V1 -> V0 is : 2From the node V1 -> V1 is : 0From the node V1 -> V2 is : 7From the node V1 -> V3 is : 9From the node V1 -> V4 is : 4

Page 43: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 43/51

10. Priority queue

Aim:

To implement priority queue

Algorithm:

Deletion:

This algorithm deletes and processes the first element in a priority queue which appears

in memory as a one-way list.

1. Set ITEM:= START->INFO (This saves the data in the first node).

2. Delete first node from the list.

3. Process ITEM.

4. Exit

Insertion:

This algorithm adds an ITEM with priority number N to a priority queue which is

maintained in memory as a one-way list.

1. Traverse the one-way list until finding a node X whose priority number exceeds

N. Insert ITEM in front of node X.

2. If no such node is found, insert ITEM as the last element of the list.

Program:

Page 44: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 44/51

#include <stdio.h>#include<conio.h>#include<math.h>#include<malloc.h>#include<process.h>

struct heap{int c;int s;int *element;

};

typedef struct heap *pqueue;pqueue initialize(int max)

{pqueue heap1;if(max<=3){printf("\n priority queue is too small\n");exit(0);}heap1=malloc(sizeof(struct heap));if(heap1==NULL){printf("\n out of space");exit(0);}heap1->element=malloc((max+1)*sizeof(int));if(heap1->element==NULL){printf("\n out of space");exit(0);}

heap1->c=max;heap1->s=0;heap1->element[0]=0;return heap1;}

int isempty(pqueue heap1){return(heap1->s==0);}

Page 45: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 45/51

int isfull(pqueue heap1){return(heap1->s==heap1->c);}void insert(int x,pqueue heap1)

{int i;if(isfull(heap1)){printf("\n heap is full" );return;}for(i=++heap1->s;heap1->element[i/2]>x;i=i/2)heap1->element[i]=heap1->element[i/2];heap1->element[i]=x;

}int deletemin(pqueue heap1){int i,child,min,last;if(isempty(heap1)){printf("\n heap is empty");return heap1->element[0];}min=heap1->element[1];last=heap1->element[heap1->s--];//heap1->s--;for(i=1;i*2<=heap1->s;i=child){

child=i*2;if(child!=heap1->s && heap1->element[child+1]<heap1->element[child])child++;

if(last>heap1->element[child])heap1->element[i]=heap1->element[child];elsebreak;

}heap1->element[i]=last;return min;}

Page 46: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 46/51

void main(){int i,j,s,ch,ele,child,space,level,c;pqueue heap1;clrscr();

printf("\n enter the size of queue");scanf("%d",&s);heap1=initialize(s);do{printf("\n Menu \n 1.insert \n2.delete\n3.display\n4.exit");printf("\n enter ur choice");scanf("%d",&ch);switch(ch){case 1:

printf("\n enter the elemet");scanf("%d",&ele);insert(ele,heap1);break;

case 2:ele=deletemin(heap1);printf("\n the deleted element is %d",ele);break;case 3:c=1;level=1;for(i=1;i<heap1->s;i++){for(j=1;j<=level;j++){if(c<=heap1->s){space=(int)(heap1->s*2)/c;printf("\t %d",heap1->element[c++]);}elsebreak;}if(level==1)level=2;else if(level>=2)level=level*2;//printf("\n");}

Page 47: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 47/51

break;default: printf("\n exit");}}while(ch<4);

}

Output:

No. of values in Unsorted List:6INPUT PARTThe value 1:34The value 2:56The value 3:14The value 4:26

The value 5:38The value 6:45

14 26 34 38 45 56

Page 48: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 48/51

11. Knapsack problem

Aim:To implement the knapsack problem using dynamic programming method.

Algorithm:// Implements the memory function method for the knapsack problem

// Input : A nonnegative integer i indicating the number of the firstitems being considered and a non negative integer jindicating the knapsack’s capacity.

// Output : The value of an optimal feasible subset of the first i items.// Note : Uses as global variables input arrays Weights[1. . .n],

Values[1 . . . n] and table V[0. . .n, 0. . . W] whose entriesare initialized with –1’s except for row 0 and column 0initialized with 0’s.

1. if V[i, j] < 02. if j < Weights[i]3. value = MFKnapsack(i – 1, j)4. else5. value = Max(MFKnapsack(i – 1, j),

Values[i] + MFKnapsack(I – 1, j – Weights[i])

6. V[i,j] = value7. return V[i, j]

PROGRAM:

#include<stdio.h>#include<conio.h>int c,c1,n,i,j,k;int q[10],x[10][10],w[10],p[10],max;void get();

Page 49: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 49/51

void knapsack();void display();void get(){printf("\nEnter the number of objects:");

scanf("%d",&n);printf("\nEnter the size of the knapsack:");scanf("%d",&c);printf("\nEnter the weight and profit of the objects");for(i=1;i<=n;i++){printf("\nEnter the weight %d:",i);scanf("%d",&w[i]);printf("\nEnter the profit of weight %d:",i);scanf("%d",&p[i]);}

}void knapsack(){for(j=1;j<=n;j++){for(i=1;i<=n;i++){x[j][i]=0;q[j]=0;}c1=c;for(i=j;i<=n&&w[i]<=c1;i++){x[j][i]=1;c1=c1-w[i];q[j]=q[j]+x[j][i]*p[i];}}max=q[i];for(i=1;i<=n;i++){if(q[i]>max){max=q[i];k=1;}}}void display(){

Page 50: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 50/51

printf("the optimal solution \t profit\n");for(i=1;i<=n;i++){printf("\n");for(j=1;j<=n;j++)

{printf("%d\t",x[i][j]);}printf("%d\t",q[i]);}printf("\nThe max profit is %d",max);}void main(){clrscr();get();

knapsack();display();getch();}

OUTPUT :Enter the number of objects : 3

Enter the maximum capacity of the Knapsack : 50

Enter the Weights and Values of 3 objects

Weight[1] = 10Value[1] = 60

Weight[2] = 20Value[2] = 100

Weight[3] = 30Value[3] = 120

The objects included in the optimal subset are : 3

The maximum Profit obtained is : 120

Page 51: List of Experiments of oops

7/29/2019 List of Experiments of oops

http://slidepdf.com/reader/full/list-of-experiments-of-oops 51/51