INDEX
DATE OF TEACHER’S
[Link]. LIST OF EXPERIMENTS GRADE
SUBMISSION SIGNATURE
Perform Linear and Binary search
1.
on an array.
Create a stack and perform
PUSH, POP, and Traverse
2.
operations on the stack using
array.
Create a stack and perform
PUSH, POP, and Traverse
3.
operations on the stack using
linked list.
Create a Linear Queue using
Linked list and implement
4. different operations such as
Insertion, Deletion, and Display
the queue elements.
Implement the following sorting
5. techniques: Insertion, Merge,
Bubble, Selection Sort
Create a linked list with nodes
having information about a
6.
student. Insert a new node at
the specified position.
1
Create a doubly linked list with
nodes having information about
an employee and perform
7.
insertion at front of double
linked list and perform deletion
at end of the doubly linked list.
Create a circular linked list
having information about a
8. college and perform Insertion at
the front end and perform
deletion at the end.
Create a Binary Tree and perform
Tree traversals (Preorder,
9.
Postorder, Inorder) using the
concept of recursion.
Implement insertion, deletion
and display (Inorder, Preorder,
Postorder) on binary search tree
10. with the information in the tree
about the details of an
automobile (type, company, year
of make)
2
EXPERIMENT – 1
AIM: Perform Linear and Binary search on an array.
ALGORITHM:
1. Linear Search Algorithm:
1. Start from the first element of the array.
2. Compare each element with the target element.
3. If the target element is found, return its position.
4. If the entire array is traversed without finding the target, return -1.
2. Binary Search Algorithm:
1. Set low to the first index and high to the last index.
2. Calculate the middle index: mid = (low + high) / 2.
3. If the target element equals the middle element, return the middle index.
4. If the target element is less than the middle element, search in the left half ( high = mid
- 1).
5. If the target element is greater than the middle element, search in the right half (low =
mid + 1).
6. Repeat the process until the element is found or low > high, indicating the element is not
in the array.
CODE:
#include <stdio.h>
void linearSearch(int arr[], int size, int key)
{
for (int i = 0; i < size; i++)
{
if (arr[i] == key)
{
printf("Element %d found at index %d.\n", key, i);
return;
}
}
printf("Element %d not found.\n", key);
}
int binarySearch(int arr[], int low, int high, int key)
3
{
while (low <= high)
{
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main()
{
int n, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[100];
for (int i = 0; i < n; i++)
{
printf("Enter the element: ");
scanf("%d", &arr[i]);
}
printf("Enter the target: ");
scanf("%d", &key);
printf("Linear Search:\n");
linearSearch(arr, n, key);
printf("Binary Search:\n");
int result = binarySearch(arr, 0, n - 1, key);
if (result != -1)
{
printf("Element %d found at index %d.\n", key, result);
}
else
{
printf("Element %d not found.\n", key);
}
return 0;
}
4
EXPERIMENT – 2
AIM: Create a stack and perform PUSH, POP, and Traverse operations on the stack
using array.
ALGORITHM:
1. Stack (Array Implementation) - PUSH Operation:
1. Check if the stack is full.
2. If full, report overflow.
3. If not, increment the top pointer.
4. Insert the element at the new top position.
2. Stack (Array Implementation) - POP Operation:
1. Check if the stack is empty.
2. If empty, report underflow.
3. If not, remove the element at the top position.
4. Decrement the top pointer.
CODE:
#include <stdio.h>
#define MAX 5
int stack[MAX];
int top = -1;
void push(int value)
{
if (top == MAX - 1)
{
printf("Stack Overflow\n");
return;
}
stack[++top] = value;
}
void pop()
{
if (top == -1)
{
5
printf("Stack Underflow\n");
return;
}
printf("Popped element: %d\n", stack[top--]);
}
void traverse()
{
printf("Stack is:\n");
if (top == -1)
{
printf("Stack is empty\n");
return;
}
for (int i = top; i >= 0; i--)
{
printf("%d ", stack[i]);
}
printf("\n");
}
int main()
{
int choice, value;
do
{
printf("Enter the choice: \n1 - PUSH\n2 - POP\n3 - Traverse\n4 -
Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the value to PUSH\n");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
traverse();
break;
case 4:
return 0;
}
} while (choice != 4);
return 0;
}
6
EXPERIMENT – 3
AIM: Create a stack and perform PUSH, POP, and Traverse operations on the stack
using linked list.
ALGORITHM:
1. Stack (Linked List Implementation) - PUSH Operation:
1. Create a new node with the given value.
2. Set the new node's next pointer to the current top.
3. Update the top to point to the new node.
2. Stack (Linked List Implementation) - POP Operation:
1. Check if the stack is empty.
2. If empty, report underflow.
3. If not, set a temporary pointer to the current top.
4. Update the top to point to the next node.
5. Free the memory of the old top node.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *top = NULL;
void push(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = top;
top = newNode;
7
}
void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
}
struct Node *temp = top;
printf("Popped element: %d\n", top->data);
top = top->next;
free(temp);
}
void traverse() {
printf("Stack is:\n");
if (top == NULL) {
printf("Stack is empty\n");
return;
}
struct Node *temp = top;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, value;
do {
printf("\nEnter your choice:\n");
printf("1 - PUSH\n2 - POP\n3 - TRAVERSE\n4 - EXIT\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to PUSH: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
traverse();
break;
8
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 4);
return 0;
}
9
EXPERIMENT – 4
AIM: Create a Linear Queue using Linked list and implement different operations
such as Insertion, Deletion, and Display the queue elements.
ALGORITHM:
1. Queue (Linked List Implementation) - Insertion:
1. Create a new node with the given value.
2. If the queue is empty, set the new node as both front and rear.
3. Otherwise, set the rear node's next pointer to the new node.
4. Update the rear pointer to the new node.
2. Queue (Linked List Implementation) - Deletion:
1. Check if the queue is empty.
2. If empty, report underflow.
3. If not, remove the front node and update the front pointer to the next node.
4. If the front becomes NULL, set the rear to NULL.
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *front = NULL;
struct Node *rear = NULL;
void enqueue(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = NULL;
10
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
}
void dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return;
}
struct Node *temp = front;
printf("Dequeued element: %d\n", front->data);
front = front->next;
if (front == NULL) {
rear = NULL;
}
free(temp);
}
void display() {
printf("Queue is:\n");
if (front == NULL) {
printf("Queue is empty\n");
return;
}
struct Node *temp = front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, value;
11
do {
printf("\nEnter your choice:\n");
printf("1 - Enqueue\n2 - Dequeue\n3 - Display\n4 - Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 4);
return 0;
}
12
EXPERIMENT – 5
AIM: Implement the following sorting techniques: Insertion, Merge, Bubble,
Selection Sort
ALGORITHM:
1. Insertion Sort Algorithm:
1. Start from the second element of the array.
2. Compare the current element with the elements before it.
3. Shift elements one position ahead to make room for the current element.
4. Insert the current element in the correct position.
5. Repeat the process for all elements.
2. Merge Sort Algorithm:
1. Divide the array into two halves.
2. Recursively sort each half.
3. Merge the two sorted halves into a single sorted array.
3. Bubble Sort Algorithm:
1. Compare adjacent elements in the array.
2. Swap them if they are in the wrong order.
3. Continue the process until no swaps are needed.
4. Repeat for all elements, reducing the range of comparison each time.
4. Selection Sort Algorithm:
1. Find the minimum element in the unsorted portion of the array.
2. Swap it with the first element of the unsorted portion.
3. Repeat for the remaining unsorted portion of the array.
CODE:
#include <stdio.h>
#include <stdlib.h>
// Insertion Sort
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
13
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
// Merge Function (for Merge Sort)
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));
for (int i = 0; i < n1; i++)
L[i] = arr[left + i];
for (int j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
free(L);
free(R);
}
// Merge Sort
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
14
merge(arr, left, mid, right);
}
}
// Bubble Sort
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// Print Array
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed!\n");
15
return 1;
}
for (int i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Create copies for each sort
int *arr1 = (int *)malloc(n * sizeof(int));
int *arr2 = (int *)malloc(n * sizeof(int));
int *arr3 = (int *)malloc(n * sizeof(int));
int *arr4 = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
arr1[i] = arr[i];
arr2[i] = arr[i];
arr3[i] = arr[i];
arr4[i] = arr[i];
}
printf("\nInsertion Sort:\n");
insertionSort(arr1, n);
printArray(arr1, n);
printf("\nMerge Sort:\n");
mergeSort(arr2, 0, n - 1);
printArray(arr2, n);
printf("\nBubble Sort:\n");
bubbleSort(arr3, n);
printArray(arr3, n);
printf("\nSelection Sort:\n");
selectionSort(arr4, n);
printArray(arr4, n);
// Free all allocated memory
free(arr);
free(arr1);
free(arr2);
free(arr3);
free(arr4);
return 0;
}
16
EXPERIMENT – 6
AIM: Create a linked list with nodes having information about a student. Insert a
new node at the specified position.
ALGORITHM:
1. Linked List - Insertion at Specific Position:
1. Traverse the list until the desired position is reached.
2. Create a new node.
3. Set the new node's next pointer to the next node of the current node.
4. Update the current node’s next pointer to the new node.
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Student {
int rollNo;
char name[50];
struct Student *next;
};
struct Student *head = NULL;
void insertAtPosition(int position, int rollNo, char *name) {
struct Student *newNode = (struct Student *)malloc(sizeof(struct
Student));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
return;
}
newNode->rollNo = rollNo;
strcpy(newNode->name, name);
newNode->next = NULL;
// Insert at the beginning
if (position == 0 || head == NULL) {
newNode->next = head;
17
head = newNode;
return;
}
struct Student *current = head;
for (int i = 0; i < position - 1 && current != NULL; i++) {
current = current->next;
}
// If position is out of range
if (current == NULL) {
printf("Position out of bounds.\n");
free(newNode);
return;
}
newNode->next = current->next;
current->next = newNode;
}
void displayList() {
struct Student *current = head;
if (current == NULL) {
printf("No students in the list.\n");
return;
}
printf("\n--- Student List ---\n");
while (current != NULL) {
printf("Roll No: %d, Name: %s\n", current->rollNo, current->name);
current = current->next;
}
}
int main() {
int n, rollNo, position;
char name[50];
printf("Enter the number of students to insert initially: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter Roll No and Name for student %d: ", i + 1);
scanf("%d %s", &rollNo, name);
insertAtPosition(i, rollNo, name);
18
}
printf("\nEnter Roll No, Name, and Position to insert a new student:
");
scanf("%d %s %d", &rollNo, name, &position);
insertAtPosition(position, rollNo, name);
displayList();
return 0;
}
19
EXPERIMENT – 7
AIM: Create a doubly linked list with nodes having information about an employee
and perform insertion at front of double linked list and perform deletion at end of
the doubly linked list.
ALGORITHM:
1. Doubly Linked List - Insertion at Front:
1. Create a new node.
2. Set the new node’s next pointer to the current head.
3. Set the current head’s previous pointer to the new node.
4. Update the head pointer to the new node.
2. Doubly Linked List - Deletion at End:
1. Traverse the list to reach the last node.
2. Update the previous node's next pointer to NULL.
3. Free the memory of the last node.
CODE:
#include <stdio.h>
#include <string.h> #include <stdlib.h>
struct Employee
{
int empId;
char name[50];
struct Employee *prev;
struct Employee *next;
};
struct Employee *head = NULL;
void insertAtFront(int empId, char *name)
{
struct Employee *newNode = (struct Employee *)malloc(sizeof(struct
Employee));
newNode->empId = empId;
strcpy(newNode->name, name);
newNode->prev = NULL;
newNode->next = head;
20
if (head != NULL)
{
head->prev = newNode;
}
head = newNode;
}
void deleteAtEnd()
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Employee *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
if (temp->prev != NULL)
{
temp->prev->next = NULL;
}
else
{
head = NULL;
}
printf("Deleted employee: %d, Name: %s\n", temp->empId, temp->name);
free(temp);
}
void displayList()
{
struct Employee *temp = head;
while (temp != NULL)
{
printf("Employee ID: %d, Name: %s\n", temp->empId, temp->name);
temp = temp->next;
}
}
int main()
{
int empId;
char name[50];
int choice;
do
{
printf("1. Insert at Front\n");
21
printf("2. Delete at End\n");
printf("3. Display List\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter Employee ID and Name: ");
scanf("%d %s", &empId, name);
insertAtFront(empId, name);
break;
case 2:
deleteAtEnd();
break;
case 3:
displayList();
break;
case 4:
return 0;
}
} while (choice != 4);
return 0;
}
22
EXPERIMENT – 8
AIM: Create a circular linked list having information about a college and perform
Insertion at the front end and perform deletion at the end.
ALGORITHM:
1. Circular Linked List - Insertion at Front:
1. Create a new node.
2. Set the new node’s next pointer to the current head.
3. Update the last node’s next pointer to point to the new node.
4. Update the head pointer to the new node.
2. Circular Linked List - Deletion at End:
1. Traverse the list to reach the second last node.
2. Set the second last node’s next pointer to the head.
3. Free the memory of the last node.
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct College {
int collegeId;
char name[50];
struct College *next;
};
struct College *head = NULL;
// Function to insert a new node at the front of the circular linked list
void insertAtFront(int collegeId, char *name) {
struct College *newNode = (struct College *)malloc(sizeof(struct
College));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
return;
}
23
newNode->collegeId = collegeId;
strcpy(newNode->name, name);
if (head == NULL) {
newNode->next = newNode; // Points to itself (first node in
circular list)
head = newNode;
} else {
struct College *temp = head;
while (temp->next != head) {
temp = temp->next;
}
newNode->next = head;
temp->next = newNode;
head = newNode;
}
}
// Function to delete a node from the end of the circular linked list
void deleteAtEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct College *temp = head;
struct College *prev = NULL;
// Only one node
if (head->next == head) {
printf("Deleted college: %d, Name: %s\n", head->collegeId, head-
>name);
free(head);
head = NULL;
return;
}
// Traverse to the last node
while (temp->next != head) {
prev = temp;
temp = temp->next;
}
prev->next = head;
printf("Deleted college: %d, Name: %s\n", temp->collegeId, temp-
>name);
24
free(temp);
}
// Function to display all nodes in the circular linked list
void displayList() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct College *temp = head;
printf("\n--- College List ---\n");
do {
printf("College ID: %d, Name: %s\n", temp->collegeId, temp->name);
temp = temp->next;
} while (temp != head);
}
int main() {
int collegeId;
char name[50];
int choice;
do {
printf("\n1. Insert at Front\n");
printf("2. Delete at End\n");
printf("3. Display List\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter College ID and Name: ");
scanf("%d %s", &collegeId, name);
insertAtFront(collegeId, name);
break;
case 2:
deleteAtEnd();
break;
case 3:
displayList();
break;
25
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 4);
return 0;
}
26
EXPERIMENT – 9
AIM: Create a Binary Tree and perform Tree traversals (Preorder, Postorder,
Inorder) using the concept of recursion.
ALGORITHM:
1. Binary Tree - Preorder Traversal (Recursive):
1. Visit the root.
2. Recursively traverse the left subtree.
3. Recursively traverse the right subtree.
2. Binary Tree - Inorder Traversal (Recursive):
1. Recursively traverse the left subtree.
2. Visit the root.
3. Recursively traverse the right subtree.
3. Binary Tree - Postorder Traversal (Recursive):
1. Recursively traverse the left subtree.
2. Recursively traverse the right subtree.
3. Visit the root.
CODE:
#include <stdio.h>
#include <stdlib.h>
// Define a Node structure
struct Node {
int data;
struct Node *left;
struct Node *right;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
exit(1);
}
27
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Preorder Traversal (Root → Left → Right)
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;
printf("%d ", node->data);
preorderTraversal(node->left);
preorderTraversal(node->right);
}
// Inorder Traversal (Left → Root → Right)
void inorderTraversal(struct Node* node) {
if (node == NULL)
return;
inorderTraversal(node->left);
printf("%d ", node->data);
inorderTraversal(node->right);
}
// Postorder Traversal (Left → Right → Root)
void postorderTraversal(struct Node* node) {
if (node == NULL)
return;
postorderTraversal(node->left);
postorderTraversal(node->right);
printf("%d ", node->data);
}
// Main function
int main() {
// Create binary tree
struct Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
// Display traversals
printf("Preorder Traversal: ");
28
preorderTraversal(root);
printf("\n");
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
return 0;
}
29
EXPERIMENT – 10
AIM: Implement insertion, deletion and display (Inorder, Preorder, Postorder) on
binary search tree with the information in the tree about the details of an
automobile (type, company, year of make) ALGORITHM:
1. Binary Search Tree - Insertion:
1. If the tree is empty, insert the node as the root.
2. If the node's value is less than the root, recursively insert it in the left subtree.
3. If the node's value is greater than the root, recursively insert it in the right subtree.
2. Binary Search Tree - Deletion:
1. If the node has no children, simply delete it.
2. If the node has one child, replace it with its child.
3. If the node has two children, find the inorder successor (or predecessor), replace the node
with it, and delete the successor (or predecessor).
ADDITIONAL: We will make the binary search tree based on year of make and
insertion and deletion will be followed by rebalancing based on the same.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure for Automobile Node
struct Automobile {
char type[50];
char company[50];
int year;
struct Automobile *left;
struct Automobile *right;
};
// Function to create a new node
struct Automobile* createNode(char* type, char* company, int year) {
struct Automobile* newNode = (struct Automobile*)malloc(sizeof(struct
Automobile));
if (!newNode) {
printf("Memory allocation failed!\n");
exit(1);
30
}
strcpy(newNode->type, type);
strcpy(newNode->company, company);
newNode->year = year;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a new node into BST
struct Automobile* insert(struct Automobile* node, char* type, char*
company, int year) {
if (node == NULL)
return createNode(type, company, year);
if (year < node->year)
node->left = insert(node->left, type, company, year);
else if (year > node->year)
node->right = insert(node->right, type, company, year);
return node;
}
// Find node with minimum year value
struct Automobile* findMin(struct Automobile* node) {
while (node->left != NULL)
node = node->left;
return node;
}
// Function to delete a node by year
struct Automobile* deleteNode(struct Automobile* root, int year) {
if (root == NULL)
return root;
if (year < root->year) {
root->left = deleteNode(root->left, year);
} else if (year > root->year) {
root->right = deleteNode(root->right, year);
} else {
// Node with one or no child
if (root->left == NULL) {
struct Automobile* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
31
struct Automobile* temp = root->left;
free(root);
return temp;
}
// Node with two children
struct Automobile* temp = findMin(root->right);
root->year = temp->year;
strcpy(root->type, temp->type);
strcpy(root->company, temp->company);
root->right = deleteNode(root->right, temp->year);
}
return root;
}
// Traversal Functions
void inorderTraversal(struct Automobile* root) {
if (root == NULL)
return;
inorderTraversal(root->left);
printf("Type: %s, Company: %s, Year: %d\n", root->type, root->company,
root->year);
inorderTraversal(root->right);
}
void preorderTraversal(struct Automobile* root) {
if (root == NULL)
return;
printf("Type: %s, Company: %s, Year: %d\n", root->type, root->company,
root->year);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct Automobile* root) {
if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("Type: %s, Company: %s, Year: %d\n", root->type, root->company,
root->year);
}
// Main Function
int main() {
struct Automobile* root = NULL;
32
int choice, year;
char type[50], company[50];
do {
printf("\n--- Automobile Management System (BST) ---\n");
printf("1. Insert an Automobile\n");
printf("2. Delete an Automobile\n");
printf("3. Display (Inorder)\n");
printf("4. Display (Preorder)\n");
printf("5. Display (Postorder)\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter Automobile Type: ");
scanf("%s", type);
printf("Enter Company Name: ");
scanf("%s", company);
printf("Enter Year of Make: ");
scanf("%d", &year);
root = insert(root, type, company, year);
break;
case 2:
printf("Enter Year of the Automobile to Delete: ");
scanf("%d", &year);
root = deleteNode(root, year);
break;
case 3:
printf("\nInorder Traversal (Sorted by Year):\n");
inorderTraversal(root);
break;
case 4:
printf("\nPreorder Traversal:\n");
preorderTraversal(root);
break;
case 5:
printf("\nPostorder Traversal:\n");
postorderTraversal(root);
break;
33
case 6:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);
return 0;
}
34