Sunteți pe pagina 1din 43

DATA STRUCTURES - ​LAB EXERCISES

1. Implementation of Singly Linked List


Aim: ​ To write a C program to perform the implementation of Singly Linked List.
ALGORITHM:
1. Define the node structure & read the choice to perform the operations.
2. If the choice is to insert at first, then,
a. Create a new node & add data to the data field.
b. Assign the address of head to the address field of new node.
c. Assign new node to the head.
3. If the choice is to insert at last, then,
a. Assign some temporary node ‘temp’ to the head.
b. If next position of temporary node ‘temp’ is not equal to NULL, then, assign temporary
node ‘temp’ next position to the temporary node ‘temp1’
c. Assign temporary node ‘temp1’ next position to NULL.
d. Delete the temporary node ‘temp’.
4. If the choice is to insert at position, ‘p’, then,
a. Assign some temporary node ‘temp’ to the head.
b. Create a new node & add data to the data field.
c. Traverse the list till the given position is reached.
d. Temporary node ‘temp’ next field is assigned to temporary node ‘temp1’ next field.
e. Temporary node ‘temp1’ address field must be changed to address of previous node.
f. Delete the temporary node ‘temp’.
5. If the choice is delete, then,
a. Assign some temporary node ‘temp’ and Traverse the list till the given position is
reached.
b. Delete the element in the data field of the current node.
6. If the choice is locate, then,
a. Read the element, ‘x’ to be searched.
b. Traverse the entire list.
c. If element, ‘x’ matches with the data field of any node, then, return its position.
d. Else, return “Element not found”.
7. If the choice is display, then,
a. Display the contents of the list until you encounter the NULL pointer.

Singly Linked list


/*Singly Linked List */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i;
struct node
{
int data;
struct node *next;
}*head,*temp,*temp1;
void main()
{ int ch;
void create();
void display();
void delete();
void mnull();
void insert();
void locate();
clrscr();
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
while(1)
{ printf("\n1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit\n");
printf(“\nEnter the choice:\n”);
scanf("%d",&ch);
switch(ch)
{case 1: create(); break;
case 2: display(); break;
case 3: mnull(); break;
case 4: delete(); break;
case 5: insert(); break;
case 6: locate(); break;
case 7: exit(0);
}
} getch();
}
void create()
{ printf("\nenter element");
if(head->next==NULL)
{ temp=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp->data);
head->next=temp;
temp->next=NULL;
}
else
{ temp1=(struct node *)malloc(sizeof(struct node));
scanf("%d",&temp1->data);
temp->next=temp1;
temp1->next=NULL;
temp=temp1;
}
}
void display()
{ temp=head;
if((temp->next)==NULL)
{ printf("\nlist empty\n");
}
else
{ printf("\t\t\n\nelements in list\t\n\n");
while(temp->next!=NULL)
{ printf("-->%d",temp->next->data);
temp=temp->next;
}
}
}
void mnull()
{ temp=head;
temp->next=NULL;
}
void delete()
{ int p;
temp=head;
printf("enter position to be deleted");
scanf("%d",&p);
for(i=1;i<p;i++)
temp=temp->next;
temp->next=temp->next->next;
}
void insert()
{ int p; int c;
temp1=(struct node *)malloc(sizeof(struct node));
printf("\nto insert 1.first 2.last 3.anywhere\n");
scanf("%d",&c);
printf("enter element to be inserted\n");
scanf("%d",&temp1->data);
switch(c)
{ case 1: temp1->next=head->next;
head->next=temp1;
break;
case 2: temp=head->next;
while(temp->next!=NULL)
temp=temp->next;
temp->next=temp1;
temp1->next=NULL;
break;
case 3:
printf("enter position\n");
scanf("%d",&p);
if(p==1)
{ temp1->next=head->next;
head->next=temp1;
}
else
{temp=head->next;
for(i=1;i<p-1;i++)
temp=temp->next;
temp1->next=temp->next;
temp->next=temp1;
}
}
}
void locate()
{ int n;
temp=head->next;
printf("enter element to be located\n");
scanf("%d",&n);
for(i=1;temp->data!=n;i++)
temp=temp->next;
if(temp->data==n)
printf("element is at position %d\n",i);
else
printf("element not found\n");
}

SAMPLE INPUT & OUTPUT:


/*Singly Linked List */
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:1
enter element 5
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:1
enter element6
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:1
enter element7
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:2
elements in list
-->5-->6-->7
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:5
to insert 1.first 2.last 3.anywhere
3
enter element to be inserted
8
enter position2
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:2
elements in list
-->5-->8-->6-->7
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:6
enter element to be located 6
element is at position 3
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:4
enter position to be deleted 1

1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit


Enter the choice:2
elements in list
-->8-->6-->7
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:3
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice:2
list empty
1.create 2.display 3.make null 4.delete 5.insert 6.locate 7.exit
Enter the choice: 7

Doubly Linked List:


Aim:​ To implement doubly linked list for performing insert, delete and search operations.
Algorithm:

1. Set a node to contain INFO and RLINK and LLINK fields.


2. Allot memory dynamically for a node and declare it as a header H.
3. To create a doubly linked list get the element N and allot memory for a node S1.
4. Set S1->INFO=N; and S1->RLINK=NULL, S1->LLINK=H.
5. Repeat the above two steps for all the elements.
6. A node can be inserted at the front, in the middle or at the end of the list.
7. To insert a node X at the front check whether the list is empty, if not set
8. X->RLINK=H->RLINK and H->RLINK=X.
9. To insert a node X at the end travel till the end of the list and assign the last node’s RLINK
value to X and set X->LLINK=last node’s RLINK.
10. To insert a node X after the specified node Y, travel the list till the node Y is reached. Set
X->RLINK=Y->RLINK , Y->RLINK=X,X->LLINK=Y and X->RLINK->LLINK=X
11. A node can be deleted at the front, in the middle or at the end of the list.
12. To delete a node X at the front set X->RLINK->LLINK=H,H->RLINK->RLINK= X.
13. To delete a node X at the end travel the list till the end and assign the previous to last
node’s RLINK value to be NULL.
14. To delete a node X after the specified node Y, travel the list till the node Y is reached Set
X->RLINK->LLINK=Y, Y->RLINK= X->RLINK.
15. To search an element E traverse the list until E is found.

doubly Linked List:


/* Doubly Linked List */
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int data;
struct node *next,*prev;
}*head,*cur,*t,*q;
void create();
void add();
void insert();
void del();
void delpos();
void disp();
void create()
{ head=(struct node *) malloc(sizeof(struct node));
head->next=NULL;
head->prev=NULL;
}
void add()
{ cur=head;
while(cur->next!=NULL)
cur=cur->next;
t=(struct node *) malloc(sizeof(struct node));
printf("Enter data\t");
scanf("%d",&t->data);
cur->next=t;
t->next=NULL;
t->prev=cur;
}
void insert()
{ int p,i;
printf("Enter the position ");
scanf("%d",&p);
cur=head;
for(i=1;i<p;i++)
cur=cur->next;
t=(struct node *) malloc(sizeof(struct node));
printf("Enter data\t");
scanf("%d",&t->data);
q=cur->next;
t->next=q;
q->prev=t;
cur->next=t;
t->prev=cur;
}
void del()
{ int d;
printf("Enter data to delete");
scanf("%d",&d);
cur=head;
while(cur->next!=NULL)
{ if(cur->next->data==d)
{ t=cur->next;
cur->next=cur->next->next;
cur->next->prev=cur;
printf("Deleted\n");
free(t);
return;
}
else
cur=cur->next;
}
printf("No such data\n");
}
void delpos()
{ int p,i;
printf("Enter the position");
scanf("%d",&p);
cur=head;
for(i=1;i<p;i++)
cur=cur->next;
t=cur->next;
cur->next=cur->next->next;
cur->next->prev=cur;
free(t); printf("Deleted\n");
}
void disp()
{ cur=head;
if(head->next==NULL)
{printf("List is empty\n");
return;
}
while(cur->next!=NULL)
{
printf("%d<==>",cur->next->data);
cur=cur->next;
}
}
void main()
{
int ch;
clrscr();
create();
do
{
printf("\n\n1.Add\n2.Insert\n3.Delete\n4.Delete by
pos\n5.Display\n6.Exit\n\n");
printf("Enter ur choice\t");
scanf("%d",&ch);
switch(ch)
{ case 1: add(); break;
case 2: insert(); break;
case 3: del(); break;
case 4: delpos(); break;
case 5: disp(); break;
case 6: exit(0); break;
}
}while(ch<=5);
getch();
}

Output of Doubly Linked list:

/* Doubly linked list*/


1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 1
Enter data 2
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 1
Enter data 3
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 1
Enter data 4
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 5
2<==>3<==>4<==>
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 2
Enter the position​ ​2
Enter data 6
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 5
2<==>6<==>3<==>4<==>
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice
3
Enter data to delete​ ​3
Deleted
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 5
2<==>6<==>4<==>
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 4
Enter the position​ ​2
Deleted
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 5
2<==>4<==>
1.Add
2.Insert
3.Delete
4.Delete by pos
5.Display
6.Exit
Enter ur choice 6
Ex.No:2 POLYNOMIAL ADDITION
#include<stdio.h>
#include<malloc.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("\n enter coeff:");
scanf("%d",&node->coeff);
printf("\n enter power:");
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 show(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=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
void main()
{
char ch;
do{
poly1=(struct link *)malloc(sizeof(struct link));
poly2=(struct link *)malloc(sizeof(struct link));
poly=(struct link *)malloc(sizeof(struct link));
printf("\nenter 1st number:");
create(poly1);
printf("\nenter 2nd number:");
create(poly2);
printf("\n1st Number:");
show(poly1);
printf("\n2nd Number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\nAdded polynomial:");
show(poly);
ch=getch();
}
while(ch=='y' || ch=='Y');
}

Result :
The polynomial is represented as a linked list and polynomial addition is performed.

3. Infix to postfix
Aim:
To implement a stack and use it to convert the infix to Postfix expression.
Source Code​ ​:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#define N 64
#define LP 10
#define RP 20
#define OPERATOR 30
#define OPERAND 40// Left parentheses precedence. Minimum of all
#define LPP 0// Addition Subtraction precedence. Minimum among all operator precedence
#define AP 1
#define SP AP// Multiplication divisor precedence.
#define MP 2
#define DP MP// Remainder precedence.
#define REMP 2
#define NONE 9
static char infix[N+1],stack[N],postfix[N+1];
static int top;
void infixtopostfix(void); /** POSTFIX CONVERSION FUNCTION **/
int gettype(char); /** TYPE OF EXPRESSION GENERATOR **/
void push(char); /** PUSH FUNCTION **/
char pop(void); /** POP FUNCTION **/
int getprec(char); /** PRECEDENCE CHECKER FUNCTION **/
void main()
{ char ch;
do { top=-1;
printf("\nEnter an infix expression\n");
fflush(stdin);
gets(infix);
infixtopostfix();
printf("\ninfix = %s\npost fix =%s\n",infix,postfix);
printf("\nDo you wish to continue\n");
ch=getche(); }while(ch=='Y' || ch=='y');}
void infixtopostfix(void)
{ int i,p,l,type,prec;
char next; i=p=0;
l=strlen(infix);
while(i<l)
{
type=gettype(infix[i]);
switch(type)
{
case LP: push(infix[i]);
break;
case RP:
while((next=pop())!='(')
postfix[p++]=next;
break;
case OPERAND:
postfix[p++]=infix[i];
break;
case OPERATOR:
prec=getprec(infix[i]);
while(top>-1 && prec <= getprec(stack[top]))
postfix[p++]=pop();
push(infix[i]); break;
} i++;
} while(top>-1)
postfix[p++]=pop();
postfix[p]='\0';
}
int gettype(char sym)
{
switch(sym)
{ case '(':
return(LP);
case ')':
return(RP);
case '+':

case '-':
case '*':
case '/':
case '%':
return(OPERATOR);
default : return(OPERAND);
}
}

void push(char sym)


{ if(top>N)
{ printf("\nStack is full\n");
exit(0);
}
else
stack[++top]=sym;
}
char pop(void)
{
if(top<=-1)
{ printf("\nStack is empty\n");
exit(0);
}
else
return(stack[top--]);
}
int getprec(char sym)
{
switch(sym)
{
case '(': return(LPP);
case '+': return(AP);
case '-': return(SP);
case '*': return(MP);
case '/': return(DP);
case '%': return(REMP);
default : return(NONE);
}}

OUTPUT for Infix to Postfix:


Enter an infix expression
(a*b)
infix = (a*b)
post fix =ab*
Do you wish to continue: y
Enter an infix expression
a*b+c
infix = a*b+c
post fix =ab*c+
Do you wish to continue

4. C program for Circular Queue:

Aim:
To implement a circular queue and perform its operation.

Source Code​ ​:
/*Circular queue */
#include<stdio.h>
#include<conio.h>
#include<process.h>
const max_length=4;
void dele();
void insert();
void display();
void working();
void main( )
{ int queue[max_length];
int rear; int front,key;
clrscr( );
rear=-1;
front=-1;
for(int count=0;count<max_length;count++)
queue[count]=0;
do
{ printf("\n\nIMPLEMENTATION OF CIRCULAR QUEUE\n");
printf("\n SELECT UR CHOICE: ");
input:
printf("\n 1.INSERT \n 2.DELETE \n 3.DISPLAY \n 4.EXIT \n");
printf("ENTER UR CHOICE: \n");
scanf(“%d”,&key);//=getch();
if(key=='4')
break;
else if(key=='1')
insert();
else if(key=='2')
delete();
else if(key=='3')
display();
else
goto input;
} while(1);
getch( );
}
void insert()
{ int item;
printf("enter value to insert : ");
scanf(“%d”,&item);
if((rear==front-1)||(front==0 && rear==max_length-1))
printf("\n\n queue is full\n\n");
else if(rear==-1 && front==-1)
{
rear++;
queue[rear]=item;
front=rear;
printf("\nitem is inserted into queue \n");
}
else if(rear==max_length-1 && front!=0)
{
rear=0;
queue[rear]=item;
printf(“item is inserted into queue");
}
else
{
rear++;
queue[rear]=item;
printf(“itemis inserted into queue");
}
}
void delete()
{
if(rear==-1 && front==-1)
printf("queue is empty");
else
{
printf( “%d”,queue[front],"is deleted from queue");
queue[front]=0;
if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==max_length-1)
front=0;
else
front++;
}
getch();
}
void display()
{
if(front!=-1 && rear!=-1)
{
printf(" The circular queue is \n ");
for(int count=0;count<max_length;count++)
printf(“%d”,queue[count],\t");
}
else
printf("no element in queue");
getch();
}
Another Circular queue program
#include<stdio.h>
int front=-1;
int rear=-1;
int size=10;
void cq_insert(int * cq,int v) // insert element into circular queue
{
int i;
if((front-rear)==1||(front==0&&rear==size-1))
{
printf("overflow!");
getch();
return;
}

else if((front==-1)&&(rear==-1))
{
front=0;
rear=0;
cq[rear]=v;
}
else if(rear==(size-1))
{
rear=0;
cq[rear]=v;
}
else
{
rear++;
cq[rear]=v;
}
printf("\n\ncircular Queue is:\n\n"); // display
if(front<=rear)
{
i=front;
while(i<=rear)
{
printf("%d ",cq[i]);
i++;
}
}
else if(rear<front)
{
i=front;
while(i<=size-1)
{
printf("%d ",cq[i]);
i++;
}
i=0;
while(i<=rear)
{
printf("%d ",cq[i]);
i++;
}
}
getch();
}
void cq_delet(int *cq) //delee the first element in circular queue
{
int i;
if((front==-1)&&(rear==-1))
{
printf("underflow!");
return;
}
else if(front==rear)
{
front=-1;
rear=-1;
}
else if(front==size-1)
front=0;
else
front++;

printf("\n\ncircular Queue is:\n\n"); //display


if(front<=rear)
{
i=front;
while(i<=rear)
{
printf("%d ",cq[i]);
i++;
}
}
else if(rear<front)
{
i=front;
while(i<=size-1)
{
printf("%d ",cq[i]);
i++;
}
i=0;
while(i<=rear)
{
printf("%d ",cq[i]);
i++;
}
}
getch();
}
void main()
{
int * cqueue,i,ch,v;
clrscr();

do
{
clrscr();
printf("CIRCULAR QUEUE\n");
printf("Enter a choice:\n");
printf("1.Insertion\n");
printf("2.Deletion\n");
printf("3.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter the element to be inserted:");
scanf("%d",&v);
cq_insert(cqueue,v);
break;
}
case 2:
{
cq_delet(cqueue);
break;
}
case 3:
exit(0);
default:
printf("Wrong choice!");
}
}while(ch!=3);
getch();
}

5. EXPRESSION TREE TRAVERSAL

Aim:
To perform In-order, Preorder and Post order traversals in Expression tree.

Algorithm:
1. Create the binary search tree
2. To perform in-order traversals
a. process the left sub tree
b. process the root
c. process the right sub-tree
3. To perform preorder traversal
a. process the root node
b. process the left
c. process the right
4. To perform post-order traversal
a. process the left node
b. process the right node.
c. process the root node.

Source Code​ ​:
#include<stdio.h>
#include<conio.h>
struct node
{ int data;
struct node*left,*right;
};
typedef struct node * nodeptr;
nodeptr root,t,p,q;
void inorder(nodeptr);
void preorder(nodeptr);
void postorder(nodeptr);
void disp(nodeptr,int,int,int);
void add();
void main()
{ int ch;
while(1)
{ printf("\n1.add\n2.disp\n3.preorder\n4.inorder\n5.Post order\n");
printf("Enter ur choice");
scanf("%d",&ch);
switch(ch)
{ case 1: add(); break;
case 2: clrscr(); disp(root,40,7,16); break;
case 3: preorder(root); break;
case 4: inorder(root); break;
case 5: postorder(root); break;
case 6: exit(0);
}
}
getch();
}
void disp(nodeptr root,int col,int row,int wid)
{gotoxy(col,row);
if(root!=NULL)
{printf("%d\t",root->data);
disp(root->left,col-wid,row+2,wid/2);
disp(root->right,col+wid,row+2,wid/2);
}
}
void preorder(nodeptr root)
{ if(root!=NULL)
{printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
}
void inorder(nodeptr root)
{ if(root!=NULL)
{ inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
}
void postorder(nodeptr root)
{ if(root!=NULL)
{postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
}
void add()
{ int x;
printf("Enter the data");
scanf("%d",&x);
t=(nodeptr)malloc(sizeof(struct node));
t->data=x;
t->right=NULL;
t->left=NULL;
if(root==NULL)
root=t;
else
{ p=q=root;
while(q!=NULL && p->data!=x)
{ p=q;
if(x<p->data) q=p->left;
else q=p->right;
}
if(p->data==x)
printf("%d is duplicate",x);
else if(x<p->data)
p->left=t;
else p->right=t;
}
}

Output for tree traversal:


/* Binary tree Traversal*/
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 1
Enter the data​ ​2
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 1
Enter the data​ ​5
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 1
Enter the data​ ​8
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 2
2

1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 3
2 5 8
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 4
2 5 8
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 5
8 5 2
1.add
2.disp
3.preorder
4.inorder
5.Post order
6.Exit
Enter ur choice 6

6. Implementation of BINARY SEARCH TREE


Aim:​ To write a C program to implement Binary Search Tree (BST).

ALGORITHM:
1. Define the BST structure & read the choice to perform the operations.
2. If the choice is insert,
a. Create a node and assign input value in it.
b. Make its left and right child pointer as NULL.
c. If the inserted value is less than the root, then, place it in the left subtree.
d. Else, place it in the right subtree.
3. If the choice is findmin,
a. Traverse the left subtree.
b. Last value in the left subtree is the minimum value.
4. If the choice is findmax,
a. Traverse the right subtree.
b. Last value in the right subtree is the maximum value.
5. If the choice is find,
a. Read the element to be searched.
b. If the search element is less than root value, continue the search in the
left sub tree.
c. If the search element is greater than root value, continue the search in
the right sub tree.
d. If search element found, then, display “Element found”.
6. If the choice is display, then display all elements in BST from root to leaves.

Binary search tree:

/*Binary search tree*/


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int data;
struct node *left,*right;
};
typedef struct node * nodeptr;
nodeptr root,t,p,q;
void add();
void search();
void findmin();
void findmax();
void disp(nodeptr,int,int,int);
void main()
{ int ch;root=NULL;
while(1)
{ printf("1.Add\n2.Search\n3.Display\n4.Findmin\n5.Findmax\n6.Exit");
printf(“\n Enter the choice:\n”);
scanf("%d",&ch);
switch(ch)
{
case 1: add(); break;
case 2: search(); break;
case 3: clrscr(); disp(root,40,7,16); break;
case 4: findmin(); break;
case 5: findmax(); break;
case 6: exit(0);
}
}
getch();
}
void add()
{ int x;
t=(nodeptr)malloc(sizeof(struct node));
printf("Enter data");
scanf("%d",&x);
t->data=x;
t->left=NULL;
t->right=NULL;
if(root==NULL)
root=t;
else
{
p=q=root;
while(q!=NULL&& x!=p->data)
{ p=q;
if(x<p->data) q=p->left;
else q=p->right;
}
if(x==p->data)
printf("%d is duplicate number\n",x);
else if (x<p->data) p->left=t;
else p->right=t;
}
}
void search()
{ int x;
printf("Enter the elt to search");
scanf("%d",&x);
p=q=root;
while(q!=NULL&& x!=p->data)
{ p=q;
if(x<p->data) q=p->left;
else q=p->right;
}
if(x==p->data)
printf("Element is present");
else
printf("Element is not present");
}
void disp(nodeptr root,int col,int row,int wid)
{ gotoxy(col,row);
if(root!=NULL)
{printf("%d\t",root->data);
disp(root->left,col-wid,row+2,wid/2);
disp(root->right,col+wid,row+2,wid/2);
}
}
void findmax()
{ t=root;
while(t->right!=NULL)
t=t->right;
printf("maximum %d",t->data);
}
void findmin()
{ t=root;
while(t->left!=NULL)
t=t->left;
printf("minimum %d",t->data);
}
SAMPLE INPUT & OUTPUT:
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:1
Enter data 1
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:1
Enter data 3
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:1
Enter data 4
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:3
​ 1
3

4
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:2
Enter the elt to search​ ​4
Element is present
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:4
minimum 1
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:5
MAXIMUM: 4
1.Add
2.Search
3.Display
4.Findmin
5.Findmax
6.Exit
Enter the choice:6

7. Program for insertion in AVL tree

Aim:​ To write a C program to Perform insertion in AVL tree.


Source code:
#include<stdio.h>
#include<conio.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);
Void main()
{
bool ht_inc;
int info ;
int choice;
Struct node *root = (struct node *)malloc(sizeof(struct node));
root = null;
while(1)
{ printf("1.insert\n");
printf("2.display\n");
printf("3.quit\n");
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("duplicate value ignored\n");
break;
case 2:
if(root==null)
{ printf("tree is empty\n");
continue;
}
printf("tree is :\n");
display(root, 1);
printf("\n\n");
printf("inorder traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("wrong choice\n");
}/*end of switch*/
}/*end of while*/
}/*end of main()*/

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);
}/*end of search()*/
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: /* right heavy */
pptr->balance = 0;
*ht_inc = false;
break;
case 0: /* balanced */
pptr->balance = 1;
break;
case 1: /* left heavy */
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 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;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = false;
}/*end of switch */
}/*end of if */
}/*end of if*/
if(info > pptr->info)
{
pptr->rchild = insert(info, pptr->rchild, ht_inc);
if(*ht_inc==true)
{
switch(pptr->balance)
{
case 1: /* left heavy */
pptr->balance = 0;
*ht_inc = false;
break;
case 0: /* balanced */
pptr->balance = -1;
break;
case -1: /* right heavy */
aptr = pptr->rchild;
if(aptr->balance == -1)
{
printf("right to right 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;
}/*end of else*/
*ht_inc = false;
}/*end of switch */
}/*end of if*/
}/*end of if*/
return(pptr);
}/*end of insert()*/
display(struct node *ptr,int level)
{
int i;
if ( ptr!=null )
{ display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*end of if*/
}/*end of display()*/
inorder(struct node *ptr)
{
if(ptr!=null)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*end of inorder()*/
OUTPUT for AVL Insertion:
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 2
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 6
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 8
Right to Right Rotation
1.Insert
2.Display
3.Quit
Enter your choice : 2
Tree is :
8
6
2
Inorder Traversal is: 2 6 8
1.Insert
2.Display
3.Quit
Enter your choice : 1
Enter the value to be inserted : 5
1.Insert
2.Display
3.Quit
Enter your choice : 2
Tree is :
8
6
5
2
Inorder Traversal is: 2 5 6 8
1.Insert
2.Display
3.Quit
Enter your choice :

8. Hashing with open addressing:

Aim:​ To write a C program to implement Hashing with Open Addressing.

Source Code:
/*Open Hashing*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
#include<malloc.h>
struct node
{
int num,tr;
struct node *flink,*blink;
}*p;
struct node *temp,*te;
void create(int n)
{
int i;
temp=(struct node*)malloc(sizeof(struct node));
temp->flink=NULL;
temp->blink=NULL;
temp->tr=0;
if(p==NULL)
p=temp;
for(i=1;i<n;i++)
{
te=(struct node*)malloc(sizeof(struct node));
te->flink=NULL;
te->blink=NULL;
te->tr=i;
temp->flink=te;
temp=temp->flink;
}
}
void deletehash(int hnum,int no)
{
struct node *f=p;
while(f!=NULL)
if(f->tr!=hnum)
f=f->flink;
else
{
struct node *dwn=f;
while((dwn->blink->num!=no) && (dwn->blink!=NULL))
dwn=dwn->blink;
if(dwn->blink==NULL)
{
printf("The record doesn't exist.You cannot delete it.\n");
break;
}
else
{
dwn->blink=dwn->blink->blink;
break;
}//inner else
}//outer else
}
void search(int hnum,int no)
{
struct node *f=p;
while(f!=NULL)
if(f->tr!=hnum)
f=f->flink;
else
{
struct node *dwn=f;
while((dwn->blink->num!=no) && (dwn->blink!=NULL))
dwn=dwn->blink;
if(dwn->blink==NULL)
{
printf("The record doesn't exist\n");
break;
}
else
{
printf("The element is found");
break;
}//inner else
}//outer else
}
void insert(int hnum,int no)
{
struct node *f=p;
while(f!=NULL)
{
if(f->tr!=hnum)
f=f->flink;
else
{
struct node *dwn=f;
while(dwn->blink!=NULL)
dwn=dwn->blink;
temp=(struct node*)malloc(sizeof(struct node));
temp->flink=temp->blink=NULL;
temp->num=no;
dwn->blink=temp;
f=f->flink;
}
}
}
void print()
{
struct node *t=p;
printf("\n\n OPEN HASHING\n\n The hash table\n");
while(t!=NULL)
{
struct node *tt=t;
printf("hash number= %d",tt->tr);
tt=tt->blink;
printf("\n");
while(tt!=NULL)
{
printf("element= %d\n",tt->num);
tt=tt->blink;
printf("\n");
}//inner while
t=t->flink;
}//outer while
}
void main()
{
int n,num,mod,op;
char ch='y';
p=NULL;
clrscr();
printf("\n\n enter the hash size\n");
scanf("%d",&n);
create(n);
while(ch=='y')
{
printf("\n 1.insert\n 2.print\n 3.delete\n 4.search\n 5.exit\n enter ur option");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\n enter the element");
scanf("%d",&num);
mod=num%n;
insert(mod,num);
break;
case 2:
print();
break;
case 3:
printf("enter the element");
scanf("%d",&num);
mod=num%n;
deletehash(mod,num);
break;
case 4:
printf("enter the element");
scanf("%d",&num);
mod=num%n;
search(mod,num);
break;
case 5:
exit(1);
} //switch
} //while
} //main

Output:
enter the hash size
5
1.insert
2.print
3.delete
4.search
5.exit
enter ur option 1
enter the element 2
1.insert
2.print
3.delete
4.search
5.exit
enter ur option 1
enter the element 3
1.insert
2.print
3.delete
4.search
5.exit
enter ur option 2

OPEN HASHING
The hash table
hash number= 0
hash number= 1
hash number= 2
element= 2
hash number= 3
element= 3
hash number= 4
1.insert
2.print
3.delete
4.search
5.exit
enter ur option5

9. PRIMS algorithm:

Aim:​ To write a C program to implement Prims algorithm to find the MST.

Source Code:
/*prims algorithm */
#include<stdio.h>
#include<conio.h>
#include<process.h>
float lcost[100],a[100][100];
int closest[100],i,j,k,min,n,c=0;
void get_mat()
{
printf("Enter the No of Vertices:");
scanf("%d",&n);
printf("enter 1000 for no path \n");
printf("enter weighted matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("cost between the edge\t%d,%d:\t",i,j);
scanf("%f",&a[i][j]);
}//inner for
}//outer for
}//fn
void prim1()
{ for(i=2;i<=n;i++)
{
lcost[i]=a[1][i];
closest[i]=1;
}
printf("minimum cost spanning tree edges are \n");
for(i=2;i<=n;i++)
{
min=lcost[2];
k=2;
for(j=3;j<=n;j++)
{
if(lcost[j]<min)
{
min=lcost[j];
k=j;
}
}
c=c+min;
printf("(%d,%d)\tcost=%d\t",closest[k],k,min);
lcost[k]=2000;
for(j=2;j<=n;j++)
if((a[k][j]<lcost[j])&&(lcost[j]<2000))
{
lcost[j]=a[k][j];
closest[j]=k;
}
printf("\n");
}//outer for
printf("\n\nWeight of minimum cost spanning tree :%d",c);
getch();
}
void main()
{int ch;
clrscr();
do
{
printf("\n1.get\n2.find path with minimum cost\n3.exit\nenter ur choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
get_mat();
break;
case 2:
prim1();
break;
case 3:
exit(0);
break;
}//switch
}while(ch<=3);//do
getch();
}//main
OUTPUT:

1.get
2.find path with minimum cost
3.exit
enter ur choice
1
Enter the No of Vertices:3
enter 1000 for no path
enter weighted matrix
cost between the edge 1,1: 1000
cost between the edge 1,2: 6
cost between the edge 1,3: 2
cost between the edge 2,1: 6
cost between the edge 2,2: 1000
cost between the edge 2,3: 5
cost between the edge 3,1: 2
cost between the edge 3,2: 5
cost between the edge 3,3: 1000

1.get
2.find path with minimum cost
3.exit
enter ur choice
2
minimum cost spanning tree edges are
(1,3) cost=2
(3,2) cost=5

Weight of minimum cost spanning tree :7

S-ar putea să vă placă și