0% found this document useful (0 votes)
93 views32 pages

Semester II Lab Programs DS 2025

The document contains multiple C programs demonstrating various data structures and algorithms, including array operations (insertion, deletion, searching, sorting), matrix operations (addition, subtraction, multiplication), and stack and queue operations. Each program is structured with user input for elements and provides a menu for interaction. The document serves as a comprehensive guide for implementing fundamental programming concepts in C.

Uploaded by

anetababy04
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)
93 views32 pages

Semester II Lab Programs DS 2025

The document contains multiple C programs demonstrating various data structures and algorithms, including array operations (insertion, deletion, searching, sorting), matrix operations (addition, subtraction, multiplication), and stack and queue operations. Each program is structured with user input for elements and provides a menu for interaction. The document serves as a comprehensive guide for implementing fundamental programming concepts in C.

Uploaded by

anetababy04
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/ 32

1. Write a program for insertion and deletion operations in an array.

#include <stdio.h>
int a[20], n, val, i, pos;
void display();
void insert();
void del();
int main() {
int choice;
printf("\nEnter the size of the array elements: ");
scanf("%d", &n);
printf("\nEnter the elements for the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
do {
printf("\n\n--------Menu-----------\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Exit\n");
printf("-----------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
del();
break;
case 3:
break;
default:
printf("\nInvalid choice!\n");
}
} while (choice != 3);
return 0;
}

void display() {
printf("\nThe array elements are:\n");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
}
void insert() {
printf("\nEnter the position for the new element: ");
scanf("%d", &pos);
printf("\nEnter the element to be inserted: ");
scanf("%d", &val);
for (i = n-1; i >= pos-1; i--) {
a[i+1] = a[i];
}
a[pos - 1] = val;
n = n + 1;

display();
}

void del() {
printf("\nEnter the position of the element to be deleted: ");
scanf("%d", &pos);

val = a[pos - 1];


for (i = pos - 1; i < n - 1; i++) {
a[i] = a[i + 1];
}
n = n - 1;

printf("\nThe deleted element is = %d", val);


display();
}
2. Write a program to search for an element in an array using Linear Search
#include <stdio.h>

// Function to perform linear search


int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return the index of the target element
}
}
return -1; // Target not found
}

int main() {
int arr[20], n, target, result;

// Input number of elements and array elements


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

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


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

// Input the target element to search


printf("Enter the element to search: ");
scanf("%d", &target);

// Perform linear search


result = linearSearch(arr, n, target);
// Display the result
if (result == -1) {
printf("Element not found in the array.\n");
} else {
printf("Element found at index %d.\n", result);
}

return 0;
}
3. Write a program to search for an element in an array using Binary Search
#include <stdio.h>

// Function to perform binary search


int binarySearch(int arr[], int size, int number) {
int low = 0, high = size - 1, mid;

while (low <= high) {


mid = (low + high) / 2;

if (arr[mid] == number) {
return mid; // Target found, return index
}

// If target is greater, ignore the left half


if (arr[mid] < number) {
low = mid + 1;
}
// If target is smaller, ignore the right half
else {
high = mid - 1;
}
}

return -1; // Target not found


}

int main() {
int arr[20], n, number, result;

// Input number of elements and array elements


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

printf("Enter the elements in ascending order:\n");


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

// Input the target element to search


printf("Enter the element to search: ");
scanf("%d", &number);
// Perform binary search
result = binarySearch(arr, n, number);

// Display the result


if (result == -1) {
printf("Element not found in the array.\n");
} else {
printf("Element found at index %d.\n", result);
}

return 0;
}
4. Write a program to sort an array using Bubble Sort
#include <stdio.h>
int main() {
int arr[20], n;
printf("Enter the number of elements: ");
scanf("%d", &n);

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


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);}

return 0;
}
5. Write a program to sort an array using Selection Sort

#include <stdio.h>
int main() {
int arr[20], n;
printf("Enter the number of elements: ");
scanf("%d", &n);

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


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n - 1; i++) {
int min= i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);}

return 0;
}
6. Write a program to sort an array using insertion Sort
#include <stdio.h>
int main() {
int arr[20], n,i,j;
printf("Enter the number of elements: ");
scanf("%d", &n);

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


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);}

return 0;
}
7. Write a program to merge two arrays
#include <stdio.h>
int main() {
int ar1[10],ar2[10],n1,n2,i,arr[50];
printf("Enter the number of elements in the array: ");
scanf("%d", &n1);
printf("\nelements of the first array are : \n");
for (i = 0; i < n1; i++)
{
scanf("%d", &ar1[i]);
}
printf("\n\nEnter the number of elements for second array: ");
scanf("%d", &n2);
for (i = 0; i < n2; i++)
{
printf("Enter element %d:\n", i + 1);
scanf("%d", &ar2[i]);
}

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


{
arr[i] = ar1[i];
}
for (i = 0; i < n2; i++)
{
arr[n1 + i] = ar2[i];
}
printf("Array after merging the two arrays:\n");
for (i = 0; i < n1 + n2; i++)
{
printf("%d ", arr[i]);
}

return 0;
}
8. Write a program to add two matrix

#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
// add the matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
// print the result
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}

9. Write a program to Subtract two matrix

#include <stdio.h>
int main() {
int m, n, i, j;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d%d", &m, &n);
int a[m][n], b[m][n], c[m][n];
printf("Enter the elements of matrix A: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of matrix B: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
// add the matrices
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
// print the result
printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}

10. Write a program to Multiply two matrix


#include <stdio.h>

int main()
{
int r1,r2,c1,c2;
printf("Enter number of rows for First Matrix:\n");
scanf("%d",&r1);
printf("Enter number of columns for First Matrix:\n");
scanf("%d",&c1);
printf("Enter number of rows for Second Matrix:\n");
scanf("%d",&r2);
printf("Enter number of columns for Second Matrix:\n");
scanf("%d",&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
else
{
int a[r1][c1],b[r2][c2],c[r1][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}

for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
c[i][j]=0;

// Multiplying i’th row with j’th column


for(int k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
return 0;
}
11. Stack Operations

#include <stdio.h>
#define MAX 15
int stack[MAX], top = -1, item;
void push();
void pop();
void display();

int main() {
printf("\n\t\t\t*** STACK OPERATION ***\n");

int ch;
do {
printf("\nMenu : ");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);

switch(ch) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("\nInvalid Entry");
}
} while(ch != 4);

return 0;
}

void push() {
if(top == MAX - 1) {
printf("\nOVERFLOW");
} else {
top++;
printf("\nEnter an item: ");
scanf("%d", &item);
stack[top] = item;
printf("\nInserted successfully");
}
}

void pop() {
if(top == -1) {
printf("\nUNDERFLOW");
} else {
item = stack[top];
top--;
printf("\n%dDeleted successfully",item);
}
}

void display() {
if(top == -1) {
printf("\nStack is empty");
} else {
for(int i = top; i >= 0; i--){
printf("%d\n", stack[i]);
}
}
}
12. QUEUE OPERATIONS
#include <stdio.h>

int que[15], n, front = 0, rear = 0, item;

void enqueue();
void dequeue();
void display();

int main() {
int ch;
printf("\nEnter size of queue: ");
scanf("%d", &n);
printf("\n\t\t\t***QUEUE OPERATIONS***\n");

do {
printf("\nMenu:\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("\nExiting program...\n");
break;
default:
printf("\nInvalid Entry\n");
}
} while (ch != 4);

return 0;
}

// Function to insert an element into the queue


void enqueue() {
if (rear == n) {
printf("\nQueue Overflow! Cannot insert more elements.\n");
} else {
printf("\nEnter an item: ");
scanf("%d", &item);
que[rear] = item;
rear++;
printf("\nInserted Successfully!\n");
display();
}
}

// Function to remove an element from the queue


void dequeue() {
if (front == rear) {
printf("\nQueue Underflow! No elements to delete.\n");
} else {
printf("\nDeleted item: %d\n", que[front]);
front++; // Move the front forward
display();
}
}

// Function to display the queue elements


void display() {
if (front == rear) {
printf("\nQueue is empty.\n");
} else {
printf("\nQueue elements: ");
for (int i = front; i < rear; i++) {
printf("%d ", que[i]);
}
printf("\n");
}
}
13. CIRCULAR QUEUE OPERATIONS

#include <stdio.h>
#include <stdlib.h>
#define size 5

int main()
{
int arr[size],R=-1,F=0,count=0,ch,n,i,x;

for(;;) // An infinite loop


{

printf("\n1. Add");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter Choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1:
if(count==size)
{
printf("Queue is full");
}
else
{
printf("Enter a number ");
scanf("%d",&n);
R=(R+1)%size;
arr[R]=n;
count=count+1;
}
break;

case 2:
if(count==0)
{
printf("Queue is empty");
}
else
{
printf("Number Deleted = %d",arr[F]);
F=(F+1)%size;
count=count-1;
}
break;

case 3:
if(count==0)
{
printf("Queue is empty");

}
else
{
x=F;
for(i=1; i<=count; i++)
{
printf("%d ",arr[x]);
x=(x+1)%size;
}

}
break;

case 4:
exit(0);
break;

default:
printf("Wrong Choice");

}
}
return 0;
}

14. Program to Insert a Node in Singly Linked List


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

struct Node *head = NULL; // Head of the linked list

// Function to insert at the beginning


void insertAtBeginning(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Inserted %d at the beginning.\n", value);
}

// Function to insert at the end


void insertAtEnd(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
} else {
struct Node *temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}
printf("Inserted %d at the end.\n", value);
}

// Function to insert at a specified position


void insertAtPosition(int value, int position) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;

if (position == 1) { // Insert at beginning if position is 1


newNode->next = head;
head = newNode;
} else {
struct Node *temp = head;
for (int i = 1; temp != NULL && i < position - 1; i++)
temp = temp->next;
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
return;
}
newNode->next = temp->next;
temp->next = newNode;
}
printf("Inserted %d at position %d.\n", value, position);
}

// Function to display the linked list


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

// Main function with switch case menu


int main() {
int choice, value, position;

do {
printf("\nMenu:\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Specified Position\n");
printf("4. Display List\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;

case 2:
printf("Enter value to insert at end: ");
scanf("%d", &value);
insertAtEnd(value);
break;

case 3:
printf("Enter value and position to insert: ");
scanf("%d %d", &value, &position);
insertAtPosition(value, position);
break;

case 4:
displayList();
break;

case 5:
printf("Exiting program.\n");
break;

default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);

return 0;
}
15. Program to Delete a Node in Singly Linked List

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

struct Node *head = NULL; // Head of the linked list

// Function to insert at the beginning


void insertAtBeginning(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Inserted %d at the beginning.\n", value);
}

// Function to delete from the beginning


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *temp = head;
head = head->next;
free(temp);
printf("Deleted node from the beginning.\n");
}

// Function to delete from the end


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *temp = head;
if (head->next == NULL) {
free(head);
head = NULL;
} else {
struct Node *prev = NULL;
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
}
printf("Deleted node from the end.\n");
}

// Function to delete from a specified position


void deleteFromPosition(int position) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *temp = head;
if (position == 1) {
head = head->next;
free(temp);
printf("Deleted node from position %d.\n", position);
return;
}
struct Node *prev = NULL;
for (int i = 1; temp != NULL && i < position; i++) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("Position out of range.\n");
return;
}
prev->next = temp->next;
free(temp);
printf("Deleted node from position %d.\n", position);
}

// Function to display the linked list


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

// Main function with switch case menu


int main() {
int choice, value, position;

do {
printf("\nMenu:\n");
printf("1. Insert a node\n");
printf("2. Delete from Beginning\n");
printf("3. Delete from End\n");
printf("4. Delete from Specified Position\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;

case 2:
deleteFromBeginning();
break;

case 3:
deleteFromEnd();
break;

case 4:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteFromPosition(position);
break;

case 5:
displayList();
break;

case 6:
printf("Exiting program.\n");
break;

default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);

return 0;
}
16. Searching a Value in the Linked List

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

struct Node *head = NULL; // Head of the linked list

// Function to insert at the beginning


void insertAtBeginning(int value) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = head;
head = newNode;
printf("Inserted %d at the beginning.\n", value);
}

// Function to search for a value in the linked list


void search(int value) {
struct Node *temp = head;
int pos = 1;
while (temp != NULL) {
if (temp->data == value) {
printf("Value %d found at position %d.\n", value, pos);
return;
}
temp = temp->next;
pos++;
}
printf("Value %d not found in the list.\n", value);
}

// Function to display the linked list


void displayList() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
struct Node *temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Main function with switch case menu
int main() {
int choice, value;

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

switch (choice) {
case 1:
printf("Enter value to insert at beginning: ");
scanf("%d", &value);
insertAtBeginning(value);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &value);
search(value);
break;
case 3:
displayList();
break;

case 4:
printf("Exiting program.\n");
break;

default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);

return 0;
}
17. a program to perform the following operations in a Doubly Linked List: (a) Create
(b) Search for an element
#include <stdio.h>
#include <stdlib.h>

// Node structure for Doubly Linked List


struct Node {
int data;
struct Node * next;
struct Node * prev;
};

struct Node* head = NULL; // Head of the list

// Function to create a new node


void create(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;

if (head == NULL) {
head = newNode;
} else {
struct Node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
printf("Inserted %d into the list.\n", value);
}

// Function to search for an element


void search(int value) {
struct Node* temp = head;
int pos = 1;
while (temp != NULL) {
if (temp->data == value) {
printf("Element %d found at position %d.\n", value, pos);
return;
}
temp = temp->next;
pos++;
}
printf("Element %d not found in the list.\n", value);
}

// Function to display the list


void display() {
if (head == NULL) {
printf("The list is empty.\n");
return;
}
struct Node* temp = head;
printf("Doubly Linked List: ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function with menu


int main() {
int choice, value;
do {
printf("\nMenu:\n");
printf("1. Create\n");
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
create(value);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &value);
search(value);
break;
case 3:
display();
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);

return 0;
}
18. Write a program to perform the following operations in a Circular Linked List: (a)
Create (b) Search an element
#include <stdio.h>
#include <stdlib.h>

// Node structure for Circular Linked List


struct Node {
int data;
struct Node* next;
};

struct Node* head = NULL;

// Function to create a new node in Circular Linked List


void create(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
return;
}
newNode->data = value;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
head->next = head;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
printf("Inserted %d into the list.\n", value);
}

// Function to search for an element in Circular Linked List


void search(int value) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
int pos = 1;
do {
if (temp->data == value) {
printf("Element %d found at position %d.\n", value, pos);
return;
}
temp = temp->next;
pos++;
} while (temp != head);

printf("Element %d not found in the list.\n", value);


}

// Function to display Circular Linked List


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

// Main function with menu


int main() {
int choice, value;
do {
printf("\nMenu:\n");
printf("1. Create\n");
printf("2. Search\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
create(value);
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &value);
search(value);
break;
case 3:
display();
break;
case 4:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);

return 0;
}

19. Write a program to perform the following operations on a binary search tree. (a)
Preorder Traversal (b) Inorder Traversal (c) Postorder Traversal

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

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

struct Node* root = NULL;

struct Node* createNode(int value) {


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

void addNode(int value) {


struct Node* newNode = createNode(value);
if (root == NULL) {
root = newNode;
printf("Node having value %d is added\n", value);
return;
}

struct Node* current = root;


struct Node* parent = NULL;
while (current != NULL) {
parent = current;
if (value > current->info) {
current = current->right;
} else if (value < current->info) {
current = current->left;
} else {
printf("\nValue %d is duplicate and not allowed\n", value);
free(newNode);
return;
}
}

if (value > parent->info) {


parent->right = newNode;
} else {
parent->left = newNode;
}
printf("Node having value %d is added\n", value);
}

void inorder(struct Node* root) {


if (root != NULL) {
inorder(root->left);
printf("[%d] ", root->info);
inorder(root->right);
}
}

void preorder(struct Node* root) {


if (root != NULL) {
printf("[%d] ", root->info);
preorder(root->left);
preorder(root->right);
}
}

void postorder(struct Node* root) {


if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("[%d] ", root->info);
}
}

int main() {
int ch, value;
do {
printf("\n1. Add Node");
printf("\n2. Inorder Traversal");
printf("\n3. Preorder Traversal");
printf("\n4. Postorder Traversal");
printf("\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter the value: ");
scanf("%d", &value);
addNode(value);
break;
case 2:
if (root == NULL)
printf("Tree is empty\n");
else {
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
}
break;
case 3:
if (root == NULL)
printf("Tree is empty\n");
else {
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
}
break;
case 4:
if (root == NULL)
printf("Tree is empty\n");
else {
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
}
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (ch != 5);
return 0;
}

20. Write a program to perform the following using recursion: (a) Find the factorial of
a number (b)Generate Fibonacci Series
#include <stdio.h>

// Function to find factorial using recursion


int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}

// Function to generate Fibonacci series using recursion


int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int choice, num, i;
do {
printf("\nMenu:\n");
printf("1. Find Factorial\n");
printf("2. Generate Fibonacci Series\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter a number to find factorial: ");
scanf("%d", &num);
printf("Factorial of %d is: %d\n", num, factorial(num));
break;
case 2:
printf("Enter the number of terms for Fibonacci series: ");
scanf("%d", &num);
printf("Fibonacci Series: ");
for (i = 0; i < num; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 3);
return 0;
}

You might also like