Ds Lab Manual
Ds Lab Manual
Laboratory Manual
DEPARTMENT OF MASTER OF
COMPUTER APPLICATIONS
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY KAKINADA
(Established by Govt. of A.P., ACT No.30 of 2008)
KAKINADA – 533 003 (A.P) INDIA
MCA COURSE STRUCTURE & SYLLABUS
(Applicable for batches admitted from 2024
2024-25)
MCA I Semester L T P C
0 0 4 2
DATA STRUCTURES USING C LAB
Course Objectives:
This Course will enable students to
• Design and implement various data structures.
• Implement operations like searching, insertion, and deletion, traversing
mechanism
• Develop applications using data structure algorithms.
Experiment 1:
a) Write a program in C to display the n terms of even natural numbers and
their sum.
b) Write a program in C to display the n terms of harmonic series and their
sum. 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.
c) Write a C program to check whether a given number is an Armstrong
number or not
d) Write a C program to calculate the factorial of a given number.
Experiment 2:
a) Write a program in C for multiplication of two square Matrices.
b) Write a program in C to find the transpose of a given matrix.
Experiment 3:
a) Write a program in C to check whether a number is a prime number or not
using the function.
b) Write a recursive program which computes the nth Fibonacci number, for
appropriate values of n.
c) Write a program in C to add numbers using call by reference.
Experiment 4:
a) Write a program in C to append multiple lines at the end of a text file.
b) Write a program in C to copy a file in another name
Experiment 5:
Write recursive program for the following
a) Write recursive and non recursive C program for calculation of Factorial of an
integer.
b) Write recursive and non recursive C program for calculation of GCD (n, m)
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY KAKINADA
(Established by Govt. of A.P., ACT No.30 of 2008)
KAKINADA – 533 003 (A.P) INDIA
MCA COURSE STRUCTURE & SYLLABUS
(Applicable for batches admitted from 2024
2024-25)
c) Write recursive and non recursive C program for Towers of Hanoi: N disks
are to be transferred from peg S to peg D with Peg I as the intermediate
peg.
Experiment 6:
a) Write a C program that uses both recursive and non recursive functions to
perform Linear search for a Key value in a given list.
b) Write a C program that uses both recursive and non recursive functions to
perform Binary search for a Key value in a given list.
Experiment 7:
a) Write a C program that implements stack (its operations) using
using arrays.
b) Write a C program that implements stack (its operations) using Linked list.
Experiment 8:
a) Write a C program that uses Stack operations to convert infix expressions into
postfix expressions.
a) Write a C program that implements Queue (its operations) using arrays.
b) Write a C program that implements Queue (its operations) using linked lists.
Experiment 9:
Write a C program that uses functions to create a singly linked list and perform
various operations on it.
Experiment 10:
Write a C program to store a polynomial expression in memory using a linked list and
perform polynomial addition.
Experiment 11:
a) Write a recursive C program for traversing a binary tree in preorder, in order and
post order.
b) Write a non recursive C program for traversing a binary tree in preorder, in order
and post order.
Experiment 12:
Implementation of Hash table using double hashing as collision resolution function.
Experiment 13:
Implementation of Binary Search trees-
trees Insertion and deletion..
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY KAKINADA
(Established by Govt. of A.P., ACT No.30 of 2008)
KAKINADA – 533 003 (A.P) INDIA
MCA COURSE STRUCTURE & SYLLABUS
(Applicable for batches admitted from 2024
2024-25)
Experiment 14:
Implementation of AVL Tree – Insertion and Deletion
Experiment 15:
a) Write a C program that implements Bubble sort, to sort a given list of integers in
ascending order.
b) Write a C program that implements Quick sort, to sort a given list of integers in
ascending order.
c) Write C program that implement Merge sort, to sort a given list of integers in
ascending order
Web resources:
1. https://ds1-iiith.vlabs.ac.in/
iiith.vlabs.ac.in/
2. https://profile.iiita.ac.in/bibhas.ghoshal/teaching_ds_lab.html
3. https://moodle.sit.ac.in/blog/data
https://moodle.sit.ac.in/blog/data-structures-laboratory/
4. https://dsalab.netlify.app/
5. https://www.vtuloop.com/data
https://www.vtuloop.com/data-structure-lab-programs-all/
DATA STRUCTURES USING C LAB
Experiment 1
a) Write a program in C to display the n terms of even natural numbers and their
sum.
Program:
#include <stdio.h>
int main() {
int n, sum = 0,even_num=0;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("The even natural numbers are: ");
for(int i = 0; i <n; i++) {
even_num+= 2;
printf("%d ", even_num);
sum += even_num;
}
printf("\nThe sum of the even natural numbers is: %d", sum);
return 0;
}
Output:
Enter the number of terms: 12
The even natural numbers are: 2 4 6 8 10 12 14 16 18 20 22 24
The sum of the even natural numbers is: 156
b) Write a program in C to display the n terms of harmonic series and their sum. 1 +
1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms.
Program:
#include <stdio.h>
int main() {
int n;
float sum = 0.0;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("The harmonic series is: ");
for(int i = 1; i <= n; i++) {
float term = (float)1 / i;
printf("%f ", term);
sum += term;
}
printf("\nThe sum of the harmonic series is: %f\n", sum);
return 0;
}
Output:
Enter the number of terms: 10
The harmonic series is: 1.000000 0.500000 0.333333 0.250000 0.200000 0.166667
0.142857 0.125000 0.111111 0.100000
The sum of the harmonic series is: 2.928968
Experiment 2
a) Write a program in C for multiplication of two square Matrices.
Program:
#include <stdio.h>
#define SIZE 3 // size of the square matrix
void multiply_matrices(int matrix1[SIZE][SIZE], int matrix2[SIZE][SIZE], int
result[SIZE][SIZE]) {
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
result[i][j] = 0;
for(int k = 0; k < SIZE; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void print_matrix(int matrix[SIZE][SIZE]) {
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int matrix1[SIZE][SIZE], matrix2[SIZE][SIZE], result[SIZE][SIZE];
printf("Enter elements of matrix 1:\n");
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
scanf("%d", &matrix1[i][j]);
}
}
printf("Enter elements of matrix 2:\n");
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++) {
scanf("%d", &matrix2[i][j]);
}
}
multiply_matrices(matrix1, matrix2, result);
printf("Matrix 1:\n");
print_matrix(matrix1);
printf("Matrix 2:\n");
print_matrix(matrix2);
printf("Resultant Matrix:\n");
print_matrix(result);
return 0;
}
Output:
Enter elements of matrix 1:
234456789
Enter elements of matrix 2:
234456789
Matrix 1:
234
456
789
Matrix 2:
234
456
789
Resultant Matrix:
44 53 62
70 85 100
109 133 157
Experiment 3
a) Write a program in C to check whether a number is a prime number or not using
the function.
Program:
#include <stdio.h>
int is_prime(int num) {
if(num <= 1) {
return 0;
}
for(int i = 2; i<= num/2; i++) {
if(num % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(is_prime(num)) {
printf("%d is a prime number.", num);
} else {
printf("%d is not a prime number.", num);
}
return 0;
}
Output:
Enter a number: 23
23 is a prime number.
b) Write a recursive program which computes the nth Fibonacci number, for
appropriate values of n.
Program:
#include <stdio.h>
int fibonacci(int n) {
if(n == 0 || n == 1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("The %dth Fibonacci number is: %d\n", n, fibonacci(n));
return 0;
}
Output:
Enter a positive integer: 10
The 10th Fibonacci number is: 55
Experiment 4
a) Write a program in C to append multiple lines at the end of a text file.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
char line[256];
char filename[100];
int i, numLines;
printf("Enter the filename to append to: ");
scanf("%s", filename);
file = fopen(filename, "a");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
printf("How many lines do you want to append? ");
scanf("%d", &numLines);
getchar(); // To consume the newline character left by scanf
for (i = 0; i < numLines; i++) {
printf("Enter line %d: ", i + 1);
fgets(line, sizeof(line), stdin);
fprintf(file, "%s", line);
}
fclose(file);
printf("Lines appended successfully.");
return EXIT_SUCCESS;
}
Output:
Enter the filename to append to: test.txt
How many lines do you want to append? 4
Enter line 1: hello
Enter line 2: hai
Enter line 3: this is
Enter line 4: test code
Lines appended successfully.
b) Write a program in C to copy a file in another name
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *sourceFile, *destFile;
char sourceFilename[100], destFilename[100];
char ch;
printf("Enter the source filename: ");
scanf("%s", sourceFilename);
printf("Enter the destination filename: ");
scanf("%s", destFilename);
sourceFile = fopen(sourceFilename, "r");
if (sourceFile == NULL) {
perror("Error opening source file");
return EXIT_FAILURE;
}
destFile = fopen(destFilename, "w");
if (destFile == NULL) {
perror("Error opening destination file");
fclose(sourceFile);
return EXIT_FAILURE;
}
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destFile);
}
fclose(sourceFile);
fclose(destFile);
printf("File copied successfully.\n");
return EXIT_SUCCESS;
}
Output:
Enter the source filename: ochote.txt
Enter the destination filename: Test.txt
File copied successfully.
Experiment 5
Write recursive program for the following
a) Write recursive and non recursive C program for calculation of Factorial of an
integer.
Recursive Progaram:
#include <stdio.h>
unsigned long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d = %llu\n", num, factorial(num));
}
return 0;
}
Output:
Enter a positive integer: 5
Factorial of 5 = 120
b) Write recursive and non recursive C program for calculation of GCD (n, m).
Recursive Program:
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two positive integers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d = %d", num1, num2, gcd(num1, num2));
return 0;
}
Output:
Enter two positive integers: 55 65
GCD of 55 and 65 = 5
c) Write recursive and non recursive C program for Towers of Hanoi: N disks are to
be transferred from peg S to peg D with Peg I as the intermediate peg.
Recursive Program:
#include <stdio.h>
void towersOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
printf("Move disk 1 from peg %c to peg %c\n", source, destination);
return;
}
towersOfHanoi(n - 1, source, auxiliary, destination);
printf("Move disk %d from peg %c to peg %c\n", n, source, destination);
towersOfHanoi(n - 1, auxiliary, destination, source);
}
int main() {
int num;
printf("Enter the number of disks: ");
scanf("%d", &num);
towersOfHanoi(num, 'A', 'C', 'B'); // A, B and C are names of rods
return 0;
}
Output:
Enter the number of disks: 3
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
Experiment 6
a) Write a C program that uses both recursive and non recursive functions to perform
Linear search for a Key value in a given list.
Recursive Program:
#include <stdio.h>
int linearSearchRecursive(int arr[], int n, int key, int index) {
if (index >= n) {
return -1;
}
if (arr[index] == key) {
return index;
}
return linearSearchRecursive(arr, n, key, index + 1);
}
int main() {
int arr[100], n, key, pos, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the key to search: ");
scanf("%d", &key);
pos = linearSearchRecursive(arr, n, key, 0);
if (pos >= 0) {
printf("Element found at position %d\n", pos + 1);
} else {
printf("Element not found in the array\n");
}
return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array: 1 2 3 4 5
Enter the key to search: 5
Element found at position 5
b) Write a C program that uses both recursive and non recursive functions to perform
Binary search for a Key value in a given list.
Recursive Program:
#include<stdio.h>
int binarySearchRecursive(int arr[], int left, int right, int key) {
if (right >= left) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid; // Key found
}
if (arr[mid] > key) {
return binarySearchRecursive(arr, left, mid - 1, key);
}
return binarySearchRecursive(arr, mid + 1, right, key);
}
return -1; // Key not found
}
int main() {
int arr[100], n, key, pos, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the key to search: ");
scanf("%d", &key);
pos = binarySearchRecursive(arr,0,n,key);
if (pos >= 0) {
printf("Element found at position %d\n", pos + 1);
} else {
printf("Element not found in the array\n");
}
return 0;
}
Output:
Enter the size of the array: 5
Enter the elements of the array: 2 3 4 5 6
Enter the key to search: 3
Element found at position 2
Non Recursive:
#include<stdio.h>
int binarySearchIterative(int arr[], int size, int key) {
int left = 0;
int right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid;
}
if (arr[mid] > key) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
return -1;
}
int main() {
int arr[100], n, key, pos, i;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the key to search: ");
scanf("%d", &key);
pos = binarySearchIterative(arr,n,key);
if (pos >= 0) {
printf("Element found at position %d\n", pos + 1);
} else {
printf("Element not found in the array\n");
}
return 0;
}
Output:
Enter the size of the array: 6
Enter the elements of the array: 3 4 5 6 7 8
Enter the key to search: 6
Element found at position 4
Experiment 7
a) Write a C program that implements stack (its operations) using arrays.
Program:
#include <stdio.h>
#define MAX_SIZE 100
int top = -1;
int stack[MAX_SIZE];
void push(int element) {
if (top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = element;
}
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() {
int i;
if (top == -1) {
printf("Stack is empty\n");
return;
}
printf("Stack elements: ");
for (i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
int main() {
int choice, element;
while (1) {
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to push: ");
scanf("%d", &element);
push(element);
break;
case 2:
element = pop();
if (element != -1) {
printf("Popped element: %d\n", element);
}
break;
case 3:
element = peek();
if (element != -1) {
printf("Top element: %d\n", element);
}
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}
Output:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the element to push: 100
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the element to push: 200
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the element to push: 300
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top element: 300
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped element: 300
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top element: 200
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack elements: 100 200
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the element to push: 500
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack elements: 100 200 500
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 5
b) Write a C program that implements stack (its operations) using Linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* top = NULL;
void push(int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = top;
top = newNode;
}
int pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return -1;
}
int element = top->data;
Node* temp = top;
top = top->next;
free(temp);
return element;
}
int peek() {
if (top == NULL) {
printf("Stack is empty\n");
return -1;
}
return top->data;
}
void display() {
Node* temp = top;
if (temp == NULL) {
printf("Stack is empty\n");
return;
}
printf("Stack elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, element;
while (1) {
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to push: ");
scanf("%d", &element);
push(element);
break;
case 2:
element = pop();
if (element != -1) {
printf("Popped element: %d\n", element);
}
break;
case 3:
element = peek();
if (element != -1) {
printf("Top element: %d\n", element);
}
break;
case 4:
display();
break;
case 5:
return 0;
default:
printf("Invalid choice\n");
}
}
return 0;
}
Output:
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the element to push: 100
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 1
Enter the element to push: 200
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack elements: 100 200
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 3
Top element: 200
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 2
Popped element: 200
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 4
Stack elements: 100
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Enter your choice: 5
Experiment 8
a) Write a C program that uses Stack operations to convert infix expressions into
postfix expressions.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct Stack {
char data[MAX_SIZE];
int top;
} Stack;
void initStack(Stack* stack) {
stack->top = -1;
}
int isEmpty(Stack* stack) {
return stack->top == -1;
}
int isFull(Stack* stack) {
return stack->top == MAX_SIZE - 1;
}
void push(Stack* stack, char element) {
if (isFull(stack)) {
printf("Stack Overflow\n");
return;
}
stack->data[++stack->top] = element;
}
char pop(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
return stack->data[stack->top--];
}
char peek(Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
}
return stack->data[stack->top];
}
int precedence(char operator) {
if (operator == '+' || operator == '-') {
return 1;
} else if (operator == '*' || operator == '/') {
return 2;
} else {
return 0;
}
}
void infixToPostfix(char infix[], char postfix[]) {
Stack stack;
initStack(&stack);
int i, j = 0;
for (i = 0; i < strlen(infix); i++) {
if (infix[i] == ' ') {
continue;
} else if (infix[i] == '(') {
push(&stack, infix[i]);
} else if (infix[i] == ')') {
while (!isEmpty(&stack) && peek(&stack) != '(') {
postfix[j++] = pop(&stack);
}
if (isEmpty(&stack)) {
printf("Invalid infix expression\n");
return;
}
pop(&stack);
} else if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/') {
while (!isEmpty(&stack) && peek(&stack) != '(' && precedence(infix[i]) <=
precedence(peek(&stack))) {
postfix[j++] = pop(&stack);
}
push(&stack, infix[i]);
} else {
postfix[j++] = infix[i];
}
}
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
postfix[j] = '\0';
}
int main() {
char infix[100], postfix[100];
printf("Enter the infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
}
Output:
Enter the infix expression: A+B*C+D
Postfix expression: ABC*+D+
c)Write a C program that implements Queue (its operations) using linked lists.
Program:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct Queue {
Node* front;
Node* rear;
} Queue;
void initQueue(Queue* queue) {
queue->front = queue->rear = NULL;
}
int isEmpty(Queue* queue) {
return queue->front == NULL;
}
void enqueue(Queue* queue, int element) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = element;
newNode->next = NULL;
if (isEmpty(queue)) {
queue->front = queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}
}
int dequeue(Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty\n");
return -1;
}
int element = queue->front->data;
Node* temp = queue->front;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
}
free(temp);
return element;
}
int front(Queue* queue) {
if (isEmpty(queue)) {
printf("Queue is empty\n");
return -1;
}
return queue->front->data;
}
void display(Queue* queue) {
Node* temp = queue->front;
if (isEmpty(queue)) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
Queue queue;
initQueue(&queue);
int choice, element;
while (1) {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Front\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to enqueue: ");
scanf("%d", &element);
enqueue(&queue, element);
break;
case 2:
element = dequeue(&queue);
if (element != -1) {
printf("Dequeued element: %d\n", element);
}
break;
case 3:
element = front(&queue);
if (element != -1) {
printf("Front element: %d\n", element);
}
break;
case 4:
display(&queue);
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
Output:
1. Enqueue
2. Dequeue
3. Front
4. Display
5. Exit
Enter your choice: 1
Enter the element to enqueue: 100
1. Enqueue
2. Dequeue
3. Front
4. Display
5. Exit
Enter your choice: 1
Enter the element to enqueue: 300
1. Enqueue
2. Dequeue
3. Front
4. Display
5. Exit
Enter your choice: 3
Front element: 100
1. Enqueue
2. Dequeue
3. Front
4. Display
5. Exit
Enter your choice: 4
Queue elements: 100 300
1. Enqueue
2. Dequeue
3. Front
4. Display
5. Exit
Enter your choice: 2
Dequeued element: 100
1. Enqueue
2. Dequeue
3. Front
4. Display
5. Exit
Enter your choice: 4
Queue elements: 300
1. Enqueue
2. Dequeue
3. Front
4. Display
5. Exit
Enter your choice: 5
Experiment 9
Write a C program that uses functions to create a singly linked list and perform various
operations on it.
Program:
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a linked list node
typedef struct Node {
int data;
struct Node* next;
} Node;
// Function to create a new node
Node* createNode(int data) {
Node* newNode = (Node*) malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to insert a node at the beginning of the list
void insertAtBeginning(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
*head = newNode;
}
}
// Function to insert a node at the end of the list
void insertAtEnd(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Function to delete a node from the list
void deleteNode(Node** head, int data) {
if (*head == NULL) return;
if ((*head)->data == data) {
Node* temp = *head;
*head = (*head)->next;
free(temp);
return;
}
Node* temp = *head;
while (temp->next != NULL) {
if (temp->next->data == data) {
Node* nodeToDelete = temp->next;
temp->next = temp->next->next;
free(nodeToDelete);
return;
}
temp = temp->next;
}
}
// Function to print the linked list
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Function to free the linked list
void freeList(Node* head) {
Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
int main() {
Node* head = NULL;
// Insert nodes at the beginning and end of the list
insertAtBeginning(&head, 10);
insertAtEnd(&head, 20);
insertAtBeginning(&head, 5);
insertAtEnd(&head, 30);
// Print the linked list
printf("Linked List: ");
printList(head);
// Delete a node from the list
deleteNode(&head, 20);
// Print the linked list after deletion
printf("Linked List after deletion: ");
printList(head);
// Free the linked list
freeList(head);
return 0;
}
Output:
Linked List: 5 -> 10 -> 20 -> 30 -> NULL
Linked List after deletion: 5 -> 10 -> 30 -> NULL
Experiment 10
Write a C program to store a polynomial expression in memory using a linked list and
perform polynomial addition.
Program:
#include <stdio.h>
#include <stdlib.h>
return result;
}
int main() {
Term* poly1 = NULL;
Term* poly2 = NULL;
Term* result = NULL;
return 0;
}
Output:
Polynomial 1: 2x^2 + 3x^1 + 1x^0 + 0
Polynomial 2: 1x^2 + 2x^1 + 1x^0 + 0
Result: 3x^2 + 5x^1 + 2x^0 + 0
Experiment 11
a) Write a recursive C program for traversing a binary tree in preorder, in order and
post order.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return 0;
}
Output:
Preorder Recursive: 1 2 4 5 3
Inorder Recursive: 4 2 5 1 3
Postorder Recursive: 4 5 2 3 1
b) Write a non recursive C program for traversing a binary tree in preorder, in order
and post order.
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return 0;
}
Output:
Preorder Non-Recursive: 1 2 4 5 3
Inorder Non-Recursive: 4 2 5 1 3
Postorder Non-Recursive: 3 1 5 2 4
Experiment 12
Implementation of Hash table using double hashing as collision resolution function.
Program:
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
return ht;
}
ht->table[index] = key;
ht->count++;
}
// Example usage
int main() {
HashTable *ht = createHashTable(TABLE_SIZE);
insert(ht, 10);
insert(ht, 20);
insert(ht, 30);
insert(ht, 40);
printHashTable(ht);
delete(ht, 20);
printf("After deleting 20:\n");
printHashTable(ht);
insert(ht, 50);
printf("After inserting 50:\n");
printHashTable(ht);
freeHashTable(ht);
return 0;
}
Output:
Index 0: 10
Index 1: [empty]
Index 2: [empty]
Index 3: 20
Index 4: 30
Index 5: 40
Index 6: [empty]
Index 7: [empty]
Index 8: [empty]
Index 9: [empty]
Search for 20: 1
Search for 50: 0
After deleting 20:
Index 0: 10
Index 1: [empty]
Index 2: [empty]
Index 3: [deleted]
Index 4: 30
Index 5: 40
Index 6: [empty]
Index 7: [empty]
Index 8: [empty]
Index 9: [empty]
After inserting 50:
Index 0: 10
Index 1: [empty]
Index 2: [empty]
Index 3: [deleted]
Index 4: 30
Index 5: 40
Index 6: 50
Index 7: [empty]
Index 8: [empty]
Index 9: [empty]
Experiment 13
Implementation of Binary Search trees insertion and deletion
Program:
#include <stdio.h>
#include <stdlib.h>
// Node with two children: Get the inorder successor (smallest in the right
subtree)
Node *temp = findMin(root->right);
// Insert nodes
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
// Delete a node
printf("Deleting 20...\n");
root = delete(root, 20);
printf("In-order traversal after deleting 20: ");
inOrder(root);
printf("\n");
return 0;
}
Output:
In-order traversal: 20 30 40 50 60 70 80
Deleting 20...
In-order traversal after deleting 20: 30 40 50 60 70 80
Experiment 14
Implementation of AVL Tree insertion and deletion
Program:
#include <stdio.h>
#include <stdlib.h>
// Right rotation
Node* rightRotate(Node *y) {
Node *x = y->left;
Node *T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = 1 + (getHeight(y->left) > getHeight(y->right) ? getHeight(y->left) :
getHeight(y->right));
x->height = 1 + (getHeight(x->left) > getHeight(x->right) ? getHeight(x->left) :
getHeight(x->right));
// Left rotation
Node* leftRotate(Node *x) {
Node *y = x->right;
Node *T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = 1 + (getHeight(x->left) > getHeight(x->right) ? getHeight(x->left) :
getHeight(x->right));
y->height = 1 + (getHeight(y->left) > getHeight(y->right) ? getHeight(y->left) :
getHeight(y->right));
// No child case
if (temp == NULL) {
temp = root;
root = NULL;
} else {
*root = *temp; // Copy the contents of the non-empty child
}
free(temp);
} else {
// Node with two children: Get the inorder successor (smallest in the right
subtree)
Node* temp = minValueNode(root->right);
root->data = temp->data; // Copy the inorder successor's data
root->right = deleteNode(root->right, temp->data); // Delete the inorder
successor
}
}
if (root == NULL) {
return root; // Tree had only one node, it is now empty
}
// Insert nodes
root = insert(root, 10);
root = insert(root, 20);
root = insert(root, 30);
root = insert(root, 40);
root = insert(root, 50);
root = insert(root, 25);
// Delete nodes
printf("Deleting 20...\n");
root = deleteNode(root, 20);
printf("In-order traversal after deleting 20: ");
inOrder(root);
printf("\n");
printf("Deleting 30...\n");
root = deleteNode(root, 30);
printf("In-order traversal after deleting 30: ");
inOrder(root);
printf("\n");
return 0;
}
Output:
In-order traversal: 10 20 25 30 40 50
Deleting 20...
In-order traversal after deleting 20: 10 25 30 40 50
Deleting 30...
In-order traversal after deleting 30: 10 25 40 50
Experiment 15
a) Write a C program that implements Bubble sort, to sort a given list of integers in
ascending order.
Program:
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:
Sorted array:
11 12 22 25 34 64 90
b) Write a C program that implements Quick sort, to sort a given list of integers
ascending order.
Program:
#include <stdio.h>
return (i + 1);
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:
Sorted array:
11 12 22 25 34 64 90
c) Write C program that implement Merge sort, to sort a given list of integers in
ascending order.
Program:
#include <stdio.h>
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:
Sorted array:
11 12 22 25 34 64 90