You are on page 1of 66

R.M.D.

ENGINEERING COLLEGE
R.S.M. NAGAR, KAVARAIPETTAI - 601 206.
GUMMIDIPOONDI TALUK, THIRUVALLUR DISTRICT.

Certified that this is a bonafide record work done by TEJESH MALEPATI with Roll /
Register number 111519104302 of III semester Computer Science and Engineering
Department during the academic year 2020-2021.

Subject Code : CS8381


Subject Name : Data Structures Lab

Faculty In-charge Head of the Department

Submitted for Anna University Practical examination held on 30.07.2021


LIST OF EXPERIMENTS
Page
Exp. No. Date Experiment Name No.

1.a 17/08/2020 ARRAY IMPLEMENTATION OF STACK ADT 4

1.b 17/08/2020 ARRAY IMPLEMENTATION OF QUEUE ADT 7

2 20/8/2020 ARRAY IMPLEMENTATION OF LIST ADT 10

3(a) 24/08/2020 APPLICATION OF LIST-POLYNOMIAL ADDITION 13

3(b) 03/09/2020 INFIX TO POSTFIX CONVERSION 18

4 07/09/2020 IMPLEMENTATION OF BINARY TREE & Its 21


OPERATIONS

IMPLEMENTATION OF BINARY SEARCH TREE


5 10/09/2020 25

6 14/09/2020 AVL TREE 31

7 28/9/2020 BINARY HEAP 40

8 05/10/2020 GRAPH REPRESENTATION 48

IMPLEMENTATION DFS AND BFS GRAPH


9 05/10/2020 TRAVERSAL 50

LINEAR SEARCH
10a 15/10/2020 55

BINARY SEARCH
10b 15/10/2020 57

BUBBLE SORT
11a 19/10/2020 59

SHELL SORT
11b 24/10/2020 61

3
QUICK SORT
11c 29/10/2020 64

HASHING
12 02/11/2020 67

Ex.No.1a
Date : 17/08/2020

ARRAY IMPLEMENTATION OF STACK ADT Aim:

To write a C program to implement Stack ADT by using arrays

Algorithm:
1. Create a Stack[ ] with MAX size as your wish.
2. Write function for all the basic operations of stack - PUSH(), POP() and
DISPLAY().
3. By using Switch case, select push() operation to insert element in the stack.
• Step 1: Check whether stack is FULL. (top == SIZE-1)
• Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not
possible!!!" and terminate the function.
• Step 3: If it is NOT FULL, then increment top value by one (top++) and
set stack[top] to value (stack[top] = value).
4. Similarly, By using Switch case, select pop() operation to delete element
from the stack.
• Step 1: Check whether stack is EMPTY. (top == -1)
• Step 2: If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
• Step 3: If it is NOT EMPTY, then delete stack[top] and decrement top
value by one (top--).
5. Similarly, By using Switch case, select display() operation to display all
element from the stack.
• Step 1: Check whether stack is EMPTY. (top == -1)
• Step 2: If it is EMPTY, then display "Stack is EMPTY!!!" and terminate
the function.
• Step 3: If it is NOT EMPTY, then define a variable 'i' and initialize with
top. Display stack[i] value and decrement i value by one (i--).
• Step 3: Repeat above step until i value becomes '0'.
6. Close the program #include <stdio.h>

void push(int x); void


pop(); void display();
int s[50],i,size=3,top=-
1;

int main()

4
{

push(10);
push(20);
push(30);
display();
pop();
display();
return 0; }

void push(int x)
{ if(top==size-1)
printf("Stack Full\n");
else
{
top=top+1;
s[top]=x;
}
}

void pop()
{ if(top==-1)
printf("Stack Empty\n");
else
{
printf("popped element : %d \n",s[top]);
top--;
}}

void display() { if(top==1)


printf("Stack Empty\n");
else
{
for(i=top; i>=0; i--)
printf("%d \n",s[i]);

}}

OUTPUT:
Stack Elements are :
30
20
10 popped element
: 30 Stack Elements
are :

5
20
10
Result:

Thus the C program to implement Stack ADT by using array is executed successfully and the

output is verified.

Ex. No: 1b
Date: 17/08/2020
1.(b) ARRAY IMPLEMENTATION OF QUEUE ADT

Aim:
To write a C program to implement Queue ADT by using arrays

Algorithm:
1. Create a Queue[ ] with MAX size as your wish.
2. Write function for all the basic operations of stack - Enqueue(), Dequeue()
and Display().
3. By using Switch case, select Enqueue() operation to insert element in to
the rear/back end of the queue.
• Check whether queue is FULL. (rear == SIZE-1)
• If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!"
and terminate the function.
• If it is NOT FULL, then increment rear value by one (rear++) and set
queue[rear] = value
4. Similarly, by using Switch case, select Dequeue() function is used to
remove the element from the front end of the queue.
• Check whether queue is EMPTY. (front == rear)
• If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not
possible!!!" and terminate the function.
• Step 3: If it is NOT EMPTY, then increment the front value by one (front
++). Then display queue[front] as deleted element. Then check
whether both front and rear are equal (front == rear), if it TRUE, then
set both front and rear to '-1' (front = rear = -1).

6
5. Similarly, by using Switch case, select display() operation to display all
element of the queue.
• Step 1: Check whether queue is EMPTY. (front == rear)
• Step 2: If it is EMPTY, then display "Queue is EMPTY!!!" and terminate
the function.
• Step 3: If it is NOT EMPTY, then define an integer variable 'i' and set 'i =
front+1'.
• Step 3: Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value is equal to rear (i <= rear)
6. Close the program

Program #include

<stdio.h>

void enqueue(int x); void


dequeue(); void display(); int q[50],i,size=3,front=-
1,rear=-1;

int main()
{

enqueue(10);
enqueue(20);
enqueue(30);
display();
dequeue();
display();
return 0;
}

void enqueue(int x)
{ if(rear==size-1)
printf("Queue Full\n");
else
{
if((front==-1) &&(rear==-1))
{
front=0;
rear=0;
} else
rear=rear+1;
q[rear]=x;
}
}

7
void dequeue()
{ if((front==-1)&&(rear==1))
printf("Queue Empty\n");
else
{
if(front==rear)
{
printf("Deleted element :
%d\n",q[front]); front=-1;
rear=-1;
}
else
{
printf("Deleted element : %d\n",q[front]);
front++;
}
}
}

void display()
{ if((front==-1)&&(rear==-1))
printf("Queue Empty\n");
else
{
printf("Queue Elements are :\n");
for(i=front; i<=rear; i++)
printf("%d \n",q[i]);

}
}

OUTPUT:
Queue Elements are :
10
20
30
Deleted element : 10
Queue Elements are :
20
30
Result
Thus the C program to implement Queue ADT by using array is executed
successfully and the output is verified.

8
EX No.2
Date : 20/08/2020
2. ARRAY IMPLEMENTATION OF LIST ADT
Aim:

To perform various operations on List ADT using array implementation.


Algorithm:

1. Start
2. Create a list of n elements
3. Display list operations as a menu
4. Accept user choice
5. If the user wants insert an element in the list
6. Get position of element to be inserted. Increment length of the list.
7. Move elements one position downwards there on Store the new element in corresponding
position.
8. If the user wants to delete an element in the list.
9. Get position of element to be deleted.Move elements one position upwards there on.
Decrement length of the list
10. If the user wants to search an element in list.Traverse the list and inspect each element
Report position if it exists.
11. Stop
Program:

#include<stdio.h> int
a[10]={23,34,56},n=3;

void insert(int p,int x)


{ int i; for(i=n-1;i>=p-1;i--)
a[i+1]=a[i];
a[p-1]=x;
n=n+1;

void delete(int p)
{ int item,i; item=a[p-
1];
printf("he deleted elements is %d",item);
for(i=p-1;i<n-1;i++)
a[i]=a[i+1];
n=n-1;
}

void display()
{ int
i;
printf("The elements in list are:\n");

9
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}

int main()
{ int x,p;
display();
printf("Enter the element & position to be inserted");
scanf("%d%d",&x,&p);
insert(p,x);
display();
printf("Enter the position to be deleted");
scanf("%d",&p); delete(p); display();
return 0;
}
OUTPUT:
The elements in list are:
23
34
56
Enter the element & position to be inserted25
2
The elements in list are:
23
25
34
56
Enter the position to be deleted3 he deleted
elements is 34The elements in list are:
23
25
56

Result

10
Thus various operations was successfully executed on list using array
implementation.

Exno.3(a) POLYNOMIAL ADDITION


Date:24/08/2020

Aim:
To write C Program to implement Polynomial Addition.

Algorithm:

1: Get the two polynomials. First polynomial is P1 and second polynomial is P2

2: For addition of two polynomials if exponents of both the polynomials are same
then we ad the coefficients. For storing the result we will create the third linked
lists say P3.

3: If Exponent of P2 is greater than exponent of P1 then keep the P3 as P2. 4: If


Exponent of P2 is greater than exponent of P1 then keep the P3 as P1

5: If Exponent of P2 is equal to the exponent of P1 then add the coefficient of P1 and


coefficient of P2 as coefficient of P3.
6: Continue the above step from 3 to 5 until end o the two polynomials.

7: If any of the polynomial is ended keep P3 as the remaining polynomial.

8: Stop the execution.

#include<stdio.h>
#include<stdlib.h> struct
node
{ float coef; int
expo; struct
node *link;
};

struct node *create(struct node *); struct


node *insert_s(struct node *,float,int); struct
node *insert(struct node *,float,int); void
display(struct node *ptr); void
poly_add(struct node *,struct node *);

main( )
{
struct node *start1=NULL,*start2=NULL;

11
printf("Enter polynomial 1 :\n");
start1=create(start1);

printf("Enter polynomial 2 :\n");


start2=create(start2);

printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);

poly_add(start1, start2);

}/*End of main()*/

struct node *create(struct node *start)


{ int
i,n,ex;
float co;
printf("Enter the number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter coeficient for term %d : ",i);
scanf("%f",&co);
printf("Enter exponent for term %d : ",i);
scanf("%d",&ex);
start=insert_s(start,co,ex);
}
return start;
}/*End of create()*/
struct node *insert_s(struct node *start,float co,int ex)
{
struct node *ptr,*tmp; tmp=(struct node
*)malloc(sizeof(struct node));
tmp->coef=co; tmp->expo=ex;
/*list empty or exp greater than first one */
if(start==NULL || ex > start->expo)
{
tmp->link=start;
start=tmp;
}
else
{
ptr=start;
while(ptr->link!=NULL && ptr->link->expo >= ex)
ptr=ptr->link; tmp->link=ptr->link;

12
ptr->link=tmp;
}
return start;
}/*End of insert()*/

struct node *insert(struct node *start,float co,int ex)


{
struct node *ptr,*tmp; tmp=(struct node
*)malloc(sizeof(struct node));
tmp->coef=co;
tmp->expo=ex;
/*If list is empty*/
if(start==NULL)
{
tmp->link=start;
start=tmp;
}
else /*Insert at the end of the list*/
{ ptr=start;
while(ptr->link!=NULL)
ptr=ptr->link;
tmp>link=ptr->link; ptr-
>link=tmp;
}
return start;
}/*End of insert()*/
void display(struct node *ptr)
{
if(ptr==NULL)
{
printf("Zero polynomial\n");
return;
}
while(ptr!=NULL)
{
printf("(%.1fx^%d)", ptr->coef,ptr->expo);
ptr=ptr->link;
if(ptr!=NULL)
printf(" + ");
else
printf("\n");
}
}/*End of display()*/
void poly_add(struct node *p1,struct node *p2)
{ struct node
*start3;
start3=NULL;

13
while(p1!=NULL && p2!=NULL)
{
if(p1->expo > p2->expo)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
else if(p2->expo > p1->expo)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
else if(p1->expo==p2->expo)
{
start3=insert(start3,p1->coef+p2->coef,p1->expo);
p1=p1->link; p2=p2->link;
}
}
/*if poly2 has finished and elements left in poly1*/
while(p1!=NULL)
{
start3=insert(start3,p1->coef,p1->expo);
p1=p1->link;
}
/*if poly1 has finished and elements left in poly2*/
while(p2!=NULL)
{
start3=insert(start3,p2->coef,p2->expo);
p2=p2->link;
}
printf("Added polynomial is : ");
display(start3);
}/*End of poly_add() */
Output:
Enter polynomial 1 :
Enter the number of terms : 4
Enter coeficient for term 1 : 3
Enter exponent for term 1 : 3
Enter coeficient for term 2 : 4
Enter exponent for term 2 : 2
Enter coeficient for term 3 : 2
Enter exponent for term 3 : 1
Enter coeficient for term 4 : 4
Enter exponent for term 4 : 0
Enter polynomial 2 :
Enter the number of terms : 3
Enter coeficient for term 1 : 5

14
Enter exponent for term 1 : 3
Enter coeficient for term 2 : 6
Enter exponent for term 2 : 1
Enter coeficient for term 3 : 2
Enter exponent for term 3 : 0
Polynomial 1 is : (3.0x^3) + (4.0x^2) + (2.0x^1) + (4.0x^0)
Polynomial 2 is : (5.0x^3) + (6.0x^1) + (2.0x^0)
Added polynomial is : (8.0x^3) + (4.0x^2) + (8.0x^1) + (6.0x^0)

Result:

Thus C Program for Polynominal addition is implemented Successfully.


Ex.No.3(b) INFIX TO POSTFIX CONVERSION
Date:03/9/2020

Aim:

To write a C program to convert infix expression to postfix expression using stack

Algorithm

1. Start
2. Define a array stack of size max = 20
3. Initialize top = -1
4. Read the infix expression character-by character If character is an operand print it If
character is an operator
Compare the operator’s priority with the stack[top] operator.
If the stack [top] has higher/equal priority than the input operator,
Pop it from the stack and print it.
Else

15
Push the input operator onto the stack
If character is a left parenthesis, then push it onto the stack.
If character is a right parenthesis, pop all operators from stack and print it
until a left parenthesis is encountered. Do not print the parenthesis.
If character = $ then Pop out all operators,
Print them and Stop

.
Program

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int a[100],top=-1;
void push(char);
char pop();

int main()
{ int
i;
char exp[100],c;
puts("Enter expression\n");
scanf("%s",exp);
for(i=0;exp[i]!='\0';i++)
{

if(isalpha(exp[i]))
putchar(exp[i]);
else
{
switch(exp[i])
{ case '*': case
'/': case '(':
push(exp[i]);
break; case '':
case '+':

if(a[top]=='*'||a[top]=='/')
{
putchar(pop());

push(exp[i]); }
else
push(exp[i]);
break; case ')':
while((c=pop())!='(')

16
putchar(c); break;
}
}
}
while(top!=-1)
putchar(pop());
return 0; } void
push(char ele) {
a[++top]=ele; }
char pop()
{ return a[top-
-]; }

OUTPUT:

Enter expression

A*B+C/D-E
AB*CD/E-+
Result:
Thus the C Program for applications of Stack is implemented Successfully.
Exno.4 IMPLEMENTATION OF BINARY TREE & ITS OPERATIONS

DATE:07/09/2020

Aim:

To write a C program to implement binary tree and its operation

Algorithm:
1. Start the program

2. Create a tree

3. Perform the Insertion, Deletion and Search operation

4. Display Preorder, Postorder, Inorder traversal data


5. Stop the program

17
Program:
// C program for different tree traversals
#include <stdio.h>
#include <stdlib.h>

/* A binary tree node has data, pointer to left child


and a pointer to right child */
struct node
{ int data;
struct node* left;
struct node* right;
};

struct node* newNode(int data)


{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data; node-
>left = NULL;
node->right = NULL;

return(node);
}

/* Given a binary tree, print its nodes according to the


"bottom-up" postorder traversal. */
void printPostorder(struct node* node)
{ if (node ==
NULL) return;

// first recur on left subtree printPostorder(node-


>left);

// then recur on right subtree


printPostorder(node->right);

// now deal with the node


printf("%d ", node->data);
}

/* Given a binary tree, print its nodes in inorder*/


void printInorder(struct node* node)
{ if (node ==
NULL)
return;

/* first recur on left child */

18
printInorder(node->left);

/* then print the data of node */


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

/* now recur on right child */ printInorder(node-


>right);
}

/* Given a binary tree, print its nodes in preorder*/ void


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

/* first print data of node */


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

/* then recur on left sutree */


printPreorder(node->left);

/* now recur on right subtree */


printPreorder(node->right);
}

/* Driver program to test above functions*/


int main()
{
struct node *root = newNode(1);
root>left = newNode(2); root-
>right = newNode(3); root->left-
>left = newNode(4); root->left->right
= newNode(5);

printf("\nPreorder traversal of binary tree is \n");


printPreorder(root);

printf("\nInorder traversal of binary tree is \n");


printInorder(root);

printf("\nPostorder traversal of binary tree is \n");


printPostorder(root);

getchar();
return 0;
}
Output:

19
Preorder traversal of binary tree is
12453
Inorder traversal of binary tree is
42513
Postorder traversal of binary tree is
45231

Result:
Thus C Program for Binary Tree is implemented Successfully.

Exno.5 Implementation of Binary Search Tree


Date:10/09/2020

Aim:
To write a C program to implement Binary Search Tree.
Algorithm:

1. Start the program


2. Create a tree

3. Perform the Insertion, Deletion and Search operation

4. Display Preorder, Postorder, Inorder traversal data

20
5. Stop the program
Program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct bst
{ int
data;
struct bst *left,*right;
}node; void insert(node *,node
*); void inorder(node *); node
*search(node *,int,node **); void
del(node *,int);
void main()
{ int choice;
char
ans='N'; int
key;
node *New,*root,*tmp,*parent;
node *get_node();
root=NULL;
clrscr(); printf("\n \t Program for Binary
Search Tree");
do
{
printf("\n1 Create \n2. Search \n3.Delete \n4.Display\n 5.Exit");
printf("\n Enter ur choice :"); scanf("%d",&choice);
switch(choice)
{ case
1: do
{
New=get_node(); printf("\n
Enter the element");
scanf("%d",&New->data);
if(root==NULL) root=New; else
insert(root,New);
printf("\n Do u want to enter moreelements?(Y/N)");
ans=getch();; }while(ans=='Y'); break; case 2:
printf("\n Enter the element which u want
tosearch");
scanf("%d",&key);
tmp=search(root,key,&parent);
printf("\n Parent of the node %d is%d",tmp->data,parent->data);
break; case 3: printf("\n Enter the element u wish to
delete");

21
scanf("%d",&key);
del(root,key); break; case
4: if(root==NULL)
printf("Tree is not
created"); else
{ printf("\n The Tree is
:"); inorder(root);
}
break; case
5: exit(0);
}
}
while(choice!=5);
}
node *get_node()
{
node *temp; temp=(node
*)malloc(sizeof(node)); temp>left=NULL;
temp->right=NULL;
return temp;
}
void insert(node *root,node *New)
{
if(New->data<root->data)
{ if(root-
>left==NULL) root>left=New;
else
insert(root->left,New);
}
if(New->data>root->data)
{ if(root-
>right==NULL) root>right=New;
else
insert(root->right,New);
}
}
node *search(node *root,int key,node **parent)
{
node *temp;
temp=root;
while(temp!=NULL)
{
if(temp->data==key)
{
printf("\n %d Element is present",temp->data);
return temp;
}

22
*parent=temp; if(temp->data>key)
temp=temp->left;
else temp=temp-
>right;
}
return NULL;
}
void del(node *root,int key)
{
node *temp,*parent,*temp_succ;
temp=search(root,key,&parent); if(temp-
>left!=NULL&&temp->right!=NULL)
{
parent=temp; temp_succ=temp-
>right;
while(temp_succ->left!=NULL)
{
parent=temp_succ;
temp_succ=temp_succ->left;
}
temp->data=temp_succ>data;
parent->right=NULL; printf("\n
Now delete it!"); return;
}
if(temp->left!=NULL&&temp->right==NULL)
{ if(parent->left==temp)
parent->left=temp->left; else
parent->right=temp>left;
temp=NULL; free(temp);
printf("\n Now Delete it !");
return;
}
if(temp->left==NULL&&temp->right!=NULL)
{ if(parent->left==temp)
parent->left=temp->right; else
parent->right=temp-
>right; temp=NULL;
free(temp); printf("\n
Now Delete it
!"); return;
}
if(temp->left==NULL&&temp->right==NULL)
{ if(parent->left==temp) parent->left=NULL;
else parent->right=NULL; printf("\n Now
Delete it
!"); return;
}

23
}
void inorder(node *temp)
{
if(temp!=NULL)
{ inorder(temp->left);
printf("\n %d",temp>data);
inorder(temp->right);
}
}
Output
:

Output:
Program for BinarySearch Tree
1. Create
2. Search3. Delete
4. Display
5. Exit
Enter your choice: 1
Enter the Element: 100
Do you want to enter more element?(Y/N) : Y
Enter the Element: 200
Do you want to enter more element?(Y/N) : Y
Enter the Element: 300
Do you want to enter more element?(Y/N) : Y
Enter the Element: 400
Do you want to enter more element?(Y/N) : Y
Enter the Element: 500
Do you want to enter more element?(Y/N) : N
Program for Binary
Search Tree
1. Create
2. Search3. Delete
4. Display
5. Exit
Enter your choice: 2
Enter the element you wish to search: 200
200 Element is present
Parent node of 200 is 100
Program for Binary
Search Tree
1. Create
2. Search
3. Delete
4. Display

24
5. Exit
Enter your choice: 4
The Tree is
100
200
300
400
500
Program for Binary
Search Tree
1. Create
2. Search
3. Delete
4. Display
5. Exit
Enter your choice: 3
Enter the element you wish to delete: 300 300
element is present!
Now Deleted!!
Program for Binary
Search Tree
1. Create
2. Search
3. Delete
4. Display
5. Exit
Enter your choice: 4
The Tree is
100
200
400
500

Result:
Thus the C Program for Binary Search Tree is implemented successfully.
Ex.No.6 AVL TREE
Date:14/09/2020

25
Aim:

To program to implement insertion in AVL trees.

ALGORITHM:-

1.Initialize all variables and functions.

2.To insert and element read the value.

3. Check whether root is nullIf yes make the new value as root.

Else check whether the value is equal to root value.

4. if yes, print “duplicate value”.Otherwise insert the value at its proper position and
balance the tree using rotations.

5. To display the tree values check whether the tree is null.If yes, print “tree is
empty”.Else print all the values in the tree form and in order ofthe tree.

6. Repeat the steps 2 to 10 for more values.

7.End

26
struct node
{ int data;
struct node
*left,*right; int ht;
};
Typedef struct node node;

node *insert(node *,int);


node *Delete(node *,int);
void preorder(node *);
void inorder(node *); int
height( node *); node
*rotateright(node *); node
*rotateleft(node *); node
*RR(node *); node
*LL(node *); node
*LR(node *); node
*RL(node *); int BF(node
*);

int main() {
node
*root=NULL
;
int x,n,i,op;

do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);

switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);

27
}
break; case
2: printf("\nEnter a
data:");
scanf("%d",&x);
root=insert(root,x)
;
break;

case 3: printf("\nEnter a data:");


scanf("%d",&x);
root=Delete(root,x); break;

case 4: printf("\nPreorder sequence:\n");


preorder(root); printf("\n\nInorder
sequence:\n");
inorder(root);
printf("\n"); break;
}
}while(op!=5);

return 0;
}

node * insert(node *T,int x)


{
if(T==NULL)
{
T=(node*)malloc(sizeof(node));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2) if(x>T>right->data)
T=RR(T); else
T=RL(T);
} else if(x<T-
>data)
{
T->left=insert(T->left,x);
if(BF(T)==2) if(x < T>left->data)
T=LL(T); else
T=LR(T);

28
}
T->ht=height(T);
return(T);
}

node * Delete(node *T,int x)


{ node
*p;

if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T); else
T=LR(T);
} else if(x<T-
>data)
{
T->left=Delete(T->left,x); if(BF(T)==-
2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}

else
{
//data to be deleted is found if(T-
>right!=NULL)
{ //delete its inorder succesor
p=T->right;

while(p->left!= NULL) p=p-


>left;

T->data=p->data;
T->right=Delete(T->right,p->data);

if(BF(T)==2)//Rebalance during windup


if(BF(T->left)>=0)
T=LL(T); else
T=LR(T);\

29
}
else
return(T->left);
}
T->ht=height(T);
return(T); }

int height(node *T) {


int lh,rh;
if(T==NULL)
return(0);
if(T>left==NULL)
lh=0; else
lh=1+T->left->ht;

if(T->right==NULL)
rh=0; else
rh=1+T->right->ht;

if(lh>rh)
return(lh);

return(rh);
}

node * rotateright(node *x)


{ node *y; y=x-
>left; x>left=y->right;
y->right=x;
x>ht=height(x); y-
>ht=height(y);
return(y); }

node * rotateleft(node *x)


{ node *y; y=x>right;
x->right=y>left; y-
>left=x; x-
>ht=height(x); y>ht=height(y);

return(y);
}

node * RR(node *T)


{
T=rotateleft(T); return(T);
}

node * LL(node *T)

30
{
T=rotateright(T);
return(T);
}

node * LR(node *T)


{
T->left=rotateleft(T->left);
T=rotateright(T);

return(T);
}

node * RL(node *T)


{
T->right=rotateright(T->right);
T=rotateleft(T); return(T);
}

int BF(node *T)


{ int lh,rh;
if(T==NULL)
return(0);

if(T->left==NULL)
lh=0; else lh=1+T->left-
>ht;

if(T->right==NULL)
rh=0; else
rh=1+T->right->ht;

return(lh-rh);
}

void preorder(node *T)


{
if(T!=NULL)
{
printf("%d(Bf=%d)",T->data,BF(T));
preorder(T->left); preorder(T>right); }
}

void inorder(node *T)


{
if(T!=NULL)
{
inorder(T->left); printf("%d(Bf=%d)",T-

31
>data,BF(T)); inorder(T-
>right);
}
}

Output:

1) Create:
2) Insert:
3) Delete:
4) Print:
5) Quit:

Enter Your Choice:1

Enter no. of elements:7

Enter tree data:5


8
2
7
1
4
3

1) Create:
2) Insert:
3) Delete:
4) Print: 5)Quit:
Enter Your Choice:4

Preorder sequence:
5(Bf=1)2(Bf=-1)1(Bf=0)4(Bf=1)3(Bf=0)8(Bf=1)7(Bf=0)

Inorder sequence:
1(Bf=0)2(Bf=-1)3(Bf=0)4(Bf=1)5(Bf=1)7(Bf=0)8(Bf=1)

1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:2

Enter a data:6

1)Create:
2)Insert:

32
3) Delete:
4) Print: 5)Quit:
Enter Your Choice:4

Preorder sequence:
5(Bf=1)2(Bf=-1)1(Bf=0)4(Bf=1)3(Bf=0)7(Bf=0)6(Bf=0)8(Bf=0)

Inorder sequence:
1(Bf=0)2(Bf=-1)3(Bf=0)4(Bf=1)5(Bf=1)6(Bf=0)7(Bf=0)8(Bf=0)

1) Create:
2) Insert:
3) Delete:
4) Print:
5) Quit:

Enter Your Choice:3

Enter a data:5

1)Create:
2) Insert:
3) Delete:
4) Print: 5)Quit:
Enter Your Choice:4

Preorder sequence:
6(Bf=1)2(Bf=-1)1(Bf=0)4(Bf=1)3(Bf=0)7(Bf=-1)8(Bf=0)

Inorder sequence:
1(Bf=0)2(Bf=-1)3(Bf=0)4(Bf=1)6(Bf=1)7(Bf=-1)8(Bf=0)
1) Create:
2) Insert:
3) Delete:
4) Print:
5) Quit:

Enter Your Choice:5


Result:
Thus the C Program to implement AVL tree is implemented Successfully.
Exno.8 BINARY HEAP
Date:28/09/2020

Aim:
To write C Program to implement Binary Heap

Algorithm:

33
1. find the smallest among the left(data[pos]) and
right child(data[pos + 1]). Check whether the smallest 2.
among them is lesser than their parent. If so, swap
3. parent and the node with smallest value. Continue the
4. above processing from index "low" to "count"
5. 40
/ \
10 30
10 is smaller tha 30. 10 is comapared with its parent and it is smaller(10 <
40). So, swap parent and 10.
10
/ \
40 30
6. To construct a heap we always consider the
* nodes which are present above the leaf. Here,
* nodes present after data[n/2 - 1] are leaf nodes
* insertHeap() - used for restoring the heap
* data - holds node values & n - node count
*/
7. To bulid a heap
/* if parent of the current node has value
* greater than its child, then swap parent
* and child nodes. Traverse up the tree
* until u hit a condition where parent node
* is having lesser value than children
*/
8. Stop

#include <stdio.h>
#include <stdlib.h>
#define HEAPCOUNT 100
int count = 0;

void insertHeap(int *data, int low, int count) {


int pos = (2 * low) + 1, current = data[low];
while (pos <= count) {
if (pos < count && data[pos] > data[pos + 1])
pos++; if (current <= data[pos])
break;
else {
data[low] = data[pos];
low = pos;
pos = (2 * low) + 1;
}
}

data[low] = current;

34
return;
}

/*
*

void buildHeap(int *data, int n) {


int low; for (low = n/2 - 1; low >=
0; low--) {
insertHeap(data, low, n-1);
}
return;
}

void buildUp(int *data, int index) {


int val = data[index]; while
(data[(index - 1) / 2] >= val) {
data[index] = data[(index - 1) / 2];
if (!index)
break; index = (index
- 1) / 2;
}
data[index] = val;
return; }

/* adding new node to the binary heap


*/ void addNode(int *data, int val, int n)
{ data[n] = val; buildUp(data,
n); return;
}
/* delete a node from binary heap */ void
deleteNode(int *data, int n) { int val =
data[0]; data[0] = data[n];
insertHeap(data, 0, n - 1); printf("%d is
deleted from the heap\n", val); return;
}

/* replacing root node with value val */


void replaceNode(int *data, int val, int n) {
int i; data[0] = val; for (i = n/2 - 1;
i >= 0; i--) insertHeap(data, i, n -
1); return;
}

void display(int *data, int n) {


int i;
printf("\nData in Binary Heap:\n");
for (i = 0; i <= n; i++) {

35
printf("%d ", data[i]);
}
printf("\n\n");
}

int main() {
int n, i, *data, temp, ch, val; data = (int
*)malloc(sizeof(int) * HEAPCOUNT);
printf("Enter the no of elements(1-80):");
scanf("%d", &n);

if (n < 1 || n > 80) {


printf("Threshold Exceeded!!\n");
return 0;
}
for (i = 0; i < n; i++) {
printf("Input %d:", i + 1);
scanf("%d", &data[i]);
count++;
}
buildHeap(data, n); while (n < (HEAPCOUNT
-1)) { printf("1. Add New Node\t2. Delete
Node\n"); printf("3. Replace Node\t4.
Display Heap\n"); printf("5. Sort\t6.
Exit\nEnter your choice:"); scanf("%d", &ch);
switch(ch) { case 1:
printf("Enter your input:");
scanf("%d", &val); addNode(data,
val, n);
n++;
break; case 2:
deleteNode(data, n -1);
n--; break;
case 3:
printf("Enter input value:");
scanf("%d", &val);
replaceNode(data, val, n);
break;
case 4:
display(data, n - 1);
break;
case 5: for (i = n - 1; i
> 0; i--) { temp
= data[i];
data[i] = data[0];
data[0] = temp;
insertHeap(data, 0, i - 1);

36
}
printf("Sorted Data:\n");
for (i = 0; i < n; i++)
printf("%d ", data[i]);
printf("\n");
case 6:
exit(0);
break;
}
}
return 0;
}
Output:
Enter the no of elements(1-80):5
Input 1:34
Input 2:23
Input 3:46
Input 4:8
Input 5:12
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
8 12 46 23 34
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:1
Enter your input:19
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
8 12 19 23 34 46
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:2
8 is deleted from the heap
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
12 23 19 46 34

37
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:3
Enter input value:27
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
19 23 27 46 34
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:4
Data in Binary Heap:
19 23 27 46 34
1. Add New Node 2. Delete Node
3. Replace Node 4. Display Heap
5. Sort 6. Exit
Enter your choice:5
Sorted Data: 46 34 27
23 19

Result:

Thus C Program for Heap is implemented successfully.


Exno 7 Graph representation

Date:05/10/2020

AIM:

To write a C program implement adjacent matrix and adjacency list .

Algorithm:
1.Create a graph with getting no. of vertices and no. of edges

2.Implement adjacency matrix

38
3. Implement adjacency list

4. Close the program

39
#include<stdio.h>

#define MAX 100

int adj[MAX][MAX]; /*Adjacency matrix*/

int n; /*Number of vertices in the graph*/

int main() { int

max_edges,i,j,origin,destin; int

graph_type;

printf("\nEnter 1 for Undirected graph\nEnter 2 for Directed


graph\n"); printf("\nEnter your choice :: ");
scanf("%d",&graph_type);

printf("\nEnter number of vertices :: ");

scanf("%d",&n);

if(graph_type==1)

max_edges = n*(n-1)/2; else

max_edges = n*(n-1);

for(i=1; i<=max_edges; i++)

{ printf("\nEnter edge [ %d ] ( -1 -1 to quit


) : ",i);

scanf("%d %d",&origin,&destin); if( (origin == -

1) && (destin == -1) ) break; if(

origin>=n || destin>=n || origin<0 || destin<0 )

{
printf("\nInvalid vertex!\n");

40
i--;

} else

{ adj[origin][destin] = 1;

if( graph_type == 1) /*if undirected graph*/

adj[destin][origin] = 1;

}/*End of for*/

printf("\nThe adjacency matrix is :: \n");

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

{ for(j=0; j<=n-1; j++)

printf("%4d",adj[i][j]);

printf("\n");

}/*End of main()*/
Output
Enter 1 for Undirected graph Enter
2 for Directed graph

Enter your choice :: 2

Enter number of vertices :: 5

Enter edge [ 1 ] ( -1 -1 to quit ) : 0 1

Enter edge [ 2 ] ( -1 -1 to quit ) : 0 2

Enter edge [ 3 ] ( -1 -1 to quit ) : 0 3

Enter edge [ 4 ] ( -1 -1 to quit ) : 1 3

Enter edge [ 5 ] ( -1 -1 to quit ) : 2 3

Enter edge [ 6 ] ( -1 -1 to quit ) : 3 4

41
Enter edge [ 7 ] ( -1 -1 to quit ) : 4 2 Enter

edge [ 8 ] ( -1 -1 to quit ) : -1 -1 The

adjacency matrix is ::

0 1 1 1 0
0 0 0 1 0
0 0 0 1 0
0 0 0 0 1
0 0 1 0 0

Process returned 0

Result:

Thus the C program for Graph representation is completed successfully.


Exno 8 IMPLEMENTATION DFS AND BFS GRAPH TRAVERSAL

BREADTH FIRST SEARCH


Date:05/10/2020

Aim
To create adjacency matrix of the given graph and to perform breadth first search
traversal.
Algorithm

1. Start
2. Obtain Adjacency matrix for the given graph
3. Define a Queue of size total number of vertices in the graph.
4. Select any vertex as starting point for traversal. Visit that vertex and insert it into the
Queue.
5. Visit all the adjacent vertices of the verex which is at front of the Queue which is not
visited and insert them into the Queue.
6. When there is no new vertex to be visit from the vertex at front of the Queue then
delete that vertex from the Queue.
7. Repeat step 5 and 6 until queue becomes empty.

42
8. When queue becomes Empty, then produce final spanning tree by removing unused
edges from the graph.
9. Stop

43
#include<stdio.h>
#include<stdlib.h>
int
cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];
int main() { int m; printf("enterno of vertices");
scanf("%d",&n); printf("ente no of edges");
scanf("%d",&m); printf("\nEDGES \n");
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j)
; cost[i][j]=1; } printf("enter
initial vertex");
scanf("%d",&v);
printf("Visitied vertices\n");
printf("%d ",v);
visited[v]=1; k=1;
while(k<n)
{
for(j=1;j<=n;j++) if(cost[v][j]!=0 &&
visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
printf("%d ",v); k++;
visit[v]=0; visited[v]=1;
}
}
Output: Enterno of
vertices7
enter no of edges8
EDGES

44
12
13
14
15
26
67
3 7 4 7 enter
initial vertex1
Visitied vertices 1
234567

Result:
Thus C Program for BFS is implemented successfully.
Ex. No. 9b Depth First Search Date:08/10/2020 Aim:
To create adjacency matrix of the given graph and to perform depth first search traversal.

45
Algorithm:

1. Start
2. Obtain Adjacency matrix for the given graph
3. Define a Stack of size total number of vertices in the graph.
4. Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
5. Visit any one of the adjacent vertex of the vertex which is at top of the stack which is not visited
and push it on to the stack.
6. Repeat step 5 until there are no new vertex to be visit from the vertex on top of the stack.
7. When there is no new vertex to be visit then use back tracking and pop one vertex from the stack.
8. Repeat steps 5, 6 and 7 until stack becomes Empty.
9. When stack becomes Empty, then produce final spanning tree by removing unused edges from the
graph.
10. Stop

46
#include<stdio.h>
#include<stdlib.h>
int
cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
int main() { int m; printf("enterno of
vertices"); scanf("%d",&n);
printf("ente no of edges");
scanf("%d", &m); printf("\nEDGES
\n");
for(k=1;k<=m;k++)
{
scanf("%d%d",&i,&j)
; cost[i][j]=1; } printf("enter
initial vertex");
scanf("%d",&v);
printf("ORDER OF VISITED VERTICES");
printf( "%d ",v); visited[v]=1; k=1;
while(k<n)
{ for(j=n;j>=1;j--
)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
} v=stk[--top];
printf( "%d
",v); k++;
visit[v]=0;
visited[v]=1;
}
}
Output: Enter no of
vertices 6
enter no of edges 8
EDGES
12 8
14 8
1 10
1 22
2 23
3 23
4
5

47
48
enter initial
vertex1
ORDER OF VISITED VERTICES 1 2 6 3 4

Result: Thus the program for DFS is implemented

Successfully.

Exno.10(a) LINEAR
SEARCH Date:15/10/2020

Aim:
To perform linear search of an element on the given array.
Algorithm:

1. Start
2. Read number of array elements n
3. Read array elements Ai, i = 0,1,2,…n–1
4. Read search value
5. Assign 0 to found
6. Check each array element against search If Ai = search then

If found = 1

Print "Element found" Print position i


Stop
7. If found = 0 then print "Element not found"

50
8. Stop

52
49
#include<stdio.h>

54
int main()
{ int a[10],item,size,pos,flag,i;
flag=1; printf("\nEnter the size
of array:"); scanf("%d",&size);
printf("\nEnter the elements:");

for(i=0;i<size;i++) scanf("%d",&a[i]);
printf("\nEnter the element to be
searched:"); scanf("%d",&item);

for(i=0;i<size;i++)
{
if(item==a[i])
{
pos=i;
flag=1;
break;
} else
continue;
} if(flag==1) printf("%d is at position
%d",item,pos+1); else printf("%d not
found",item); return 0;
}

OUTPUT:

Enter the size of array: 10 30 20 15 11


Enter the elements to be searched: 20
20 is at position 3

Result:

Thus the C Program to implement Linear search is completed.


Exno.10b BINARY SEARCH
Date:15/10/2020
Aim:
To locate an element in a sorted array using Binary search method

Algorithm:

1. Start
2. Read number of array elements, say n
3. Create an array arr consisting n sorted elements
4. Get element, say key to be located
5. Assign 0 to lower and n to upper
6. While (lower < upper)
Determine middle element mid = (upper+lower)/2
If key = arr[mid] then
Print mid
Stop
Else if key > arr[mid] then
lower mid+ 1
else upper = mid – 1

7. Print "Element not found"


8. Stop #include<stdio.h> int main()
{ int a[10],i,n,item,flag=0,low,high,mid;
printf("Enter the size of an array: ");
scanf("%d",&n); printf("Enter the elements in
ascending order:
"); for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("Enter the number to be search:
");
scanf("%d",&item); low=0;
high=n-1;

while(low<=high)
{
mid=(low+high)/2;
if(item==a[mid])
{ flag=1; break;
}
else if(item<a[mid])
high=mid-1; else
low=mid+1;
}
if(flag==0)
printf("%d is not found",item);
else

56
printf("%d is at position %d",item,mid+1);
return 0;
}

OUTPUT:

Enter the size of array: 5


Enter the elements in ascending order: 10 20 30 40 50
Enter the element to be searched: 40
40 is at position 4

Result:

Thus the C Program to implement Binary search is successfully completed.


Exno.11a Implementation of Sorting Algorithms-Bubble Sort, Shell
Sort, Quick Sort
Date: 19/10/2020
BUBBLE SORT
Aim:

To sort an array of N numbers using Bubble sort.

Algorithm

1. Start
2. Read number of array elements n
3. Read array elements Ai
4. Index i varies from 0 to n-2
5. Index j varies from i+1 to n-1
6. Traverse the array and compare each pair of elements
If Ai > Aj then Swap Ai
and A

7. Stop
program
#include <stdio.h>
int main()
{
int array[100], n, i, j, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter elements one by one \n");

57
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
for (i = 0 ; i < n-1; i++)
{
for (j= 0 ; j < n-1; j++)
{
if (array[j] > array[j+1]) /* For decreasing order use <
*/
{
swap = array[j]; array[j] = array[j+1];
array[j+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( i= 0 ; i< n ; i++ )
printf("%d\n", array[i]);
return 0;
}

OUTPUT :
Enter number of elements
4
Enter elements one by one
4
2
3
1
Sorted list in ascending order:
1
2
3
4

Result:
Thus a C program for the concept of bubble sort was implemented successfully
Ex no 11b Shell Sort

Date:19/10/2020 Aim:

To sort an array of N numbers using shell sort.

Algorithm:

58
1− Initialize the value of h

2 − Divide the list into smaller sub-list of equal interval h

3 − Sort these sub-lists using insertion sort

4 − Repeat until complete list is sorted

59
#include <stdio.h> void

shellSort(int array[], int n) {

// Rearrange elements at each n/2, n/4, n/8, ...

intervals for (int interval = n / 2; interval > 0; interval

/= 2) { for (int i = interval; i < n; i += 1) { int temp

= array[i];

int j;

for (j = i; j >= interval && array[j - interval] > temp; j -= interval) {

array[j] = array[j - interval];

array[j] = temp;

// Print an array void printArray(int

array[], int size) { for (int i = 0; i < size;

++i) { printf("%d ", array[i]);

printf("\n");

} int main() { int data[] = {9, 8, 3, 7, 5,

6, 4, 1}; int size = sizeof(data) /

sizeof(data[0]); shellSort(data, size);

printf("Sorted array: \n");

printArray(data, size);

}
Output:

Sorted Array:

60
13

56

78

Result:

Thus a C program for the concept of shell sort was implemented successfully.

Exno. 11.c QUICK


SORT Date:29/10/2020 AIM:

To write a C program to implement the concept of Quick sort.

ALGORITHM:

1: Start.

2: Choose any element of the array to be the pivot.


3: Divide all other elements (except the pivot) into two partitions.

All elements less than the pivot must be in the first partition.
All elements greater than the pivot must be in the second partition.
4: Use recursion to sort both partitions.

5: Join the first sorted partition, the pivot, and the second sorted partition.

61
6: Stop

Program
#include<stdio.h> void
quicksort(int a[],int,int);

Int main()
{
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++) scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}

void quicksort(int x[10], int first, int last)


{
int pivot, j, temp, i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--; if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

62
}
}

OUTPUT:

Enter size of the array: 5


Enter 5 elements:
3
8
0
1
2
Sorted elements: 0 1 2 3 8

Result: Thus a C program for the concept of Quick sort was implemented
successfully.
Exno. 12 HASHING
Date:2/11/2020

Aim:
To Write a C Program to implement Collision Resolution Techniques in
Hashing
Algorithm:

1. Create a structure, data (hash table item) with key and value as data.
2. Now create an array of structure, data of some certain size (10, in this case). But, the
size of array must be immediately updated to a prime number just greater than initial
array capacity (i.e 10, in this case).
3. A menu is displayed on the screen.

63
4. User must choose one option from four choices given in the menu
5. Perform all the operations
6. Stop

64
Program

#include<stdio.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 ;
} int
main()
{
int key,arr[20],hash[20],i,n,s,op,j,k ;
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:

65
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);
}
Output:
Enter the size of the hash table: 10
Enter the number of elements: 7
Enter Elements: 12

66
23
42
11
45
38
59
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: 11
Element at position 2: 12
Element at position 3: 23
Element at position 4: 42
Element at position 5: 45
Element at position 6: -1
Element at position 7: -1
Element at position 8: 38
Element at position 9: 59

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: 11
Element at position 2: 12
Element at position 3: 23
Element at position 4: -1
Element at position 5: 45
Element at position 6: -1
Element at position 7: 42
Element at position 8: 38
Element at position 9: 59

1.Linear Probing
2.Quadratic Probing
3.Exit
Enter your option: 3 Result:
Thus hashing has been performed successfully.

67

You might also like