You are on page 1of 46

1.

a) Single linked list


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int element;
struct node*next;
}*list=NULL,*p;
struct node*find(int);
struct node*findpre(int);
void insert(int f);
void delet(int g);
void display();
void main()
{
int data,ch;
clrscr();
printf("singly linked list");
printf("\n menu:\n\t1.insert\n\t2.delet\n\t3.find\n\t4.display\n\t5.exit");
do
{
printf("\n\t enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\tenter the element to be insert:");
scanf("%d",&data);
insert(data);
break;
case 2:printf("\n\t enter the element to be delete:");
scanf("%d",&data);
delet(data);
break;
case 3:printf("\n\t enter the element to be find:");
scanf("%d",&data);
find(data);
printf("\n\t the element is present in the position:%d",p);
break;
case 4:display();
break;
case 5:
exit(0);
}
}
while(ch<5);
getch();
}
void insert(int x)
{
struct node *newnode;
int pos;
newnode=malloc(sizeof(struct node));
newnode->element=x;
if(list->next==NULL)
{
list->next=newnode;
newnode->next=NULL;
}
else
{
printf("\n\t enter the values of the element after which is to be inserted:");
scanf("%d",&pos);
p=find(pos);
newnode->next=p->next;
p->next=newnode;
}
}
struct node*find(int s)
{
p=list->next;
while(p!=NULL&&p->element!=s)
p=p->next;
return p;
}
void delet(int x)
{
struct node*temp;
temp=malloc(sizeof(struct node));
p=findpre(x);
if(p->next!=NULL)
{
temp=p->next;
p->next=temp->next;
printf("\n\t the deleted element is:%d",temp->element);
free(temp);
}
else
printf("\n\t Elements are not found:");
}
struct node*findpre(int s)
{
p=list;
while (p->next!=NULL&&p->next->element!=s)
p=p->next;
return p;
}
void display()
{
if(list->next==NULL)
printf("\n\tlist is empty:");
else
{
p=list->next;
printf("\n\t elements are:\n\t");
while(p!=NULL)
{
printf("%d--->",p->element);
p=p->next;
}
printf("NULL\n");
}
}
























Output:

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

enter the element to be insert:10

enter your choice:1

enter the element to be insert:20

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

enter your choice:1

enter the element to be insert:30

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

enter your choice:4

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

enter your choice:2

enter the element to be delete:30

the deleted element is:30
enter your choice:4

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

enter your choice:3

enter the element to be find:20

the element is present in the position:2264
enter your choice:5
1.b)Double linked list
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *lptr,*rptr;
}*head,*p;
struct node *ins_beg(int x, struct node *first);
struct node *ins_end(int x,struct node *first);
struct node *find(int x);
void display(struct node *first);
struct node *dele(struct node *first,int del);
void main()
{
int choice,x,del;
head=NULL;
clrscr();
printf(DOUBLY LINKED LIST);
printf("\n menu:\n");
printf(" 1.insert-beg\n 2.insert-end\n 3.delete\n 4.find\n 5.display\n 6.exit\n");
while(1)
{
printf("\n enter ur choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n enter the data to be inserted:");
scanf("%d",&x);
head=ins_beg(x,head);
break;
case 2:
printf("\n enter the data to be inserted:");
scanf("%d",&x);
head=ins_end(x,head);
break;
case 3:
printf("\n enter the data to be deleted:");
scanf("%d",&del);
head=dele(head,del);
break;
case 4:
printf("\n enter the element to be searched:");
scanf("%d",&x);
find(x);
printf("the element is present in the location:%d",p);
break;
case 5:
display(head);
break;
case 6:
exit(0);
default:printf("\n invalid entry!!!!");
getch();
}
}
}
struct node *ins_beg(int x,struct node *first)
{
struct node *new,*cur,*prev;
new=malloc(sizeof(struct node));
if(first==NULL)
{
new->data=x;
new->lptr=NULL;
new->rptr=NULL;
return new;
}
else
{
new->data=x;
new->lptr=NULL;
new->rptr=first;
return new;
}
}
struct node *ins_end(int x,struct node *first)
{
struct node*new,*cur,*prev;
new=malloc(sizeof(struct node));
if(first==NULL)
{
new->data=x;
new->lptr=NULL;
new->rptr=NULL;
return new;
}
else
{
cur=first;
while(cur->rptr!=NULL)
{
prev=cur;
cur=cur->rptr;
}
cur->rptr=new;
new->data=x;
new->lptr=cur;
new-> rptr=NULL;
return first;
}
}
struct node *find(int x)
{
p=head->rptr;
while(p!=NULL&&p->data!=x)
p=p->rptr;
return p;
}
struct node *dele(struct node *first,int del)
{
struct node *prev,*cur;
cur=first;
if(first==NULL)
{
printf("\n no data present!!!!");
getch();
}
else if(first->data==del)
{
printf("\n data %dis deleted \n",first->data);
first=first->rptr;
getch();
return first;
}
else
{
while(cur->rptr!=NULL&&cur->data!=del)
{
prev=cur;
cur=cur->rptr;
}
if(cur->rptr==NULL&&cur->data!=del)
printf("\n data not present!!!!");
else if(cur->rptr!=NULL&&cur->data==del)
{
prev->rptr=cur->rptr;
(cur->rptr)->lptr=prev;
printf("\n data %d is deleted\n",cur->data);
}
else if(cur->rptr==NULL&&cur->data==del)
{
prev->rptr=NULL;
printf("\n data %d is deleted\n",cur->data);
}
getch();
return first;
}
}
void display(struct node *first)
{
struct node *temp;
temp=first;
if(temp==NULL)
printf("\n no data present!!!!");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->rptr;
}
getch();
}




















Output:

Double linked list
menu:
1. insertbeg
2. insertend
3. delete
4. find
5. display
6. exit
enter your choice:1
enter the data to be inserted:10
enter your choice:1
enter the data to be inserted:20
enter your choice:1
enter the data to be inserted:30
enter your choice:5
30 20 10
enter your choice:2
enter the data to be inserted:40
enter your choice:5
30 20 10 40
enter your choice:3
enter the data to be deleted:10
Data 10 is deleted
enter your choice:5
30 20 40
enter your choice:4
enter the element to be searched:20
the element is present in the location:2260
enter your choice:4
enter the element to be searched:40
the element is present in the location:2280
enter your choice:6












2. Polynomial addition
#include<malloc.h>
#include<stdio.h>
#include<conio.h>
struct link
{
int coeff;
int pow;
struct link*next;
};
struct link*poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\nEnter the coefficient:");
scanf("%d",&node->coeff);
printf("\nenter the power");
scanf("%d",&node->pow);
node->next=(struct link*)malloc(sizeof (struct link));
node=node->next;
node->next=NULL;
printf("\ndo u wt to continue(y/n):");
ch=getch();
printf("%c",ch);
}
while(ch=='y'||ch=='y');
}
void display (struct link*node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next&&poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow < poly2->pow)
{
poly ->pow=poly2->pow;
poly ->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly ->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link*)malloc(sizeof(struct link));
poly =poly->next;
poly->next=NULL;
}
while(poly1->next||poly2->next)
{
if(poly1->next)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly->coeff;
poly2=poly->next;
}
poly->next=(struct link*)malloc(sizeof (struct link));
poly=poly ->next;
poly->next=NULL;
}
}
void main()
{
poly1=(struct link*)malloc(sizeof(struct link));
poly2=(struct link*)malloc(sizeof(struct link));
poly=(struct link*)malloc(sizeof(struct link));
clrscr();
printf("\nEnter the first polynominal");
create(poly1);
printf("\nFirst polynominal");
display(poly1);
printf("\nEnter the second polynominal");
create(poly2);
printf("\nsecond polynominal");
display(poly2);
polyadd(poly1,poly2,poly);
printf("Addition of two polynominal");
display(poly);
getch();
}

















Output:

Enter the first polynomial
Enter the coefficient:2
enter the power3
do u wt to continue(y/n):y
Enter the coefficient:4
enter the power2
do u wt to continue(y/n):n
First polynominal2x^3+4x^2
Enter the second polynominal
Enter the coefficient:4
enter the power3
do u wt to continue(y/n):y
Enter the coefficient:2
enter the power1
do u wt to continue(y/n):n
Second polynomial:4x^3+2x^1
Addition of two polynomial: 2x^3+4x^2+2x^1















3.Conversion of infix to postfix expression

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
char inf[40],post[40];
int top=0,st[20];
void postfix();
void push(int);
char pop();
void main()
{
clrscr();
printf(TO CONVERT INFIX TO POSTFIX);
printf("\n Enter the infix expression\n");
scanf("%s",inf);
postfix();
getch();
}
void postfix()
{
int i,j=0;
for(i=0;inf[i]!='\0';i++)
{
switch(inf[i])
{
case '+':
while(st[top]>=1)
post[j++]=pop();
push(1);
break;
case '-':
while(st[top]>=1)
post[j++]=pop();
push(2);
break;
case '*':
while(st[top]>=3)
post[j++]=pop();
push(3);
break;
case '/':
while(st[top]>=4)
post[j++]=pop();
push(4);
break;
case '^':
while(st[top]>=4)
post[j++]=pop();
push(5);
break;
case '(':
push(0);
break;
case ')':
while(st[top]!=0)
post[j++]=pop();
top--;
break;
default:
post[j++]=inf[i];
}
}
while(top>0)
post[j++]=pop();
printf("\n\tPostfix expression is=>\n\t\t%s",post);
}

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

char pop()
{
int ele;
char e;
ele=st[top];
top--;
switch(ele)
{
case 1:
e='+';
break;
case 2:
e='-';
break;
case 3:
e='*';
break;
case 4:
e='/';
break;
case 5:
e='^';
break;
}
return(e);
}





Output
Enter the infix expression
a+b-c*e+f
Postfix expression is=>
ab+ce*-f+

















4. Double ended queue:
#include<stdio.h>
#include<alloc.h>
#include<math.h>
#include<conio.h>
struct node
{
int number;
struct node *next;
}*head,*current;
void main()
{
int x,p,n;
void create();
void display();
void insertfront();
void insertrear();
void deletefront();
void deleterear();
void exit();
clrscr();
printf("deque");
printf("\n 1.create");
printf("\n 2.display");
printf("\n 3.insert at front");
printf("\n 4.insert at rear");
printf("\n 5.delete at front");
printf("\n 6.delete at rear");
printf("\n 7.exit");
while(1)
{
printf("\n enter ur choice:");
scanf("%d",&n);
switch(n)
{
case 1:create();
break;
case 2:display();
break;
case 3:insertfront();
break;
case 4:insertrear();
break;
case 5:deletefront();
break;
case 6:deleterear();
break;
case 7:exit();
break;
}
}
getch();
}
void create()
{
struct node*front;
front=malloc(sizeof(struct node));
printf("\n enter the value:");
scanf("%d",&front->number);
front->next=NULL;
if(head==NULL)
{
head=front;
current=head;
}
else
{
current->next=front;
current=current->next;
}
}
void display()
{
struct node *front;
front=head;
printf("\n the elements are:\n");
while(front!=NULL)
{
printf("%d\t",front->number);
front=front->next;
}
}
void insertfront()
{
struct node *front;
front=malloc(sizeof(struct node));
printf("\n enter the value to insert in front position:");
scanf("%d",&front->number);
front->next=NULL;
if(head==NULL)
{
head=front;
}
else
{
front->next=head;
head=front;
}
}
void insertrear()
{
struct node*rear;
rear=malloc(sizeof (struct node));
printf("\n enter the value to insert in rear posistion:");
scanf("%d",&rear->number);
rear->next=NULL;
if(head==NULL)
{
head=rear;
}
else
{
current->next=rear;
current =current->next;
}
}
void deletefront()
{
struct node *front;
if(head==NULL)
{
printf("\n empty");
}
front=head;
head=front->next;
printf("\n first element is deleted \n");
free(front);
}
void deleterear()
{
struct node *rear,*prev;
if(head==NULL)
{
printf("\n empty");
}
else if(head->next==NULL)
{
rear=head;
free(rear);
}
else
{
current=head;
while(current->next!=NULL)
{
prev=current;
current=current->next;
}
rear=current;
prev->next=NULL;
printf("\n last element is deleted");
free(rear);
}
}































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



5. Expression tree traversals

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<ctype.h>
#define size 20
typedef struct node
{
char data;
struct node *left;
struct node *right;
}btree;
btree *stack[size];
int top;
void main()
{
btree *root;
char exp[80];
btree *create(char exp[80]);
void inorder(btree *root);
void preorder(btree *root);
void postorder(btree *root);
clrscr();
printf("\n Enter the postfix expression\n");
scanf("%s",exp);
top=-1;
root=create(exp);
printf("\n The tree is created:");
printf("\n The inorder traversal is .... \n");
inorder(root);
printf("\n The preorder traversal is .... \n");
preorder(root);
printf("\n The postorder traversal is ...\n");
postorder(root);
getch();
}
btree* create(char exp[])
{
btree *temp;
int pos;
char ch;
void push(btree *);
btree *pop();
pos=0;
ch=exp[pos];
while(ch!='\0')
{
temp=(btree *) malloc(sizeof(btree));
temp->left=temp->right=NULL;
temp->data=ch;
if(isalpha(ch))
push(temp);
else if(ch=='+' || ch=='*' || ch=='-' || ch=='/')
{
temp->right=pop();
temp->left=pop();
push(temp);
}
else
printf("Invalid character in expression\n");
pos++;
ch=exp[pos];
}
temp=pop();
return(temp);
}
void push(btree *Node)
{
if(top+1 >= size)
printf("Error: stack is full\n");
top++;
stack[top]=Node;
}
btree* pop()
{
btree *Node;
if(top==-1)
printf("Error: Stack is Empty\n");
Node=stack[top];
top--;
return(Node);
}
void inorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
inorder(temp->left);
printf("%c",temp->data);
inorder(temp->right);
}
}
void preorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
printf("%c",temp->data);
preorder(temp->left);
preorder(temp->right);
}
}
void postorder(btree *root)
{
btree *temp;
temp=root;
if(temp!=NULL)
{
postorder(temp->left);
postorder(temp->right);
printf("%c",temp->data);
}
}



Output
Enter the Postfix Expression
ab+cd+*
The tree is created.
The inorder traversal is
a+b*c+d
The preorder traversal is
*+ab+cd
The postorder traversal is
ab+cd+*







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











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






























7.AVL Tree
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
typedef enum{FALSE,TRUE}bool;
struct node
{
int info;
int balance;
struct node*lchild;
struct node*rchild;
};
struct node*insert(int,struct node*,int*);
struct node*search(struct node*,int);
main()
{
bool ht_inc;
int info;
int choice;
struct node*root=(struct node*)malloc(sizeof(struct node));
root=NULL;
clrscr();
printf("\n1.Insert\n");
printf("2.Display");
printf("\n3.Quit");
while(1)
{
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted:");
scanf("%d",&info);
if(search(root,info)==NULL)
root=insert(info,root,&ht_inc);
else
printf("\n Duplicate value ignored");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is:\n");
display(root,1);
printf("\n\n");
break;
case 3:
exit(1);
default:
printf("Wrong Choice\n");
}
}
getch();
}
struct node*search(struct node*ptr,int info)
{
if(ptr!=NULL)
if(info<ptr->info)
ptr=search(ptr->lchild,info);
else if(info>ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}
struct node*insert(int info,struct node*pptr,int *ht_inc)
{
struct node*aptr;
struct node*bptr;
if(pptr==NULL)
{
pptr=(struct node*)malloc(sizeof(struct node));
pptr->info=info;
pptr->lchild=NULL;
pptr->rchild=NULL;
pptr->balance=0;
*ht_inc=TRUE;
return(pptr);
}
if(info<pptr->info)
{
pptr->lchild=insert(info,pptr->lchild,ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1:
pptr->balance=0;
*ht_inc=FALSE;
break;
case 0:
pptr->balance=1;
break;
case 1:
aptr=pptr->lchild;
if(aptr->balance==1)
{
printf("Left to Left rotation\n");
pptr->lchild=aptr->rchild;
aptr->rchild=pptr;
pptr->balance=0;
aptr->balance=0;
pptr=aptr;
}
else
{
printf("Left to Right rototation\n");
bptr=aptr->rchild;
aptr->rchild=bptr->lchild;
bptr->lchild=aptr;
pptr->lchild=bptr->rchild;
bptr->rchild=pptr;
if(bptr->balance==1)
pptr->balance=-1;
else
pptr->balance=0;
if(bptr->balance==-1)
aptr->balance=1;
else
aptr->balance=0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc=FALSE;
}
}
}
if(info>pptr->info)
{
pptr->rchild=insert(info,pptr->rchild,ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case 1:
pptr->balance=0;
*ht_inc=FALSE;
break;
case 0:
pptr->balance=-1;
break;
case -1:
aptr=pptr->rchild;
if(aptr->balance==-1)
{
printf("Right toRight rotation\n");
pptr->rchild=aptr->lchild;
aptr->lchild=pptr;
pptr->balance=0;
aptr->balance=0;
pptr=aptr;
}
else
{
printf("Right to Left rotation\n");
bptr=aptr->lchild;
aptr->lchild=bptr->rchild;
bptr->rchild=aptr;
pptr->rchild=bptr->lchild;
bptr->lchild=pptr;
if(bptr->balance==-1)
pptr->balance=1;
else
pptr->balance=0;
if(bptr->balance==1)
aptr->balance=-1;
else
aptr->balance=0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc=FALSE;
}
}
}
return(pptr);
}
display(struct node*ptr,int level)
{
int i;
if(ptr!=NULL)
{
display(ptr->lchild,level+1);
printf("\n");
for(i=0;i<level;i++)
printf(" ");
printf("%d",ptr->info);
display(ptr->rchild,level+1);
}
}








Output:
Left to left rotation
1.Insert
2.Display
3.QuitEnter your choice:1
Enter the value to be inserted:8
Enter your choice:1
Enter the value to be inserted:5
Enter your choice:1
Enter the value to be inserted:10
Enter your choice:1
Enter the value to be inserted:3
Enter your choice:1
Enter the value to be inserted:7
Enter your choice:1
Enter the value to be inserted:1
Left to Left rotation
Enter your choice:2
Tree is:
1
3
5
7
8
10
Enter your choice:3
Right to right rotation
1.Insert
2.Display
3.Quit
Enter your choice:1
Enter the value to be inserted:3
Enter your choice:1
Enter the value to be inserted:1
Enter your choice:1
Enter the value to be inserted:7
Enter your choice:1
Enter the value to be inserted:5
Enter your choice:1
Enter the value to be inserted:8
Enter your choice:1
Enter the value to be inserted:10
Right to right rotation
Enter your choice:2
Tree is:
1
3
5
7
8
10
Enter your choice:3
Right to left rotation
1.Insert
2.Display
3.Quit
Enter your choice:1
Enter the value to be inserted:20
Enter your choice:1
Enter the value to be inserted:10
Enter your choice:1
Enter the value to be inserted:30
Enter your choice:1
Enter the value to be inserted:5
Enter your choice:1
Enter the value to be inserted:15
Enter your choice:1
Enter the value to be inserted:12
Left to right rotation
Enter the value to be inserted:18
Enter your choice:2
Tree is:
5
10
12
15
18
20
30
Enter your choice:3

Left to left rotation
1.Insert
2.Display
3.Quit
Enter your choice:1
Enter the value to be inserted:10
Enter your choice:1
Enter the value to be inserted:8
Enter your choice:1
Enter the value to be inserted:15
Enter your choice:1
Enter the value to be inserted:12
Enter your choice:1
Enter the value to be inserted:17
Enter your choice:1
Enter the value to be inserted:11
Left to right rotation
Enter the value to be inserted:14
Enter your choice:2
Tree is:

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











8.Priority queue
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 1000
typedef int ElementType;
struct HeapStruct;
typedef struct HeapStruct *PriorityQueue;
priorityQueueInitialize(int MaxElement);
void insert(ElementType X, PriorityQueue H);
ElementType DeleteMin(PriorityQueue H);
ElementType FindMin(PriorityQueue H);
int IsEmpty(PriorityQueue H);
int IsFull(PriorityQueue H);
void display(PriorityQueue H);
#define MINPQSIZE 10
#define MINDATA-32767
struct HeapStruct
{
int Capacity;
int Size;
ElementType *Element;
};
PriorityQueue Initialize(int MaxElements)
{
PriorityQueue H;
if(MaxElements<MINPQSIZE)
printf("priority queue Size is too Small");
H=(struct HeapStruct*)malloc(sizeof(struct HeapStruct));
if(H==NULL)
printf("Out of space!!!");
H->Element=(int*)malloc(sizeof(struct HeapStruct));
if(H==NULL)
printf("out of space");
H->Capacity=MaxElements;
H->Size=0;
H->Element[0]=MINDATA;
return H;
}
void Insert(ElementType X,PriorityQueue H)
{
int i;
if(IsFull(H))
{
printf("\nPriority queue is full");
return;
}
for(i=++H->Size;H->Element[i/2]>X;i/=2)
H->Element[i]=H->Element[i/2];
H->Element[i]=X;
}
ElementType DeleteMin(PriorityQueue H)
{
int i,child;
ElementType MinElement,LastElement;
if(IsEmpty(H))
{
printf("\nPriority Queue is empty");
return H->Element[0];
}
MinElement=H->Element[i];
LastElement=H->Element[H->Size--];
for(i=1;i*2<=H->Size;i=child)
{
child=i*2;
if(child !=H->Size&&H->Element[child+1]<H->Element[child]);
child++;
if(LastElement>H->Element[child])
H->Element[i]=H->Element[child];
else
break;
}
H->Element[i]=LastElement;
return MinElement;
}
ElementType FindMin(PriorityQueue H)
{
if(!IsEmpty(H))
return H->Element[1];
printf("Priority Queue is empty");
return H->Element[0];
}
int IsEmpty(PriorityQueue H)
{
return H->Size==0;
}
int IsFull(PriorityQueue H)
{
return H->Size==H->Capacity;
}
void display(PriorityQueue H)
{
int i=1;
printf("\nthe element in the prinoity Queue are");
while(i<=H->Size)
{
printf("%d\t",H->Element[i]);
i++;
}
}
main()
{
PriorityQueue H;
int b,a,j,n;
clrscr();
H=Initialize(MAX);
printf("\n1.Insert\n2.Delete min\n3.Find min\n4.Display\n5.exit");
while(1)
{
printf("\nenter ur choice");
scanf("%d",&b);
switch(b)
{
case 1:
printf("\nEnter the num");
scanf("%d",&j);
Insert(j,H);
break;
case 2:
n=DeleteMin(H);
printf("the delete element is %d \n",n);
break;
case 3:
a=FindMin(H);
printf("\nthe mini element is %d \n",a);
break;
case 4:
display(H);
break;
case 5:
exit(0);
break;
}
}
}
Output:
1.Insert
2.Delete min
3.Find min
4.Display
5.exit
enter ur choice1
Enter the num45
enter ur choice1
Enter the num56
enter ur choice1
Enter the num12
enter ur choice1
Enter the num4
enter ur choice1
Enter the num13
enter ur choice4
the element in the prinoity Queue are4 12 45 56 13
enter ur choice2
the delete element is 4
enter ur choice4
the element in the prinoity Queue are 12 13 45 56
enter ur choice3
the mini element is 12
enter ur choice5
9.Hashing Technique
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#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 Probing");
for(i=0;i<MAX;i++)
a[i]=-1;
do
{
printf("\n Enter the Number: ");
scanf("%d",&num);
key=create(num);
linear_prob(a,key,num);
printf("\n Do u wish to continue?(y/n)");
ans=getch();
}while(ans=='y');
display(a);
getch();
}
int create(int num)
{
int key;
key=num%10;
return key;
}
void linear_prob(int a[MAX],int key,int num)
{
int flag,i,count=0;
void display(int a[]);
flag=0;
if(a[key]==-1)
a[key]=num;
else
{
i=0;
while(i<MAX)
{
if(a[i]!=-1)
count++;
i++;
}
if(count==MAX)
{
printf("\n Hash Table is Full");
display(a);
getch();
exit(1);
}
for(i=key+1;i<MAX;i++)
if(a[i]== -1)
{
a[i]=num;
flag=1;
break;
}
for(i=0;i<key&&flag==0;i++)
if(a[i]== -1)
{
a[i]=num;
flag=1;
break;
} } }

void display(int a[MAX])
{
int i;
printf("\n The Hash Table is .... \n");
for(i=0;i<MAX;i++)
printf("\n %d %d",i,a[i]);
}



Output
Collision Handling By Linear Probing
Enter the Number 131
Do u wish to continue?(y/n)y
Enter the Number 21
Do u wish to continue?(y/n)y
Enter the Number 3
Do u wish to continue?(y/n)y
Enter the Number 4
Do u wish to continue?(y/n)y
Enter the Number 5
Do u wish to continue?(y/n)y
Enter the Number 8
Do u wish to continue?(y/n)y
Enter the Number 9
Do u wish to continue?(y/n)y
Enter the Number 18
Do u wish to continue?(y/n)n
The Hash Table is
0 18
1 131
2 21
3 3
4 4
5 5
6 -1
7 -1
8 8
9 9




























10.Prims Algorithm Minimal Spanning Tree
#include<stdio.h>
#include<conio.h>
# define SIZE 20
# define INFINITY 32767
void prim(int G[][SIZE],int nodes)
{
int select[SIZE],i,j,k;
int min_dist,v1,v2,total=0;
for(i=0;i<nodes;i++)
select[i]=0;
printf("\n\n The Minimal Spanning Tree is :\n");
select[0]=1;
for(k=1;k<nodes;k++)
{
min_dist=INFINITY;
for(i=0;i<nodes;i++)
{
for(j=0;j<nodes;j++)
{
if(G[i][j] && ((select[i] && !select[j]) || (!select[i] && select[j])))
{
if(G[i][j]<min_dist)
{
min_dist=G[i][j];
v1=i;
v2=j;
}
}
}
}
printf("\n Edge (%d,%d) and weight = %d",v1,v2,min_dist);
select[v1]=select[v2]=1;
total=total+min_dist;
}
printf("\n\n\t Total Path Length is = %d",total);
}
void main()
{
int G[SIZE][SIZE],nodes;
int v1,v2,length,i,j,n;
clrscr();
printf("\n\t Enter Number of Nodes in the Graph ");
scanf("%d",&nodes);
printf("\n Enter Number of Edges in the Graph ");
scanf("%d",&n);
for(i=0;i<nodes;i++)
for(j=0;j<nodes;j++)
G[i][j]=0;
printf("\n Enter edges and weights \n");
for(i=0;i<n;i++)
{
printf("\n Enter Edge by v1 and v2 :");
scanf("%d %d",&v1,&v2);
printf("\n Enter corresponding weight :");
scanf("%d",&length);
G[v1][v2]=G[v2][v1]=length;
}
getch();
printf("\n\t");
clrscr();
prim(G,nodes);
getch();
}




























OUTPUT
Enter the number of nodes in the graph: 7
Enter the number of edges in the graph: 9
Enter edges and weights:
Enter edge by V1&V2:0 1
Enter the corresponding weight: 27
Enter edge by V1&V2:1 2
Enter the corresponding weight: 16
Enter edge by V1&V2:2 3
Enter the corresponding weight: 12
Enter edge by V1&V2:3 4
Enter the corresponding weight: 22
Enter edge by V1&V2:4 5
Enter the corresponding weight: 25
Enter edge by V1&V2:0 5
Enter the corresponding weight: 06
Enter edge by V1&V2:1 6
Enter the corresponding weight: 14
Enter edge by V1&V2:4 6
Enter the corresponding weight: 24
Enter edge by V1&V2:3 6
Enter the corresponding weight: 18
The minimum spanning tree is:
Edge (0 5) and weight = 6
Edge (4 5) and weight = 25
Edge (3 4) and weight =22
Edge (2 3) and weight = 12
Edge (1 2) and weight =16
Edge (1 6) and weight =14
Total path length is = 95

You might also like