C Lab Solutions
C Lab Solutions
November 8, 2024
# include <stdio.h>
#include <stdlib.h>
1
if (root == NULL) {
return 0; // If tree is empty, return 0
}
if (root->left == NULL && root->right == NULL) {
return 1; // Current node is a leaf node, return 1
}
// Recursively count leaf nodes in the left and right subtrees
return countLeafNodes(root->left) + countLeafNodes(root->right);
}
while (1) {
// Display menu options
printf("\n--- BST Menu ---\n");
printf("1. Insert\n");
printf("2. Display (Inorder)\n");
printf("3. Count Leaf Nodes\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Insert a new node into the BST
printf("Enter the value to insert: ");
scanf("%d", &value);
root = insert(root, value);
printf("Value inserted.\n");
break;
case 2:
// Display the BST using inorder traversal
printf("Inorder Traversal: ");
inorderTraversal(root);
2
printf("\n");
break;
case 3:
// Count and display the number of leaf nodes
printf("Number of leaf nodes: %d\n", countLeafNodes(root));
break;
case 4:
// Exit the program
printf("Exiting program...\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
[ ]:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
3
} else {
return searchNode(root->right, key);
}
}
4
root->right = deleteNode(root->right, temp->data); // Delete the␣
inorder successor
↪
}
return root;
}
int main() {
int choice, data;
struct Node* root = NULL;
printf("BST OPERATIONS\n");
printf("1. Insertion\n");
printf("2. Deletion\n");
printf("3. Preorder Traversal\n");
printf("4. Inorder Traversal\n");
printf("5. Postorder Traversal\n");
printf("6. Exit\n");
5
while (1) {
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to Insert: ");
scanf("%d", &data);
root = insertNode(root, data); // Update root
break;
case 2:
printf("Enter data to Delete: ");
scanf("%d", &data);
root = deleteNode(root, data); // Update root
break;
case 3:
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 5:
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
break;
case 6:
exit(0);
break;
default:
printf("Invalid Choice!\n");
}
}
return 0;
}
#include <stdio.h>
#define MAX 10
6
int stack[MAX];
void pop() {
if (top == -1) {
printf("Stack is empty\n");
} else {
int value = stack[top];
top--;
printf("The value deleted is: %d\n", value);
}
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
} else {
printf("Stack elements are: ");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
}
}
int main() {
int choice, value;
while (1) {
printf("\n--- Stack Menu ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
7
case 1:
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting program...\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Initialize the new node with data and link it to the top of the stack
8
newNode->data = value;
newNode->next = top;
// Store the top node temporarily and get the data to be deleted
struct Node* temp = top;
int poppedValue = temp->data;
// Traverse the stack from top to bottom and print each element
struct Node* temp = top;
printf("Stack elements are: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
9
int main() {
int choice, value;
while (1) {
// Display menu options
printf("\n--- Stack Menu ---\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
#include <stdio.h>
#define MAX 10 // Define the maximum size of the queue
10
int front = -1; // Index for the front of the queue
int rear = -1; // Index for the rear of the queue
11
// Function to display the elements of the queue
void display() {
if (isEmpty()) {
printf("Queue is empty\n");
} else {
printf("Queue elements are: ");
for (int i = front; i <= rear; i++) {
printf("%d ", queue[i]);
}
printf("\n");
}
}
while (1) {
// Display menu options
printf("\n--- Queue Menu ---\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\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(value);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting program...\n");
return 0;
default:
printf("Invalid choice, please try again.\n");
}
}
return 0;
12
}
#include <stdio.h>
#include <stdlib.h>
// If the queue is empty, both front and rear point to the new node
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode; // Attach the new node to the end of the queue
rear = newNode; // Update the rear pointer to the new node
}
13
printf("Enqueued: %d\n", value);
}
if (front == NULL) { // If the queue becomes empty, set rear to NULL as␣
↪well
rear = NULL;
}
struct Node* temp = front; // Start from the front of the queue
printf("Queue elements are: ");
while (1) {
14
// Display menu options
printf("\n--- Queue Menu ---\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Prompt user to enter a value to enqueue into the queue
printf("Enter the value to enqueue: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
// Call dequeue function to remove the front element
dequeue();
break;
case 3:
// Call display function to show queue elements
display();
break;
case 4:
// Exit the program
printf("Exiting program...\n");
return 0;
default:
// Handle invalid menu choices
printf("Invalid choice, please try again.\n");
}
}
return 0;
}
[ ]:
15