DATA Structure Information
DATA Structure Information
No+4
N
o
20 Write a program to accept six digit integer number and store it in circular double 112-117
linked list (each digit in separate node) and perform addition and subtraction
operation on it. Also consider carry
21 Write a program to accept six digit integer number and store it in circular single 118-123
linked list (each digit in separate node) and perform addition and subtraction
operation on it. Also consider carry.
22 Write a program to accept two sorted double linked lists and merge them in a 124-129
single linked list in such a way that the resultant linked list will be a sorted one.
23 Write a program to accept two sorted single linked lists and merge them in a 130-134
single linked list in such a way that the resultant linked list will be a sorted one.
26 Write a program to accept a string of parenthesis and check its validity using 145-148
stack. Implement stack using double linked list
27 Write a program to implement circular queue using circular single linked list and 149-153
perform Add, delete and display operations.
28 Write a program to implement Josephus problem using circular double linked 154-157
list.
29 Write a program to implement Josephus problem using circular double linked 158-160
list.
30 Write a program to implement double ended queue using double linked list and 161-167
perform Add, delete and display operations.
31 Write a program to accept a string from user and store its each character in a 168-172
separate node of circular single linked list and check whether the entered string
is a palindrome or not.
32 Implenet Queue using array. 173-176
33 Write a program to implement a circular queue and perform the following 177-181
operations on it. Addq, Delq, Display entire queue.
34 Write a program to implement a double-ended queue where the user can 182-189
remove the element from both the front and rear ends of the queue.
35 Write a program to implement a circular double-ended queue where the user 190-196
can add and remove the elements from both the front and rear ends of the
queue.
36 write a program for a Bank simulation of its teller operations to see how waiting 197-203
times would be affected by adding another teller.
37 write a program to keep track of patients as they enter a medical clinic, assigning 204-209
patients to the doctor on a first-come, first-serve basis.
39 Write a menu driven program to create a Binary Tree and perform following non- 215-221
recursive operations on it.
1. Preorder traversal, 2. Display mirror image of a tree without creating new
tree, 3. Display height of a tree.
40 Write a menu driven program to create a Binary Tree and perform following non- 222-229
recursive operations on it.
1. Postorder Traversal, 2. Display a tree levelwise, 3. Display leaf nodes of a
tree.
41 Write a menu driven program to create a Binary Tree and perform following non- 230-236
recursive operations on it.
1. Inorder Traversal, 2. Display mirror image of a tree by creating new tree, 3.
Equality of two trees.
42 Write a menu driven program to create a Binary Tree and perform following non- 237-244
recursive operations on it.
1. Postorder Traversal, 2. Preorder Traversal, 3. Copy a tree.
43 Write a menu driven program to create a Binary Search Tree and perform 245-251
following non-recursive operations on it.
1. Preorder traversal, 2. Display mirror image of a tree without creating new
tree, 3. Display height of a tree.
44 Write a menu driven program to create a Binary Search Tree and perform 252-260
following non-recursive operations on it.
1. Postorder Traversal, 2. Display a tree levelwise, 3. Display leaf nodes of a
tree.
45 Write a menu driven program to create a Binary Search Tree and perform 261-268
following non-recursive operations on it.
1. Inorder Traversal, 2. Display mirror image of a tree by creating new tree, 3.
Delete a node from a tree.
46 Write a menu driven program to create a Binary Search Tree and perform 269-276
following non-recursive operations on it.
1. Postorder Traversal, 2. Preorder Traversal, 3. Inorder Traversal.
47 Write a menu driven program to create a Binary Search Tree and perform 277-286
following non-recursive operations on it.
1. Copy a tree, 2. Equality of two trees, 3. Delete a node from a tree.
48 Write a menu driven program to create a Binary Search Tree and perform 287-293
following non-recursive operations on it.
1. Insert a node in a tree, 2. Display the height of the tree, 3. Delete a node from
a tree.
51 Write a C Program to implement Heap sort using Max heap in descending order 305-308
52 Write a C Program to implement Heap sort using Min heap in ascending order 309-312
53 Write a menu driven program to implement Inorder Threaded Binary tree and 313-319
traverse it in Inorder, Preorder and Postorder way.(use inorder threads only)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
typedef struct {
char items[MAX];
int top;
} Stack;
// Main function
int main() {
char infix[MAX], postfix[MAX];
infixToPostfix(infix, postfix);
return 0;
}
Q2 Write a code for infix to prefix:-
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Stack structure
typedef struct {
char items[MAX];
int top;
} Stack;
prefix[j] = '\0';
reverse(prefix); // Reverse the result to get the correct prefix expression
reverse(infix); // Optional: Restore the original infix expression
}
int main() {
char infix[MAX], prefix[MAX];
infixToPrefix(infix, prefix);
return 0;
}
Q3 Write a code for prefix to postfix
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Stack structure
typedef struct {
char items[MAX][MAX];
int top;
} Stack;
prefixToPostfix(prefix);
return 0;
}
Q4. Write a code for prefix to infix
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Stack structure
typedef struct {
char items[MAX][MAX];
int top;
} Stack;
prefixToInfix(prefix);
return 0;
}
Q5 Write a code for postfix to prefix
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Stack structure
typedef struct {
char items[MAX][MAX];
int top;
} Stack;
postfixToPrefix(postfix);
return 0;
}
Q6 Write a code for postfix to infix
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
// Stack structure
typedef struct {
char items[MAX][MAX];
int top;
} Stack;
postfixToInfix(postfix);
return 0;
}
Q7 Converst decimal number into bineary number.
#include <stdio.h>
#include <stdlib.h>
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToBinary(decimal);
return 0;
}
Q8 Reverse string using Stack
#include <stdio.h>
#include <string.h>
char stack[20];
int top = -1;
void push(char);
char pop();
int main()
{
char str[20], ch;
int l, i;
printf("Enter string: ");
scanf("%s", str);
l = strlen(str);
for (i = 0; i < l; i++)
push(str[i]);
printf("Reversed string: ");
for (i = 0; i < l; i++)
{
ch = pop();
printf("%c", ch);
}
return 0;
}
void push(char c)
{
if (top < 19)
{ // Ensure we don't exceed the stack size
top++;
stack[top] = c;
}
else
{
printf("Stack overflow\n");
}
}
char pop()
{
if (top >= 0)
{
char c = stack[top];
top--;
return c;
} else
{
printf("Stack underflow\n");
return 0;
}
}
Q9 Check the balance of parenthesis.
#include <stdio.h>
#include <string.h>
#define MAX 1000
char stack[MAX];
int top = -1;
// Function to push an element to the stack
void push(char item)
{
if (top < MAX - 1)
{
stack[++top] = item;
}
}
// Function to pop an element from the stack
char pop()
{
if (top >= 0)
{
return stack[top--];
}
return '\0'; // Return null character if stack is empty
}
// Function to check if the given character is an opening parenthesis
int isOpening(char ch)
{
return ch == '(' || ch == '{' ||ch == '[';
}
// Function to check if the given character is a closing parenthesis
int isClosing(char ch)
{
return ch == ')' || ch == '}' ||
ch == ']';
}
// Function to check if the opening and closing parentheses match
int isMatching(char open, char close)
{
return (open == '(' && close ==')') ||(open == '{' && close == '}') ||(open ==
'[' && close == ']');
}
// Function to check if the parentheses are balanced
int isBalanced(char* expr)
{
for (int i = 0; i <strlen(expr); i++)
{
char ch = expr[i];
if (isOpening(ch))
{
push(ch);
}
else if (isClosing(ch))
{
if (top == -1 ||!isMatching(pop(), ch))
{
return 0;
}
}
}
return top == -1;
}
int main()
{
char expr[MAX];
printf("Enter an expression: ");
scanf("%s", expr);
if (isBalanced(expr))
{
printf("The parentheses are balanced.\n");
}
else
{
printf("The parentheses are not balanced.\n");
}
return 0;
}
Q 10 Implement a Single linked list with the following
operations:
1. Insert node at the beginning
2. Insert node at end
3. Insert node in middle
4. delete node from end
5. delete node from start
6. delete node from mid
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node *prev;
struct Node *next;
};
int main() {
struct Node *head = NULL;
int choice, value, pos;
struct Node *temp = NULL;
while (1) {
printf("\nMenu:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert in Middle\n");
printf("4. Delete from Start\n");
printf("5. Delete from End\n");
printf("6. Delete from Middle\n");
printf("7. Display List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
printf("Enter value to insert in middle: ");
scanf("%d", &value);
printf("Enter the position after which to insert: ");
scanf("%d", &pos);
temp = head;
for (int i = 1; i < pos && temp != NULL; i++) {
temp = temp->next;
}
if (temp != NULL) {
insertInMiddle(temp, value);
} else {
printf("Invalid position!\n");
}
break;
case 4:
deleteFromStart(&head);
break;
case 5:
deleteFromEnd(&head);
break;
case 6:
printf("Enter the position after which to delete: ");
scanf("%d", &pos);
temp = head;
for (int i = 1; i < pos && temp != NULL; i++) {
temp = temp->next;
}
if (temp != NULL && temp->next != NULL) {
deleteFromMiddle(temp);
} else {
printf("Invalid position!\n");
}
break;
case 7:
displayList(head);
break;
case 8:
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
Q 11 Implement a double linked list with the following
operations:
1. Insert node at the beginning
2. Insert node at end
3. Insert node in middle
4. delete node from end
5. delete node from start
6. delete node from mid
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// Insert node at the beginning
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}
// Insert node at the end
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}
// Insert node in the middle (after a given node)
void insertInMiddle(struct Node* prevNode, int data) {
if (prevNode == NULL) {
printf("Previous node cannot be NULL\n");
return;
}
struct Node* newNode = createNode(data);
newNode->next = prevNode->next;
newNode->prev = prevNode;
if (prevNode->next != NULL) {
prevNode->next->prev = newNode;
}
prevNode->next = newNode;
}
// Delete node from the start
void deleteFromStart(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);
}
// Delete node from the end
void deleteFromEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
struct Node* temp = *head;
if (temp->next == NULL) { // Only one node in the list
free(temp);
*head = NULL;
return;
}
while (temp->next != NULL) {
temp = temp->next;
}
temp->prev->next = NULL;
free(temp);
}
// Delete node from the middle (after a given node)
void deleteFromMiddle(struct Node* node) {
if (node == NULL || node->next == NULL) {
printf("Invalid operation\n");
return;
}
struct Node* temp = node->next;
node->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = node;
}
free(temp);
}
// Display the list
void displayList(struct Node* head) {
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, value, pos;
struct Node* temp = NULL;
while (1) {
printf("\nMenu:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert in Middle\n");
printf("4. Delete from Start\n");
printf("5. Delete from End\n");
printf("6. Delete from Middle\n");
printf("7. Display List\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(&head, value);
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(&head, value);
break;
case 3:
printf("Enter value to insert in middle: ");
scanf("%d", &value);
printf("Enter the position after which to insert: ");
scanf("%d", &pos);
temp = head;
for (int i = 1; i < pos && temp != NULL; i++) {
temp = temp->next;
}
if (temp != NULL) {
insertInMiddle(temp, value);
} else {
printf("Invalid position!\n");
}
break;
case 4:
deleteFromStart(&head);
break;
case 5:
deleteFromEnd(&head);
break;
case 6:
printf("Enter the position after which to delete: ");
scanf("%d", &pos);
temp = head;
for (int i = 1; i < pos && temp != NULL; i++) {
temp = temp->next;
}
if (temp != NULL && temp->next != NULL) {
deleteFromMiddle(temp);
} else {
printf("Invalid position!\n");
}
break;
case 7:
displayList(head);
break;
case 8:
exit(0);
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
}
return 0;
}
Q13 Write a program to perform following operations on single variable
polynomials using singly linked list. A. Accept a polynomial, B. Display C.
Addition of two polynomials
#include <stdio.h>
#include <stdlib.h>
struct Node {
};
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = NULL;
return newNode;
scanf("%d", &n);
newNode->next = *head;
*head = newNode;
} else {
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}
// Function to display the polynomial
if (temp == NULL) {
printf("Polynomial is empty.\n");
return;
printf(" + ");
temp = temp->next;
printf("\n");
if (result == NULL) {
temp = result;
} else {
temp = temp->next;
poly1 = poly1->next;
if (result == NULL) {
temp = result;
} else {
temp = temp->next;
poly2 = poly2->next;
} else {
if (coeffSum != 0) {
if (result == NULL) {
result = createNode(coeffSum, poly1->exp);
temp = result;
} else {
temp = temp->next;
poly1 = poly1->next;
poly2 = poly2->next;
if (result == NULL) {
temp = result;
} else {
temp = temp->next;
poly1 = poly1->next;
}
// Add remaining terms of poly2
if (result == NULL) {
temp = result;
} else {
temp = temp->next;
poly2 = poly2->next;
return result;
int main() {
int choice;
while (1) {
printf("\nMenu:\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
acceptPolynomial(&poly1);
break;
case 2:
acceptPolynomial(&poly2);
break;
case 3:
printf("Polynomial 1: ");
displayPolynomial(poly1);
break;
case 4:
printf("Polynomial 2: ");
displayPolynomial(poly2);
break;
case 5:
break;
case 6:
displayPolynomial(result);
break;
case 7:
exit(0);
default:
return 0;
}
Q14 Write a program to perform following operations on two variable
polynomials using singly linked list. A. Accept a polynomial, B. Display C.
Addition of two polynomials
#include <stdio.h>
#include <stdlib.h>
struct Term {
};
newTerm->coeff = coeff;
newTerm->xPower = xPower;
newTerm->yPower = yPower;
newTerm->next = NULL;
return newTerm;
while (1) {
break;
if (*head == NULL) {
*head = temp;
} else {
last->next = temp;
last = temp;
}
}
if (temp == NULL) {
printf("Polynomial is empty.\n");
return;
if (temp->coeff != 0) {
printf(" + ");
temp = temp->next;
printf("\n");
poly1 = poly1->next;
poly2 = poly2->next;
} else {
poly1 = poly1->next;
poly2 = poly2->next;
if (result == NULL) {
result = temp;
} else {
last->next = temp;
last = temp;
return result;
int main() {
acceptPolynomial(&poly1);
acceptPolynomial(&poly2);
displayPolynomial(poly1);
displayPolynomial(poly2);
displayPolynomial(result);
return 0;
}
Q 15 Write a program to perform following operations on single variable
polynomials using double linked list. A. Accept a polynomial, B. Display
C. Addition of two polynomials
#include <stdio.h>
#include <stdlib.h>
struct Node {
};
newNode->coeff = coeff;
newNode->exp = exp;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
scanf("%d", &n);
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
*head = newNode;
} else {
temp = temp->next;
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
temp->next = newNode;
newNode->prev = temp;
if (temp == NULL) {
printf("Polynomial is empty.\n");
return;
printf(" + ");
temp = temp->next;
}
printf("\n");
if (result == NULL) {
temp = result;
} else {
temp->next->prev = temp;
temp = temp->next;
poly1 = poly1->next;
if (result == NULL) {
temp = result;
} else {
temp->next->prev = temp;
temp = temp->next;
poly2 = poly2->next;
} else {
if (coeffSum != 0) {
if (result == NULL) {
temp = result;
} else {
temp->next->prev = temp;
temp = temp->next;
poly1 = poly1->next;
poly2 = poly2->next;
}
// Add remaining terms of poly1
if (result == NULL) {
temp = result;
} else {
temp->next->prev = temp;
temp = temp->next;
poly1 = poly1->next;
if (result == NULL) {
temp = result;
} else {
temp->next->prev = temp;
temp = temp->next;
poly2 = poly2->next;
}
return result;
int main() {
int choice;
while (1) {
printf("\nMenu:\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
acceptPolynomial(&poly1);
break;
case 2:
acceptPolynomial(&poly2);
break;
case 3:
printf("Polynomial 1: ");
displayPolynomial(poly1);
break;
case 4:
printf("Polynomial 2: ");
displayPolynomial(poly2);
break;
case 5:
printf("Polynomials added.\n");
break;
case 6:
displayPolynomial(result);
break;
case 7:
exit(0);
default:
return 0;
}
Q16 Write a program to perform following operations on two variable
polynomials using double linked list. A. Accept a polynomial, B. Display
C. Addition of two polynomials
#include <stdio.h>
#include <stdlib.h>
struct Term {
int coefficient;
int xPower;
int yPower;
};
newTerm->coefficient = coefficient;
newTerm->xPower = xPower;
newTerm->yPower = yPower;
newTerm->prev = NULL;
newTerm->next = NULL;
return newTerm;
}
// Function to accept a polynomial
scanf("%d", &numTerms);
printf("Enter coefficient, x power, and y power for term %d: ", i + 1);
if (*head == NULL) {
*head = newTerm;
tail = *head;
} else {
tail->next = newTerm;
newTerm->prev = tail;
tail = newTerm;
}
// Function to display the polynomial
if (head == NULL) {
printf("Polynomial is empty!\n");
return;
if (temp->next != NULL) {
printf(" + ");
temp = temp->next;
printf("\n");
if (coef != 0) {
if (result == NULL) {
result = newTerm;
tail = result;
} else {
tail->next = newTerm;
newTerm->prev = tail;
tail = newTerm;
poly1 = poly1->next;
poly2 = poly2->next;
if (result == NULL) {
result = newTerm;
tail = result;
} else {
tail->next = newTerm;
newTerm->prev = tail;
tail = newTerm;
poly1 = poly1->next;
} else {
if (result == NULL) {
result = newTerm;
tail = result;
} else {
tail->next = newTerm;
newTerm->prev = tail;
tail = newTerm;
poly2 = poly2->next;
if (result == NULL) {
result = newTerm;
tail = result;
} else {
tail->next = newTerm;
newTerm->prev = tail;
tail = newTerm;
poly1 = poly1->next;
if (result == NULL) {
result = newTerm;
tail = result;
} else {
tail->next = newTerm;
newTerm->prev = tail;
tail = newTerm;
poly2 = poly2->next;
return result;
}
int main() {
acceptPolynomial(&poly1);
acceptPolynomial(&poly2);
displayPolynomial(poly1);
displayPolynomial(poly2);
displayPolynomial(result);
return 0;
}
Q 17 Write a program to perform following operations on student
database using single linked list.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int rollNumber;
char name[50];
};
newStudent->rollNumber = rollNumber;
strcpy(newStudent->name, name);
newStudent->next = NULL;
return newStudent;
}
void displayStudents() {
printf("\nStudent List:\n");
temp = temp->next;
displayReverse(node->next);
newStudent->next = head;
head = newStudent;
if (head == NULL) {
head = newStudent;
} else {
temp = temp->next;
temp->next = newStudent;
temp = temp->next;
if (temp != NULL) {
struct Student* newStudent = createStudent(rollNumber, name);
newStudent->next = temp->next;
temp->next = newStudent;
} else {
head = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
if (temp == NULL) {
return;
}
prev->next = temp->next;
free(temp);
// Main function
int main() {
char name[50];
while (1) {
printf("\nMenu:\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
displayStudents();
break;
case 2:
displayReverse(head);
break;
case 3:
scanf("%d", &rollNumber);
insertAtStart(rollNumber, name);
break;
case 4:
scanf("%d", &rollNumber);
getchar();
name[strcspn(name, "\n")] = 0;
insertAtEnd(rollNumber, name);
break;
case 5:
printf("Enter Roll Number and Name to Insert: ");
scanf("%d", &rollNumber);
getchar();
name[strcspn(name, "\n")] = 0;
scanf("%d", &afterRoll);
break;
case 6:
scanf("%d", &rollNumber);
deleteStudent(rollNumber);
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
return 0;
}
Q 18 Write a program to perform following operations on student
database using double linked list.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int rollNumber;
char name[50];
};
newStudent->rollNumber = rollNumber;
strcpy(newStudent->name, name);
newStudent->prev = NULL;
newStudent->next = NULL;
return newStudent;
void displayStudents() {
printf("\nStudent List:\n");
temp = temp->next;
void displayReverse() {
temp = temp->next;
temp = temp->prev;
if (head != NULL) {
head->prev = newStudent;
newStudent->next = head;
head = newStudent;
if (head == NULL) {
head = newStudent;
} else {
temp->next = newStudent;
newStudent->prev = temp;
temp = temp->next;
if (temp != NULL) {
newStudent->next = temp->next;
newStudent->prev = temp;
if (temp->next != NULL) {
temp->next->prev = newStudent;
temp->next = newStudent;
} else {
}
}
temp = temp->next;
if (temp == NULL) {
return;
if (temp->prev != NULL) {
temp->prev->next = temp->next;
} else {
head = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
free(temp);
}
// Main function
int main() {
char name[50];
while (1) {
printf("\nMenu:\n");
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
displayStudents();
break;
case 2:
displayReverse();
break;
case 3:
scanf("%d", &rollNumber);
insertAtStart(rollNumber, name);
break;
case 4:
scanf("%d", &rollNumber);
getchar();
name[strcspn(name, "\n")] = 0;
insertAtEnd(rollNumber, name);
break;
case 5:
scanf("%d", &rollNumber);
getchar();
name[strcspn(name, "\n")] = 0;
scanf("%d", &afterRoll);
insertInBetween(rollNumber, name, afterRoll);
break;
case 6:
scanf("%d", &rollNumber);
deleteStudent(rollNumber);
break;
case 7:
printf("Exiting...\n");
exit(0);
default:
return 0;
}
Q 19 Write a program to perform following operations on student
database using circular single linked list. A. Display all students
information, B. Display information in reverse order, C. Insert new
students record in between, at last and at start, D. Delete existing
students record from its place.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int rollNo;
char name[50];
int age;
};
newStudent->rollNo = rollNo;
strcpy(newStudent->name, name);
newStudent->age = age;
newStudent->next = newStudent;
return newStudent;
void displayStudents() {
if (last == NULL) {
return;
do {
temp = temp->next;
if (start->next != last->next) {
displayStudentsReverse(start->next);
}
// C1. Insert new student at the beginning
if (last == NULL) {
last = newStudent;
} else {
newStudent->next = last->next;
last->next = newStudent;
if (last == NULL) {
last = newStudent;
} else {
newStudent->next = last->next;
last->next = newStudent;
last = newStudent;
}
// C3. Insert new student in between
int count = 1;
temp = temp->next;
count++;
if (count == pos - 1) {
newStudent->next = temp->next;
temp->next = newStudent;
if (temp == last) {
last = newStudent;
} else {
if (last == NULL) {
if (temp->rollNo == rollNo) {
if (temp == last) {
free(temp);
last = NULL;
} else {
last->next = temp->next;
free(temp);
return;
prev = temp;
temp = temp->next;
if (temp->rollNo == rollNo) {
prev->next = temp->next;
free(temp);
return;
}
prev = temp;
temp = temp->next;
int main() {
char name[50];
while (1) {
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
displayStudents();
break;
case 2:
if (last != NULL) {
displayStudentsReverse(last->next);
} else {
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
printf("Enter Roll No to delete: ");
scanf("%d", &rollNo);
deleteStudent(rollNo);
break;
case 7:
exit(0);
default:
printf("Invalid choice.\n");
return 0;
}
Q 20 Write a program to perform following operations on student
database using circular double linked list. A. Display all students
information, B. Display information in reverse order, C. Insert new
students record in between, at last and at start, D. Delete existing
students record from its place.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int rollNo;
char name[50];
int age;
};
newStudent->rollNo = rollNo;
strcpy(newStudent->name, name);
newStudent->age = age;
newStudent->next = newStudent->prev = NULL;
return newStudent;
void displayStudents() {
if (last == NULL) {
return;
do {
temp = temp->next;
void displayStudentsReverse() {
if (last == NULL) {
return;
temp = temp->prev;
if (last == NULL) {
last = newStudent;
} else {
newStudent->next = last->next;
newStudent->prev = last;
last->next->prev = newStudent;
last->next = newStudent;
if (last == NULL) {
last = newStudent;
} else {
newStudent->next = last->next;
newStudent->prev = last;
last->next->prev = newStudent;
last->next = newStudent;
last = newStudent;
if (last == NULL) {
return;
int count = 1;
temp = temp->next;
count++;
if (count == pos - 1) {
struct Student* newStudent = createStudent(rollNo, name, age);
newStudent->next = temp->next;
newStudent->prev = temp;
temp->next->prev = newStudent;
temp->next = newStudent;
if (temp == last) {
last = newStudent;
} else {
if (last == NULL) {
return;
free(temp);
last = NULL;
} else {
last->next = temp->next;
temp->next->prev = last;
free(temp);
return;
temp = temp->next;
if (temp->rollNo == rollNo) {
prev->next = temp->next;
temp->next->prev = prev;
free(temp);
return;
prev = temp;
temp = temp->next;
}
int main() {
char name[50];
while (1) {
printf("7. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
displayStudents();
break;
case 2:
displayStudentsReverse();
break;
case 3:
printf("Enter Roll No, Name, Age: ");
break;
case 4:
break;
case 5:
break;
case 6:
scanf("%d", &rollNo);
deleteStudent(rollNo);
break;
case 7:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
Q21 Write a program to accept six digit integer number and store it in
circular double linked list (each digit in separate node) and perform
addition and subtraction operation on it. Also consider carry.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int digit;
};
newNode->digit = digit;
newNode->next = newNode;
newNode->prev = newNode;
return newNode;
// Function to insert digit at the end of the circular doubly linked list
if (head == NULL) {
head = newNode;
} else {
last->next = newNode;
newNode->prev = last;
newNode->next = head;
head->prev = newNode;
// Function to initialize the list with each digit of the input number
insertDigit(digit);
// Function to display the number from the circular doubly linked list
void displayNumber() {
do {
printf("%d", temp->digit);
temp = temp->next;
printf("\n");
temp = temp->prev;
if (carry != 0) {
} else {
displayNumber();
}
// Function to subtract a number from the circular doubly linked list
if (diff < 0) {
temp->digit = 10 + diff;
borrow = 1;
} else {
temp->digit = diff;
borrow = 0;
temp = temp->prev;
if (borrow != 0) {
} else {
displayNumber();
// Main function
int main() {
scanf("%d", &number);
return 0;
createList(number);
displayNumber();
while (1) {
printf("\nMenu:\n");
printf("3. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a number to add (0-999999): ");
scanf("%d", &operand);
addNumber(operand);
break;
case 2:
scanf("%d", &operand);
subtractNumber(operand);
break;
case 3:
printf("Exiting...\n");
exit(0);
default:
return 0;
}
Q 22 Write a program to accept six digit integer number and store it in
circular single linked list (each digit in separate node) and perform
addition and subtraction operation on it. Also consider carry.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int digit;
};
newNode->digit = digit;
newNode->next = NULL;
return newNode;
if (*last == NULL) {
*last = newNode;
newNode->next = newNode; // Circular link
} else {
newNode->next = (*last)->next;
(*last)->next = newNode;
*last = newNode;
if (last == NULL) {
return;
do {
printf("%d", temp->digit);
temp = temp->next;
printf("\n");
int carry = 0;
do {
temp1 = temp1->next;
temp2 = temp2->next;
if (carry) {
insertNode(resultLast, carry);
int borrow = 0;
do {
if (diff < 0) {
diff += 10;
borrow = 1;
} else {
borrow = 0;
insertNode(resultLast, diff);
temp1 = temp1->next;
temp2 = temp2->next;
int digit;
insertNode(last, digit);
number /= 10;
}
// Main function to perform operations
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
acceptNumber(&last1, num1);
acceptNumber(&last2, num2);
displayList(last1);
displayList(last2);
// Perform addition
printf("\nSum: ");
displayList(resultLast);
// Reset result list for subtraction
resultLast = NULL;
printf("\nDifference: ");
displayList(resultLast);
return 0;
}
Q23 Write a program to accept two sorted double linked lists and
merge them in a single linked list in such a way that the resultant
linked list will be a sorted one.
#include <stdio.h>
#include <stdlib.h>
// Function to merge two sorted doubly linked lists into a sorted singly linked
list
struct SNode* mergeSortedDLists(struct DNode* head1, struct DNode* head2)
{
struct SNode* resultHead = NULL;
struct SNode* tail = NULL;
if (resultHead == NULL) {
resultHead = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = tail->next;
}
}
// Append remaining nodes from head1 or head2
while (head1 != NULL) {
struct SNode* newNode = createSNode(head1->data);
if (resultHead == NULL) {
resultHead = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = tail->next;
}
head1 = head1->next;
}
return resultHead;
}
int main() {
struct DNode* head1 = NULL;
struct DNode* head2 = NULL;
int n1, n2, data;
printf("Enter the values for the first sorted doubly linked list: ");
for (int i = 0; i < n1; i++) {
scanf("%d", &data);
appendDNode(&head1, data);
}
// Input number of nodes for the second doubly linked list
printf("Enter the number of nodes for the second sorted doubly linked list:
");
scanf("%d", &n2);
printf("Enter the values for the second sorted doubly linked list: ");
for (int i = 0; i < n2; i++) {
scanf("%d", &data);
appendDNode(&head2, data);
}
return 0;
}
Q 24 Write a program to accept two sorted single linked lists
and merge them in a single linked list in such a way that the
resultant linked list will be a sorted one.
#include <stdio.h>
#include <stdlib.h>
return mergedHead;
}
if (head == NULL) {
head = new_node;
tail = head;
} else {
tail->next = new_node;
tail = tail->next;
}
}
return head;
}
// Main function
int main() {
// Input two sorted linked lists
printf("Enter the first sorted linked list:\n");
struct Node* list1 = inputList();
printf("Enter the second sorted linked list:\n");
struct Node* list2 = inputList();
return 0;
}
Q 25 Write a program to implement following operations using stack. A.
Factorial of a given number B. Decimal to binary conversion
Implement a stack using Single linked list
#include <stdio.h>
#include <stdlib.h>
// Stack structure
struct Stack {
struct StackNode* top;
};
while (!isEmpty(stack)) {
result *= pop(stack);
}
int main() {
int choice, number;
printf("Stack Operations:\n");
printf("1. Factorial of a given number\n");
printf("2. Decimal to binary conversion\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a number to find its factorial: ");
scanf("%d", &number);
printf("Factorial of %d is: %d\n", number, factorial(number));
break;
case 2:
printf("Enter a decimal number to convert to binary: ");
scanf("%d", &number);
printf("Binary representation: ");
decimalToBinary(number);
printf("\n");
break;
default:
printf("Invalid choice!\n");
break;
}
return 0;
}
Q 26Write a program to implement following operations using
stack. A. Factorial of a given number B. Decimal to binary conversion
Implement a stack using Double linked list
#include <stdio.h>
#include <stdlib.h>
// Factorial calculation
int num;
printf("Enter a number to calculate factorial: ");
scanf("%d", &num);
int fact = factorial(&stack, num);
printf("Factorial of %d is: %d\n", num, fact);
// Decimal to Binary conversion
printf("Enter a decimal number to convert to binary: ");
scanf("%d", &num);
printf("Binary representation of %d is: ", num);
decimalToBinary(&stack, num);
printf("\n");
return 0;
}
Q27 Write a program to accept a string of parenthesis and check its validity
using stack. Implement stack using double linked list
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Stack structure
struct Stack {
struct StackNode* top;
};
if (stack->top != NULL) {
stack->top->prev = newNode;
}
stack->top = newNode;
}
if (stack->top != NULL) {
stack->top->prev = NULL;
}
free(temp);
return data;
}
int main() {
char str[100];
if (isValidParentheses(str)) {
printf("The parentheses string is valid.\n");
} else {
printf("The parentheses string is invalid.\n");
}
return 0;
}
Q28 Write a program to implement circular queue using circular single linked
list and perform Add, delete and display operations.
#include <stdio.h>
#include <stdlib.h>
while (1) {
printf("\nMenu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to enqueue: ");
scanf("%d", &data);
enqueue(&queue, data);
break;
case 2:
data = dequeue(&queue);
if (data != -1) {
printf("Dequeued element: %d\n", data);
}
break;
case 3:
display(&queue);
break;
case 4:
printf("Exiting the program.\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
}
Q29 Write a program to implement Josephus problem using
circular double linked list.
#include <stdio.h>
#include <stdlib.h>
// Define the structure for the node of the doubly linked list
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
while (n > 1) {
// Traverse to the k-th node
for (int i = 1; i < k; i++) {
temp = temp->next;
}
free(toDelete);
n--;
}
// Define the structure for the node of the circular linked list
struct Node {
int data;
struct Node* next;
};
while (n > 1) {
// Traverse to the k-th node
for (int i = 1; i < k; i++) {
temp = temp->next;
}
return 0;
}
Q31 Write a program to implement double ended queue using double linked
list and perform Add, delete and display operations.
#include <stdio.h>
#include <stdlib.h>
// Define the structure for the node of the doubly linked list
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
initDeque(&deque);
while (1) {
// Menu-driven approach
printf("\nMenu:\n");
printf("1. Add to Front\n");
printf("2. Add to Rear\n");
printf("3. Delete from Front\n");
printf("4. Delete from Rear\n");
printf("5. Display Deque\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the element to add to front: ");
scanf("%d", &data);
addFront(&deque, data);
break;
case 2:
printf("Enter the element to add to rear: ");
scanf("%d", &data);
addRear(&deque, data);
break;
case 3:
data = deleteFront(&deque);
if (data != -1) {
printf("Deleted from front: %d\n", data);
}
break;
case 4:
data = deleteRear(&deque);
if (data != -1) {
printf("Deleted from rear: %d\n", data);
}
break;
case 5:
display(&deque);
break;
case 6:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
Q 32Write a program to accept a string from user and store its each character
in a separate node of circular single linked list and check whether the entered
string is a palindrome or not.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to insert a new node at the end of the circular linked list
void insertNode(struct Node** head, char data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head; // Circular link
} else {
struct Node* temp = *head;
// Traverse to the last node
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head; // Circular link
}
}
// Function to check if the string is a palindrome using the circular linked list
int isPalindrome(struct Node* head) {
if (head == NULL || head->next == head) {
return 1; // Empty or single character list is a palindrome
}
// Step 2: Reverse the second half starting from the node after the middle
struct Node* secondHalf = middle->next;
middle->next = head; // Close the first half to the head to form a circular list
secondHalf = reverseList(secondHalf); // Reverse the second half
// Step 3: Compare the first half with the reversed second half
struct Node* firstHalf = head;
while (secondHalf != head) {
if (firstHalf->data != secondHalf->data) {
return 0; // Not a palindrome
}
firstHalf = firstHalf->next;
secondHalf = secondHalf->next;
}
return 1; // Palindrome
}
int main() {
char str[100];
struct Node* head = NULL;
// Insert each character of the string into the circular linked list
for (int i = 0; i < strlen(str); i++) {
insertNode(&head, str[i]);
}
return 0;
}
Q33 -Implenet Queue using array.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int items[MAX];
int front;
int rear;
} Queue;
while (1) {
printf("\nQueue Menu:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Print Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
printQueue(&q);
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Q34 Write a program to implement a circular queue and perform
the following operations on it. Addq, Delq, Display entire queue.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
struct CircularQueue q;
initQueue(&q);
int choice, value;
while (1) {
printf("\nCircular Queue Menu:\n");
printf("1. Add element to queue (Addq)\n");
printf("2. Delete element from queue (Delq)\n");
printf("3. Display entire queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to add: ");
scanf("%d", &value);
Addq(&q, value);
break;
case 2:
Delq(&q);
break;
case 3:
Display(&q);
break;
case 4:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
Q 35-Write a program to implement a double-ended queue where
the user can remove the element from both the front and rear ends
of the queue.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct {
int items[MAX];
int front;
int rear;
} Deque;
while (1) {
printf("\nDeque Menu:\n");
printf("1. Enqueue at Front\n");
printf("2. Enqueue at Rear\n");
printf("3. Dequeue from Front\n");
printf("4. Dequeue from Rear\n");
printf("5. Print Deque\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to enqueue at the front: ");
scanf("%d", &value);
enqueueFront(&dq, value);
break;
case 2:
printf("Enter the value to enqueue at the rear: ");
scanf("%d", &value);
enqueueRear(&dq, value);
break;
case 3:
dequeueFront(&dq);
break;
case 4:
dequeueRear(&dq);
break;
case 5:
printDeque(&dq);
break;
case 6:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
Q36 -Write a program to implement a circular double-ended queue
where the user can add and remove the elements from both the
front and rear ends of the queue.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
struct CircularDeque dq;
initDeque(&dq);
int choice, value;
while (1) {
printf("\nCircular Deque Menu:\n");
printf("1. Add element to front\n");
printf("2. Add element to rear\n");
printf("3. Delete element from front\n");
printf("4. Delete element from rear\n");
printf("5. Display deque\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to add at front: ");
scanf("%d", &value);
addFront(&dq, value);
break;
case 2:
printf("Enter the value to add at rear: ");
scanf("%d", &value);
addRear(&dq, value);
break;
case 3:
delFront(&dq);
break;
case 4:
delRear(&dq);
break;
case 5:
display(&dq);
break;
case 6:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
Q37-write a program for a Bank simulation of its teller
operations to see how waiting times would be affected by
adding another teller.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int arrivalTime;
int serviceTime;
int waitingTime;
} Customer;
typedef struct {
Customer customers[MAX_CUSTOMERS];
int front;
int rear;
} Queue;
void initializeQueue(Queue *q) {
q->front = -1;
q->rear = -1;
}
int main() {
Queue queue;
initializeQueue(&queue);
srand(time(NULL));
int totalCustomers;
printf("Enter the number of customers: ");
scanf("%d", &totalCustomers);
// Run simulations
float avgWaitTimeSingle = simulateSingleTeller(&queue,
totalCustomers);
initializeQueue(&queue); // Re-initialize for the next simulation
// Display results
printf("\nAverage Waiting Time:\n");
printf("Single Teller: %.2f minutes\n", avgWaitTimeSingle);
printf("Two Tellers: %.2f minutes\n", avgWaitTimeTwo);
return 0;
}
Q38- write a program to keep track of patients as they enter a
medical clinic, assigning patients to the doctor on a first-come, first-
serve basis.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
if (isEmpty(q)) {
q->front = q->rear = 0;
} else {
q->rear = (q->rear + 1) % MAX;
}
q->patients[q->rear].id = id;
strcpy(q->patients[q->rear].name, name);
printf("Patient %s (ID: %d) added to the queue.\n", name, id);
}
int main() {
struct Queue queue;
initQueue(&queue);
int choice, id;
char name[30];
while (1) {
printf("\nClinic Queue Management System:\n");
printf("1. Add a patient\n");
printf("2. Serve a patient\n");
printf("3. Display all patients in queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter patient ID: ");
scanf("%d", &id);
printf("Enter patient name: ");
scanf("%s", name);
addPatient(&queue, id, name);
break;
case 2:
servePatient(&queue);
break;
case 3:
displayQueue(&queue);
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
Q39 write a program to implement josephus problem.
Code:
#include <stdio.h>
#include <stdlib.h>
// Queue structure
typedef struct {
int items[MAX];
int front, rear;
} Queue;
int main() {
int n, k;
Queue q;
initializeQueue(&q);
return 0;
}
Q40 -Write a menu driven program to create a Binary Tree and
perform following non-recursive operations on it.
1. Preorder traversal, 2. Display mirror image of a tree without
creating new tree, 3. Display height of a tree.
Code:
#include <stdio.h>
#include <stdlib.h>
while (!isEmpty(stack)) {
root = pop(&stack);
int currentHeight = (int)(long)pop(&heightStack); // Cast back to
int
if (currentHeight > maxHeight) maxHeight = currentHeight;
if (root->right) {
push(&stack, root->right);
push(&heightStack, (Node*)(long)(currentHeight + 1));
}
if (root->left) {
push(&stack, root->left);
push(&heightStack, (Node*)(long)(currentHeight + 1));
}
}
return maxHeight;
}
while (1) {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Preorder traversal\n");
printf("3. Display mirror image of the tree\n");
printf("4. Display height of the tree\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
if (root == NULL) root = createNode(data);
else root = insertNode(root, data);
break;
case 2:
printf("Preorder traversal: ");
preorderTraversal(root);
break;
case 3:
mirrorImage(root);
printf("Mirror image displayed (original tree modified).\n");
break;
case 4:
printf("Height of the tree: %d\n", heightOfTree(root));
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice! Please enter again.\n");
break;
}
}
return 0;
}
Q41 -Write a menu driven program to create a Binary Tree and
perform following non-recursive operations on it.
1. Postorder Traversal, 2. Display a tree levelwise, 3. Display leaf
nodes of a tree.
Code:
#include <stdio.h>
#include <stdlib.h>
while (!isStackEmpty(stack2)) {
Node *node = pop(&stack2);
printf("%d ", node->data);
}
printf("\n");
}
while (front) {
Node *node = dequeue(&front);
printf("%d ", node->data);
if (node->left) enqueue(&front, &rear, node->left);
if (node->right) enqueue(&front, &rear, node->right);
}
printf("\n");
}
while (!isStackEmpty(stack)) {
Node *node = pop(&stack);
if (!node->left && !node->right) {
printf("%d ", node->data);
}
if (node->right) push(&stack, node->right);
if (node->left) push(&stack, node->left);
}
printf("\n");
}
while (1) {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Postorder traversal\n");
printf("3. Display tree levelwise\n");
printf("4. Display leaf nodes\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
if (root == NULL) root = createNode(data);
else root = insertNode(root, data);
break;
case 2:
printf("Postorder traversal: ");
postorderTraversal(root);
break;
case 3:
printf("Tree levelwise: ");
displayLevelwise(root);
break;
case 4:
printf("Leaf nodes: ");
displayLeafNodes(root);
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice! Please enter again.\n");
break;
}
}
return 0;
}
Q42-Write a menu driven program to create a Binary Tree and
perform following non-recursive operations on it.
1. Inorder Traversal, 2. Display mirror image of a tree by creating
new tree, 3. Equality of two trees.
Code:
#include <stdio.h>
#include <stdlib.h>
return root;
}
curr = pop(&stack);
printf("%d ", curr->data);
curr = curr->right;
}
printf("\n");
}
return mirrorRoot;
}
push(&stack1, root1);
push(&stack2, root2);
while (!isStackEmpty(&stack1) && !isStackEmpty(&stack2)) {
Node* node1 = pop(&stack1);
Node* node2 = pop(&stack2);
push(&stack1, node1->right);
push(&stack1, node1->left);
push(&stack2, node2->right);
push(&stack2, node2->left);
}
int main() {
Node *root = NULL, *mirrorRoot = NULL;
int choice, data, result;
do {
printf("\nMenu:\n");
printf("1. Inorder Traversal\n");
printf("2. Display Mirror Image of the Tree\n");
printf("3. Check Equality of Two Trees\n");
printf("4. Insert a Node\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Inorder Traversal: ");
inorderTraversal(root);
break;
case 2:
mirrorRoot = mirrorTree(root);
printf("Mirror Image (Inorder Traversal): ");
inorderTraversal(mirrorRoot);
break;
case 3: {
Node *secondTree = NULL;
printf("Enter elements of second tree (-1 to stop): ");
while (1) {
scanf("%d", &data);
if (data == -1) break;
secondTree = insert(secondTree, data);
}
result = areEqual(root, secondTree);
printf("The trees are %s.\n", result ? "equal" : "not equal");
break;
}
case 4:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);
return 0;
}
Q 43-Write a menu driven program to create a Binary Tree and
perform following non-recursive operations on it.
1. Postorder Traversal, 2. Preorder Traversal, 3. Copy a tree.
Code:
#include <stdio.h>
#include <stdlib.h>
while (!isStackEmpty(stack1)) {
Node *node = pop(&stack1);
push(&stack2, node);
if (node->left) push(&stack1, node->left);
if (node->right) push(&stack1, node->right);
}
while (!isStackEmpty(stack2)) {
Node *node = pop(&stack2);
printf("%d ", node->data);
}
printf("\n");
}
while (!isStackEmpty(stackOriginal)) {
Node *nodeOriginal = pop(&stackOriginal);
Node *nodeCopy = pop(&stackCopy);
if (nodeOriginal->left) {
nodeCopy->left = createNode(nodeOriginal->left->data);
push(&stackOriginal, nodeOriginal->left);
push(&stackCopy, nodeCopy->left);
}
if (nodeOriginal->right) {
nodeCopy->right = createNode(nodeOriginal->right->data);
push(&stackOriginal, nodeOriginal->right);
push(&stackCopy, nodeCopy->right);
}
}
return rootCopy;
}
while (1) {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Postorder traversal\n");
printf("3. Preorder traversal\n");
printf("4. Copy the tree\n");
printf("5. Display inorder traversal of copied tree\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
if (root == NULL) root = createNode(data);
else root = insertNode(root, data);
break;
case 2:
printf("Postorder traversal: ");
postorderTraversal(root);
break;
case 3:
printf("Preorder traversal: ");
preorderTraversal(root);
break;
case 4:
rootCopy = copyTree(root);
printf("Tree copied successfully.\n");
break;
case 5:
if (rootCopy) {
printf("Inorder traversal of copied tree: ");
inorderTraversal(rootCopy);
printf("\n");
} else {
printf("Tree has not been copied yet.\n");
}
break;
case 6:
exit(0);
break;
default:
printf("Invalid choice! Please enter again.\n");
break;
}
}
return 0;
}
Q44 -Write a menu driven program to create a Binary Search Tree
and perform following non-recursive operations on it.
1. Preorder traversal, 2. Display mirror image of a tree without
creating new tree, 3. Display height of a tree.
Code:
#include <stdio.h>
#include <stdlib.h>
// Menu-driven program
int main() {
Node* root = NULL;
int choice, data;
do {
printf("\nBinary Search Tree Operations:\n");
printf("1. Insert\n");
printf("2. Preorder Traversal\n");
printf("3. Display Mirror Image (In-place)\n");
printf("4. Display Height of the Tree\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Preorder Traversal: ");
preorderTraversal(root);
break;
case 3:
printf("Displaying mirror image of the tree...\n");
mirrorTree(root);
printf("Tree mirrored.\n");
break;
case 4:
printf("Height of the Tree: %d\n", treeHeight(root));
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 5);
return 0;
}
Q45 Write a menu driven program to create a Binary Search Tree
and perform following non-recursive operations on it.
1. Postorder Traversal, 2. Display a tree levelwise, 3. Display leaf
nodes of a tree.
Code:
#include <stdio.h>
#include <stdlib.h>
return root;
}
while (!isStackEmpty(stack1)) {
Node *node = pop(&stack1);
push(&stack2, node);
while (!isStackEmpty(stack2)) {
Node *node = pop(&stack2);
printf("%d ", node->data);
}
printf("\n");
}
while (!isQueueEmpty(front)) {
Node *node = dequeue(&front);
printf("%d ", node->data);
while (!isStackEmpty(stack)) {
Node *node = pop(&stack);
while (1) {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Postorder traversal\n");
printf("3. Display tree levelwise\n");
printf("4. Display leaf nodes\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
if (root == NULL) root = createNode(data);
else root = insertNode(root, data);
break;
case 2:
printf("Postorder traversal: ");
postorderTraversal(root);
break;
case 3:
printf("Tree levelwise: ");
displayLevelwise(root);
break;
case 4:
printf("Leaf nodes: ");
displayLeafNodes(root);
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice! Please enter again.\n");
break;
}
}
return 0;
}
Q46 -Write a menu driven program to create a Binary Search Tree
and perform following non-recursive operations on it.
1. Inorder Traversal, 2. Display mirror image of a tree by creating
new tree, 3. Delete a node from a tree.
Code:
#include <stdio.h>
#include <stdlib.h>
// Menu-driven program
int main() {
Node* root = NULL;
int choice, data;
do {
printf("\nBinary Search Tree Operations:\n");
printf("1. Insert\n");
printf("2. Inorder Traversal\n");
printf("3. Display Mirror Image of the Tree (New Tree)\n");
printf("4. Delete a Node\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder Traversal: ");
inorderTraversal(root);
break;
case 3:
printf("Displaying mirror image of the tree...\n");
Node* mirror = mirrorTree(root);
printf("Mirror Image (In-order): ");
inorderTraversal(mirror);
break;
case 4:
printf("Enter data to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 5);
return 0;
}
Q47 -Write a menu driven program to create a Binary Search Tree
and perform following non-recursive operations on it.
1. Postorder Traversal, 2. Preorder Traversal, 3. Inorder Traversal.
Code:
#include <stdio.h>
#include <stdlib.h>
return root;
}
while (!isStackEmpty(stack1)) {
Node *node = pop(&stack1);
push(&stack2, node);
if (node->left) push(&stack1, node->left);
if (node->right) push(&stack1, node->right);
}
while (!isStackEmpty(stack2)) {
Node *node = pop(&stack2);
printf("%d ", node->data);
}
printf("\n");
}
while (!isStackEmpty(stack)) {
Node *node = pop(&stack);
printf("%d ", node->data);
current = pop(&stack);
printf("%d ", current->data);
current = current->right;
}
printf("\n");
}
// Menu-driven program to perform operations
int main() {
Node *root = NULL;
int choice, data;
while (1) {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Postorder traversal\n");
printf("3. Preorder traversal\n");
printf("4. Inorder traversal\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
if (root == NULL) root = createNode(data);
else root = insertNode(root, data);
break;
case 2:
printf("Postorder traversal: ");
postorderTraversal(root);
break;
case 3:
printf("Preorder traversal: ");
preorderTraversal(root);
break;
case 4:
printf("Inorder traversal: ");
inorderTraversal(root);
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice! Please enter again.\n");
break;
}
}
return 0;
}
Q48 Write a menu driven program to create a Binary Search Tree
and perform following non-recursive operations on it.
1. Copy a tree, 2. Equality of two trees, 3. Delete a node from a
tree.
Code:
#include <stdio.h>
#include <stdlib.h>
if (currentOriginal->left) {
currentCopy->left = createNode(currentOriginal->left->data);
push(&s1, currentOriginal->left);
push(&s2, currentCopy->left);
}
if (currentOriginal->right) {
currentCopy->right = createNode(currentOriginal->right-
>data);
push(&s1, currentOriginal->right);
push(&s2, currentCopy->right);
}
}
return newRoot;
}
while (!isStackEmpty(&s1)) {
Node *current1 = pop(&s1);
Node *current2 = pop(&s2);
current->data = successor->data;
if (successorParent->left == successor)
successorParent->left = successor->right;
else
successorParent->right = successor->right;
free(successor);
}
return root;
}
// Menu-driven program
int main() {
Node* root = NULL;
int choice, data;
Node *root2 = NULL;
do {
printf("\nBinary Search Tree Operations:\n");
printf("1. Insert\n");
printf("2. Copy a Tree\n");
printf("3. Equality of Two Trees\n");
printf("4. Delete a Node\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Copying the tree...\n");
Node* copy = copyTree(root);
printf("Tree copied successfully.\n");
break;
case 3:
printf("Enter data to insert into second tree: ");
scanf("%d", &data);
root2 = insert(root2, data);
if (areTreesEqual(root, root2))
printf("The trees are equal.\n");
else
printf("The trees are not equal.\n");
break;
case 4:
printf("Enter data to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
printf("Node deleted (if it existed).\n");
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
break;
}
} while (choice != 5);
return 0;
}
Q49 -Write a menu driven program to create a Binary Search Tree
and perform following non-recursive operations on it.
1. Insert a node in a tree, 2. Display the height of the tree, 3. Delete
a node from a tree.
Code:
#include <stdio.h>
#include <stdlib.h>
switch (choice) {
case 1:
printf("Enter data to insert: ");
scanf("%d", &data);
root = insertNode(root, data);
printf("Node inserted successfully.\n");
break;
case 2:
printf("Height of the tree: %d\n", height(root));
break;
case 3:
printf("Enter data to delete: ");
scanf("%d", &data);
root = deleteNode(root, data);
printf("Node deleted successfully, if it existed.\n");
break;
case 4:
exit(0);
default:
printf("Invalid choice! Please enter again.\n");
break;
}
}
return 0;
}
Q50 -Right Threaded Binary Tree
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\n1. Insert a node into the tree\n2. Display Inorder
Traversal\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder traversal of the Right Threaded Binary
Tree:\n");
inorder(root);
printf("\n");
break;
case 3:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
Q51-Fully TBST
Code:
#include <stdio.h>
#include <stdlib.h>
while(1) {
printf("\n--- Fully Threaded Binary Tree ---\n");
printf("1. Insert Node\n");
printf("2. Inorder Traversal\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter the data to insert: ");
scanf("%d", &data);
root = insert(root, data);
printf("Node inserted successfully.\n");
break;
case 2:
printf("In-order Traversal of the Fully Threaded Binary
Tree (TBT):\n");
inorderTraversal(root);
printf("\n");
break;
case 3:
printf("Exiting...\n");
return;
default:
printf("Invalid choice. Please try again.\n");
}
}
}
// Main function
int main() {
interactiveMenu();
return 0;
}
Q52-Write a C Program to implement Heap sort using Max heap in
descending order
Code:
#include <stdio.h>
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
Q53 Write a C Program to implement Heap sort using Min heap in
ascending order
Code:
#include <stdio.h>
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
printf("Sorted array in ascending order: ");
printArray(arr, n);
return 0;
}
Q54-Write a menu driven program to implement Inorder Threaded
Binary tree and traverse it in Inorder, Preorder and Postorder
way.(use inorder threads only)
Code:
#include <stdio.h>
#include <stdlib.h>
inorderThread(root->left, prev);
// If the left child is NULL, set the left pointer to the predecessor
(prev node)
if (root->left == NULL && *prev != NULL) {
root->left = *prev;
}
// If the right child is NULL, set the right pointer to the successor
(to be threaded)
if (root->right == NULL && *prev != NULL) {
root->right = *prev;
root->rightThread = 1; // Mark this as a thread
}
inorderThread(root->right, prev);
}
// Postorder traversal
void postorderTraversal(struct Node* root) {
if (root == NULL)
return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ", root->data);
}
while (1) {
printf("\nMenu:\n");
printf("1. Insert Node\n");
printf("2. Inorder Traversal\n");
printf("3. Preorder Traversal\n");
printf("4. Postorder Traversal\n");
printf("5. 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);
inorderThread(root, &prev);
break;
case 2:
printf("Inorder Traversal: ");
inorderTraversal(root);
printf("\n");
break;
case 3:
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
break;
case 4:
printf("Postorder Traversal: ");
postorderTraversal(root);
printf("\n");
break;
case 5:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
Q55-Left Threaded Binary Tree
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* root = NULL;
int choice, data;
while (1) {
printf("\n1. Insert a node into the tree\n2. Display Inorder
Traversal\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the value to insert: ");
scanf("%d", &data);
root = insert(root, data);
break;
case 2:
printf("Inorder traversal of the Left Threaded Binary
Tree:\n");
inorder(root);
printf("\n");
break;
case 3:
exit(0);
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}