DSA Lab
DSA Lab
1. Write an interactive program to search an element in the given linear array using linear and
binary searching technique.
PART B
1. Write program to evaluate a postfix expression.
3. Write an interactive program to perform insertion and deletion operations in Linear Queue.
7. Write an interactive program to perform insertion operation in linked list-at the beginning,
at the end and in-between.
8. Program to create a binary tree and also print the preordered values, inorder values,
postorder values.
9. Write a program to add two polynomials of one variable and_n degree and represent them
as linked list.
1. Write an interactive program to search an element in the given linear array using linear and binary searching
technique.
if (arr[mid] == key) {
return mid; // If key found at mid
} else if (arr[mid] < key) {
low = mid + 1; // Search in right half
} else {
high = mid - 1; // Search in left half
}
}
return -1; // Return -1 if not found
}
int main() {
int arr[100], n, key, choice, result;
switch (choice) {
case 1:
result = linearSearch(arr, n, key); // Call linear search
if (result != -1)
printf("Element found at position %d (0-based index).\n", result);
else
printf("Element not found in the array.\n");
break;
case 2:
// Before binary search, make sure the array is sorted
// Simple bubble sort for sorting
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
default:
printf("Invalid choice! Please enter 1 or 2.\n"); // Handle invalid choice
}
int main() {
// Move elements that are greater than key one position ahead
}
output
Enter 5 elements:
45 12 78 23 9
9 12 23 45 78
Output
Enter number of elements: 6
Enter 6 elements:
45 12 9 67 34 23
Array in ascending order:
9 12 23 34 45 67
int main() {
int arr[100], n, i, j, minIndex, temp;
// Swap the found minimum element with the first unsorted element
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
Output
Enter number of elements: 5
Enter 5 elements:
23 45 12 9 34
Array in ascending order:
9 12 23 34 45
int main() {
int arr[100], n;
Output
Enter number of elements: 6
Enter 6 elements:
30 12 45 22 5 9
Array in ascending order:
5 9 12 22 30 45
int main() {
int arr[100]; // Array with maximum size 100
int n, i, pos, value;
// Validate position
if (pos < 1 || pos > n + 1) {
printf("Invalid position!\n");
return 1; // Exit the program with error
}
// Shift elements to the right to make space for the new element
for (i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
Output
Enter number of elements: 4
Enter 4 elements:
10 20 30 40
Enter the position to insert the element (1 to 5): 3
Enter the value to insert: 25
Array after insertion:
10 20 25 30 40
7. Write an interactive program to implement the following operations on stack.
#include <stdio.h> // For standard input and output
#define SIZE 100 // Define maximum size of stack
switch (choice) {
case 1:
push(); // Call push function
break;
case 2:
pop(); // Call pop function
break;
case 3:
peek(); // Call peek function
break;
case 4:
display(); // Call display function
break;
case 5:
printf("Exiting program.\n");
return 0; // Exit the program
default:
printf("Invalid choice! Please enter 1 to 5.\n");
}
}
int main() {
int n;
Output
Enter the number of disks: 3
Steps to solve Tower of Hanoi:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
int main() {
char expression[100];
return 0;
}
Output
Enter a postfix expression (e.g. 53+62-*): 53+62-*
Result = 10
char stack[SIZE];
int top = -1;
// Reverse a string
void reverse(char* exp) {
int len = strlen(exp);
for (int i = 0; i < len / 2; i++) {
char temp = exp[i];
exp[i] = exp[len - i - 1];
exp[len - i - 1] = temp;
}
}
if (isalnum(ch)) {
postfix[j++] = ch; // Operand goes to output
}
else if (ch == '(') {
push(ch); // Push opening bracket
}
else if (ch == ')') {
while (top != -1 && peek() != '(') {
postfix[j++] = pop(); // Pop until '('
}
pop(); // Remove '('
}
else if (isOperator(ch)) {
while (top != -1 && precedence(peek()) >= precedence(ch)) {
postfix[j++] = pop(); // Pop operators with higher/equal precedence
}
push(ch); // Push current operator
}
}
return 0;
}
Output
Enter an infix expression: (A+B)*C
Prefix expression: *+ABC
3. Write an interactive program to perform insertion and deletion operations in Linear Queue.
#include <stdio.h> // For input and output
while (1) {
// Display menu
printf("\n--- Linear Queue Operations Menu ---\n");
printf("1. Insert (Enqueue)\n");
printf("2. Delete (Dequeue)\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
// Handle choices
switch (choice) {
case 1:
enqueue(); // Call insert function
break;
case 2:
dequeue(); // Call delete function
break;
case 3:
display(); // Call display function
break;
case 4:
printf("Exiting program.\n");
return 0; // Exit program
default:
printf("Invalid choice! Please enter 1 to 4.\n");
}
}
Output
--- Linear Queue Operations Menu ---
1. Insert (Enqueue)
2. Delete (Dequeue)
3. Display
4. Exit
Enter your choice: 1
Enter value to insert: 10
10 inserted into the queue.
// First insertion
if (front == -1) {
front = rear = 0;
}
// Wrap around
else if (rear == SIZE - 1 && front != 0) {
rear = 0;
}
else {
rear++;
}
queue[rear] = value;
printf("%d inserted into the queue.\n", value);
}
}
// Function to delete an element from circular queue
void dequeue() {
// Check if the queue is empty
if (front == -1) {
printf("Queue Underflow! No elements to delete.\n");
} else {
printf("%d deleted from the queue.\n", queue[front]);
while (1) {
// Display menu
printf("\n--- Circular Queue Menu ---\n");
printf("1. Insert (Enqueue)\n");
printf("2. Delete (Dequeue)\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
return 0;
}
Output
#include <stdio.h> // For input-output
// First insertion
if (front == -1) {
front = rear = 0;
}
// Wrap around
else if (rear == SIZE - 1 && front != 0) {
rear = 0;
}
else {
rear++;
}
queue[rear] = value;
printf("%d inserted into the queue.\n", value);
}
}
// Function to delete an element from circular queue
void dequeue() {
// Check if the queue is empty
if (front == -1) {
printf("Queue Underflow! No elements to delete.\n");
} else {
printf("%d deleted from the queue.\n", queue[front]);
while (1) {
// Display menu
printf("\n--- Circular Queue Menu ---\n");
printf("1. Insert (Enqueue)\n");
printf("2. Delete (Dequeue)\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
return 0;
}
5. Write a program to delete an item from the linked list.
#include <stdio.h> // For input/output
#include <stdlib.h> // For malloc() and free()
struct Node* head = NULL; // Start of the linked list (global pointer)
if (head == NULL) {
head = newNode; // First node
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next; // Traverse to last node
}
temp->next = newNode; // Add new node at the end
}
}
// If list is empty
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
return;
}
if (temp == NULL) {
printf("List is empty.\n");
return;
}
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data); // Print data
temp = temp->next; // Move to next node
}
printf("NULL\n");
}
while (1) {
printf("\n--- Linked List Menu ---\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice); // Read user input
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(value);
break;
case 3:
display();
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Enter 1-4.\n");
}
}
return 0;
}
Output
--- Linked List Menu ---
1. Insert
2. Delete
3. Display
4. Exit
Enter your choice: 1
Enter value to insert: 10
switch (choice) {
case 1: // If user chooses Push
printf("Enter value to push: ");
scanf("%d", &value);
push(value); // Call push function
break;
case 2: // If user chooses Pop
pop(); // Call pop function
break;
case 3: // If user chooses Display
display(); // Call display function
break;
case 4: // Exit the program
exit(0);
default: // Handle invalid input
printf("Invalid choice! Try again.\n");
}
}
Output
--- Stack Menu ---
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 50
50 pushed to stack
7. Write an interactive program to perform insertion operation in linked list-at the beginning, at the end and in-
between.
#include <stdio.h> // For input-output functions
#include <stdlib.h> // For malloc() and exit()
while (1) {
// Show the menu
printf("\n--- Linked List Insertion Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice); // Read user's choice
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value); // Call function
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value); // Call function
break;
case 3:
printf("Enter value and position to insert: ");
scanf("%d %d", &value, &position);
insertAtPosition(value, position); // Call function
break;
case 4:
displayList(); // Show current list
break;
case 5:
exit(0); // Exit program
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}
Output
#include <stdio.h> // For input-output functions
#include <stdlib.h> // For malloc() and exit()
while (1) {
// Show the menu
printf("\n--- Linked List Insertion Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice); // Read user's choice
switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value); // Call function
break;
case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value); // Call function
break;
case 3:
printf("Enter value and position to insert: ");
scanf("%d %d", &value, &position);
insertAtPosition(value, position); // Call function
break;
case 4:
displayList(); // Show current list
break;
case 5:
exit(0); // Exit program
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}
8. Program to create a binary tree and also print the preordered values, inorder values,
postorder values.
#include <stdio.h>
#include <stdlib.h>
if (value == -1) {
return NULL; // No node created
}
// Main function
int main() {
struct Node* root = NULL; // Declare root pointer
printf("\n");
return 0;
}
Output
Create the binary tree
Enter data (-1 for no node): 10
Enter left child of 10:
Enter data (-1 for no node): 20
Enter left child of 20:
Enter data (-1 for no node): -1
Enter right child of 20:
Enter data (-1 for no node): -1
Enter right child of 10:
Enter data (-1 for no node): 30
Enter left child of 30:
Enter data (-1 for no node): -1
Enter right child of 30:
Enter data (-1 for no node): -1
Inorder Traversal: 20 10 30
Preorder Traversal: 10 20 30
Postorder Traversal: 20 30 10
9. Write a program to add two polynomials of one variable and_n degree and represent them as linked list.
#include <stdio.h>
#include <stdlib.h>
return result;
}
// Main function
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* sum = NULL;
int n, coeff, power;
// Display results
printf("\nFirst Polynomial: ");
display(poly1);
return 0;
}
Output
Enter number of terms in first polynomial: 3
Enter coefficient and power for term 1: 3 2
Enter coefficient and power for term 2: 5 1
Enter coefficient and power for term 3: 6 0
Enter number of terms in second polynomial: 3
Enter coefficient and power for term 1: 2 2
Enter coefficient and power for term 2: 1 1
Enter coefficient and power for term 3: 4 0