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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
}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){
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");}}
{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;
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;}
#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){
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));
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
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);
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
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
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){
• 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{
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();}
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:");
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(){
[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]);}}
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("\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()
{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);}
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");}
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),
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]);}