DS Labprint 2022 Regulation FINAL REPORT

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 83

SRI RAAJA RAAJAN COLLEGE OF ENGINEERING AND TECHNOLOGY

CS3311- DATA STRUCTURES LABORATORY


INDEX

EX. NO. PAGE SIGN


NAME OF THE EXPERIMENT
NO.
1. Array implementation of ADTs
1.A Stack ADTs

1.B Queue ADTs


2. Array implementation of List ADT

Linked list implementation


3. 3.A.Linked list implementation of Stack ADTs
3.B.Linked list implementation of Queue ADTs

4. Applications of List, Stack and Queue ADTs

4.A. Implementation of Polynomial ADT

4.B. Conversion of Infix into Postfix Expression

5. Implementation of Binary Trees and operations of Binary Trees

6. Implementation of Binary Search Trees


7. Implementation of AVL Trees
8. Implementation of Heaps using Priority Queues.
9. 9.A.Graph representation

9.B.Graph Traversal-BFS and DFS


10. Implementation of Searching algorithms –Linear Search and Binary
Search.

Implementation of Sorting algorithms –Bubble sort,ShellSort,Quick


Sort, Insertion sort, Merge sort
11. Implementation of Hashing Technique(Linear and Quadratic
Probing)
12. 12.A. Implementation of Dijkstra’s Algorithm
12.B. Implementation of Prims’s Algorithm
ARRAY IMPLEMENTATION OF STACK
AIM:
To write a ‘C’ PROGRAM to implement the stack using array.
ALGORITHM:
1. Start the program
2. According to switch case the operations are selected.
3. In case 1, data are added using PUSH operation.
4. In case 2, data are deleted from stack using POP operation.
5. In case 3, elements of array in stack are displayed using display Function.
6. End the program.
7. PROCEDURE FOR PUSH: -
a) Initialize pointer variable and assign top is equal to -1
b) Get the data to be pushed from user.
c) If the pointer head IS pointed to maximum of Stack or above the maximum.
d) Print Stack is full.
e) Otherwise Increment the top pointer.
f) Put the data into the top of the array of stack.
g) End the Program.
8. PROCEDURE FOR POP: -
a) Initialize the pointer variable.
b) Check weather Stack is full or not.
c) IF top is equal to 0, Print “Stack is EMPTY”
d) Otherwise, top element of array is assigned to item.
e) Decrement the top pointer
f) Print the deleted data
g) End POP program
9. PROCEDURE FOR DISPLAY: -
a) Initialize Pointer variable
b) By using for loop top is initializing to i Print data[i] until I >= condition is False
c) When every looping process I is decremented in for loop
d) End DISPLAY Program.
10. Stop the Program.
PROGRAM: (ARRAY IMPLEMENTATION OF STACK ADT)

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK : ");
scanf("%d",&n);
printf("\n STACK OPERATIONS USING ARRAY");
printf("\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\nExiting...");
break;
}
default:
{
printf ("\n Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\nSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;}}
void pop()
{
if(top<=-1)
{
printf("\n Stack is under flow");
}
else
{
printf("\nThe popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Enter Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT
Enter the size of STACK : 5
STACK OPERATIONS USING ARRAY
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:3
Enter the Choice:1
Enter a value to be pushed:5
Enter the Choice:1
Enter a value to be pushed:6
Enter the Choice:3
The elements in STACK
6
5
3
Enter Next Choice
Enter the Choice:2
The popped elements is 6
Enter the Choice:3
The elements in STACK
5
3
Enter Next Choice
Enter the Choice:4
Exiting...

RESULT: Thus, the program for implementation of stack is executed and its output is verified.
IMPLEMENTATION OF QUEUE USING ARRAY
AIM:

To write a ‘C’ PROGRAM to implement the queue using array.

ALGORITHM:

1. Start the program

2. According to switch case the operations are selected.

3. In case 1, data are added using insert operation.

4. In case 2, data are deleted from stack using delete operation.

5. In case 3, elements of array in stack are displayed using display Function.

6. End the program.

PROGRAM:( QUEUE ADT USINGARRAY)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define SIZE 5
int front = - 1;
int rear = - 1;
int q[SIZE];
void insert( );
void del( );
void display( );
void main()

int choice;

do

printf("\t Menu");

printf("\n 1. Insert");
printf("\n 2. Delete");
printf("\n 3. Display ");

printf("\n 4. Exit");

printf("\n Enter YourChoice:");

scanf("%d", &choice); switch(choice)


{

case1:

insert( );

display( );

break;

case2:
del( );
display( );
break;

case 3:
display( );

break;
case 4:
printf("EndofProgram...................................!!!!");

exit(0);

}}

while(choice != 4);

void insert( )

int no;

printf("\n Enter No.:");

scanf("%d", &no);

if(rear < SIZE - 1)

q[++rear]=no;
if(front==-1)

front=0;// front=front+1;

else

printf("\n Queue overflow");

}
}

void del( )

if(front == - 1)

printf("\n Queue Underflow");

return;

else

printf("\n Deleted Item:-->%d\n", q[front]);

if(front == rear)

front = - 1; rear = -
1;

else

front = front + 1;

}}

void display( )

int i;

if( front == - 1)

printf("\nQueueisempty.......................................");

return;

for(i = front; i<=rear; i++)

printf("\t%d",q[i]);

}
OUTPUT

RESULT: Thus, the program for implementation of queue is executed and its output is verified.
ARRAY IMPLEMENTATION OF LIST ADTS
AIM:
To write a ‘C’ PROGRAM to implement the array-based circular queue.
ALGORITHM:
1. Start the program.
2. Declare the necessary variables.
3. Use a do while loop and switch statement to call the functions.
4. Insert: a) Read the value to be inserted
b) Increment rear and front if it is the first element.
c) Else increment the rear by 1.
5. Delete: a) Increment front by 1.
b) Print the deleted elements.
6. Display: a) If the both front=rear=1, print empty queue.
b) Else use for loop varying from front to rear and print the elements.
7. Stop the program.
PROGRAM:( ARRAY IMPLEMENTATION OF LISTADT)

#include<stdio.h>

#include<conio.h>#defin

e MAX 10

void create();

void insert();

void deletion();

void search();

void display();

int a,b[20], n, p, e, f, i, pos;

void main()

clrscr();

int ch; char

g='y';

do

printf("\n main Menu");

printf("\n1.Create\n2.Delete\n3.Search\n4.Insert\n5.Display\n6.Exit\n");
printf("\n Enter your Choice");

scanf("%d", &ch);

switch(ch)

case1:

create();

break;

case2:

deletion();

break;

case3:

search();

break;

case4:

insert();

break;

case5:

display();

break;

case6:

exit();

break;

default:

printf("\n Enter the correct choice:");

printf("\n Do u want to continue:::");

scanf("\n%c", &g);

while(g=='y'||g=='Y');

getch();

void create()

printf("\n Enter the number ofnodes");


scanf("%d",&n);

for(i=0;i<n;i++)

printf("\nEnterthe Element:",i+1);

scanf("%d",&b[i]);

void deletion()

printf("\n Enter the position u want to delete::");

scanf("%d", &pos);

if(pos>=n)

printf("\n Invalid Location::");

else

for(i=pos+1;i<n;i++)

b[i-1]=b[i];

n--;

printf("\n The Elements after deletion");

for(i=0;i<n;i++)

printf("\t%d", b[i]);

void search()

printf("\n Enter the Element to besearched:");

scanf("%d",&e);
for(i=0;i<n;i++)

if(b[i]==e)

printf("Value is in the %d Position", i);

}}}

void insert()

printf("\n Enter the position u need to insert::");

scanf("%d", &pos);

if(pos>=n)

printf("\n invalid Location::");

} else

for(i=MAX-1;i>=pos-1;i--)

b[i+1]=b[i];

printf("\n Enter the element to insert::\n");

scanf("%d",&p);

b[pos]=p; n+

+;

printf("\n The list after insertion::\n");

display();

voiddisplay()

printf("\n The Elements of The list ADT are:"); for(i=0;i<n;i++)

printf("\n\n%d", b[i]);

}
}
OUTPUT

RESULT: Thus, the program for implementation of list ADT is executed and its output is verified.
IMPLEMENTATION OF STACK USING LINKED LIST
AIM:
To write a C program to implement stack using array.
ALGORITHM:
1. Start the program
2. According to switch case the operations are selected.
3. In case 1, data are added using PUSH operation.
4. In case 2, data are deleted from stack using POP operation.
5. In case 3, elements of array in stack are displayed using display Function.
6. End the program.

PROGRAM:( STACK ADT USING LINKEDLIST)

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct node

int data;

struct node *next;

}*top,*new1,*first;
void main()

int wish,opt;

void create(),push(),pop(),view();

do

clrscr();

printf("Stack using linked list menu"); printf("\n1.Create\n2.Push\n3.Pop\n4.View\

n5.Exit\n");

printf("\nEnter your option(1,2,3,4,5):");

scanf("%d",&wish);

switch(wish)

case 1: create(); break; case 2:

push(); break; case 3: pop();


break;

case 4: view(); break; case 5:

exit(0);

printf("\nDo you wnat to continue(0/1):");

scanf("%d",&opt);

}while(opt==1);

voidcreate()

intch;

top=(struct node*)malloc(sizeof(struct node));

top->next=NULL;

do

clrscr();

printf("Enter the data:\n");

scanf("%d",&top->data);

printf("Do you want to insert another(1/0)\n");

scanf("%d",&ch);

if(ch==1)

new1=(struct node*)malloc(sizeof(struct node));

new1->next=top;

top=new1;

first=top;
}

else

break;
}while(ch==1);

void push()

top=first;

new1=(struct node*)malloc(sizeof(struct node));

printf("Enter the element to be pushed:");

scanf("%d",&new1->data); new1-

>next=top; top=new1;

first=top;

void pop()

clrscr(); top=first;

if(top==NULL)

printf("\n Stack is empty"); else

{
printf("\nThe element popped out from stack is %d",top->data); top=top->next;
first=top;

}}

void view()

printf("\nStack contents\n");

while(top->next!=NULL)

printf("%d->",top->data);

top=top->next;}

printf("%d\n",top->data);

getch();
}
OUTPUT

RESULT: Thus, the program for implementation of stack is executed and its output is verified.
IMPLEMENTATION OF QUEUE ADTS USING LINKED LIST
AIM:
To write a C program for the Queue Implementation.
ALGORITHM:
1. Start the program.
2. Declare the necessary variables.
3. Use a do while loop and switch statement to call the functions.
4. Insertion:
a) Step 1: Allocate the space for the new node PTR
b) Step 2: SET PTR -> DATA = VAL
c) Step 3: IF FRONT = NULL SET FRONT = REAR = PTR SET FRONT -> NEXT = REAR -> NEXT = NULL ELSE
SET REAR -> NEXT = PTR SET REAR = PTR SET REAR -> NEXT = NULL [END OF IF]
d) Step
4: END
5. Delete: a) Step 1: IF FRONT = NULL Write " Underflow " Go to Step 5 [END OF IF]
b) Step 2: SET PTR = FRONT
c) Step 3: SET FRONT = FRONT -> NEXT
d) Step 4: FREE PTR e) Step
5: END
6. Display:
7. Stop the program

PROGRAM:( QUEUE ADT USING LINKEDLIST)


#include<stdio.h>

#include<conio.h>

struct node

int info;

struct node *link;

}*front = NULL, *rear = NULL;

void insert();

void delet();

void display();

nt item;

void main()

{
int ch; do

printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");

printf("\nEnter your choice: ");

scanf("%d", &ch);

switch(ch)

case1:

insert();

break;

case2:

delet();

break;

case3:

display();

break;

case4:

exit(0);

default:

printf("\n\nInvalid choice. Please try again...\n");

} while(1);

getch();

void insert()

printf("\n\nEnter ITEM: ");

scanf("%d", &item);

if(rear == NULL)

rear = (struct node *)malloc(sizeof(struct node));

rear->info = item;

rear->link = NULL;
front = rear;

else{

rear->link = (struct node *)malloc(sizeof(structnode)); rear =rear->link;

rear->info = item; rear-

>link =NULL;

}}

void delet(){ struct node

*ptr; if(front == NULL)

printf("\n\nQueue is empty.\n"); else{

ptr = front;

item = front->info; front =

front->link; free(ptr);

printf("\nItem deleted: %d\n", item); if(front ==

NULL)

rear = NULL;

}
}

void display()

struct node *ptr = front; if(rear == NULL)

printf("\n\nQueue is empty.\n"); else

printf("\n\n");

while(ptr != NULL)

printf("%d\t",ptr->info); ptr = ptr->link;

}
}
}
OUTPUT

RESULT: Thus, the program for simulation of queue using linked list implementation is executed and its output is verified.
POLYNOMIAL MANIPULATION USING LINKED LIST
AIM:
To write a ‘C’ PROGRAM to represent a polynomial as a linked list and perform the addition of polynomial.
ALGORITHM:
1. Create Link polynomials
2. Read the coefficients and exponent values of polynomials.
3. Set the pointers P1 and P2 to both polynomials respectively to traverse them.
4. Start from first node and compare the exponent of two polynomials i)
If ( P1 -- exp = = P2 -- exp ) P3- coeff = P1 – coeff + P2 --- coeff P3 --- exp = P1 – exp
ii) If ( P1 -- exp < P2 -- exp ) P3 --- exp = P2 – exp; P3- -- coeff = P2 --- coeff Move q to next node
iii) Else P3 --- exp = P1 – exp: P3- -- coeff = P1 --- coeff iv) Move p to next node
5. Append the remaining nodes of either of the polynomials to the resultant linked list.
6. End
PROGRAM (IMPLEMENTATION OF POLYNOMIAL ADT)

#include<stdio.h>

#include<conio.h>
main()
{
int a[10], b[10], c[10],m,n,k,k1,i,j,x;
clrscr();
printf("\n\tPolynomial Addition\n"); printf("\
t===================\n");
printf("\n\tEnter the no. of terms of the polynomial:");
scanf("%d", &m);
printf("\n\tEnter the degrees andcoefficients:");
for(i=0;i<2*m;i++)
scanf("%d",&a[i]);

printf("\n\tFirst polynomial is:");

k1=0;
if(a[k1+1]==1)
printf("x^%d", a[k1]); else
printf("%dx^%d", a[k1+1],a[k1]); k1+=2;

while (k1<i)
{
printf("+%dx^%d", a[k1+1],a[k1]);
k1+=2;
}
printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:");
scanf("%d", &n);
printf("\n\tEnter the degrees and co-efficients:");
for(j=0;j<2*n;j++)
scanf("%d", &b[j]);
printf("\n\tSecond polynomial is:");
k1=0;
if(b[k1+1]==1)
printf("x^%d", b[k1]);
else
printf("%dx^%d",b[k1+1],b[k1]);
k1+=2;
while (k1<2*n)
{
printf("+%dx^%d", b[k1+1],b[k1]);

k1+=2;
} i=0;
j=0;
k=0;
while (m>0 && n>0)
{
if (a[i]==b[j])
{ c[k+1]=a[i+1]+b[j+1];
c[k]=a[i]; m-;
n--;
i+=2;
j+=2;
}
else if (a[i]>b[j])
{ c[k+1]=a[i+1];
c[k]=a[i]; m-;
i+=2;
}
else
{ c[k+1]=b[j+1];
c[k]=b[j]; n--;
j+=2;
}
k+=2;
}
while (m>0)
{ c[k+1]=a[i+1];
c[k]=a[i];

k+=2;
i+=2;
m--;
}
while (n>0)
{ c[k+1]=b[j+1];
c[k]=b[j];
k+=2; j+=2;
n--;
}
printf("\n\n\n\n\tSum of the two polynomials is:"); k1=0;
if (c[k1+1]==1)
printf("x^%d", c[k1]); else
printf("%dx^%d", c[k1+1],c[k1]); k1+=2;

while (k1<k)
{
if (c[k1+1]==1)
printf("+x^%d", c[k1]); else
printf("+%dx^%d", c[k1+1], c[k1]); k1+=2;
}
getch();
return 0;
}

OUTPUT

RESULT: Thus, the program for implementation of polynomial addition is executed and its output is verified.
CONVERSION FROM INFIX TO POST EXPRESSION
AIM:
To write a ‘C’ PROGRAM to implement the conversion from infix to postfix expression using stack.
ALGORITHM:
Step 1: Read the expression from left to right.
Step 2: If the input symbol red is ‘(‘then push it on to the stack.
Step 3: If the input symbol read is an operand, then place it in the postfix expression.
Step 4: If the input symbol read is an operator, then, Check if precedence (stack operator) >= precedence (input
operator) If so, remove all elements from stack and place it in postfix Otherwise, push the operator being read onto
the stack.
Step 5: If the input symbol read is a closing parenthesis ‘)’ then, pop all the elements from the stack, place them in
postfix expression till ‘(’ is not popped.
Step 6: Finally print the postfix expression.
PROGRAM: (CONVERSION OF INFIX EXPRESSION TO POSTFIX)
#include<stdio.h>

char stack[20];

int top = -1;

void push(charx)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return0;
if(x == '+' || x == '-') return1;
if(x == '*' || x == '/')
return 2;
}
main()
{
char exp[20]; char
*e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(') push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}

else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}
OUTPUT

RESULT:
Thus, the program for implementation of conversion from infix to postfix expression is executed and its output is
verified
BINARY TREE
AIM:
To write a ‘C’ PROGRAM to implement the binary tree.
ALGORITHM:
1. Start the program.
2. Get the root node and initialized the pointer.
3. Get the Values of the child nodes.
4. Call the function “Create” to construct tree.
5. According to the switch statement the Traversals was selected.
6. PROCEDURE – CREATE:
a) Use IF loop to check whether the mode is Null.
b) Create a Node using malloc function.
c) Store the value in the Node function.
d) Make the right and left Pointers to Null.
e) Else if (n>node-> data).
f) Node->right = Create (node->right, n);
g) Else if (n< node-> data)
h) Node -> left = Create (node-> left, n); i) Return the node value to main program.
7. End the process.

PROGRAM: (IMPLEMENTATION BINARY TREE AND OPERATIONS OF BINARYTREES)

#include<stdio.h>

#include<stdlib.h>

struct tree {
int data;
struct tree *left; struct
tree *right;
} *root = NULL, *node = NULL, *temp = NULL;

struct tree* insert(int key,struct tree *leaf)

if(leaf == 0)

{
struct tree *temp;
temp = (struct tree *)malloc(sizeof(struct tree));
temp->data = key;
temp->left = 0;
temp->right = 0;
printf("Data inserted!\n");
return temp;
}
else {
if(key < leaf->data)
leaf->left = insert(key,leaf->left);
else
leaf->right = insert(key,leaf->right);
}
return leaf;
}

struct tree* search(int key,struct tree *leaf)

if(leaf != NULL)

{
if(key == leaf->data)
{
printf("Data found!\n");
return leaf;
}
else {
if(key < leaf->data)
return search(key,leaf->left);
else
return search(key,leaf->right);
}
}

else {

printf("Data not found!\n");


return NULL;

}}

struct tree* minvalue(struct tree


*node) { if(node ==
NULL)
return NULL;

if(node->left)
return minvalue(node->left);
e
l
s
e
return
node
void inorder(struct tree *leaf) {

if(leaf == NULL)
return; preorder(leaf-
>left); printf("%d\n",leaf->data);
preorder(leaf->right);
}

void postorder(struct tree *leaf) { if(leaf == NULL)


return; preorder(leaf->left); preorder(leaf->right); printf("%d\
n",leaf->data);
}

struct tree* delete(struct tree *leaf, int key) {

if(leaf == NULL)
printf("Element Not Found!\n");
else
if(key <leaf->data)
leaf->left = delete(leaf->left, key); else if(key >leaf->data)
leaf->right = delete(leaf->right, key);
else {
if(leaf->right && leaf->left) {

temp = minvalue(leaf->right);

leaf->data = temp->data;

leaf->right = delete(leaf->right,temp->data);

}
else {

temp = leaf;
if(leaf->left == NULL) leaf = leaf-
>right;
else if(leaf->right == NULL) leaf = leaf-
>left;
free(temp);
} printf("Data delete successfully!\n");
}
}

int main() {
int key, choice;
while(choice != 7) {
printf("1. Insert\n2. Search\n3. Delete\n4. Display\n5. Min Value\n6.
Max Value\n7. Exit\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\nEnter the value to insert:\n");
scanf("%d", &key);
root = insert(key, root);
break;
case 2:
printf("\nEnter the value to search:\n");
scanf("%d", &key);
search(key,root);
break;
case 3:
printf("\nEnter the value to delete:\n");
scanf("%d", &key);
delete(root,key); break;
case 4:
printf("Preorder:\n");
preorder(root);
printf("Inorder:\n");
inorder(root);
printf("Postorder:\n");
postorder(root);
break;
case 5:
if(minvalue(root) == NULL)
printf("Tree is empty!\n");
Else
printf("Minimum value is %d\n", minvalue(root)->data);
break;
case 6:
if(maxvalue(root) == NULL) printf("Tree is
empty!\n");
else
printf("Maximum value is %d\n", maxvalue(root)->data);

break;
case 7:
printf("Bye Bye!\n"); exit(0);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
OUTPUT

RESULT: Thus, the Program for implementation of binary tree is executed and its output is verified.
BINARY SEARCH TREE
AIM:
To write a ‘C’ PROGRAM to implement the binary Search tree.
ALGORITHM:
1. Start the program.
2. Get the root node and initialized the pointer.
3. Get the Values of the child nodes.
4. Call the function “Create” to construct tree.
5. According to the switch statement the Traversals was selected.
6. PROCEDURE – CREATE:
a) Use IF loop to check whether the mode is Null.
b) Create a Node using malloc function.
c) Store the value in the Node function.
d) Make the right and left Pointers to Null.
e) Else if (n>node-> data).
f) Node->right = Create (node->right, n);
g) Else if (n< node-> data)
h) Node -> left = Create (node-> left, n); i) Return the node value to main program.
7. End the process.
PROGRAM: (IMPLEMENTATION OF BINARY SEARCHTREE)

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>

struct tree

int data;

struct tree *lchild;

struct tree *rchild;


}*t,*temp;

int element;

void inorder(struct tree *);

void preorder(struct tree *);

void postorder(struct tree *);


struct tree * create(struct tree *,int);
struct tree * find(struct tree *, int);
struct tree * insert(struct tree *,int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
struct tree * findmax(struct tree *);
voidmain()
{

int ch;

do

printf("\n\t\t\tBINARY SEARCH TREE"); printf("\n\t\t\


t************ ****");

printf("\nMain Menu\n");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");

printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");

printf("\nEnter ur choice :");


scanf("%d",&ch);

switch(ch)
{

case 1:

printf("\nEnter the data:");


scanf("%d",&element);

t=create(t,element);

inorder(t);

break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=insert(t,element);
inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=del(t,element);
inorder(t);
break;
case 4:
printf("\nEnter the data:");
scanf("%d",&element);
temp=find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=findmin(t);
printf("\nMax element=%d",temp->data);
break;
case 6:
temp=findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
inorder(t);
break;
case 8:
preorder(t);
break;
case 9:

postorder(t);
break;
case 10:
exit(0);

}
}
while(ch<=10);
}
struct tree * create(struct tree *t, int element)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL; t-
>rchild=NULL;
return t;
}
struct tree * find(struct tree *t, int element)
{
if(t==NULL)
return NULL;
if(element<t->data)
return(find(t->lchild,element));
else

if(element>t->data)
return(find(t->rchild,element));
else

return t;
}
struct tree *findmin(struct tree *t)
{
if(t==NULL)
return NULL;
else

if(t->lchild==NULL)
return t;
else

} return(findmin(t->lchild));

struct tree *findmax(struct tree *t)


{
if(t!=NULL)

{
while(t->rchild!=NULL) t=t->rchild;
}
return t;
}
struct tree *insert(struct tree *t,int element)
{
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree)); t->data=element;
t->lchild=NULL;
t->rchild=NULL;
return t;

}
else
{ if(element<t->data)
{
t->lchild=insert(t->lchild,element);
}
else
if(element>t->data)
{
t->rchild
} =insert(t->rchild,element);
else
if(element==t->data)
}
struct tree * del(struct tree *t, int element)
{
if(t==NULL)
printf("element not found\n");
else

if(element<t->data)
t->lchild=del(t->lchild,element);
else
if(element>t->data)
t->rchild=del(t->rchild,element);
else

if(t->lchild&&t->rchild)
{
}
else
temp=findmin(t->rchild);
t->data=temp->data;
t- >rchild=del(t->rchild,t->data);

temp=t;
if(t->lchild==NULL)
t=t->rchild;
else
if(t->rchild==NULL)
t=t->lchild;
free(temp);
}
return t;
}

void inorder(struct tree *t)


{
if(t==NULL)
return;
else
{
}}

inorder(t->lchild);
printf("\t%d",t->data);
inorder(t->rchild);

void preorder(struct tree *t)


{
if(t==NULL)
return;
else
{

printf("\t%d",t->data);
preorder(t->lchild);
preorder(t->rchild);
}
}

void postorder(struct tree *t)


{
if(t==NULL)
return;
else
{

postorder(t->lchild);
postorder(t->rchild);
printf("\t%d",t->data);
}
}
OUTPUT:
RESULT: Thus, the Program for implementation of binary search tree is executed and its output is verified.
AVL TREE

AIM:

To write a ‘C’ PROGRAM to implement the insertion in AVL tree.

ALGORTHIM:

1. Start the program

2. Get the variables to declare.

3. Get the position of the height through left and as well as right.

4. If the height doesn’t match with the balance factor make a single rotation. Through right side.

5. Do the same step for left side too.

6. If the balance factor doesn’t match the height means go for double rotation in left as well as right.

7. Display the AVL tree.

8. Stop the program.

PROGRAM: (IMPLEMENTATION OF AVL TREE)

#include<stdio.h>#include<malloc

.h>

typedef enum { FALSE ,TRUE } bool;

struct node

int info;

intbalance;

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;

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(structnode));

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 ofelse*/

*ht_inc =FALSE;

}/*End of switch */

}/*End of if*/

}/*End of if*/
return(pptr);

}/*End of insert()*/

display(struct node *ptr,intlevel)

{
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

RESULT: Thus, the Program for implementation of insertion in AVL tree is executed and its output is verified.
PRIORITY QUEUE USING HEAPS
AIM:
To write a ‘C’ PROGRAM to implement the priority queue using heaps.
ALGORTHIM:
1. Start the program
2. Declare the variables
3. Check the condition of for qfull() and we can insert the elements if(rear==SIZE-1) we can insert the element
4. Check the condition for qempty and delete the elements in the queue if(front==-1)||(front>rear)) we can delete the
element.
5. Check the condition for qempty and display the elements in the queue if(front==-1)||(front>rear)) we can dispaly
the element.
6. Stop the program
PROGRAM: (IMPLEMENTATION OF PRIORITY QUEUE USINGHEAPS)

#include<stdio.h>
#include<conio.h>

#include <stdlib.h>

enum{FALSE=0,TRUE=-1};
struct Node
{
struct Node *Previous;
int Data;
struct Node *Next;
}Current;
struct Node *head;
struct Node *ptr;
static int NumOfNodes;
int PriorityQueue(void);
int Maximum(void);
int Minimum(void);
void Insert(int);
int Delete(int);
void Display(void);
int Search (int);
void main()
{
int choice;
int DT;
PriorityQueue();
while(1)
{
printf("\nEnter ur Choice:");
printf("\n1.Insert\n2.Display\n3.Delete\n4.Search\n5.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter a data to enter Queue");
scanf("%d",&DT);
Insert(DT);
break;

case 2:
Display();
break;
case 3:

{
int choice,DataDel; printf("\nEnter ur choice:");
printf("\n1.Maximum Priority queue\n2.Minimum priority Queue\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
DataDel=Maximum();
Delete(DataDel);
printf("\n%d is deleted\n",DataDel);
break;
case 2:
DataDel=Minimum(); Delete(DataDel);
printf("\n%d is deleted\n",DataDel);
break;
default:
printf("\nSorry Not a correct Choice\n");
}
}
break;
case 4:
printf("\nEnter a data to Search in Queue:");
scanf("%d",&DT);
if(Search(DT)!=FALSE)
printf("\n %d is present in queue",DT);
else
printf("\n%d is not present in queue",DT);
break;
case 5:
exit(0);
default:
printf("\nCannot process ur choice\n");
} }}
int PriorityQueue(void)
{
Current.Previous=NULL;

printf("\nEnter first element of Queue:");


scanf("%d",&Current.Data); Current.Next=NULL;
head=&Current;
ptr=head;
NumOfNodes++;
return;
}
int Maximum(void)
{
int Temp; ptr=head;
Temp=ptr->Data;

while(ptr->Next!=NULL)
{
if(ptr->Data>Temp)
Temp=ptr->Data;
ptr=ptr->Next;
}
if(ptr->Next==NULL && ptr->Data>Temp)
Temp=ptr->Data;
return(Temp);
}
intMinimum(void)
{
int Temp; ptr=head;
Temp=ptr->Data;
while(ptr->Next!=NULL)

{
if(ptr->Data<Temp)
Temp=ptr->Data;
ptr=ptr->Next;
}
if(ptr->Next==NULL && ptr->Data<Temp)
Temp=ptr->Data;
return(Temp);
}
void Insert(int DT)
{

struct Node *newnode;


newnode=(struct Node *)malloc(sizeof(struct Node));
newnode->Next=NULL;
newnode->Data=DT;
while(ptr->Next!=NULL)
ptr=ptr->Next;
if(ptr->Next==NULL)
{
newnode->Next=ptr->Next;
ptr->Next=newnode;
}
NumOfNodes++;
}

int Delete(int DataDel)


{
struct Node *mynode,*temp;
ptr=head;
if(ptr->Data==DataDel)
{
temp=ptr; ptr=ptr->Next;
ptr->Previous=NULL;
head=ptr;
NumOfNodes--;
return(TRUE);
}
else

{
while(ptr->Next->Next!=NULL)
{
if(ptr->Next->Data==DataDel)
{
mynode=ptr;
temp=ptr->Next;
mynode->Next=mynode->Next->Next;
mynode->Next->Previous=ptr; free(temp);
NumOfNodes--;
return(TRUE);
}

ptr=ptr->Next;
}
if(ptr->Next->Next==NULL && ptr->Next->Data==DataDel)
{
temp=ptr->Next; free(temp);
ptr->Next=NULL;
NumOfNodes--;
` return(TRUE);
}
}

return(FALSE);
}
int Search(int DataSearch)
{
ptr=head;
while(ptr->Next!=NULL)
{

if(ptr->Data==DataSearch)
return ptr->Data;
ptr=ptr->Next;
}

if(ptr->Next==NULL && ptr->Data==DataSearch)


return ptr->Data;
return(FALSE);

}
voidDisplay(void)
{
ptr=head;
printf("\nPriority Queue is as Follows:-\n"); while(ptr!
=NULL)
{
printf("\t\t%d",ptr->Data); ptr=ptr->Next;
}
}
OUTPUT

RESULT: Thus, the Program for implementation of priority queue using heaps is executed and its output is verified.
IMPLEMENTATION OF BINARY SEARCH
AIM:
To write a ‘C’ program to implement binary search.
ALGORITHM:
Step 1: Start the program
Step 2: Define function for Binary Search as
a) Sort the array in ascending order
b) Let lb=0 and ub=n-1
c) Read the data to be searched ‘X’
d) Find the mid position of the given array Mid=(lb+ub)/2
e) Compare X with a[mid] If equal then Goto step (g)
Else If X less than a[mid] then ub=mid-1
If X greater than a[mid] then lb=mid+1 f)
If lb<=ub Repeat steps (d) and (e) for the sub array lb to sub Else Goto step (g)
g) If(lb>ub) Print “Search Success” Else Print “Search Failed” h) Return
Step3: Stop the program.
PROGRAM:

#include <stdio.h>
void sequential_search(int array[], int size, int n)
{
int i;
for (i = 0; i < size; i++)
{
if (array[i] == n)
{
printf("%d found at location %d.\n", n, i+1);
break;
}
}
if (i ==size)
printf("Not found! %d is not present in the list.\n", n);
}

void binary_search(int array[], int size, int n)


{
int i, first, last, middle; first = 0;
last = size - 1;
middle = (first+last) / 2;
while (first <= last) {
if (array[middle] < n) first = middle + 1;
else if (array[middle] == n)
{
printf("%d found at location %d.\n", n, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last) / 2;


}
if ( first > last )
printf("Not found! %d is not present in the list.\n", n);

int main()
{
int a[200], i, j, n, size;

printf("Enter the size of the list:"); scanf("%d", &size);


printf("Enter %d Integers in ascending order\n", size);
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
printf("Enter value to find\n");
scanf("%d", &n);
printf("Sequential search\n");
sequential_search(a, size, n);
printf("Binary search\n");
binary_search(a, size, n);
return0;
}

OUTPUT
RESULT: Thus, the C program to implement Binary Search was executed successfully and the output was verified.

MERGE SORT
AIM:
To write a ‘C’ program to implement merge sort.
ALGORITHM:
Step 1: Read the elements into the array
Step 2: Take the second element. Compare it with the first element. If the second element less than the first element
interchange them
Step 3: Take the third element compare it first and second element and insert it in the correct position by shifting the
elements in the array. So that the first, second and third elements are in sorted array
Step 4: In general take the ith element and compare it with the all the elements before it and place it in the proper
position by shifting the elements one position right.
Step 5: When the ith element is placed, the elements in the array from the 0th to the ith position will be in the sorted
order
Step 6: The above process is continued for all the elements in the array.
Step 7: Stop.
PROGRAM: (MERGE SORT)

#include<stdio.h>

#include<conio.h>

void merge(int [],int ,int ,int );

void part(int [],int ,int );

void main()

int arr[30];

int i,size;

printf("\n\t------- Mergesortingmethod------------------------------------\n\n");

printf("Enter total no. of elements : ");

scanf("%d",&size);

for(i=0; i<size; i++){ printf("Enter %d element : ",i+1);

scanf("%d",&arr[i]);

part(arr,0,size-1);

printf("\n\t------- Mergesortedelements--------------------------------------\n\n");

for(i=0; i<size; i++)

printf("%d ",arr[i]);

getch();

}
void part(int arr[],int min,intmax)

intmid;

if(min<max){ mid=(min+max)/2;

part(arr,min,mid);

part(arr,mid+1,max);

merge(arr,min,mid,max);

}}

void merge(int arr[],int min,int mid,int max)

int tmp[30];

int i,j,k,m;

j=min;

m=mid+1;

for(i=min; j<=mid && m<=max ; i++)

if(arr[j]<=arr[m])

tmp[i]=arr[j]; j++;}

else

{ tmp[i]=arr[m];

m++;

}}

if(j>mid)

for(k=m; k<=max; k++)

{ tmp[i]=arr[k];

i++;

}
}

Else
{
for(k=j; k<=mid; k++)

tmp[i]=arr[k];

i++;

}
}

for(k=min; k<=max; k++)

arr[k]=tmp[k];

OUTPUT

RESULT:
Thus, the C program to implement Merge Sort was executed successfully and the output was verified.
INSERTION SORT
AIM:
To write a ‘C’ program to implement insertion sort.
ALGORITHM:
Step 1: Read the elements into the array
Step 2: Take the second element. Compare it with the first element. If the second element less than the first element
interchange them
Step 3: Take the third element compare it first and second element and insert it in the correct position by shifting the
elements in the array. So that the first, second and third elements are in sorted array
Step 4: In general, take the ith element and compare it with the all the elements before it and place it in the proper position
by shifting the elements one position right.
Step 5: When the ith element is placed, the elements in the array from the 0th to the ith position will be in the sorted order
Step 6: The above process is continued for all the elements in the array.
Step 7: Stop.
PROGRAM: (INSERTION SORT)

#include <stdio.h>

#define MAX 100

int main()
{
int arr[MAX],limit;
int i,j,temp;

printf("Enter total number of elements: ");


scanf("%d",&limit);
/*Read array*/
printf("Enter array elements: \n");
for(i=0; i<limit; i++)
{
printf("Enter element %3d: ",i+1);
scanf("%d",&arr[i]);
}

/*sort elements in Ascending Order*/


for(i=1; i<(limit); i++)
{
j=i;
while(j>0 && arr[j]<arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;

j--;
}
}
printf("Array elements in Ascending Order:\n");
for(i=0; i<limit; i++)
printf("%d ",arr[i]);
printf("\n");

/*sort elements in Descending Order*/


for(i=1; i<(limit); i++)
{
j=i;
while(j>0 && arr[j]>arr[j-1])
{
temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;

j--;
}
}

printf("Array elements in Descending Order:\n");


for(i=0; i<limit; i++)
printf("%d ",arr[i]);
printf("\n");

return 0;
}
OUTPUT

Enter total number of elements: 10


Enter array elements:
Enter element 1: 40
Enter element 2: 10
Enter element 3: 100
Enter element 4: 20
Enter element 5: 90
Enter element 6: 60
Enter element 7: 80
Enter element 8: 70
Enter element 9: 30
Enter element10: 50
Array elements in Ascending Order:
10 20 30 40 50 60 70 80 90 100

RESULT: Thus, the C program to implement Insertion Sort was executed successfully and the output was verified.
SELECTION SORT
AIM:
To write a program to implement Selection Sort.
ALGORITHM:
Step1 : Start the program.
Step2 : Set MIN to location 0
Step3 : Search the minimum element in the list
Step4 : Swap with value at location MIN
Step5 : Increment MIN to point to next element
Step6 : Repeat until list is sorted
Step7 : Stop the program.
PROGRAM: (SELECTION SORT)

#include <stdio.h>

#define MAX 100

int main()
{
int arr[MAX],limit;
int i,j,temp,position;

printf("Enter total number of elements: ");


scanf("%d",&limit);

/*Read array*/
printf("Enter array elements: \n");
for(i=0; i<limit; i++)
{
printf("Enter element %3d: ",i+1);
scanf("%d",&arr[i]);
}

/*sort elements in Ascending Order*/


for(i=0; i<(limit); i++)
{
position=i;
for(j=i+1; j<limit; j++)
{

if(arr[position]>arr[j])
{
position=j;
}
if(position!=i)
{
temp=arr[i];
arr[i]=arr[position];
arr[position]=temp;
}
}

printf("Array elements in Ascending Order:\n");


for(i=0; i<limit; i++)
printf("%d ",arr[i]);
printf("\n");
/*sort elements in Descending Order*/
for(i=0; i<(limit); i++)
{
position=i;
for(j=i+1; j<limit; j++)
{
if(arr[position]<arr[j])
{
position=j;
}
if(position!=i)
{
temp=arr[i];
arr[i]=arr[position];
arr[position]=temp;
}
}
} printf("Array elements in Descending Order:\n");
for(i=0; i<limit; i++)
printf("%d ",arr[i])
printf("\n");
return 0;
}

OUTPUT

Enter total number of elements: 5


Enter array elements:
Enter element 1: 11
Enter element 2: 2
Enter element 3: 1
Enter element 4: 223
Enter element 5: 4
Array elements in Ascending Order:
1 2 4 11 223
Array elements in Descending Order:
223 11 4 2 1

RESULT: Thus, the program to implement Selection Sort was executed successfully and the output was verified.

OPEN ADDRESSING LINEAR PROBING


AIM:
To write a ‘C’ PROGRAM to implement the linear probing hashing technique.
ALGORTHIM:
1. start the program
2. Declare the variables
3. Check the condition for loop for linear probing and set FLAG as 0 and COUNT as 0
4. By increment of count we can insert the element in the hash table.
5. check the condition for full and display the elements in the hash table if(count==MAX), by help of for loop we can
display the elements.
6. stop the program

PROGRAM: (IMPLEMENTATION OF OPEN ADDRESSING USING LINEAR AND QUADRATIC


PROBING)
#include <stdio.h>
#include <conio.h>
int tsize;
int hasht(int key)
{
int i ;
i = key%tsize ;
return i;
}
//-------LINEAR PROBING-------
int rehashl(int key)
{
int i ;
i = (key+1)%tsize ;
return i ;
}

//-------QUADRATIC PROBING-------
int rehashq(int key, int j)
{
int i ;
i = (key+(j*j))%tsize ;
return i ;
}
void main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
clrscr() ;
printf ("Enter the size of the hash table: ");
scanf ("%d",&tsize);

printf ("\nEnter the number of elements: ");


scanf ("%d",&n);

for (i=0;i<tsize;i++)
hash[i]=-1 ;

printf ("Enter Elements: ");


for (i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

do
{
printf("\n\n1.Linear Probing\n2.Quadratic Probing \n3.Exit \nEnter your option: ");
scanf("%d",&op);
switch(op)
{
case 1:
for (i=0;i<tsize;i++)
hash[i]=-1 ;

for(k=0;k<n;k++)
{
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashl(i);
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;

case 2:
for (i=0;i<tsize;i++)
hash[i]=-1 ;
for(k=0;k<n;k++)
{
j=1;
key=arr[k] ;
i = hasht(key);
while (hash[i]!=-1)
{
i = rehashq(i,j);
j++ ;
}
hash[i]=key ;
}
printf("\nThe elements in the array are: ");
for (i=0;i<tsize;i++)
{
printf("\n Element at position %d: %d",i,hash[i]);
}
break ;

}
}while(op!=3);

getch() ;
}

OUTPUT

Enter the size of the hash table: 10

Enter the number of elements: 8


Enter Elements: 72 27 36 24 63 81 92 101
1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 1

The elements in the array are:


Element at position 0: -1
Element at position 1: 81
Element at position 2: 72
Element at position 3: 63
Element at position 4: 24
Element at position 5: 92
Element at position 6: 36
Element at position 7: 27
Element at position 8: 101
Element at position 9: -1

1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 2

The elements in the array are:


Element at position 0: -1
Element at position 1: 81
Element at position 2: 72
Element at position 3: 63
Element at position 4: 24
Element at position 5: 101
Element at position 6: 36
Element at position 7: 27
Element at position 8: 92
Element at position 9: -1

1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 3
*/

RESULT: Thus, the Program for implementation of linear probing hashing technique is executed and its output is
verified.

DIJKSTRA’S ALGORITHM
AIM:
To write a ‘C’ PROGRAM to implement the Dijkstra’s algorithm.
ALGORITHM
Step1: Start the program.
Step2: Read the number of vertices
Step3: Read the weight of every pair of vertices.
Step4: Get the source vertex & destination.
Step5: Construct the graph.
Step6: Start finding the start node to all other neighboring nodes.
Step7: Nearest path is selected using array until the end node is reached.
Step8: Print the shortest path.
Step9: Terminate the program.
PROGRAM: (IMPLEMENTATION OF DIJKSTRA’S ALGORITHM)

#include <stdio.h>
#include <limits.h>
#define V 9

int minDistance(int dist[], int sptSet[])


{
// Initialize min value
int min = INT_MAX, min_index;
int v;
for (v = 0; v < V; v++)
if (sptSet[v] == 0 && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}

void printSolution(int dist[], int n) {


printf("Vertex Distance from Source\n");
int i;
for (i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}

void dijkstra(int graph[V][V], int src) {


int dist[V]; // The output array. dist[i] will hold the shortest

int sptSet[V]; // sptSet[i] will 1 if vertex i is included in shortest


// path tree or shortest distance from src to i is finalized

// Initialize all distances as INFINITE and stpSet[] as 0


int i, count, v;
for (i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = 0;

dist[src] = 0;
for (count = 0; count < V - 1; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (v = 0; v < V; v++)

if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u]


+ graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist, V);
}

int main() {
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 0, 10, 0, 2, 0, 0},
{0, 0, 0, 14, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};

dijkstra(graph, 0);

return 0;
}
OUTPUT:
$ gcc Dijkstra.c
$ ./a.out

Vertex Distance from Source


0 0
1 4
2 12
3 19
4 21
5 11
6 9
7 8
8 14

RESULT: Thus, the Program for implementation of Dijkstra ‘s algorithm is executed and its output is verified.

PRIM’S ALGORITM
AIM:
To write a ‘C’ PROGRAM to implement the Prim’s algorithm.
ALGORTHIM:
1. Start the program
2. Declare the variables.
3. Enter the edges and its edges in the graph.
4. Construct the graph.
5. Pick one arbitrary vertex and consider it as visited.
6. The unvisited vertices in the new edge are considered.
7. Repeat the step 6 until u cover all the edges.
8. Print the minimum spanning with the corresponding weights.
9. End the program.

PROGRAM: (IMPLEMENTATION OF PRIM’S ALGORITHM)


#include<stdio.h>
#include<stdlib.h>

#define infinity 9999


#define MAX 20

int G[MAX][MAX],spanning[MAX][MAX],n;

int prims();

int main()
{
int i,j,total_cost;
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
total_cost=prims();
printf("\nspanning tree matrix:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",spanning[i][j]);
}
printf("\n\nTotal cost of spanning tree=%d",total_cost);
return 0;
}

int prims()
{
int cost[MAX][MAX];
int u,v,min_distance,distance[MAX],from[MAX];
int visited[MAX],no_of_edges,i,min_cost,j;
//create cost[][] matrix,spanning[][]
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(G[i][j]==0)
cost[i][j]=infinity;
else
cost[i][j]=G[i][j];
spanning[i][j]=0;
}
//initialise visited[],distance[] and from[]
distance[0]=0;
visited[0]=1;
for(i=1;i<n;i++)
{
distance[i]=cost[0][i];
from[i]=0;
visited[i]=0;
}
min_cost=0; //cost of spanning tree
no_of_edges=n-1; //no. of edges to be added
while(no_of_edges>0)
{
//find the vertex at minimum distance from the tree
min_distance=infinity;
for(i=1;i<n;i++)
if(visited[i]==0&&distance[i]<min_distance)
{
v=i;
min_distance=distance[i];
}
u=from[v];
//insert the edge in spanning tree
spanning[u][v]=distance[v];
spanning[v][u]=distance[v];
no_of_edges--;
visited[v]=1;
//updated the distance[] array
for(i=1;i<n;i++)
if(visited[i]==0&&cost[i][v]<distance[i])
{
distance[i]=cost[i][v];
from[i]=v;
}
min_cost=min_cost+cost[u][v];
}
return(min_cost);
}

OUTPUT
Enter no. of vertices:6
Enter the adjacency matrix:
031600
305030
150564
605002
036006
004260

spanning tree matrix:

031000
300030
100004
000002
030000
004200

Total cost of spanning tree=13

RESULT: Thus, the C program to implement Linear Search was executed successfully and the output was verified.

You might also like