dhamu - all progromes batch-1
TRANSCRIPT
-
8/4/2019 Dhamu - All Progromes Batch-1
1/60
Ex.No: 1a
DATE: 15.07.11
SINGLY LINKED LIST
PROGRAM:
#include#include
#include
#define TRUE 1#define FALSE 0
typedef struct SLL
{int data;
struct SLL*next;
}
node;
node *create();void main()
{int choice, val;
char ans;
node *head;void display(node *);
node *search(node*,int);
void insert(node*);
void delete(node**);head=NULL;
do
{
clrscr();printf("\n Program to perform various operationS on linked list");
printf("\n 1.Create");
printf("\n 2.Display");printf("\n 3.Search for an item:");
printf("\n 4. Insert an element in a list");
printf("\n 5. Delete an element from list");printf("\n 6. Quit");
printf("\n Enter the choice (1-6):\n");
scanf("%d",&choice);
switch(choice)
{
case 1:head=create();
break;
case 2:
-
8/4/2019 Dhamu - All Progromes Batch-1
2/60
display(head);
break;case 3:
printf("\n Enter the element you want to search ");
scanf("%d",&val);
search(head,val);break;
case 4:
insert(head);break;
case 5:
delete(&head);break;
case 6:
exit(0);
default:
clrscr();printf("\n Invalid choice, try again");
getch();}
}
while(choice!=6);}
node *create()
{
node *temp,*New,*head;int val,flag;
char ans='y';
node*get_node();
temp=NULL;flag=TRUE;
do
{printf("\n Enter the element:");
scanf("%d",&val);
New=get_node();if(New==NULL)
printf("\n Memory is not allocated");
New->data=val;
if(flag){
head=New;
temp=head;flag=FALSE;
}
else
-
8/4/2019 Dhamu - All Progromes Batch-1
3/60
{
temp->next=New;temp=New;
}
printf("\n Do you want to enter more elements? (y/n)");
ans=getch();}
while(ans=='y');
printf("\n The singly linked list is expected \n");getch();
clrscr();
return head;}
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));temp->next=NULL;
return temp;}
void display(node *head)
{node *temp;
temp=head;
if(temp==NULL)
{printf("\n The list is empty\n");
getch();
clrscr();
return;}
while(temp!=NULL)
{printf("%d->",temp->data);
temp=temp->next;
}printf("NULL");
getch();
clrscr();
}node *search(node*head,int key)
{
node*temp;int found;
temp=head;
if(temp==NULL)
-
8/4/2019 Dhamu - All Progromes Batch-1
4/60
{
printf("\n The linkage list is empty\n");getch();
clrscr();
return NULL;
}else
found=FALSE;
while(temp!=NULL&&!found){
if(temp->data!=key)
temp=temp->next;else
found=TRUE;
}
if(found)
{printf("\n The element is present in the list\n");
getch();return temp;
}
else{
printf("The element is not present in the list\n");
getch();
return NULL;}
}
void insert(node *head)
{node *temp,*New;
int val;
temp=head;if(temp==NULL)
{
printf("\n Insertion is not possible");getch();
return;
}
clrscr();printf("\n Enter the element after which you want to insert");
scanf("%d",&val);
temp=search(head,val);if(temp!=NULL)
{
printf("\n Enter the element which you want to insert:");
-
8/4/2019 Dhamu - All Progromes Batch-1
5/60
scanf("%d",&val);
New=(node*)malloc(sizeof(node));if(New==NULL)
printf("\n Memory is not allocated \n");
New->data=val;
New->next=NULL;New->next=temp->next;
temp->next=New;
printf("\n The element is inserted\n");getch();
clrscr();
}}node *get_prev(node *head,int val)
{
node *temp,*prev;
int flag;
temp=head;if(temp==NULL)
return 0;flag=FALSE;
prev=NULL;
while(temp!=NULL&&!flag){
if(temp->data!=val)
{
prev=temp;temp=temp->next;
}
else
flag=TRUE;}
if(flag)
return prev;else
return NULL;
}void delete(node**head)
{
node*temp,*prev;
int key;temp=*head;
if(temp==NULL)
{printf("\n The list is empty");
getch();
clrscr();
-
8/4/2019 Dhamu - All Progromes Batch-1
6/60
return;
}clrscr();
printf("\n Enter the element you wnat to delete");
scanf("%d",&key);
temp=search(*head,key);if(temp!=NULL)
{
prev=get_prev(*head,key);if(prev!=NULL)
{
prev->next=temp->next;free(temp);
}
else
{
*head=temp->next;free(temp);
}printf("\n The element is deleted\n");
getch();
clrscr();}
}
OUTPUT:-
Program to perform various operations on linked list
1. Create2. Display
3. Search for an item:
4. Insert an element in a list5. Delete an element from list
6. Quit
Enter the choice (1-6):
1
Enter the element:12
Do you want to enter more elements?(y/n) y
Enter the element:
15
Do you want to enter more elements? (y/n) y
-
8/4/2019 Dhamu - All Progromes Batch-1
7/60
Enter the element:
32Do you want to enter more elements? (y/n) n
The singly linked list is expected
Enter the choice (1-6):2
12->15->32->NULL
Enter the choice (1-6):
3
Enter the element you want to search
12
The element is present in the list
Enter the choice (1-6):4
Enter the element after which you want to insert32
The element is present in the list
Enter the element which you want to insert:44
The element is inserted
Enter the choice (1-6):2
12->15->32->44->NULL
Enter the choice (1-6):5
Enter the element you want to delete:
32The element is present in the list
The element is deleted
Enter the choice (1-6):
2
12->15->44->NULL
-
8/4/2019 Dhamu - All Progromes Batch-1
8/60
EX.NO:1b
DATE: 15.7.11
DOUBLY LINKED LIST
PROGRAM:
#include
#include
#includestruct node
{
int data;struct node *next,*prev;
}*New,*New1,*temp,*start,*dummy;
void add(void);
struct node *get_node();
void display(void);void delet(void);
int find(int);int first=1;
void main()
{char ans;
int choice,num,found=0;
start=NULL;
do{clrscr();
printf("\n\t Program for doubly linked list\n");
printf("\n\n 1.Insertion of element \n\n");
printf("\n\n 2. Deletion of element from the list\n\n");printf("\n\n3. Display of doubly linked list\n\n");
printf("\n\n 4. Searching of a particular node in the list\n\n");
printf("\n\n5. Exit\n");printf("\n Enter the choice");
scanf("%d",&choice);
switch(choice){
case 1:
add();
break;case 2:
delet();
break;case 3:
display();
break;
-
8/4/2019 Dhamu - All Progromes Batch-1
9/60
case 4:
printf("\n Enter the number which is to be searched");scanf("%d",&num);
temp=start;
while((temp!=NULL)&&(found==0))
found=find(num);if(found)
printf("\n The number is present in the list");
elseprintf("\n The number is NOT present in the list");
break;
case 5:exit(0);
}
printf("\n Do you want to continue? \n");
ans=getch();
}while(ans=='y'||ans=='Y');
getch();}
void add(void)
{clrscr();
New=get_node();
printf("\n\n Enter the element \n");
scanf("%d",&New->data);if(first==1)
{
start=New;
first=0;}
else
{dummy=start;
while(dummy->next!=NULL)
dummy=dummy->next;dummy->next=New;
New->prev=dummy;
}}
struct node*get_node(){
New1=malloc(sizeof(struct node));
New1->next=NULL;New1->prev=NULL;
return(New1);
}
-
8/4/2019 Dhamu - All Progromes Batch-1
10/60
void display(void)
{clrscr();
temp=start;
if(temp==NULL)
printf("\n The doubly linked list is empty");else
{
while(temp!=NULL){
printf("\n\n%d->",temp->data);
temp=temp->next;}
printf("NULL");
}
getch();
}int find(int num)
{if(temp->data==num)
return(1);
elsetemp=temp->next;
return 0;
}
void delet(void){
int num,flag=0;
int found;
int last=0;clrscr();
temp=start;
if(temp==NULL)printf("\n\nSorry:DLL is not created\n");
else{
printf("Enter the number to be deleted");scanf("%d",&num);
while((flag==0)&&(temp!=NULL))
{
found=find(num);flag=found;
}
if(found==0)printf("\n Number not found");
else
{
-
8/4/2019 Dhamu - All Progromes Batch-1
11/60
if(temp==start)
{start=start->next;
temp->next=NULL;
start->prev=NULL;
free(temp);getch();
printf("\n The starting node is deleted");
}else
{
if(temp->next==NULL)last=1;
else
last=0;
(temp->next)->prev=temp->prev;
(temp->prev)->next=temp->next;temp->prev=NULL;
temp->next=NULL;free(temp);
if(last)
printf("\n The last nod is deleted");else
printf("\n The intermediate nod is deleted");
}
}}
}
-
8/4/2019 Dhamu - All Progromes Batch-1
12/60
OUTPUT:-
Program for doubly linked list:-
1. Insertion of element
2. Deletion of element from the list
3. Display of doubly linked list4. Searching of a particular node in the list
5. Exist
Enter the choice: 1Enter the element 2
Do you want to continue?
YEnter the choice 1
Enter the element 5
Do you want to continue?
Y
Enter the choice 35->NULL
2->NULLDo you want to continue?
Y
Enter the choice 4Enter the number which is to be searched 2
The number is present in the list
Do you want to continue?
YEnter the choice 2
Enter the number to be deleted 2
The starting node is deleted
Do you want to continue?Y
Enter the choice 5
Quit
-
8/4/2019 Dhamu - All Progromes Batch-1
13/60
EX.NO:2
DATE: 22.7.11
ADDITION OF POLYNOMIAL
PROGRAM:
#include#include
#include
#include#include
#define TRUE 1
#define FLASE 0typedef struct pnode
{
float coef;
int exp;
struct pnode *next;}p;
void main(){
p*p1,*p2,*p3;
p*get_poly(),*add(p*,p*);void display(p*);
clrscr();
printf("\n enter the first polynomial\n\n");
p1=get_poly();clrscr();
printf("\n enter the second polynomial\n\n");
p2=get_poly();
clrscr();printf("\n the first polynomial is:\n");
display(p1);
printf("\n the second polynomial is :\n");display(p2);
p3=add(p1,p2);
printf("\n the addition of the polynomial is :\n");display(p3);
exit(0);
}
p*get_poly(){
p*temp,*New,*last;
int exp,flag;float coef;
p*get_node();
char ans='y';
-
8/4/2019 Dhamu - All Progromes Batch-1
14/60
temp=NULL;
flag=TRUE;printf("\n enter the polynomial is desending order of exponent \n");
do
{
printf("\n enter the co-efficient and exponent of atom:");scanf("%f%d",&coef,&exp);
New=get_node();
if(New==NULL)printf("\n memory cannot be allocated");
New->coef=coef;
New->exp=exp;if(flag==1)
{
temp=New;
last=temp;
flag=FLASE;}
else{
last->next=New;
last=New;}
printf("\n do you want to add more terms?(y/n)");
ans=getch();
}while(ans=='y');
return(temp);
}
p*get_node(){
p*temp;
temp=(p*)malloc(sizeof(p));temp->next=NULL;
return(temp);
}void display(p*head)
{
p*temp;
temp=head;if(temp==NULL)
{
printf("\n the polynomial is empty \n");getch();
return;
}
-
8/4/2019 Dhamu - All Progromes Batch-1
15/60
printf("\n");
while(temp->next!=NULL){
printf("%0.1fx^%d+",temp->coef,temp->exp);
temp=temp->next;
}printf("%0.1fx^%d",temp->coef,temp->exp);
getch();
}p*add(p*first,p*second)
{
p*p1,*p2,*temp,*dummy;char ch;
float coef;
p*append(int,float,p*);
p1=first;
p2=second;temp=(p*)malloc(sizeof(p));
if(temp==NULL)printf("\n memory cannot be allocated");
dummy=temp;
while(p1!=NULL&&p2!=NULL){
if(p1->exp==p2->exp)
{
coef=p1->coef+p2->coef;temp=append(p1->exp,coef,temp);
p1=p1->next;
p2=p2->next;
}else if (p1->expexp)
{
coef=p2->coef;temp=append(p2->exp,coef,temp);
p2=p2->next;
}else if(p1->exp>p2->exp)
{
coef=p1->coef;
temp=append(p1->exp,coef,temp);p1=p1->next;
}
}while(p1!=NULL)
{
temp=append(p1->exp,p2->coef,temp);
-
8/4/2019 Dhamu - All Progromes Batch-1
16/60
p1=p1->next;
}while(p2!=NULL)
{
temp=append(p2->exp,p2->coef,temp);
p2=p2->next;}
temp->next=NULL;
temp=dummy->next;free(dummy);
return(temp);
}p * append(int exp,float coef,p*temp)
{
p*New,*dummy;
New=(p*)malloc(sizeof(p));if(New==NULL)
printf("\n memory cannot be allocated\n");New->exp=exp;
New->coef=coef;
New->next=NULL;dummy=temp;
dummy->next=New;
dummy=New;
return(dummy);}
-
8/4/2019 Dhamu - All Progromes Batch-1
17/60
OUTPUT:-
Enter the first polynomial
Enter the polynomial in descending order of exponent
Enter the coefficient and exponent of a term :5 2
Do you want to add more terms?????(y/n)yEnter the coefficient and exponent of a term :2 1
Do you want to add more terms?????(y/n)yEnter the coefficient and exponent of a term :7 0
Do you want to add more terms?????(y/n)n
Enter the Second polynomial
Enter the polynomial in descending order of exponent
Enter the coefficient and exponent of a term :11 2
Do you want to add more terms?????(y/n)y
Enter the coefficient and exponent of a term :6 1
Do you want to add more terms?????(y/n)y
Enter the coefficient and exponent of a term :3 0
Do you want to add more terms?????(y/n)n
The first polynomial is:
5.0x^2+2.0x^1+7.0x^0
The second polynomial is:
11.0x^2+6.0x^1+3.0x^0
The addition of the polynomial is:
16.0x^2+8.0x^1+10.0x^0
-
8/4/2019 Dhamu - All Progromes Batch-1
18/60
EX NO:3 IMPLEMENTATION OF STACK AND USE IT TO CONVERT
DATE:29.7.2011 INFIX TO POSTFIX EXPRESSION
PROGRAM:
#include
#include
#includechar inf[40],post[40];
int top=0,st[20];
void postfix();void push (int);
char pop();
void main (void)
{
clrscr();printf("\n\t Enter the infix expression :\n\n\t\t");
scanf("%s",inf);postfix();
getch();
}void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++){
switch (inf[i])
{
case '+':while (st[top]>=1)
post[j++]=pop();
push(1);break;
case '-':
while(st[top]>=1)post[j++]=pop();
push(2);
break;
case'*':while(st[top]>=3)
post[j++]=pop();
push(3);break;
case'/':
while(st[top]>=3)
-
8/4/2019 Dhamu - All Progromes Batch-1
19/60
post[j++]=pop();
push(4);break;
case'^':
while(st[top]>=4)
post[j++]=pop();push(5);
break;
case '(':push(0);
break;
case ')':while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:post[j++]=inf[i];
}}
while(top>0)
post[j++]=pop();printf("\n\tpostfix expression is :\n\n\t\t%s",post);
}
void push(int ele)
{top++;
st[top]=ele;
}
char pop(){
int el;
char e;el=st[top];
top--;
switch(el){
case 1:
e='+';
break;
case 2:
e='-';break;
case 3:
-
8/4/2019 Dhamu - All Progromes Batch-1
20/60
-
8/4/2019 Dhamu - All Progromes Batch-1
21/60
EX.NO.: 4
DATE: 29.07.11
CIRCUL QUEUE IMPLEMENTATION
PROGRAM:
#include#define qsize 50
int qu[qsize];
int front,rear;void insert()
{
int item;if(rear==qsize)
rear=1;
else
rear++;
if(front!=rear){
printf("Enter items to be inserted\n");scanf("%d",&item);
qu[rear]=item;
if(front==0)front++;
}
else
printf("ovear flow\n");return;
}
int delete()
{int item;
if(front!=0)
{item=qu[front];
if(front==rear)
{front=rear=0;
return(item);
}
if(front==qsize)front=1;
else
front++;return(item);
}
printf("Under Flow\n");
-
8/4/2019 Dhamu - All Progromes Batch-1
22/60
return(0);
}void display()
{
int i;
for(i=front;i
-
8/4/2019 Dhamu - All Progromes Batch-1
23/60
OUTPUT:
1.Insert an Element into shop by Producer
2.get an element from the shop by consumer
3.quit
1Enter items to be inserted
25
cqueue after insertion is:25
1Enter items to be inserted
50
cqueue after insertion is:
2550
2
item deleted id :25cqueue after deletion is:50
3
-
8/4/2019 Dhamu - All Progromes Batch-1
24/60
EX.NO.: 5
DATE: 05.08.11.
EXPRESSION TREE
PROGRAM:
#include#include
#include
typedef struct bin{
int 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='n';
node*New,*root;
root=NULL;
clrscr();do
{
printf("\n Program for implementing simple binary tree");
printf("\n 1.Create");printf("\n 2.Inorder");
printf("\n 3.Preorder");
printf("\n 4.Postorder");printf("\n 5. exit");
printf("\n\t Enter the choice:");
scanf("%d",&choice);switch(choice)
{
case 1:root=NULL;
do{
New=get_node();
printf("\n Enter the element:");scanf("%d", &New->data);
if(root==NULL)
root=New;
-
8/4/2019 Dhamu - All Progromes Batch-1
25/60
else
insert(root,New);printf("\n Do you want to enter more element: (y/n)");
ans=getch();}
while(ans=='y'||ans=='Y');
clrscr();break;
case 2: if(root==NULL)
printf("tree is not created");else
inorder(root);
break;case 3: if(root==NULL)
printf("Tree is not created");
else
preorder(root);
break;case 4:if(root==NULL)
printf("\n Tree is not created!");else
postorder(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 Where to insert left or right of %d",root->data);
ch=getch();
if((ch=='r')||(ch=='R')){
if(root->right==NULL)
{root->right=New;}
else
{
-
8/4/2019 Dhamu - All Progromes Batch-1
26/60
if(root->left==NULL)
{root->left=New;}
else
insert(root->left,New);
}}
void inorder(node*temp)
{if(temp!=NULL)
{
inorder(temp->left);printf("\n \t %d",temp->data);
inorder(temp->right);}}
void preorder(node*temp)
{
if(temp!=NULL){
printf("\t %d",temp->data);preorder(temp->left);
preorder(temp->right);}}
void postorder(node*temp){
if(temp!=NULL)
{
postorder(temp->left);postorder(temp->right);
printf("\n\t%d",temp->data);
}
getch();}
-
8/4/2019 Dhamu - All Progromes Batch-1
27/60
OUTPUT:
Program for Implementing simple Binary Tree1.Create
2.Inorder
3.Preorder4.postorder
5.exit
Enter the choice1
Enter the element 50
Do you want to Enter more element?(y/n) y
Enter the element 20
Where to insert left/right of 50 LDo you want to Enter more element?(y/n) y
Enter the element 70
Where to insert left/right of 50 R
Do you want to Enter more element?(y/n)n
Enter the choice 2
20 50 70
Enter the choice 3
50 20 70
Enter the choice 4
20 70 50
Enter the choice 5
-
8/4/2019 Dhamu - All Progromes Batch-1
28/60
EX.NO:6
DATE: 12.8.11
BINARY SEARCH TREE
PROGRAM:
#include
#include
#include#define null 0
typedef struct bst
{int data;
struct bst*left,*right;
}
node;
void insert(node*,node*);void inorder(node*);
node*search(node*,int,node**);void del(node*, int);
void main()
{int choice;
char ans='n';
int key;
node*New,*root,*tmp,*parent;node*get_node();
root=NULL;
clrscr();
printf("\n Program for binary search tree");do
{
printf("\n 1.Create \n2.Search\n3.Delete\n4.Display\n5.Exit");printf("\nEnter the choice");
scanf("%d",&choice);
switch(choice){
case 1:
do
{New=get_node();
printf("\n Enter the element");
scanf("%d",&New->data);if(root==NULL)
root=New;
else
-
8/4/2019 Dhamu - All Progromes Batch-1
29/60
insert(root,New);
printf("\n Do you want to enter more elements :( y/n)");ans=getch();
}
while(ans=='y');
break;case 2:
printf("\n Enter the element which you want to search");
scanf("%d",&key);tmp=search(root,key,&parent);
printf("\n Parent of node %d is %d",tmp->data,parent->data);
break;case 3:
printf("\n Enter the element u wish to deletr");
scanf("%d",&key);
del(root,key);
break;case 4:
if(root==NULL)printf("\n Tree is not created");
else
{printf("\n The tree is:");
inorder(root);
}
break;case 5:
exit(0);
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)
{
if(New->datadata){
if(root->left==NULL)
root->left=New;
-
8/4/2019 Dhamu - All Progromes Batch-1
30/60
else
insert(root->left,New);}
if(New->data>root->data)
{
if(root->right==NULL)root->right=New;
else
insert(root->right,New);}}
node *search(node*root,int key,node**parent)
{node *temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==key){
printf("\n The %d element is present ",temp->data);return temp;
}
*parent=temp;if(temp->data>key)
temp=temp->left;
else
temp=temp->right;}
return NULL;
}
void del(node*root,int key){
node*temp,*parent,*temp_succ;
temp=search(root,key,&parent);if(temp->left!=NULL&&temp->right!=NULL)
{
parent=temp;temp_succ=temp->right;
while(temp_succ->left!=NULL)
{
parent=temp_succ;temp_succ=temp_succ->left;
}
temp->data=temp_succ->data;parent->left=NULL;
printf("\n Now deleted it!");
return;
-
8/4/2019 Dhamu - All Progromes Batch-1
31/60
}
if(temp->left==NULL&&temp->right==NULL){
if(parent->left==temp)
parent->left=temp->left;
elseparent->right=temp->left;
temp=NULL;
free(temp);printf("\n Now deleted it!");
return;
}if(temp->left==NULL&&temp->right!=NULL)
{
if(parent->left==temp)
parent->left=temp->right;
elseparent->right=temp->right;
temp=NULL;free(temp);
printf("\n Now deleted it!");
return;}
if(temp->left==NULL&&temp->right==NULL)
{
if(parent->left==temp)parent->left==NULL;
else
parent->right=NULL;
printf("\n Now deleted it!");return;
}}
void inorder(node*temp){
if(temp!=NULL)
{inorder(temp->left);
printf("%d\t\t",temp->data);
inorder(temp->right);
}}
-
8/4/2019 Dhamu - All Progromes Batch-1
32/60
OUTPUT:-
Program for binary search tree
1. Create2. Search
3. Delete
4. Display5. Exit
Enter the choice1
Enter the element12
Do you want to enter more elements :( y/n)Enter the element
23
Do you want to enter more elements :( y/n)
Enter the element45
Do you want to enter more elements :( y/n)
Enter the element67
Do you want to enter more elements :( y/n)
Enter the choice 2
Enter the element which you want to search12
The 12 element is present
Parent of node 12 is 28263
Enter the choice 3
Enter the element u wish to deletr67
The 67 element is present
Now deleted it!
Enter the choice 4
The tree is: 12 23 45
-
8/4/2019 Dhamu - All Progromes Batch-1
33/60
Ex.No: 7
DATE: 19.08.11
AVL TREE
PROGRAM:
#include
#include
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));
clrscr();
root=NULL;printf("\n 1. Insert\n2. Display\n3. Exit");
while(1)
{
printf("\n Enter your choice:");scanf("%d",&choice);
switch(choice)
{case 1:
printf("\n Enter the value to be inserted : ");
scanf("%d",&info);if(search(root,info)==NULL)
root=insert(info,root,&ht_inc);
else
printf("\nDuplicate value ignored\n");break;
case 2:
if(root==NULL){
printf("\nTree is empty");
continue;
-
8/4/2019 Dhamu - All Progromes Batch-1
34/60
}
printf("\n tree is:\n");display(root, 1);
printf("\n \n");
printf("In order Traversal is: ");
inorder(root);printf("\n");
break;
case 3:exit(1);
default:
printf("\n Wrong choice\n");}}}
struct node*search(struct node*ptr,int info)
{
if(ptr!=NULL)
if(infoinfo)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(infoinfo)
{
pptr->lchild=insert(info,pptr->lchild,ht_inc);
if(*ht_inc==TRUE){
switch(pptr->balance)
{case -1:
pptr->balance=0;
*ht_inc=FALSE;
-
8/4/2019 Dhamu - All Progromes Batch-1
35/60
break;
case 0:pptr->balance=1;
break;
case 1:
aptr=pptr->lchild;if(aptr->balance==1)
{
printf("\n Left to Left Rotation\n");pptr->lchild=aptr->rchild;
aptr->rchild=pptr;
pptr->balance=0;aptr->balance=0;
pptr=aptr;
}
else
{printf("\n Left to Right Rotation\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;}}}}
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:
-
8/4/2019 Dhamu - All Progromes Batch-1
36/60
pptr->balance=-1;
break;case -1:
aptr=pptr->rchild;
if(aptr->balance==-1)
{printf("\n Right to Right Rotation");
pptr->rchild=aptr->lchild;
aptr->lchild=pptr;pptr->balance=0;
aptr->balance=0;
pptr=aptr;}
else
{
printf("\n Right to Left Rotation\n");
bptr=aptr->lchild;aptr->lchild=bptr->rchild;
bptr->rchild=aptr;pptr->rchild=aptr;
pptr->rchild=bptr->lchild;
bptr->lchild=pptr;if(bptr->balance==-1)
pptr->balance=1;
else
pptr->balance=0;if(bptr->balance==1)
aptr->balance=-1;
else
aptr->balance=0;bptr->balance=0;
pptr=bptr;
}}}}return(pptr);
}
display(struct node*ptr,int level){
int i;
if(ptr!=NULL)
{display(ptr->rchild,level+1);
printf("\n\n");
for(i=0;iinfo);
display(ptr->lchild,level+1);
-
8/4/2019 Dhamu - All Progromes Batch-1
37/60
}}
inorder(struct node*ptr){
if(ptr!=NULL)
{
inorder(ptr->lchild);printf("\t%d",ptr->info);
inorder(ptr->rchild);
}}
OUTPUT:-
1. Insert
2. Display3. Exit
Enter your choice: 1
Enter the value to be inserted: 10
Enter your choice: 1
Enter the value to be inserted: 20
Enter your choice: 1
Enter the value to be inserted: 30
Right to Right Rotation
Enter your choice: 2
Tree is:
30
20
10
In order Traversal is: 10 20 30
-
8/4/2019 Dhamu - All Progromes Batch-1
38/60
Ex.No: 8
DATE: 26.08.11
PRIORITY QUEUE
PROGRAM:
#include
#include#define SIZE 5
void main(void)
{int rear,front,que[SIZE],choice;
int Qfull(int rear),Qempty(int rear,int front);
int insert(int que[SIZE],int rear, int front);
int delet(int que[SIZE],int front);
void display(int que[SIZE],int rear,int front);char ans;
clrscr();front=0;
rear=-1;
do{clrscr();
printf("\n\t\t Priority Queue\n");
printf("\n Main Menu");
printf("\n 1.Insert\n2.Delete\n3.Display");printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{case 1:
if(Qfull(rear))
printf("\n Queue is full");else
rear=insert(que,rear,front);
break;case 2:
if(Qempty(rear,front))
printf("\n cannot delete element");
elsefront=delet(que,front);
break;case 3:
if(Qempty(rear,front))
printf("\n Queue is empty");
-
8/4/2019 Dhamu - All Progromes Batch-1
39/60
else
display(que,rear,front);break;
default:
printf("\n wrong choice");
break;}
printf("\n Do you want to continue?");
ans=getche();}
while(ans=='Y'||ans=='y');
getch();}
int insert(int que[SIZE],int rear,int front)
{
int item,j;
printf("\n Enter the element:");scanf("%d",&item);
if(front==-1)front++;
j=rear;
while(j>=0&&itemrear))
-
8/4/2019 Dhamu - All Progromes Batch-1
40/60
return 1;
elsereturn 0;
}
void display(int que[SIZE],int rear,int front)
{int i;printf("\n The queue is:");
for(i=front;i
-
8/4/2019 Dhamu - All Progromes Batch-1
41/60
EX.NO.: 9
DATE: 26.08.11.
IMPLEMENTATION OF HASHING TECHNIQUES
USING LINEAR PROBING
PROGRAM:
#include
#include#include
#define MAX 10
void 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 Probling");for(i=0;i
-
8/4/2019 Dhamu - All Progromes Batch-1
42/60
if(a[key]==-1)
a[key]=num;else
{
i=0;
while(i
-
8/4/2019 Dhamu - All Progromes Batch-1
43/60
OUTPUT:-
Collision handling by Linear Probing
Enter the Number 12
Do You Wish To Continue? (y/N) y
Enter the Number 34
Do You Wish To Continue? (y/N) y
Enter the Number 79
Do You Wish To Continue? (y/N) n
The Hash Table is....
0-1
1-1212
3-1434
5-1
6-17-1
8-1
979
-
8/4/2019 Dhamu - All Progromes Batch-1
44/60
EX.NO.: 10
DATE: 02.09.11.
TOPOLOGICAL SORT IMPLEMENTATION
PROGRAM:
#include
#include
#define MAX 10void main()
{int G[MAX][MAX],n,i,j,k,visited[MAX],indegree[MAX],edges,u,v;
printf("\nEnter no.of vertices :");scanf("%d",&n);
//Initialize the adjacency matrix
for(i=0;i
-
8/4/2019 Dhamu - All Progromes Batch-1
45/60
//decrement the indegree of nodes adjacent to j
for(k=0;k
-
8/4/2019 Dhamu - All Progromes Batch-1
46/60
EX.NO.: 11
DATE: 09.09.11.
DIJKSTRAS ALGORITHM IMPLEMENTATION
PROGRAM:
#define INFINITY 9999#include
#define MAX 10
typedef struct node{
struct node *next;
int vertex,weight;}node;
node *G[10];
int n;
void readgraph();
void insert(int vi,int vj,int w);void djikstra(int startnode);
void main(){
int u;
clrscr();readgraph();
printf("\n enter the starting node");
scanf("%d",&u);
djikstra(u);getch();
}
void djikstra(int startnode)
{int distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
node *p;
for(i=0;i
-
8/4/2019 Dhamu - All Progromes Batch-1
47/60
if(distance[i]next)
if(!visited[p->vertex])
if(mindistance+p->weightvertex]){
distance[p->vertex]=mindistance+p->weight;
pred[p->vertex]=nextnode;}
count++;
}
for(i=0;i
-
8/4/2019 Dhamu - All Progromes Batch-1
48/60
{
node *p,*q;q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
q->weight=w;if(G[vi]==NULL)
G[vi]=q;
else{
p=G[vi];
while(p->next!=NULL)p=p->next;
p->next=q;
}
}
OUTPUT:
enter no of vertices 3
enter adjacency matrix: 4
5
769
0
9
98
7
enter the starting node4
distance of 9999=1352 path=0
-
8/4/2019 Dhamu - All Progromes Batch-1
49/60
Ex.No:12
DATE: 16.09.2011
PRIMS ALGORITHM
PROGRAM:
#include#include
#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
-
8/4/2019 Dhamu - All Progromes Batch-1
50/60
printf("\n Enter the number of nodes in the graph");
scanf("%d",&nodes);printf("\n Enter the number edges in the graph");
scanf("%d",&n);
for(i=0;i
-
8/4/2019 Dhamu - All Progromes Batch-1
51/60
OUTPUT:-
Prim's algorithm is
Enter the number of nodes in the graph 5
Enter the number edges in the graph 5
Enter the edges and weights
Enter the edge by v1 and v2: 0 1
Enter the corresponding weight: 1
Enter the edge by v1 and v2: 1 2
Enter the corresponding weight: 2
Enter the edge by v1 and v2: 2 3
Enter the corresponding weight: 3
Enter the edge by v1 and v2: 3 4Enter the corresponding weight: 4
Enter the edge by v1 and v2: 4 5
Enter the corresponding weight: 5
The minimal spanning tree is:
Edge (01) and weight=1
Edge (12) and weight=2Edge (23) and weight=3
Edge (34) and weight=4
Total path length is=10
-
8/4/2019 Dhamu - All Progromes Batch-1
52/60
Ex.No:12
DATE: 16.09.2011
KRUSKALS ALGORITHM
PROGRAM:
#include
#includetypedef struct edgel
{
int cost;int x,y;
}edgel;
edgel e[20];int ne,nv,visited[15];
int sum=0;
void getdata();
void kruskal(int,int,int);
void getdata(){
int i,j,temp1;printf("\n Enter the no.of vertex");
scanf("%d",&nv);
printf("\n Enter the no.of edges");scanf("%d",&ne);
for(i=1;i
-
8/4/2019 Dhamu - All Progromes Batch-1
53/60
{
if(visited[x]==0||visited[y]==0){
visited[x]=visited[y]==1;
printf("%d\t%d\t%d\n\n",x,y,e[i].cost);
sum+=e[i].cost;}}
void main()
{int i;
clrscr();
printf("\n\n program for kruskal algotrithm");getdata();
printf("\n \t the mininum spanning tree is \n\n.....");
printf("vertex1\t vertex2\tcost\n\n");
for(i=1;i
-
8/4/2019 Dhamu - All Progromes Batch-1
54/60
EX.NO.: 13
DATE: 23.09.11.BACKTRACKIN ALGORITHM FOR KNAPSACK PROBLEM
PROGRAM:
#include#include
float final_profit=-1.0;
int p[9]={0,11,21,31,33,43,53,55,65};int w[]={0,1,11,21,23,33,43,53,55};
int m=110;
int n=8;int temp[9],x[9];
float final_wt=-1.0;
float bound_calculation(int cp,int cw,int k)
{int ub,c,i;
ub=cp;
c=cw;
for(i=k+1;i
-
8/4/2019 Dhamu - All Progromes Batch-1
55/60
x[j]=temp[j];
}}
if(bound_calculation(cp,cw,k)>=final_profit)
{
temp[k]=0;if(kfinal_profit)&&(k==n)){
final_profit=cp;
final_wt=cw;for(j=1;j
-
8/4/2019 Dhamu - All Progromes Batch-1
56/60
OUTPUT:
knapsack problem using backtracking algorithum
capacity of knapstack=110------------------
11 121 11
31 21
33 2343 33
53 43
55 53
65 55
----------------
following items are selected from knapsasck......item1
final weight =109.00
finalprofit=159.00item2
final weight =109.00
finalprofit=159.00
item3final weight =109.00
finalprofit=159.00
item4
final weight =109.00finalprofit=159.00
item5
final weight =109.00finalprofit=159.00
-
8/4/2019 Dhamu - All Progromes Batch-1
57/60
EX.NO.: 14
DATE: 23.09.11.BRANCH & BOUND ALGORITHM IMPLEMENTATION
PROGRAM:
#include#include
#define MAX 10
typedef struct{
int nodes[MAX];
int vertex;int min;
}
path_node;
path_node TSP(int source,path_node list,int element[][MAX],int max_no_cities){
int i,j;
path_node new_list,new_path,new_min;
if(list.vertex==0){
new_min.min=element[source][1];
new_min.nodes[max_no_cities-1]=source;new_min.vertex=max_no_cities;
return new_min;
}
for(i=0;i
-
8/4/2019 Dhamu - All Progromes Batch-1
58/60
int i;
printf("\n\n the minimum cost is %d\n",path.min);printf("\n the path is...\n");
for(i=0;i
-
8/4/2019 Dhamu - All Progromes Batch-1
59/60
OUTPUT:
how many number of cities are there?5
enter distance from city 1 to 2?3
enter distance from city 1 to 3?2
enter distance from city 1 to 4?4enter distance from city 1 to 5?5
enter distance from city 2 to 1?3
enter distance from city 2 to 3?4enter distance from city 2 to 4?2
enter distance from city 2 to 5?4
enter distance from city 3 to 1?5enter distance from city 3 to 2?2
enter distance from city 3 to 4?5
enter distance from city 3 to 5?3
enter distance from city 4 to 1?5
enter distance from city 4 to 2?2enter distance from city 4 to 3?4
enter distance from city 4 to 5?5enter distance from city 5 to 1?3
enter distance from city 5 to 2?2
enter distance from city 5 to 3?4enter distance from city 5 to 4?2
the minimum cost is 12
the path is...
1--3--5--4--2--1
-
8/4/2019 Dhamu - All Progromes Batch-1
60/60
EX.NO.: 15
DATE: 23.09.11.RANSDOMIZED ALGORITHM IMPLEMENTATION
PROGRAM:
#include#include
#include
#includeint main(void)
{
int a[10]={10,20,30,40,11,12,13,10,30,30};int n=10;
void repetition(int a[],int n);
clrscr();
printf("\n\t program finds the repeated element\n");repetition(a,n);
getch();
return 0;}
void repetition(int a[],int n){
int i,j,count;
time_t t;srand((unsigned)time(&t));
count=1;
while(count