DATA STRUTURE LAB FILE
Btech 1st year 2nd sem
Stack data structure
Unit 1
1. Write a C program to perform All Stack Operation.
Such as PUSH, POP, PEEK, isEmpty, isFull.
#include <stdio.h>
#define MAX 5 // maximum size of stack
int stack[MAX];
int top = -1;
// Function to check if stack is full
int isFull() {
if (top == MAX - 1)
printf("Stack is Full \n”);
else
printf("Stack is not Full\n”);
}
// Function to check if stack is empty
int isEmpty() {
if (top == -1)
printf("Stack is Empty \n”);
else
printf("Stack is not Empty \n”);
}
// Function to push element into stack
void push(int value) {
if (top==MAX-1)
printf("Stack Overflow! Cannot push %d\n", value);
else
{
top++;
stack[top] = value;
printf("%d pushed into stack\n", value);
}
}
// Function to pop element from stack
void pop() {
if (top==-1)
printf("Stack Underflow! Cannot pop\n");
else
{
printf("%d popped from stack\n", stack[top]);
top--;
}
}
// Function to peek top element of stack
void peek() {
if (top==-1)
printf("Stack is empty! No top element\n");
else
printf("Top element is %d\n", stack[top]);
}
// Main function
int main() {
push(10);
push(20);
push(30);
pop();
pop();
peek();
isEmpty();
isFull();
return 0;
}
OUTPUT : 10 pushed into stack
20 pushed into stack
30 pushed into stack
30 popped from stack
20 popped from stack
Top element is 10
Stack is not Empty
Stack is not Full
2. Write a C program to perform Display operation on
stack.
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void display() {
int i;
if (top == -1) {
printf("Stack is empty.\n");
} else {
printf("Stack elements are:\n");
for (i = top; i >= 0; i--) {
printf("%d\n", stack[i]);
}
}
}
int main() {
// Sample stack values
stack[0] = 10;
stack[1] = 20;
stack[2] = 30;
top = 2;
display();
return 0;
}
OUTPUT : Stack elements are:
30
20
10
3. Write a C program to implement Tower of Hanoi using
Recursion Function.
#include <stdio.h>
// Function to solve Tower of Hanoi
void towerOfHanoi(int n, char source, char auxiliary, char destination) {
// Base case
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
// Move n-1 disks from source to auxiliary
towerOfHanoi(n - 1, source, destination, auxiliary);
// Move nth disk from source to destination
printf("Move disk %d from %c to %c\n", n, source, destination);
// Move n-1 disks from auxiliary to destination
towerOfHanoi(n - 1, auxiliary, source, destination);
}
int main() {
int n;
printf("Enter number of disks: ");
scanf("%d", &n);
// A = Source, B = Auxiliary, C = Destination
towerOfHanoi(n, 'A', 'B', 'C');
return 0;
}
OUTPUT : Enter number of disks: 3
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
4. Write a C program to perform Factorial using
Recursion Operation .
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %d", factorial(n));
return 0;
}
OUTPUT : Enter a number: 3
Factorial = 6
5. Write a C program to perform Fibonacci Series using
Recursion Operation .
#include <stdio.h>
int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
printf("%d ", fibonacci(i));
return 0;
}
OUTPUT : Enter number of terms: 10
0 1 1 2 3 5 8 13 21 34
6. Write a C program to perform Sum of First N Natural
Numbers using Recursion Operation .
#include <stdio.h>
int sum(int n) {
if (n == 0)
return 0;
else
return n + sum(n - 1);
}
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("Sum = %d", sum(n));
return 0;
}
OUTPUT : Enter n: 5
Sum = 15
7. Write a C program to perform Count Digits of a
Number Using Recursion Operation .
#include <stdio.h>
int countDigits(int n) {
if (n == 0)
return 0;
else
return 1 + countDigits(n / 10);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Number of digits = %d", countDigits(n));
return 0;
}
OUTPUT : Enter a number: 12345
Number of digits = 5
8. Write a C program to perform Power of a Number
Using Recursion Operation .
#include <stdio.h>
int power(int x, int n) {
if (n == 0)
return 1;
else
return x * power(x, n - 1);
}
int main() {
int x, n;
printf("Enter base and exponent: ");
scanf("%d %d", &x, &n);
printf("Result = %d", power(x, n));
return 0;
}
OUTPUT : Enter base and exponent: 2 2
Result = 4
9. Write a C program to perform Reverse of a Number
Using Recursion Operation .
#include <stdio.h>
int reverse(int n, int rev) {
if (n == 0)
return rev;
else
return reverse(n / 10, rev * 10 + (n % 10));
}
int main() {
int n, rev;
printf("Enter a number: ");
scanf("%d", &n);
rev = reverse(n, 0);
if (n == rev)
printf("Palindrome number");
else
printf("Not a palindrome number");
return 0;
}
OUTPUT : Enter a number: 121
Palindrome number
10. Write a C program to perform GCD of Two Numbers
(Euclidean Algorithm) Using Recursion Operation .
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD = %d", gcd(a, b));
return 0;
}
OUTPUT : Enter two numbers: 5 10
GCD = 5
11. Write a C program to perform Print Numbers from
1 to N Using Recursion Operation .
#include <stdio.h>
void printNumbers(int n) {
if (n == 0)
return;
printNumbers(n - 1);
printf("%d ", n);
}
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printNumbers(n);
return 0;
}
OUTPUT : Enter n: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
12. Write a C program to perform Print Numbers from N
to 1 Using Recursion Operation .
#include <stdio.h>
void printReverse(int n) {
if (n == 0)
return;
printf("%d ", n);
printReverse(n - 1);
}
int main() {
int n;
printf("Enter n: ");
scanf("%d", &n);
printReverse(n);
return 0;
}
OUTPUT : Enter n: 20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
QUEUE DATA STRUCTURE
UNIT 2
1. Write a C program to perform ENQUEUE, DEQUEUE,
PEEK, isEmpty, isFull operation on Queue.
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
// Check if queue is full
int isFull() {
if (rear == MAX - 1)
printf("Queue is Full \n");
else
printf("Queue is not full \n");
}
// Check if queue is empty
int isEmpty() {
if (front == -1 || front > rear)
printf("queue is Empty \n");
else
printf("Queue is not Empty \n");
}
// Enqueue operation
void enqueue(int value) {
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
if (front == -1)
front = 0;
queue[++rear] = value;
printf("Inserted %d\n", value);
}
}
// Dequeue operation
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
} else {
printf("%d deleted from queue\n", queue[front]);
front++;
}
}
// Peek operation
void peek() {
if (front == -1 || front > rear) {
printf("Queue is Empty\n");
} else {
printf("Front element is %d\n", queue[front]);
}
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
peek();
isEmpty();
isFull();
return 0;
}
OUTPUT : Inserted 10
Inserted 20
Inserted 30
10 deleted from queue
Front element is 20
Queue is not Empty
Queue is not full
2. Write a C program to perform DISPLAY operation on
Queue Using while loop.
#include <stdio.h>
#define MAX 5
int queue[MAX];
int front = -1, rear = -1;
// Display operation using while loop
void display() {
int i;
if (front == -1 || front > rear) {
printf("Queue is Empty\n");
} else {
printf("Queue elements: ");
i = front;
while (i <= rear) {
printf("%d ", queue[i]);
i++;
}
printf("\n");
}
}
int main() {
// Sample values
queue[0] = 10;
queue[1] = 20;
queue[2] = 30;
front = 0;
rear = 2;
display();
return 0;
}
OUTPUT : Queue elements: 10 20 30
3. Write a C program to Implement CIRCULAR QUEUE
and perform Enqueue, Dequeue, Peek, isEmpty,
isFull , Display operation.
#include <stdio.h>
#define SIZE 5
int cq[SIZE];
int front = -1, rear = -1;
/* Check if Queue is Full */
int isFull() {
if ((rear + 1) % SIZE == front)
printf("Queue is Full \n");
else
printf("Queue is not Full \n");
}
/* Check if Queue is Empty */
int isEmpty() {
if (front == -1)
printf("Queue is Empty \n");
else
printf("Queue is not Empty \n");
}
/* Insert element */
void enqueue(int x) {
if ((rear + 1) % SIZE == front) {
printf("Queue Overflow (Queue is Full)\n");
return;
}
if (front == -1) // First insertion
front = 0;
rear = (rear + 1) % SIZE;
cq[rear] = x;
printf("Inserted: %d\n", x);
}
/* Delete element */
void dequeue() {
if (front == -1) {
printf("Queue Underflow (Queue is Empty)\n");
return;
}
printf("Deleted: %d\n", cq[front]);
if (front == rear) { // Queue becomes empty
front = rear = -1;
} else {
front = (front + 1) % SIZE;
}
}
/* View front element */
void peek() {
if (front == -1) {
printf("Queue is Empty\n");
} else {
printf("Front element: %d\n", cq[front]);
}
}
/* Display queue */
void display() {
if (front == -1) {
printf("Queue is Empty\n");
return;
}
printf("Circular Queue elements: ");
int i = front;
while (1) {
printf("%d ", cq[i]);
if (i == rear)
break;
i = (i + 1) % SIZE;
}
printf("\n");
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
dequeue();
dequeue();
peek();
isEmpty();
isFull();
display();
enqueue(10);
enqueue(20);
enqueue(40);
isEmpty();
isFull();
peek();
dequeue();
peek();
display();
return 0;
}
OUTPUT : Inserted: 10
Inserted: 20
Inserted: 30
Deleted: 10
Deleted: 20
Front element: 30
Queue is not Empty
Queue is not Full
Circular Queue elements: 30
Inserted: 10
Inserted: 20
Inserted: 40
Queue is not Empty
Queue is not Full
Front element: 30
Deleted: 30
Front element: 10
Circular Queue elements: 10 20 40
4. Write a C program to Implement PRIORITY QUEUE
and perform INSERT, DELETE, Display operation.
#include <stdio.h>
int pq[10];
int size = 0;
/* Insert element based on priority */
void insert(int value) {
int i = size - 1;
/* Shift elements using if (no for loop) */
while (i >= 0 && pq[i] > value) {
pq[i + 1] = pq[i];
i--;
}
pq[i + 1] = value;
size++;
printf("Inserted: %d\n", value);
}
/* Delete highest priority element */
void delete() {
if (size == 0) {
printf("Priority Queue is Empty\n");
} else {
printf("Deleted: %d\n", pq[0]);
int i = 0;
while (i < size - 1) {
pq[i] = pq[i + 1];
i++;
}
size--;
}
}
/* Recursive display function (no for loop) */
void displayRec(int index) {
if (index == size)
return;
printf("%d ", pq[index]);
displayRec(index + 1);
}
/* Display */
void display() {
if (size == 0) {
printf("Priority Queue is Empty\n");
} else {
printf("Priority Queue elements: ");
displayRec(0);
printf("\n");
}
}
int main() {
insert(30);
insert(10);
insert(20);
display();
delete();
display();
return 0;
}
OUTPUT : Inserted: 30
Inserted: 10
Inserted: 20
Priority Queue elements: 10 20 30
Deleted: 10
Priority Queue elements: 20 30
LINKED LIST DATA STRUCTURE
UNIT 3
1. Write a C program to Implement singly Linked list
and perform all the operations which are listed
below.
✔ Insert at Beginning
✔ Insert at End
✔ Insert at Specific Position
✔ Delete from Beginning
✔ Delete from End
✔ Delete from Specific Position
✔ Display Linked List
#include <stdio.h>
#include <stdlib.h>
/* Node structure */
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
/* Insert at beginning */
void insert_begin() {
int value;
struct node *newnode;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter value: ");
scanf("%d", &value);
newnode->data = value;
newnode->next = head;
head = newnode;
printf("Node inserted at beginning\n");
}
/* Insert at end */
void insert_end() {
int value;
struct node *newnode, *temp;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter value: ");
scanf("%d", &value);
newnode->data = value;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
} else {
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
printf("Node inserted at end\n");
}
/* Insert at specific position */
void insert_pos() {
int value, pos, i;
struct node *newnode, *temp;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter value and position: ");
scanf("%d%d", &value, &pos);
newnode->data = value;
if (pos == 1) {
newnode->next = head;
head = newnode;
return;
}
temp = head;
for (i = 1; i < pos - 1; i++) {
if (temp == NULL) {
printf("Invalid position\n");
return;
}
temp = temp->next;
}
newnode->next = temp->next;
temp->next = newnode;
printf("Node inserted at position %d\n", pos);
}
/* Delete from beginning */
void delete_begin() {
struct node *temp;
if (head == NULL) {
printf("List is empty\n");
return;
}
temp = head;
head = head->next;
free(temp);
printf("Node deleted from beginning\n");
}
/* Delete from end */
void delete_end() {
struct node *temp, *prev;
if (head == NULL) {
printf("List is empty\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
return;
}
temp = head;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
printf("Node deleted from end\n");
}
/* Delete from specific position */
void delete_pos() {
int pos, i;
struct node *temp, *prev;
printf("Enter position: ");
scanf("%d", &pos);
if (head == NULL) {
printf("List is empty\n");
return;
}
if (pos == 1) {
temp = head;
head = head->next;
free(temp);
return;
}
temp = head;
for (i = 1; i < pos; i++) {
prev = temp;
temp = temp->next;
if (temp == NULL) {
printf("Invalid position\n");
return;
}
}
prev->next = temp->next;
free(temp);
printf("Node deleted from position %d\n", pos);
}
/* Display linked list */
void display() {
struct node *temp;
if (head == NULL) {
printf("List is empty\n");
return;
}
temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
int choice;
do {
printf("\n--- Linked List Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete from Position\n");
printf("7. Display\n");
printf("0. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 1)
insert_begin();
else if (choice == 2)
insert_end();
else if (choice == 3)
insert_pos();
else if (choice == 4)
delete_begin();
else if (choice == 5)
delete_end();
else if (choice == 6)
delete_pos();
else if (choice == 7)
display();
else if (choice == 0)
printf("Exiting program...\n");
else
printf("Invalid choice\n");
} while (choice != 0);
return 0;
}
OUTPUT :
--- Linked List Menu ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display
0. Exit
Enter choice: 1
Enter value: 20
Node inserted at beginning
--- Linked List Menu ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display
0. Exit
Enter choice: 1
Enter value: 30
Node inserted at beginning
--- Linked List Menu ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display
0. Exit
Enter choice: 7
Linked List: 30 -> 20 -> NULL
--- Linked List Menu ---
1. Insert at Beginning
2. Insert at End
3. Insert at Position
4. Delete from Beginning
5. Delete from End
6. Delete from Position
7. Display
0. Exit
Enter choice: 2
Enter value: 40
Node inserted at end
array DATA STRUCTURE
UNIT 4
1. Write a C program to Read and Display Element
of an Array.
#include <stdio.h>
int main()
{
int arr[100], n, i;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Array elements are:\n");
for(i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT : Enter number of elements: 5
Enter 5 elements:
1
2
3
4
5
Array elements are:
12345
2. Write a C program to Find Sum of Array
Elements.
#include <stdio.h>
int main()
{
int arr[100], n, i, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
printf("Sum of array elements = %d", sum);
return 0;
}
OUTPUT : Enter number of elements: 3
Enter 3 elements:
3
4
5
Sum of array elements = 12
3. Write a C program for Linear Search in Array.
#include <stdio.h>
int main()
{
int arr[100], n, i, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
for(i = 0; i < n; i++)
{
if(arr[i] == key)
{
printf("Element %d found at position %d", key, i + 1);
found = 1;
break;
}
}
if(found == 0)
{
printf("Element %d not found in the array", key);
}
return 0;
}
OUTPUT : Enter number of elements: 3
Enter 3 elements:
3
4
5
Enter element to search: 4
Element 4 found at position 2
4. Write a C program For Binary Search in Array.
#include <stdio.h>
int main()
{
int arr[100], n, i;
int low, high, mid, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements in sorted order:\n", n);
for(i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter element to search: ");
scanf("%d", &key);
low = 0;
high = n - 1;
while(low <= high)
{
mid = (low + high) / 2;
if(arr[mid] == key)
{
printf("Element %d found at position %d", key, mid + 1);
found = 1;
break;
}
else if(arr[mid] < key)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if(found == 0)
{
printf("Element %d not found in the array", key);
}
return 0;
}
OUTPUT : Enter number of elements: 2
Enter 2 elements:
3
4
Enter element to search: 4
Element 4 found at position 2
tree DATA STRUCTURE
UNIT 5
1. Write a C program to implement all Basic Binary
Tree Operations.
This single program includes:
✔ Create Binary Tree
✔ Insert Node
✔ Search Element
✔ Count Total Nodes
✔ Count Leaf Nodes
✔ Count Internal Nodes
✔ Find Height
✔ Find Depth of a Node
✔ Find Maximum Element
✔ Find Minimum Element
#include <stdio.h>
#include <stdlib.h>
/* Structure of tree node */
struct node
int data;
struct node *left, *right;
};
/* Create new node */
struct node* createNode(int data)
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
/* Insert node in binary tree */
struct node* insert(struct node* root, int data)
if(root == NULL)
return createNode(data);
if(data < root->data)
root->left = insert(root->left, data);
else
root->right = insert(root->right, data);
return root;
/* Search element */
int search(struct node* root, int key)
if(root == NULL)
return 0;
if(root->data == key)
return 1;
if(key < root->data)
return search(root->left, key);
else
return search(root->right, key);
/* Count total nodes */
int countNodes(struct node* root)
if(root == NULL)
return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
/* Count leaf nodes */
int countLeaf(struct node* root)
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
return countLeaf(root->left) + countLeaf(root->right);
/* Count internal nodes */
int countInternal(struct node* root)
{
if(root == NULL || (root->left == NULL && root->right == NULL))
return 0;
return 1 + countInternal(root->left) + countInternal(root->right);
/* Find height of tree */
int height(struct node* root)
int lh, rh;
if(root == NULL)
return -1;
lh = height(root->left);
rh = height(root->right);
return (lh > rh ? lh : rh) + 1;
/* Find depth of node */
int depth(struct node* root, int key, int level)
if(root == NULL)
return -1;
if(root->data == key)
return level;
if(key < root->data)
return depth(root->left, key, level + 1);
else
return depth(root->right, key, level + 1);
/* Find maximum element */
int findMax(struct node* root)
if(root->right == NULL)
return root->data;
return findMax(root->right);
/* Find minimum element */
int findMin(struct node* root)
if(root->left == NULL)
return root->data;
return findMin(root->left);
/* Main function */
int main()
struct node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
insert(root, 60);
insert(root, 80);
printf("Total nodes: %d\n", countNodes(root));
printf("Leaf nodes: %d\n", countLeaf(root));
printf("Internal nodes: %d\n", countInternal(root));
printf("Height of tree: %d\n", height(root));
printf("Depth of node 60: %d\n", depth(root, 60, 0));
if(search(root, 40))
printf("Element 40 found\n");
else
printf("Element 40 not found\n");
printf("Maximum element: %d\n", findMax(root));
printf("Minimum element: %d\n", findMin(root));
return 0;
OUTPUT : Total nodes: 7
Leaf nodes: 4
Internal nodes: 3
Height of tree: 2
Depth of node 60: 2
Element 40 found
Maximum element: 80
Minimum element: 20
2.
Hashing data structure
Unit 6
1. Write a C program to implement Basic Hash Table
Using Array.
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
/* Initialize hash table */
void init()
{
int i;
for(i = 0; i < SIZE; i++)
hashTable[i] = -1; // -1 indicates empty slot
}
/* Simple hash function */
int hashFunction(int key)
{
return key % SIZE;
}
/* Insert element */
void insert(int key)
{
int index = hashFunction(key);
if(hashTable[index] == -1)
{
hashTable[index] = key;
printf("Key %d inserted at index %d\n", key, index);
}
else
{
printf("Collision occurred at index %d\n", index);
}
}
/* Search element */
void search(int key)
{
int index = hashFunction(key);
if(hashTable[index] == key)
printf("Key %d found at index %d\n", key, index);
else
printf("Key %d not found\n", key);
}
/* Delete element */
void deleteKey(int key)
{
int index = hashFunction(key);
if(hashTable[index] == key)
{
hashTable[index] = -1;
printf("Key %d deleted from index %d\n", key, index);
}
else
{
printf("Key %d not found, cannot delete\n", key);
}
}
/* Display hash table */
void display()
{
int i;
printf("\nHash Table:\n");
for(i = 0; i < SIZE; i++)
printf("Index %d : %d\n", i, hashTable[i]);
}
/* Main function */
int main()
{
init();
insert(10);
insert(20);
insert(25);
search(20);
search(15);
deleteKey(20);
display();
return 0;
}
OUTPUT : Key 10 inserted at index 0
Collision occurred at index 0
Key 25 inserted at index 5
Key 20 not found
Key 15 not found
Key 20 not found, cannot delete
Hash Table:
Index 0 : 10
Index 1 : -1
Index 2 : -1
Index 3 : -1
Index 4 : -1
Index 5 : 25
Index 6 : -1
Index 7 : -1
Index 8 : -1
Index 9 : -1
2. Write a C program to implement Linear Probing
(Collision Resolution Technique) and some below
operation.
a. Insert using linear probing
b. Search using linear probing
c. Delete using linear probing
#include <stdio.h>
#define SIZE 10
int hashTable[SIZE];
/* Initialize hash table */
void init()
{
int i;
for(i = 0; i < SIZE; i++)
hashTable[i] = -1; // -1 indicates empty slot
}
/* Hash function */
int hashFunction(int key)
{
return key % SIZE;
}
/* Insert using Linear Probing */
void insert(int key)
{
int index = hashFunction(key);
int i = 0;
while(hashTable[(index + i) % SIZE] != -1 && i < SIZE)
i++;
if(i < SIZE)
{
hashTable[(index + i) % SIZE] = key;
printf("Key %d inserted\n", key);
}
else
{
printf("Hash table is full\n");
}
}
/* Search using Linear Probing */
void search(int key)
{
int index = hashFunction(key);
int i = 0;
while(hashTable[(index + i) % SIZE] != -1 && i < SIZE)
{
if(hashTable[(index + i) % SIZE] == key)
{
printf("Key %d found at index %d\n",
key, (index + i) % SIZE);
return;
}
i++;
}
printf("Key %d not found\n", key);
}
/* Delete using Linear Probing */
void deleteKey(int key)
{
int index = hashFunction(key);
int i = 0;
while(hashTable[(index + i) % SIZE] != -1 && i < SIZE)
{
if(hashTable[(index + i) % SIZE] == key)
{
hashTable[(index + i) % SIZE] = -1;
printf("Key %d deleted\n", key);
return;
}
i++;
}
printf("Key %d not found, cannot delete\n", key);
}
/* Display hash table */
void display()
{
int i;
printf("\nHash Table:\n");
for(i = 0; i < SIZE; i++)
printf("Index %d : %d\n", i, hashTable[i]);
}
int main()
{
init();
insert(10);
insert(20);
insert(30);
insert(25);
search(20);
search(15);
deleteKey(20);
display();
return 0;
}
OUTPUT : Key 10 inserted
Key 20 inserted
Key 30 inserted
Key 25 inserted
Key 20 found at index 1
Key 15 not found
Key 20 deleted
Hash Table:
Index 0 : 10
Index 1 : -1
Index 2 : 30
Index 3 : -1
Index 4 : -1
Index 5 : 25
Index 6 : -1
Index 7 : -1
Index 8 : -1
Index 9 : -1