C Data Structures Lab Manual
C Data Structures Lab Manual
List of Experiments:
ii) C Programs to implement the Searching Techniques – Linear & Binary Search
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort
i) Implement a singly linked list and perform insertion and deletion operations.
i) Implement a doubly linked list and perform various operations to understand its
ii) Implement a circular linked list and perform insertion, deletion, and traversal.
iii) Implement a stack or queue to perform comparison and check for symmetry.
Exercise 9: Hashing
#include <stdio.h>
int main()
int arr[MAX_SIZE];
scanf("%d", &size);
scanf("%d", &arr[i]);
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
while(i<j)
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; /* increment i */
j--; /* decrement j */
return 0;
ii) C Programs to implement the Searching Techniques – Linear & Binary Search
Linear Search:
#include <stdio.h>
int main()
scanf("%d", &arr[i]);
scanf("%d", &num);
if(arr[i] == num)
flag = 1;
break;
if(flag == 0)
return 0;
Binary Search:
#include <stdio.h>
int main()
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
scanf("%d", &arr[i]);
scanf("%d", &num);
first = 0;
last = 9;
if(arr[middle] == num)
flag = 1;
break;
first = middle + 1;
else
last = middle - 1;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(flag == 0)
return 0;
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort
Bubble Sort:
#include<stdio.h>
int main()
scanf("%d", &arr[i]);
if(arr[j]>arr[j+1])
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
DS LAB MANUAL (PITW ) NIHAZRAHIM
return 0;
Selection Sort:
#include<stdio.h>
int main()
scanf("%d", &arr[i]);
min = i;
if(arr[j]<arr[min])
DS LAB MANUAL (PITW ) NIHAZRAHIM
min = j;
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
return 0;
Insertion Sort:
#include<stdio.h>
int main()
scanf("%d", &arr[i]);
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
temp = arr[i];
j = i-1;
arr[j+1] = arr[j];
j--;
arr[j+1] = temp;
return 0;
i) Implement a singly linked list and perform insertion and deletion operations.
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
DS LAB MANUAL (PITW ) NIHAZRAHIM
void insertNode();
void deleteNode();
void displayList();
int main()
int choice;
while(1)
printf("4. Exit\n");
scanf("%d", &choice);
switch(choice)
case 1:
insertNode();
break;
case 2:
deleteNode();
break;
case 3:
DS LAB MANUAL (PITW ) NIHAZRAHIM
displayList();
break;
case 4:
exit(0);
default:
break;
return 0;
newNode->data = data;
newNode->next = NULL;
return newNode;
void insertNode()
int data;
scanf("%d", &data);
if(start == NULL)
start = newNode;
DS LAB MANUAL (PITW ) NIHAZRAHIM
else
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
void deleteNode()
int data;
scanf("%d", &data);
start = temp->next;
free(temp);
return;
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
prev = temp;
temp = temp->next;
if(temp == NULL)
return;
prev->next = temp->next;
free(temp);
void displayList()
if(start == NULL)
printf("List is empty.\n");
return;
while(temp != NULL)
temp = temp->next;
printf("\n");
DS LAB MANUAL (PITW ) NIHAZRAHIM
Iterative Approach:
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
void insertNode(int);
void reverseListIteratively();
void displayList();
int main()
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
printf("Original list:\n");
displayList();
reverseListIteratively();
printf("Reversed list:\n");
displayList();
DS LAB MANUAL (PITW ) NIHAZRAHIM
return 0;
newNode->data = data;
newNode->next = NULL;
return newNode;
if(start == NULL)
start = newNode;
else
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
void reverseListIteratively()
while(currentNode != NULL)
nextNode = currentNode->next;
currentNode->next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
start = prevNode;
void displayList()
if(start == NULL)
printf("List is empty.\n");
return;
while(temp != NULL)
temp = temp->next;
printf("NULL\n");
DS LAB MANUAL (PITW ) NIHAZRAHIM
Recursive Approach:
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
void insertNode(int);
void displayList();
int main()
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
printf("Original list:\n");
displayList();
start = reverseListRecursively(start);
printf("Reversed list:\n");
displayList();
DS LAB MANUAL (PITW ) NIHAZRAHIM
return 0;
newNode->data = data;
newNode->next = NULL;
return newNode;
if(start == NULL)
start = newNode;
else
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
return currentNode;
currentNode->next = NULL;
nextNode->next = currentNode;
return reverseRest;
void displayList()
if(start == NULL)
printf("List is empty.\n");
return;
while(temp != NULL)
temp = temp->next;
printf("NULL\n");
DS LAB MANUAL (PITW ) NIHAZRAHIM
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
new_node->data = data;
new_node->next = NULL;
if(*head == NULL)
*head = new_node;
else
while(temp->next != NULL)
temp = temp->next;
temp->next = new_node;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(position < 1)
printf("Invalid position.\n");
return;
new_node->data = data;
new_node->next = NULL;
if(position == 1)
new_node->next = *head;
*head = new_node;
else
if(prev_node == NULL)
printf("Invalid position.\n");
return;
prev_node = prev_node->next;
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(prev_node == NULL)
printf("Invalid position.\n");
return;
new_node->next = prev_node->next;
prev_node->next = new_node;
*head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
if(temp == NULL)
return;
prev->next = temp->next;
free(temp);
while(temp != NULL)
temp = temp->next;
printf("NULL\n");
int main()
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
display(head);
delete(&head, 20);
display(head);
display(head);
DS LAB MANUAL (PITW ) NIHAZRAHIM
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
new_node->data = data;
new_node->next = NULL;
if(*head == NULL)
*head = new_node;
else
while(temp->next != NULL)
temp = temp->next;
DS LAB MANUAL (PITW ) NIHAZRAHIM
temp->next = new_node;
if(current_node == NULL)
return;
while(current_node->next != NULL)
if(current_node->data == current_node->next->data)
delete_node = current_node->next;
current_node->next = delete_node->next;
free(delete_node);
else
current_node = current_node->next;
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
while(temp != NULL)
temp = temp->next;
printf("NULL\n");
int main()
insert(&head, 10);
insert(&head, 20);
insert(&head, 20);
insert(&head, 30);
insert(&head, 30);
insert(&head, 30);
insert(&head, 40);
display(head);
removeDuplicates(head);
display(head);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node
DS LAB MANUAL (PITW ) NIHAZRAHIM
int coefficient;
int exponent;
};
new_node->coefficient = coefficient;
new_node->exponent = exponent;
new_node->next = NULL;
if(*head == NULL)
*head = new_node;
else
while(temp->next != NULL)
temp = temp->next;
temp->next = new_node;
void addPolynomials(struct node *poly1, struct node *poly2, struct node **result)
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
poly1 = poly1->next;
poly2 = poly2->next;
else
poly1 = poly1->next;
poly2 = poly2->next;
while(poly1 != NULL)
poly1 = poly1->next;
while(poly2 != NULL)
poly2 = poly2->next;
while(poly != NULL)
poly = poly->next;
if(poly != NULL)
printf(" + ");
printf("\n");
int main()
insert(&poly1, 5, 2);
insert(&poly1, 3, 0);
printf("Polynomial 1: ");
display(poly1);
insert(&poly2, 3, 2);
DS LAB MANUAL (PITW ) NIHAZRAHIM
printf("Polynomial 2: ");
display(poly2);
printf("Result: ");
display(result);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
};
new_node->data = data;
new_node->prev = NULL;
if(*head == NULL)
new_node->next = NULL;
*head = new_node;
DS LAB MANUAL (PITW ) NIHAZRAHIM
else
new_node->next = *head;
(*head)->prev = new_node;
*head = new_node;
new_node->data = data;
new_node->next = NULL;
if(*head == NULL)
new_node->prev = NULL;
*head = new_node;
else
while(temp->next != NULL)
temp = temp->next;
temp->next = new_node;
new_node->prev = temp;
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(*head == NULL)
printf("Deque is empty.\n");
return;
*head = (*head)->next;
if(*head != NULL)
(*head)->prev = NULL;
free(temp);
if(*head == NULL)
printf("Deque is empty.\n");
return;
while(temp->next != NULL)
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
temp = temp->next;
if(temp->prev != NULL)
temp->prev->next = NULL;
else
*head = NULL;
free(temp);
if(head == NULL)
printf("Deque is empty.\n");
return;
printf("Deque: ");
while(head != NULL)
head = head->next;
printf("\n");
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
int main()
insert_rear(&deque, 10);
insert_rear(&deque, 20);
insert_front(&deque, 30);
insert_front(&deque, 40);
display(deque);
delete_front(&deque);
delete_rear(&deque);
display(deque);
insert_rear(&deque, 50);
insert_front(&deque, 60);
display(deque);
delete_front(&deque);
delete_rear(&deque);
delete_front(&deque);
delete_rear(&deque);
display(deque);
return 0;
i) Implement a doubly linked list and perform various operations to understand its
#include <stdio.h>
#include <stdlib.h>
struct node
DS LAB MANUAL (PITW ) NIHAZRAHIM
int data;
};
new_node->data = data;
new_node->prev = NULL;
if(*head == NULL)
new_node->next = NULL;
*head = new_node;
else
new_node->next = *head;
(*head)->prev = new_node;
*head = new_node;
new_node->data = data;
new_node->next = NULL;
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(*head == NULL)
new_node->prev = NULL;
*head = new_node;
else
while(temp->next != NULL)
temp = temp->next;
temp->next = new_node;
new_node->prev = temp;
if(position < 1)
printf("Invalid position.\n");
return;
new_node->data = data;
if(position == 1)
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
new_node->prev = NULL;
if(*head == NULL)
new_node->next = NULL;
else
new_node->next = *head;
(*head)->prev = new_node;
*head = new_node;
else
int i = 1;
if(temp == NULL)
printf("Invalid position.\n");
return;
temp = temp->next;
i++;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
new_node->next = temp->next;
new_node->prev = temp;
if(temp->next != NULL)
temp->next->prev = new_node;
temp->next = new_node;
printf("Invalid position.\n");
return;
if(position == 1)
*head = (*head)->next;
if(*head != NULL)
(*head)->prev = NULL;
else
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
int i = 1;
if(temp == NULL)
printf("Invalid position.\n");
return;
temp = temp->next;
i++;
if(temp->prev != NULL)
temp->prev->next = temp->next;
else
*head = temp->next;
if(temp->next != NULL)
temp->next->prev = temp->prev;
free(temp);
while(temp != NULL)
temp = temp->next;
printf("\n");
if(temp == NULL)
return;
while(temp->next != NULL)
temp = temp->next;
while(temp != NULL)
temp = temp->prev;
DS LAB MANUAL (PITW ) NIHAZRAHIM
printf("\n");
int main()
insert_at_front(&head, 10);
insert_at_front(&head, 20);
insert_at_end(&head, 30);
insert_at_end(&head, 40);
display_forward(head);
display_reverse(head);
display_forward(head);
display_reverse(head);
delete_at_position(&head, 4);
delete_at_position(&head, 1);
delete_at_position(&head, 6);
display_forward(head);
display_reverse(head);
return 0;
ii) Implement a circular linked list and perform insertion, deletion, and traversal.
#include <stdio.h>
#include <stdlib.h>
DS LAB MANUAL (PITW ) NIHAZRAHIM
struct node
int data;
};
new_node->data = data;
if(*head == NULL)
new_node->next = new_node;
*head = new_node;
else
while(temp->next != *head)
temp = temp->next;
temp->next = new_node;
new_node->next = *head;
*head = new_node;
new_node->data = data;
if(*head == NULL)
new_node->next = new_node;
*head = new_node;
else
while(temp->next != *head)
temp = temp->next;
temp->next = new_node;
new_node->next = *head;
if(*head == NULL)
return;
prev_node = (*head)->next;
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(position == 1)
while(prev_node->next != *head)
prev_node = prev_node->next;
cur_node = *head;
*head = (*head)->next;
prev_node->next = *head;
free(cur_node);
else
int i = 1;
prev_node = prev_node->next;
i++;
cur_node = prev_node->next;
prev_node->next = cur_node->next;
free(cur_node);
else
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
printf("Invalid position\n");
if(head == NULL)
printf("List is empty.\n");
else
do
temp = temp->next;
} while(temp != head);
printf("\n");
int main()
insert_at_front(&head, 10);
insert_at_front(&head, 20);
DS LAB MANUAL (PITW ) NIHAZRAHIM
insert_at_end(&head, 30);
insert_at_end(&head, 40);
display_list(head);
delete_at_pos(&head, 3);
display_list(head);
delete_at_pos(&head, 1);
display_list(head);
delete_at_pos(&head, 4);
display_list(head);
return 0;
#include <stdio.h>
#include <stdlib.h>
int stack[MAX_SIZE];
if(top == MAX_SIZE - 1)
printf("Stack overflow.\n");
return;
stack[++top] = data;
DS LAB MANUAL (PITW ) NIHAZRAHIM
int pop()
if(top == -1)
printf("Stack underflow.\n");
return -1;
return stack[top--];
int peek()
if(top == -1)
printf("Stack is empty.\n");
return -1;
return stack[top];
void display()
if(top == -1)
printf("Stack is empty.\n");
else
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
printf("\n");
int main()
push(10);
push(20);
display();
push(30);
push(40);
display();
display();
element = peek();
display();
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node
DS LAB MANUAL (PITW ) NIHAZRAHIM
int data;
};
new_node->data = data;
new_node->next = top;
top = new_node;
int pop()
if(top == NULL)
printf("Stack is empty.\n");
return -1;
top = top->next;
free(temp);
return data;
int peek()
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(top == NULL)
printf("Stack is empty.\n");
return -1;
return top->data;
void display()
if(top == NULL)
printf("Stack is empty.\n");
else
while(temp != NULL)
temp = temp->next;
printf("\n");
int main()
push(10);
DS LAB MANUAL (PITW ) NIHAZRAHIM
push(20);
display();
push(30);
push(40);
display();
display();
element = peek();
display();
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int stack[MAX_SIZE];
if(top == MAX_SIZE - 1)
printf("Stack overflow.\n");
return;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
stack[++top] = data;
int pop()
if(top == -1)
printf("Stack underflow.\n");
return -1;
return stack[top--];
char ch;
ch = expr[i];
if(isdigit(ch))
push(ch - '0');
else
operand2 = pop();
operand1 = pop();
switch(ch)
DS LAB MANUAL (PITW ) NIHAZRAHIM
case '+':
push(operand1 + operand2);
break;
case '-':
push(operand1 - operand2);
break;
case '*':
push(operand1 * operand2);
break;
case '/':
push(operand1 / operand2);
break;
return pop();
int main()
char expr[MAX_SIZE];
scanf("%s", expr);
return 0;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
char stack[MAX_SIZE];
if(top == MAX_SIZE - 1)
printf("Stack overflow.\n");
return;
stack[++top] = symbol;
char pop()
if(top == -1)
printf("Stack underflow.\n");
return -1;
return stack[top--];
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
push(symbol);
if(top == -1)
return false;
if((top_symbol == '(' && symbol != ')') || (top_symbol == '[' && symbol != ']') || (top_symbol
== '{' && symbol != '}'))
return false;
int main()
char expression[MAX_SIZE];
scanf("%s", expression);
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(is_balanced_expression)
else
return 0;
#include <stdio.h>
#include <stdbool.h>
int queue[MAX_SIZE];
bool is_empty()
bool is_full()
if(is_full())
printf("Queue overflow.\n");
return;
else if(is_empty())
front = rear = 0;
else
rear++;
queue[rear] = data;
int dequeue()
if(is_empty())
printf("Queue underflow.\n");
data = queue[front];
else
data = queue[front];
front++;
return data;
void display()
if(is_empty())
printf("Queue is empty.\n");
return;
printf("\n");
int main()
enqueue(10);
enqueue(20);
enqueue(30);
display();
DS LAB MANUAL (PITW ) NIHAZRAHIM
display();
enqueue(40);
enqueue(50);
display();
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int data;
} Node;
bool is_empty()
new_node->data = data;
new_node->next = NULL;
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(is_empty())
else
rear->next = new_node;
rear = new_node;
int dequeue()
if(is_empty())
printf("Queue underflow.\n");
else
data = front->data;
if(front == rear)
else
front = front->next;
DS LAB MANUAL (PITW ) NIHAZRAHIM
free(temp);
return data;
void display()
if(is_empty())
printf("Queue is empty.\n");
return;
while(ptr != NULL)
ptr = ptr->next;
printf("\n");
int main()
enqueue(10);
enqueue(20);
enqueue(30);
display();
display();
enqueue(40);
enqueue(50);
display();
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int job_id;
int pages;
int priority;
} Job;
bool is_empty()
void print_jobs()
if(is_empty())
{
DS LAB MANUAL (PITW ) NIHAZRAHIM
return;
while(temp != NULL)
printf("Job ID: %d, Pages: %d, Priority: %d\n", temp->job_id, temp->pages, temp->priority);
temp = temp->next;
void add_job()
if(MAX_JOBS <= 0)
return;
scanf("%d", &job_id);
scanf("%d", &pages);
scanf("%d", &priority);
new_job->job_id = job_id;
new_job->pages = pages;
DS LAB MANUAL (PITW ) NIHAZRAHIM
new_job->priority = priority;
new_job->next = NULL;
if(is_empty())
else
tail->next = new_job;
tail = new_job;
MAX_JOBS--;
void remove_job()
if(is_empty())
return;
head = head->next;
free(temp);
int main()
DS LAB MANUAL (PITW ) NIHAZRAHIM
int choice = 0;
while(true)
printf("4. Exit\n");
scanf("%d", &choice);
switch(choice)
case 1:
add_job();
break;
case 2:
remove_job();
break;
case 3:
print_jobs();
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
return 0;
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 5
int queue[MAX_SIZE];
bool is_empty()
bool is_full()
if(is_full())
printf("Queue is full.\n");
return;
else if(is_empty())
front = rear = 0;
DS LAB MANUAL (PITW ) NIHAZRAHIM
else
queue[rear] = data;
int dequeue()
if(is_empty())
printf("Queue is empty.\n");
return data;
data = queue[front];
if(front == rear)
else
return data;
void display()
DS LAB MANUAL (PITW ) NIHAZRAHIM
if(is_empty())
printf("Queue is empty.\n");
return;
int i = front;
do
i = (i + 1) % MAX_SIZE;
printf("\n");
int main()
enqueue(10);
enqueue(20);
enqueue(30);
enqueue(40);
enqueue(50);
display();
display();
return 0;
DS LAB MANUAL (PITW ) NIHAZRAHIM
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char stack[MAX_SIZE];
return 0;
return 1;
return 2;
return 3;
else
return -1;
if (top == MAX_SIZE - 1) {
printf("Stack overflow\n");
exit(1);
DS LAB MANUAL (PITW ) NIHAZRAHIM
stack[++top] = element;
char pop()
if (top == -1) {
printf("Stack underflow\n");
exit(1);
top--;
return element;
char peek()
if (top == -1) {
printf("Stack is empty\n");
exit(1);
return stack[top];
int i = 0;
int j = 0;
if (isdigit(infix[i])) {
DS LAB MANUAL (PITW ) NIHAZRAHIM
postfix[j++] = infix[i++];
push(infix[i++]);
postfix[j++] = pop();
i++;
} else {
postfix[j++] = pop();
push(infix[i++]);
postfix[j++] = pop();
postfix[j] = '\0';
int i, result;
int number;
if (isdigit(postfix[i])) {
push(postfix[i] - '0');
} else {
DS LAB MANUAL (PITW ) NIHAZRAHIM
operand2 = pop();
operand1 = pop();
switch (postfix[i]) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
push(result);
return pop();
int main()
char infix[MAX_SIZE];
char postfix[MAX_SIZE];
scanf("%[^\n]", infix);
DS LAB MANUAL (PITW ) NIHAZRAHIM
infix_to_postfix(infix, postfix);
return 0;
#include<stdio.h>
#include<string.h>
int main()
char str[100];
scanf("%s", str);
len = strlen(str);
if (str[i] != str[j]) {
flag = 1;
break;
if (flag == 1)
else
return 0;
DS LAB MANUAL (PITW ) NIHAZRAHIM
iii) Implement a stack or queue to perform comparison and check for symmetry.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
char data;
};
bool is_empty()
temp->data = data;
temp->next = top;
top = temp;
char pop()
if (is_empty()) {
printf("Stack underflow\n");
exit(1);
DS LAB MANUAL (PITW ) NIHAZRAHIM
top = top->next;
free(temp);
return data;
char peek()
if (is_empty()) {
printf("Stack is empty\n");
exit(1);
return top->data;
void print_stack()
if (is_empty()) {
printf("Stack is empty\n");
} else {
temp = temp->next;
printf("\n");
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
int i;
push(str[i]);
if (len % 2 == 1) {
i++;
if (peek() == str[i]) {
pop();
} else {
return false;
i++;
return is_empty();
int main()
char str[MAX_SIZE];
scanf("%[^\n]", str);
if (is_symmetrical(str)) {
} else {
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
if (root == NULL) {
DS LAB MANUAL (PITW ) NIHAZRAHIM
return create_node(data);
} else {
return root;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
int main()
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
DS LAB MANUAL (PITW ) NIHAZRAHIM
inorder(root);
printf("\n");
return 0;
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
};
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
if (root == NULL) {
return create_node(data);
} else {
return root;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
if (root != NULL) {
preorder(root->left);
preorder(root->right);
if (root != NULL) {
postorder(root->left);
postorder(root->right);
DS LAB MANUAL (PITW ) NIHAZRAHIM
int main()
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
printf("\n");
preorder(root);
printf("\n");
postorder(root);
printf("\n");
return 0;
Exercise 9: Hashing
#define TABLE_SIZE 10
#define MAX_NAME 256
typedef struct {
char name[MAX_NAME];
int age;
} person;
person* hash_table[TABLE_SIZE];
unsigned int hash(char* name)
{
int length = strlen(name);
unsigned int hash_value = 0;
for (int i = 0; i < length; i++) {
hash_value += name[i];
hash_value = (hash_value * name[i]) % TABLE_SIZE;
}
return hash_value;
}
person* create_person(char* name, int age)
{
person* new_person = (person*)malloc(sizeof(person));
strncpy(new_person->name, name, MAX_NAME);
new_person->age = age;
return new_person;
}
void insert(person* p)
{
int index = hash(p->name);
for (int i = 0; i < TABLE_SIZE; i++) {
int attempt = (index + i) % TABLE_SIZE;
if (hash_table[attempt] == NULL) {
hash_table[attempt] = p;
return;
}
}
printf("Hash table is full\n");
}
person* search(char* name)
{
int index = hash(name);
for (int i = 0; i < TABLE_SIZE; i++) {
int attempt = (index + i) % TABLE_SIZE;
if (hash_table[attempt] != NULL && strcmp(hash_table[attempt]->name, name) == 0)
{
return hash_table[attempt];
DS LAB MANUAL (PITW ) NIHAZRAHIM
}
}
return NULL;
}
void delete(person* p)
{
int index = hash(p->name);
for (int i = 0; i < TABLE_SIZE; i++) {
int attempt = (index + i) % TABLE_SIZE;
hash_table[attempt] = NULL;
free(p);
return;
}
}
printf("Person not found in hash table\n");
}
void print_hash_table()
{
printf("Hash table:\n");
printf("-----------\n");
for (int i = 0; i < TABLE_SIZE; i++) {
if (hash_table[i] != NULL) {
printf("%d:%s:%d\n", i, hash_table[i]->name, hash_table[i]->age);
} else {
printf("%d:NULL\n", i);
}
}
}
int main()
{
for (int i = 0; i < TABLE_SIZE; i++) {
hash_table[i] = NULL;
}
insert(create_person("Alice", 25));
insert(create_person("Bob", 30));
insert(create_person("Charlie", 35));
insert(create_person("Dave", 40));
insert(create_person("Eve", 45));
insert(create_person("Frank", 50));
insert(create_person("Grace", 55));
insert(create_person("Henry", 60));
print_hash_table();
person* result = search("Alice");
DS LAB MANUAL (PITW ) NIHAZRAHIM
if (result == NULL) {
printf("Person not found in hash table\n");
} else {
printf("%s:%d\n", result->name, result->age);
}
delete(result);
print_hash_table();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CACHE_SIZE 10
typedef struct {
char key[MAX_KEY];
char value[MAX_VALUE];
} cache_entry;
cache_entry* hash_table[CACHE_SIZE];
hash_value += key[i];
return hash_value;
}
DS LAB MANUAL (PITW ) NIHAZRAHIM
return new_entry;
if (hash_table[attempt] == NULL) {
return;
return;
printf("Cache is full\n");
return hash_table[attempt]->value;
return NULL;
free(hash_table[attempt]);
hash_table[attempt] = NULL;
return;
void print_cache()
printf("Cache:\n");
printf("------\n");
if (hash_table[i] != NULL) {
} else {
printf("NULL\n");
int main()
hash_table[i] = NULL;
put("key1", "value1");
put("key2", "value2");
put("key3", "value3");
put("key4", "value4");
put("key5", "value5");
put("key6", "value6");
put("key7", "value7");
put("key8", "value8");
put("key9", "value9");
put("key10", "value10");
print_cache();
printf("%s\n", get("key1"));
printf("%s\n", get("key2"));
printf("%s\n", get("key3"));
printf("%s\n", get("key4"));
printf("%s\n", get("key5"));
printf("%s\n", get("key6"));
DS LAB MANUAL (PITW ) NIHAZRAHIM
printf("%s\n", get("key7"));
printf("%s\n", get("key8"));
printf("%s\n", get("key9"));
printf("%s\n", get("key10"));
delete("key1");
print_cache();
return 0;