0% found this document useful (0 votes)
36 views15 pages

C Lab Solutions

Lab solutions of C programs

Uploaded by

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

C Lab Solutions

Lab solutions of C programs

Uploaded by

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

c-lab-solutions

November 8, 2024

[ ]: Implement BST and count the number of leaf nodes

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

// Define the structure of each node in the BST


struct Node {
int data; // Data field to store the node's value
struct Node* left; // Pointer to the left child node
struct Node* right; // Pointer to the right child node
};

// Function to create a new node with given value


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

// Function to insert a node into the BST


struct Node* insert(struct Node* root, int value) {
if (root == NULL) { // If tree is empty, create a new node as root
return createNode(value);
}
if (value < root->data) { // Insert in the left subtree if value is smaller
root->left = insert(root->left, value);
} else if (value > root->data) { // Insert in the right subtree if value␣
↪is larger

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


}
return root; // Return the unchanged root pointer
}

// Function to count the number of leaf nodes in the BST


int countLeafNodes(struct Node* root) {

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

// Function to display the BST in an inorder traversal


void inorderTraversal(struct Node* root) {
if (root == NULL) {
return;
}
inorderTraversal(root->left); // Traverse left subtree
printf("%d ", root->data); // Display current node's data
inorderTraversal(root->right); // Traverse right subtree
}

// Main function for BST operations and counting leaf nodes


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

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

[ ]:

[ ]: BST Operations and Traversals

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

struct Node {
int data;
struct Node* left;
struct Node* right;
};

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Fixed␣
↪sizeof

newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Function to search a node in the tree


struct Node* searchNode(struct Node* root, int key) {
if (root == NULL || root->data == key) {
return root;
}
if (key < root->data) {
return searchNode(root->left, key);

3
} else {
return searchNode(root->right, key);
}
}

// Function to insert a new node


struct Node* insertNode(struct Node* root, int data) {
if (root == NULL) {
return createNode(data);
}
if (data < root->data) {
root->left = insertNode(root->left, data);
} else if (data > root->data) {
root->right = insertNode(root->right, data);
}
return root;
}

// Function to delete a node


struct Node* deleteNode(struct Node* root, int data) {
if (root == NULL) {
return root;
}
if (data < root->data) {
root->left = deleteNode(root->left, data);
} else if (data > root->data) {
root->right = deleteNode(root->right, data);
} else {
// Node to be deleted found
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;
}
// Node with two children: get the inorder successor (smallest in the␣
↪right subtree)

struct Node* temp = root->right;


while (temp && temp->left != NULL) {
temp = temp->left;
}
root->data = temp->data; // Copy the inorder successor's content to␣
↪this node

4
root->right = deleteNode(root->right, temp->data); // Delete the␣
inorder successor

}
return root;
}

// Preorder traversal of the tree (Root -> Left -> Right)


void preorder(struct Node* root) {
if (root == NULL) {
return;
}
printf("%d -> ", root->data);
preorder(root->left);
preorder(root->right);
}

// Inorder traversal of the tree (Left -> Root -> Right)


void inorder(struct Node* root) {
if (root == NULL) {
return;
}
inorder(root->left);
printf("%d -> ", root->data);
inorder(root->right);
}

// Postorder traversal of the tree (Left -> Right -> Root)


void postorder(struct Node* root) {
if (root == NULL) {
return;
}
postorder(root->left);
postorder(root->right);
printf("%d -> ", root->data);
}

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

[ ]: Implementation of stack using Array

#include <stdio.h>
#define MAX 10

int top = -1;

6
int stack[MAX];

void push(int value) {


if (top == MAX - 1) {
printf("Stack is full\n");
} else {
top++;
stack[top] = value;
printf("Value pushed successfully.\n");
}
}

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

[ ]: Implementation of stack using Linkedlist

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

// Define the structure of each node in the linked list


struct Node {
int data; // Data field to store the stack element
struct Node* next; // Pointer to the next node
};

// Initialize top of stack as NULL, meaning stack is initially empty


struct Node* top = NULL;

// Function to push an element onto the stack


void push(int value) {
// Allocate memory for a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

// Check if memory allocation was successful


if (newNode == NULL) {
printf("Stack overflow\n");
return;
}

// Initialize the new node with data and link it to the top of the stack

8
newNode->data = value;
newNode->next = top;

// Update the top pointer to the new node


top = newNode;
printf("Value pushed successfully.\n");
}

// Function to pop an element from the stack


void pop() {
// Check if the stack is empty
if (top == NULL) {
printf("Stack is empty\n");
return;
}

// Store the top node temporarily and get the data to be deleted
struct Node* temp = top;
int poppedValue = temp->data;

// Move the top pointer to the next node


top = top->next;

// Free memory of the removed node


free(temp);
printf("The value deleted is: %d\n", poppedValue);
}

// Function to display all elements in the stack


void display() {
// Check if the stack is empty
if (top == NULL) {
printf("Stack is empty\n");
return;
}

// 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");
}

// Main function to provide a menu-driven interface for stack operations

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

// Switch case to handle user's choice


switch (choice) {
case 1:
// Prompt user to enter a value to push onto the stack
printf("Enter the value to push: ");
scanf("%d", &value);
push(value);
break;
case 2:
// Call pop function to remove the top element
pop();
break;
case 3:
// Call display function to show stack 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;
}

[ ]: Implementation of queue using Array

#include <stdio.h>
#define MAX 10 // Define the maximum size of the queue

int queue[MAX]; // Array to hold the queue elements

10
int front = -1; // Index for the front of the queue
int rear = -1; // Index for the rear of the queue

// Function to check if the queue is full


int isFull() {
if (rear == MAX - 1) {
return 1; // Queue is full
}
return 0; // Queue is not full
}

// Function to check if the queue is empty


int isEmpty() {
if (front == -1 || front > rear) {
return 1; // Queue is empty
}
return 0; // Queue is not empty
}

// Function to enqueue (insert) an element into the queue


void enqueue(int value) {
if (isFull()) {
printf("Queue is full\n");
} else {
if (front == -1) {
front = 0; // If the queue is empty, set front to 0
}
rear++; // Move the rear pointer to the next position
queue[rear] = value; // Insert the element at the rear
printf("Enqueued: %d\n", value);
}
}

// Function to dequeue (remove) an element from the queue


void dequeue() {
if (isEmpty()) {
printf("Queue is empty\n");
} else {
int dequeuedValue = queue[front]; // Store the value to be dequeued
front++; // Move the front pointer to the next position
if (front > rear) { // If the queue becomes empty
front = rear = -1; // Reset front and rear pointers
}
printf("Dequeued: %d\n", dequeuedValue);
}
}

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

// Main function for menu-driven queue operations


int main() {
int choice, value;

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
}

[ ]: Implementation of queue using Linkedlist

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

// Define the structure of each node in the linked list


struct Node {
int data; // Data field to store the queue element
struct Node* next; // Pointer to the next node
};

// Pointers to the front and rear of the queue


struct Node* front = NULL;
struct Node* rear = NULL;

// Function to check if the queue is empty


int isEmpty() {
if (front == NULL) {
return 1; // Queue is empty
}
return 0; // Queue is not empty
}

// Function to enqueue (insert) an element into the queue


void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //␣
↪Create a new node

if (newNode == NULL) { // Check if memory allocation failed


printf("Memory allocation failed\n");
return;
}

newNode->data = value; // Set the data of the new node


newNode->next = NULL; // The new node should point to NULL (it's the␣
↪last node in the queue)

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

// Function to dequeue (remove) an element from the queue


void dequeue() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}

struct Node* temp = front; // Store the front node temporarily


int dequeuedValue = temp->data; // Get the data of the front node

front = front->next; // Move the front pointer to the next node

if (front == NULL) { // If the queue becomes empty, set rear to NULL as␣
↪well

rear = NULL;
}

free(temp); // Free the memory of the removed node


printf("Dequeued: %d\n", dequeuedValue);
}

// Function to display the elements of the queue


void display() {
if (isEmpty()) {
printf("Queue is empty\n");
return;
}

struct Node* temp = front; // Start from the front of the queue
printf("Queue elements are: ");

// Traverse the queue and print the data of each node


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

// Main function for menu-driven queue operations


int main() {
int choice, value;

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

You might also like