0% found this document useful (0 votes)
11 views

sachin

The document contains five programming assignments by Sachin Sharma, a B.Tech CSE student, focusing on various data structures and algorithms. Each program includes objectives, source code, and sample outputs demonstrating functionalities such as dynamic memory allocation for employee records, a menu-driven array manipulation program, merging sorted arrays, stack operations, and queue operations. The assignments emphasize practical coding skills in C programming, particularly in handling arrays, structures, and dynamic memory.

Uploaded by

xelawoc531
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

sachin

The document contains five programming assignments by Sachin Sharma, a B.Tech CSE student, focusing on various data structures and algorithms. Each program includes objectives, source code, and sample outputs demonstrating functionalities such as dynamic memory allocation for employee records, a menu-driven array manipulation program, merging sorted arrays, stack operations, and queue operations. The assignments emphasize practical coding skills in C programming, particularly in handling arrays, structures, and dynamic memory.

Uploaded by

xelawoc531
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 92

PROGRAM NO 1

NAME: SACHIN SHARMA


COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a program to dynamically allocate the records of n employees using


calloc(). Insert the records and print them. The structure will be as follows:
struct emp{ char name[30]; int age; double salary; };

PROGRAM:
#include <stdio.h>
#include <stdlib.h>

struct emp {
char name[30];
int age;
double salary;
};

int main() {
int n, i;
struct emp *employees;

printf("Enter the number of employees: ");


scanf("%d", &n);

employees = (struct emp *)calloc(n, sizeof(struct emp));


if (employees == NULL) {
printf("Memory allocation failed.\n");
return 1;
}

for (i = 0; i< n; i++) {


printf("Enter details for employee %d:\n", i + 1);
printf("Name: ");
scanf(" %[^\n]", employees[i].name);
printf("Age: ");
scanf("%d", &employees[i].age);
printf("Salary: ");
scanf("%lf", &employees[i].salary);
}

printf("\nEmployee Records:\n");
for (i = 0; i< n; i++) {
printf("Employee %d:\n", i + 1);
printf("Name: %s\n", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("Salary: %.2lf\n", employees[i].salary);
}

free(employees);

return 0;
}

OUTPUT:
Enter the number of employees: 2
Enter details for employee 1:
Name:sachin
Age: 25
Salary: 163000
Enter details for employee
2: Name: priyanshu
Age: 25
Salary: 170000

Employee Records:
Employee 1:
Name:sachin
Age: 25
Salary: 163000.00
Employee 2:
Name: priyanshu
Age: 25
Salary: 170000.00
PROGRAM NO 2
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE:12-11-24

Objective: Write a menu driven program having following functionalities: input an


array, output, insert an element, delete an element, sort array in ascending order, sort an
array in descending order [without taking global variables].

PROGRAM:
#include <stdio.h>
void inputArray(int arr[], int *n) {
printf("Enter size of array: ");
scanf("%d", n);
for (int i = 0; i< *n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
}
void outputArray(int arr[], int n) {
for (int i = 0; i< n; i++) {
printf("%d ", arr[i]);
}

printf("\n");
}
void insertElement(int arr[], int *n) {
int pos, ele;
printf("Enter position to insert: ");
scanf("%d", &pos);
printf("Enter element to insert: ");
scanf("%d", &ele);
for (int i = *n; i>pos - 1; i--) {

arr[i] = arr[i - 1];


}
arr[pos - 1] = ele;
(*n)++;
}

void deleteElement(int arr[], int *n)


{ int pos;
printf("Enter position to delete: ");
scanf("%d", &pos);
for (int i = pos - 1; i< *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--;
}

void sortAscending(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;
}
}

}
}

void sortDescending(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;
}
}
}
}

int main() {
int arr[100], n = 0,
choice; while (1) {
printf("\nMenu:\n1. Input Array\n2. Output Array\n3. Insert Element\n4. Delete
Element\n5. Sort Ascending\n6. Sort Descending\n7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: inputArray(arr, &n); break;
case 2: outputArray(arr, n); break;
case 3: insertElement(arr, &n); break;
case 4: deleteElement(arr, &n); break;
case 5: sortAscending(arr, n); break;
case 6: sortDescending(arr, n); break;
case 7: return 0;
default: printf("Invalid choice\n");
}
}
}

OUTPUT:
Menu:
1. Input Array
2. Output Array
3. Insert Element

4. Delete Element
5. Sort Ascending
6. Sort Descending
7. Exit
Enter your choice: 1
Enter size of array:
3 Enter element 1:
1
Enter element 2: 2
Enter element 3:
3 Menu:
1. Input Array
2. Output Array
3. Insert Element
4. Delete Element
5. Sort Ascending

6. Sort Descending
7. Exit
Enter your choice:
2123
Menu:
1. Input Array
2. Output Array

3. Insert Element
4. Delete Element
5. Sort Ascending
6. Sort Descending
7. Exit

Enter your choice: 3


Enter position to insert: 3
Enter element to insert:
4

Menu:

1. Input Array
2. Output Array
3. Insert Element
4. Delete Element
5. Sort Ascending

6. Sort Descending
7. Exit
Enter your choice:
21243

Menu:
1. Input Array
2. Output Array
3. Insert Element
4. Delete Element

5. Sort Ascending
6. Sort Descending
7. Exit
Enter your choice: 4

Enter position to delete: 2

Menu:
1. Input Array
2. Output Array

3. Insert Element
4. Delete Element
5. Sort Ascending
6. Sort Descending
7. Exit

Enter your choice:


2134

Menu:
1. Input Array

2. Output Array
3. Insert Element
4. Delete Element
5. Sort Ascending
6. Sort Descending

7. Exit
Enter your choice: 5

Menu:
1. Input Array
2. Output Array
3. Insert Element
4. Delete Element
5. Sort Ascending

6. Sort Descending
7. Exit
Enter your choice:
2134

Menu:
1. Input Array
2. Output Array
3. Insert Element
4. Delete Element

5. Sort Ascending
6. Sort Descending
7. Exit
Enter your choice: 6

Menu:
1. Input Array
2. Output Array
3. Insert Element
4. Delete Element

5. Sort Ascending
6. Sort Descending
7. Exit
Enter your choice:
2431
PROGRAM NO 3
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE:12-11-24

Objective: Write a program to first sort the elements stored in two arrays in ascending
order and then merge them into a third array [without taking global variables].

PROGRAM:
#include <stdio.h>
void sortArray(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;
}
}
}
}

void mergeArrays(int arr1[], int n1, int arr2[], int n2, int merged[]) {
int i = 0, j = 0, k = 0;
while (i< n1 && j < n2) {
if (arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
while (i< n1) {
merged[k++] = arr1[i++];
}
while (j < n2) {
merged[k++] = arr2[j++];
}
}

int main() {
int n1, n2;
printf("Enter size of first array: ");
scanf("%d", &n1);
int arr1[n1];
printf("Enter elements of first array:\n");
for (int i = 0; i< n1; i++) {
scanf("%d", &arr1[i]);
}

printf("Enter size of second array: ");


scanf("%d", &n2);
int arr2[n2];
printf("Enter elements of second array:\n");
for (int i = 0; i< n2; i++) {
scanf("%d", &arr2[i]);
}
sortArray(arr1, n1);
sortArray(arr2, n2);

int merged[n1 + n2];


mergeArrays(arr1, n1, arr2, n2, merged);

printf("Merged and sorted array:\n");


for (int i = 0; i< n1 + n2; i++) {
printf("%d ", merged[i]);
}
return 0;
}

OUTPUT:
Enter elements of first
array: 1
2
3
4

6
Enter size of second array: 3
Enter elements of second
array: 5
7

8
Merged and sorted
array: 1 2 3 4 5 6 7 8
PROGRAM NO 4
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE:12-11-24

Objective: Write a menu driven program of a stack using array having the push, pop and
display operations[without taking global variables].

PROGRAM:
#include <stdio.h>
#define MAX 100
void push(int stack[], int *top) {
int value;
if (*top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
printf("Enter value to push: ");
scanf("%d", &value); stack[++
(*top)] = value;
}

void pop(int stack[], int *top) {


if (*top == -1) {
printf("Stack Underflow\n");
return;
}
printf("Popped value: %d\n", stack[(*top)--]);
}

void display(int stack[], int top) {


if (top == -1) {
printf("Stack is empty\n");
return;
}
printf("Stack elements:\n");
for (int i = top; i>= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}

int main() {
int stack[MAX], top = -1, choice;
while (1) {
printf("\nMenu:\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: push(stack, &top);
break; case 2: pop(stack, &top);
break;
case 3: display(stack, top); break;
case 4: return 0;
default: printf("Invalid choice\n");
}
}

}
OUTPUT:
Menu:
1. Push

2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push:
3

Menu:
1. Push
2. Pop
3. Display

4. Exit
Enter your choice: 1
Enter value to push:
2

Menu:

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push:
1

Menu:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice:
3 Stack elements:
123

Menu:
1. Push
2. Pop

3. Display
4. Exit
Enter your choice:
2 Popped value: 1

Menu:
1. Push
2. Pop
3. Display
4. Exit

Enter your choice:


3 Stack elements:
23

Menu:

1. Push
2. Pop
3. Display
4. Exit
Enter your choice:
2 Popped value: 2
Menu:
1. Push

2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements:
3
PROGRAM NO 5
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE:12-11-24

Objective: Write a menu driven program of a queue using array having add, delete and
display operations [without taking global variables].

PROGRAM:
#include <stdio.h>
#define MAX 100

void add(int queue[], int *front, int *rear) {


int value;
if (*rear == MAX - 1) {
printf("Queue Overflow\n");
return;
}
printf("Enter value to add: ");
scanf("%d", &value);
if (*front == -1) *front = 0;
queue[++(*rear)] = value;
}

void delete(int queue[], int *front, int *rear) {


if (*front == -1 || *front > *rear) {
printf("Queue Underflow\n");
return;
}
printf("Deleted value: %d\n", queue[(*front)++]);
if (*front > *rear) {
*front = *rear = -1;
}
}

void display(int queue[], int front, int rear) {


if (front == -1 || front > rear) {
printf("Queue is empty\n");
return;
}
printf("Queue elements:\n");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}

int main() {
int queue[MAX], front = -1, rear = -1, choice;
while (1) {
printf("\nMenu:\n1. Add\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: add(queue, &front, &rear); break;
case 2: delete(queue, &front, &rear); break;
case 3: display(queue, front, rear); break;
case 4: return 0;
default: printf("Invalid choice\n");
}

}
}

OUTPUT:
Menu:
1. Add

2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter value to add: 10

Menu:
1. Add
2. Delete
3. Display

4. Exit
Enter your choice: 1
Enter value to add: 20

Menu:

1. Add
2. Delete
3. Display
4. Exit
Enter your choice:
2 Deleted value: 10
Menu:
1. Add

2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter value to add: 30

Menu:
1. Add
2. Delete
3. Display

4. Exit
Enter your choice: 1
Enter value to add: 40

Menu:

1. Add
2. Delete
3. Display
4. Exit
Enter your choice:
3 Queue elements:
40 30 20

Menu:
1. Add

2. Delete
3. Display
4. Exit
Enter your choice:
2 Deleted value: 40
PROGRAM NO 6
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a menu driven program of a circular queue using array having add,
delete and display operations [without taking global variables].

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
void add(int queue[], int *front, int *rear, int value) {
if ((*front == 0 && *rear == MAX - 1) || (*rear == (*front - 1) % (MAX - 1)))
{ printf("Queue is full. Cannot add %d.\n", value);
return;
}
if (*front == -1) {
*front = *rear = 0;
} else if (*rear == MAX - 1 && *front != 0) {
*rear = 0;

} else {
(*rear)++;
}
queue[*rear] = value;
printf("Added %d to the queue.\n", value);

}
void delete(int queue[], int *front, int *rear) {
if (*front == -1) {
printf("Queue is empty. Cannot delete.\n");
return;
}
printf("Deleted %d from the queue.\n", queue[*front]);
if (*front == *rear) {
*front = *rear = -1;

} else if (*front == MAX - 1) {


*front = 0;
} else {
(*front)++;
}

void display(int queue[], int front, int rear) {


if (front == -1) {
printf("Queue is empty.\
n"); return;
}
printf("Queue elements: ");
if (rear >= front) {
for (int i = front; i<= rear; i++) {

printf("%d ", queue[i]);


}
} else {
for (int i = front; i< MAX; i++) {
printf("%d ", queue[i]);
}
for (int i = 0; i<= rear; i++) {
printf("%d ", queue[i]);
}

}
printf("\n");
}

int main() {

int queue[MAX], front = -1, rear = -1, choice, value;

do {
printf("\nMenu:\n");
printf("1. Add\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to add: ");
scanf("%d", &value);
add(queue, &front, &rear,
value); break;
case 2:
delete(queue, &front,
&rear); break;
case 3:
display(queue, front,
rear); break;
case 4:

printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}

} while (choice != 4);

return 0;
}

OUTPUT:
Menu:
1. Add
2. Delete
3. Display
4. Exit

Enter your choice: 1


Enter value to add: 40
Added 40 to the queue.

Menu:

1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter value to add: 63
Added 63 to the queue.

Menu:

1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter value to add: 60
Added 60 to the queue.

Menu:
1. Add

2. Delete
3. Display
4. Exit
Enter your choice: 3
Queue elements: 40 63 60

Menu:
1. Add
2. Delete
3. Display

4. Exit
Enter your choice: 2
Deleted 40 from the queue.
PROGRAM NO 7
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE:12-11-24

Objective: Write a menu driven program of a stack using linked list having the push, pop
and display operations[without taking global variables].

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void push(struct Node **top) {
int value;
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
return;
}
printf("Enter value to push: ");
scanf("%d", &value);
newNode->data =
value; newNode->next =
*top;
*top = newNode;
}
void pop(struct Node **top) {
if (*top == NULL) {
printf("Stack Underflow\n");
return;
}
struct Node *temp = *top;
printf("Popped value: %d\n", temp->data);

*top = (*top)->next;
free(temp);
}

void display(struct Node *top) {


if (top == NULL) {
printf("Stack is empty\n");
return;
}
printf("Stack elements:\n");
struct Node *temp = top;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node *top = NULL;
int choice;
while (1) {
printf("\nMenu:\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: push(&top); break;
case 2: pop(&top); break;
case 3: display(top);
break; case 4: return 0;
default: printf("Invalid choice\n");
}
}
}

OUTPUT:
Menu:
1. Push
2. Pop
3. Display

4. Exit
Enter your choice: 1
Enter value to push:
10

Menu:

1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push:
20
Menu:
1. Push

2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push:
30

Menu:
1. Push
2. Pop
3. Display

4. Exit
Enter your choice:
3 Stack elements:
30 20 10

Menu:
1. Push
2. Pop
3. Display
4. Exit

Enter your choice:


2 Popped value: 30

Menu:
1. Push

2. Pop
3. Display
4. Exit
Enter your choice:
2 Popped value: 20

Menu:
1. Push
2. Pop

3. Display
4. Exit
Enter your choice:
3 Stack elements:
10
PROGRAM NO 8
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a menu driven program of a queue using linked list having add, delete
and display operations [without taking global variables].

PROGRAM:
#include <stdio.h>
#include
<stdlib.h> struct
Node {
int data;
struct Node *next;
};
void add(struct Node **front, struct Node **rear) {
int value;
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
return;
}
printf("Enter value to add: ");
scanf("%d", &value);
newNode->data =
value; newNode->next =
NULL; if (*rear == NULL)
{
*front = *rear = newNode;
} else {
(*rear)->next = newNode;
*rear = newNode;
}
}

void delete(struct Node **front, struct Node **rear) {


if (*front == NULL) {
printf("Queue Underflow\n");
return;
}
struct Node *temp = *front;
printf("Deleted value: %d\n", temp->data);

*front = (*front)-
>next; if (*front ==
NULL) {
*rear = NULL;
}
free(temp);

void display(struct Node *front) {


if (front == NULL) {
printf("Queue is empty\n");

return;
}
printf("Queue elements:\n");
struct Node *temp = front;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");

int main() {
struct Node *front = NULL, *rear = NULL;
int choice;
while (1) {
printf("\nMenu:\n1. Add\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: add(&front, &rear); break;
case 2: delete(&front, &rear);
break; case 3: display(front); break;
case 4: return 0;
default: printf("Invalid choice\n");

}
}
}

OUTPUT:
Menu:

1. Add
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter value to add:
46
Menu:
1. Add

2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter value to add: 47

Menu:
1. Add
2. Delete
3. Display

4. Exit
Enter your choice: 1
Enter value to add: 23

Menu:
1. Add
2. Delete
3. Display
4. Exit
Enter your choice:
3 Queue elements:
46 47 23

Menu:
1. Add

2. Delete
3. Display
4. Exit
Enter your choice:
2 Deleted value: 46
PROGRAM NO 9
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a menu driven program of a circular linked list with functions: insertion
at the beginning, deletion at the end, insertion between two nodes, deletion of first node,
deletion of last node, deletion of a node whose position is given, deletion of any node and
display. *

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};

void insertAtBeginning(struct Node **head) {


int value;
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
return;
}
printf("Enter value to insert at the beginning: ");
scanf("%d", &value);
newNode->data =
value; if (*head ==
NULL) {
newNode->next = newNode;
*head = newNode;
} else {

struct Node *temp = *head;


while (temp->next != *head) {
temp = temp->next;
}
temp->next =
newNode; newNode->next =
*head;
*head = newNode;
}
}

void deleteAtEnd(struct Node **head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}

struct Node *temp = *head, *prev = NULL;


if (temp->next == *head) {
free(temp);
*head = NULL;
return;
}
while (temp->next != *head) {
prev = temp;
temp = temp->next;
}

prev->next = *head;
free(temp);
}

void insertBetweenNodes(struct Node **head) {


int value, pos, i = 1;
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
return;
}
printf("Enter value to insert: ");
scanf("%d", &value);
printf("Enter position to insert (1-based index): ");
scanf("%d", &pos);

newNode->data = value;
struct Node *temp = *head;
while (i<pos - 1 && temp->next != *head) {
temp = temp->next;
i++;
}
if (temp->next == *head &&pos != 1) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp-
>next; temp->next =
newNode;
}
void deleteFirstNode(struct Node **head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = *head;
if (temp->next == *head) {
free(temp);
*head = NULL;
} else {
struct Node *last = *head;
while (last->next != *head)
{
last = last->next;
}
*head = (*head)->next;
last->next = *head;
free(temp);

}
}

void deleteLastNode(struct Node **head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = *head, *prev = NULL;
if (temp->next == *head) {
free(temp);
*head = NULL;
return;
}

while (temp->next != *head) {


prev = temp;
temp = temp->next;
}
prev->next =
*head;
free(temp);
}

void deleteNodeAtPosition(struct Node **head) {


int pos, i = 1;
printf("Enter position to delete (1-based index): ");
scanf("%d", &pos);

if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = *head, *prev = NULL;
if (pos == 1) {
deleteFirstNode(head);

return;
}
while (i<pos&& temp->next != *head) {
prev = temp;
temp = temp->next;

i++;
}
if (temp->next == *head &&i<pos) {
printf("Position out of range\n");
return;
}
prev->next = temp-
>next; free(temp);
}

void deleteAnyNode(struct Node **head) {


int value;
printf("Enter value to delete: ");
scanf("%d", &value);

if (*head == NULL) {
printf("List is empty\n");
return;
}

struct Node *temp = *head, *prev = NULL;


if (temp->data == value) {
deleteFirstNode(head);
return;
}

while (temp->data != value && temp->next != *head) {


prev = temp;
temp = temp->next;
}
if (temp->data != value) {
printf("Value not found in the list\n");
return;
}
prev->next = temp-
>next; free(temp);
}

void display(struct Node *head) {


if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = head;
printf("Circular Linked List: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}

int main() {
struct Node *head = NULL;
int choice;
while (1) { printf("\
nMenu:\n");
printf("1. Insert at the beginning\n");
printf("2. Delete at the end\n");
printf("3. Insert between two nodes\n");
printf("4. Delete first node\n");
printf("5. Delete last node\n");
printf("6. Delete node at given position\n");
printf("7. Delete any node\n");
printf("8. Display list\n");
printf("9. Exit\n");
printf("Enter your choice:
"); scanf("%d", &choice);
switch (choice) {

case 1: insertAtBeginning(&head); break;


case 2: deleteAtEnd(&head); break;
case 3: insertBetweenNodes(&head); break;
case 4: deleteFirstNode(&head); break;
case 5: deleteLastNode(&head); break;

case 6: deleteNodeAtPosition(&head); break;


case 7: deleteAnyNode(&head); break;
case 8: display(head);
break; case 9: return 0;
default: printf("Invalid choice\n");

}
}
}

OUTPUT:
Menu:

1. Insert at the beginning


2. Delete at the end
3. Insert between two nodes
4. Delete first node
5. Delete last node

6. Delete node at given position


7. Delete any node
8. Display list
9. Exit

Enter your choice: 1


Enter value to insert at the beginning: 10

Menu:
1. Insert at the beginning

2. Delete at the end


3. Insert between two nodes
4. Delete first node
5. Delete last node
6. Delete node at given position

7. Delete any node


8. Display list
9. Exit
Enter your choice: 1
Enter value to insert at the beginning: 20

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes

4. Delete first node


5. Delete last node
6. Delete node at given position
7. Delete any node
8. Display list

9. Exit
Enter your choice: 1
Enter value to insert at the beginning: 30

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes
4. Delete first node

5. Delete last node


6. Delete node at given position
7. Delete any node
8. Display list
9. Exit

Enter your choice: 8


Circular Linked List: 30 20 10

Menu:
1. Insert at the beginning

2. Delete at the end


3. Insert between two nodes
4. Delete first node
5. Delete last node
6. Delete node at given position

7. Delete any node


8. Display list
9. Exit
Enter your choice: 2

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes

4. Delete first node


5. Delete last node
6. Delete node at given position
7. Delete any node
8. Display list

9. Exit
Enter your choice: 8
Circular Linked List: 30
20

Menu:

1. Insert at the beginning


2. Delete at the end
3. Insert between two nodes
4. Delete first node
5. Delete last node

6. Delete node at given position


7. Delete any node
8. Display list
9. Exit
Enter your choice: 3
Enter value to insert:
40
Enter position to insert : 3

Menu:
1. Insert at the beginning

2. Delete at the end


3. Insert between two nodes
4. Delete first node
5. Delete last node

6. Delete node at given position


7. Delete any node
8. Display list
9. Exit
Enter your choice: 8

Circular Linked List: 30 20 40

Menu:
1. Insert at the beginning
2. Delete at the end

3. Insert between two nodes


4. Delete first node
5. Delete last node
6. Delete node at given position
7. Delete any node

8. Display list
9. Exit
Enter your choice: 4

Menu:

1. Insert at the beginning


2. Delete at the end
3. Insert between two nodes
4. Delete first node
5. Delete last node

6. Delete node at given position


7. Delete any node
8. Display list
9. Exit

Enter your choice: 8


Circular Linked List: 20
40

Menu:
1. Insert at the beginning

2. Delete at the end


3. Insert between two nodes
4. Delete first node
5. Delete last node
6. Delete node at given position

7. Delete any node


8. Display list
9. Exit
Enter your choice: 1
Enter value to insert at the beginning: 69

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes

4. Delete first node


5. Delete last node
6. Delete node at given position
7. Delete any node
8. Display list

9. Exit
Enter your choice: 6
Enter position to delete : 2

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes
4. Delete first node

5. Delete last node


6. Delete node at given position
7. Delete any node
8. Display list
9. Exit

Enter your choice: 8


Circular Linked List: 69
40

Menu:
1. Insert at the beginning

2. Delete at the end


3. Insert between two nodes
4. Delete first node
5. Delete last node
6. Delete node at given position

7. Delete any node


8. Display list
9. Exit
Enter your choice: 7
Enter value to delete:
40
Menu:
1. Insert at the beginning
2. Delete at the end

3. Insert between two nodes


4. Delete first node
5. Delete last node
6. Delete node at given position
7. Delete any node

8. Display list
9. Exit
Enter your choice: 8
Circular Linked List:
69
PROGRAM NO 10
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a menu driven program of a doubly linked list with functions: insertion
at the beginning, deletion at the end, insertion between two nodes, deletion of first node,
deletion of last node, deletion of a node whose position is given, deletion of any node and
display[without taking global variables].

PROGRAM:
#include <stdio.h>
#include
<stdlib.h> struct
Node {
int data;
struct Node *prev;
struct Node *next;
};
void insertAtBeginning(struct Node **head) {
int value;
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
return;
}
printf("Enter value to insert at the beginning: ");
scanf("%d", &value);
newNode->data =
value; newNode->prev
= NULL;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;

}
*head = newNode;
}

void deleteAtEnd(struct Node **head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}

while (temp->next != NULL) {


temp = temp->next;
}
temp->prev->next =
NULL; free(temp);
}

void insertBetweenNodes(struct Node **head) {


int value, pos, i = 1;
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (!newNode) {
printf("Memory allocation failed\n");
return;
}

printf("Enter value to insert: ");


scanf("%d", &value);
printf("Enter position to insert (1-based index): ");
scanf("%d", &pos);

newNode->data = value;
struct Node *temp = *head;
while (i<pos - 1 && temp != NULL) {
temp = temp->next;
i++;

}
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;

}
newNode->next = temp-
>next; newNode->prev =
temp;
if (temp->next != NULL) {
temp->next->prev = newNode;

}
temp->next = newNode;
}

void deleteFirstNode(struct Node **head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}

struct Node *temp = *head;


*head = (*head)->next;
if (*head != NULL) {
(*head)->prev = NULL;
}

free(temp);
}

void deleteLastNode(struct Node **head) {


if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = *head;
if (temp->next == NULL) {
free(temp);
*head = NULL;
return;
}
while (temp->next != NULL) {

temp = temp->next;
}
temp->prev->next =
NULL; free(temp);
}
void deleteNodeAtPosition(struct Node **head) {
int pos, i = 1;
printf("Enter position to delete (1-based index): ");
scanf("%d", &pos);

if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = *head;
if (pos == 1) {
deleteFirstNode(head);
return;
}
while (i<pos&& temp != NULL) {
temp = temp->next;
i++;
}

if (temp == NULL) {
printf("Position out of range\n");
return;
}
if (temp->next != NULL) {

temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}

free(temp);
}

void deleteAnyNode(struct Node **head) {


int value;
printf("Enter value to delete: ");
scanf("%d", &value);

if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = *head;
if (temp->data == value) {
deleteFirstNode(head);
return;
}
while (temp != NULL && temp->data != value) {
temp = temp->next;
}
if (temp == NULL) {
printf("Value not found in the list\n");
return;
}

if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;

}
free(temp);
}

void display(struct Node *head) {


if (head == NULL) {
printf("List is empty\n");
return;
}

struct Node *temp = head;


printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {

struct Node *head = NULL;


int choice;
while (1) { printf("\
nMenu:\n");
printf("1. Insert at the beginning\n");
printf("2. Delete at the end\n");
printf("3. Insert between two nodes\n");
printf("4. Delete first node\n");
printf("5. Delete last node\n");
printf("6. Delete node at given position\n");
printf("7. Delete any node\n");
printf("8. Display list\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: insertAtBeginning(&head); break;
case 2: deleteAtEnd(&head); break;
case 3: insertBetweenNodes(&head); break;
case 4: deleteFirstNode(&head); break;
case 5: deleteLastNode(&head); break;
case 6: deleteNodeAtPosition(&head); break;
case 7: deleteAnyNode(&head); break;
case 8: display(head); break;

case 9: return 0;
default: printf("Invalid choice\n");
}
}
}

OUTPUT:
Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes

4. Delete first node


5. Delete last node
6. Delete node at given position
7. Delete any node
8. Display list

9. Exit
Enter your choice: 1
Enter value to insert at the beginning: 9

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes
4. Delete first node

5. Delete last node


6. Delete node at given position
7. Delete any node
8. Display list
9. Exit

Enter your choice: 1


Enter value to insert at the beginning: 18

Menu:
1. Insert at the beginning

2. Delete at the end


3. Insert between two nodes
4. Delete first node
5. Delete last node
6. Delete node at given position

7. Delete any node


8. Display list
9. Exit
Enter your choice: 1
Enter value to insert at the beginning: 27
Menu:
1. Insert at the beginning
2. Delete at the end

3. Insert between two nodes


4. Delete first node
5. Delete last node
6. Delete node at given position
7. Delete any node

8. Display list
9. Exit
Enter your choice: 8
Doubly Linked List: 27 18
9

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes
4. Delete first node

5. Delete last node


6. Delete node at given position
7. Delete any node
8. Display list
9. Exit

Enter your choice: 2

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes
4. Delete first node
5. Delete last node
6. Delete node at given position

7. Delete any node


8. Display list
9. Exit
Enter your choice: 8
Doubly Linked List: 27
18

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes

4. Delete first node


5. Delete last node
6. Delete node at given position
7. Delete any node
8. Display list

9. Exit
Enter your choice: 3
Enter value to insert:
36
Enter position to insert : 3

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes
4. Delete first node

5. Delete last node


6. Delete node at given position
7. Delete any node
8. Display list

9. Exit
Enter your choice: 8
Doubly Linked List: 27 18 36

Menu:

1. Insert at the beginning


2. Delete at the end
3. Insert between two nodes
4. Delete first node
5. Delete last node

6. Delete node at given position


7. Delete any node
8. Display list
9. Exit
Enter your choice: 5

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes

4. Delete first node


5. Delete last node
6. Delete node at given position
7. Delete any node
8. Display list

9. Exit
Enter your choice: 8
Doubly Linked List: 27
18

Menu:
1. Insert at the beginning
2. Delete at the end
3. Insert between two nodes
4. Delete first node

5. Delete last node


6. Delete node at given position
7. Delete any node
8. Display list
9. Exit

Enter your choice: 7


Enter value to delete:
27

Menu:
1. Insert at the beginning

2. Delete at the end


3. Insert between two nodes
4. Delete first node
5. Delete last node
6. Delete node at given position

7. Delete any node


8. Display list
9. Exit
Enter your choice: 8
Doubly Linked List:
18
PROGRAM NO 11
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a program to sort and merge linked lists. *


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};

struct Node* createNode(int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = value;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int value) {
struct Node* newNode =
createNode(value); if (*head == NULL) {
*head = newNode;

} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

void printList(struct Node* head)


{ struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

void sortList(struct Node* head)


{ if (head == NULL) return;

struct Node *i, *j;


int temp;
for (i = head; i != NULL; i = i->next) {
for (j = i->next; j != NULL; j = j->next) {
if (i->data > j->data) {

temp = i->data;
i->data = j-
>data; j->data =
temp;
}
}

}
}

struct Node* mergeSortedLists(struct Node* list1, struct Node* list2) {


struct Node* result = NULL;

// Base cases
if (list1 == NULL) return list2;
if (list2 == NULL) return list1;

if (list1->data <= list2->data) {


result = list1;
result->next = mergeSortedLists(list1->next, list2);

} else {
result = list2;
result->next = mergeSortedLists(list1, list2->next);
}

return result;
}

int main() {
struct Node* list1 = NULL;
struct Node* list2 = NULL;

int choice, value;

printf("Enter values for the first linked list (enter -1 to stop):\n");


while (1) {
printf("Enter value: ");
scanf("%d", &value);
if (value == -1) break;
insertEnd(&list1, value);
}

printf("Enter values for the second linked list (enter -1 to stop):\n");


while (1) {
printf("Enter value: ");
scanf("%d", &value);
if (value == -1) break;
insertEnd(&list2, value);
}

printf("\nSorting the first linked list...\n");


sortList(list1);
printf("Sorted first list: ");
printList(list1);

printf("\nSorting the second linked list...\n");


sortList(list2);
printf("Sorted second list: ");
printList(list2);

struct Node* mergedList = mergeSortedLists(list1, list2);

printf("\nMerged sorted list: ");


printList(mergedList);

return 0;

OUTPUT:
Enter values for the first linked list (enter -1 to stop):
Enter value: 20
Enter value: 30

Enter value: 10
Enter value: 40
Enter value: 63
Enter value: -1
Enter values for the second linked list (enter -1 to stop):
Enter value: 1
Enter value: 2
Enter value: 3
Enter value: 5
Enter value: 6
Enter value: -1

Sorting the first linked list...


Sorted first list: 10 20 30 40 63

Sorting the second linked list...


Sorted second list: 1 2 3 4 5 6

Merged sorted list: 1 2 3 4 5 6 10 20 30 40 63


PROGRAM NO 12
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a program to implement ascending priority queue with following


functions i)insert ii)serve iii)display (use double pointer )

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
int priority;
struct Node *next;
};

void insert(struct Node **head, int value, int priority) {


struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
struct Node *temp = *head, *prev = NULL;
newNode->data = value;
newNode->priority =
priority; newNode->next =
NULL;
if (*head == NULL || (*head)->priority > priority) {
newNode->next = *head;
*head = newNode;
return;
}
while (temp != NULL && temp->priority <= priority) {
prev = temp;
temp = temp->next;

}
prev->next = newNode;
newNode->next =
temp;
}

void serve(struct Node **head) {


if (*head == NULL) {
printf("Queue is empty\n");
return;
}

struct Node *temp = *head;


*head = (*head)->next;
printf("Served: %d\n", temp->data);
free(temp);
}

void display(struct Node *head) {


if (head == NULL) {
printf("Queue is empty\n");
return;
}
struct Node *temp = head;
printf("Priority Queue: ");
while (temp != NULL) {
printf("%d(%d) ", temp->data, temp->priority);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node *head = NULL;
int choice, value, priority;

while (1) { printf("\


nMenu:\n");
printf("1. Insert\n");
printf("2. Serve\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice:
"); scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value and priority: ");
scanf("%d %d", &value, &priority);
insert(&head, value, priority);
break;
case 2:

serve(&head);
break;
case 3:
display(head);
break;
case 4:
exit(0);
default:
printf("Invalid choice\n");

}
}
return 0;
}

OUTPUT:
Menu:
1. Insert
2. Serve
3. Display
4. Exit

Enter your choice: 1


Enter value and priority: 35
10

Menu:
1. Insert
2. Serve
3. Display
4. Exit
Enter your choice: 1

Enter value and priority: 45


8

Menu:
1. Insert

2. Serve
3. Display
4. Exit
Enter your choice: 1

Enter value and priority: 55


1

Menu:
1. Insert

2. Serve
3. Display
4. Exit
Enter your choice: 3
Priority Queue: 55(1) 45(8) 35(10)

Menu:
1. Insert
2. Serve
3. Display

4. Exit
Enter your choice:
2 Served: 55
PROGRAM NO 13
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a program to sort a given array using merge sort.


PROGRAM:
#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;

int leftArr[n1], rightArr[n2];

for (int i = 0; i < n1; i++)


leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
} k+
+;
}

while (i < n1) {


arr[k] = leftArr[i];
i++;
k++;

while (j < n2) {


arr[k] = rightArr[j];
j++;
k++;
}
}

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);

merge(arr, left, mid, right);


}
}

void printArray(int arr[], int size) {


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

printf("\n");
}

int main() {
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];

printf("Enter the elements:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Unsorted array: \n");


printArray(arr, n);

mergeSort(arr, 0, n - 1);

printf("Sorted array: \n");


printArray(arr, n);

return 0;

}
OUTPUT:
Enter the number of elements: 4
Enter the elements:
23
57
11
96
Unsorted array:
23 57 11 96
Sorted array:
11 23 57 96
PROGRAM NO 14
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a program to sort a given array using quick sort.


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;

for (int j = low; j < high; j++) {


if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}

int temp = arr[i +


1]; arr[i + 1] =
arr[high]; arr[high] =
temp;

return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

void display(int arr[], int size) {


for (int i = 0; i< size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter elements: ");
for (int i = 0; i< n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);

printf("Sorted array:
"); display(arr, n);

return 0;
}

OUTPUT:
Enter number of elements: 6
Enter elements: 18
47
23
12

75
52
Sorted array: 12 18 23 47 52 75
PROGRAM NO 15
NAME: SACHIN SHARMA
COURSE: B.TECH CSE
SEMESTER: 3RD
ROLL NO: 63
DATE: 12-11-24

Objective: Write a program to implement a binary search tree with following operation:
insertion, deletion and display.

PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};

struct Node* createNode(int value) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node* insert(struct Node* root, int value) {


if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);

} else if (value > root->data) {


root->right = insert(root->right, value);
}

return root;

struct Node* findMin(struct Node* root) {


while (root->left != NULL) {
root = root->left;

}
return root;
}

struct Node* delete(struct Node* root, int value) {


if (root == NULL) {
return root;
}

if (value < root->data) {

root->left = delete(root->left, value);


} else if (value > root->data) {
root->right = delete(root->right, value);
} else {
if (root->left == NULL) {

struct Node* temp = root->right;


free(root);
return temp;
} else if (root->right == NULL) {
struct Node* temp = root-
>left; free(root);
return temp;
}

struct Node* temp = findMin(root-


>right); root->data = temp->data;
root->right = delete(root->right, temp->data);
}

return root;
}

void display(struct Node* root) {


if (root != NULL) {
display(root->left);
printf("%d ", root->data);
display(root->right);
}
}

int main() {
struct Node* root = NULL;
int choice, value;

while (1) {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display (In-order traversal)\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root,
value); break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = delete(root,
value); break;
case 3:
printf("BST (In-order traversal): ");
display(root);
printf("\n");

break;
case 4:
exit(0);
default:
printf("Invalid choice\n");

}
}

return 0;

OUTPUT:
Menu:
1. Insert
2. Delete

3. Display (In-order traversal)


4. Exit
Enter your choice: 1
Enter value to insert:
42

Menu:
1. Insert
2. Delete
3. Display (In-order traversal)
4. Exit

Enter your choice: 1


Enter value to insert:
76

Menu:
1. Insert

2. Delete
3. Display (In-order traversal)
4. Exit
Enter your choice: 1
Enter value to insert:
55
Menu:
1. Insert
2. Delete

3. Display (In-order traversal)


4. Exit
Enter your choice: 3
BST (In-order traversal): 42 55 76

Menu:
1. Insert
2. Delete
3. Display (In-order traversal)
4. Exit

Enter your choice: 2


Enter value to delete:
42

Menu:
1. Insert

2. Delete
3. Display (In-order traversal)
4. Exit
Enter your choice: 3
BST (In-order traversal): 55 76

Menu:
1. Insert
2. Delete
3. Display (In-order traversal)

4. Exit
Enter your choice: 1
Enter value to insert:
99

Menu:
1. Insert
2. Delete
3. Display (In-order traversal)
4. Exit

Enter your choice: 3


BST (In-order traversal): 55 76 99

You might also like