Dsa Lab Questions
Dsa Lab Questions
#include<stdio.h>
#include<stdlib.h>
#define SIZE 4
void push(int);
void pop();
void display();
int main()
while (1)
switch (choice)
case 1:
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
if (top == SIZE - 1)
else
top++;
stack[top] = value;
printf("\nInsertion Success!!");
void pop()
if (top == -1)
else
top--;
void display()
if (top == -1)
printf("\nStack is empty!!");
else
int i;
printf("\nStack elements are:\n");
printf("%d\n", stack[i]);
Output-
#include <stdlib.h>
struct Node {
int data;
};
typedef struct Node NODE;
// Function declarations
int main() {
while (1) {
// Menu
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
push(&top, data);
break;
case 2:
pop(&top);
break;
case 3:
display(top);
break;
case 4:
exit(0);
default:
return 0;
if (newNode == NULL) {
exit(1);
newNode->data = data;
newNode->next = NULL;
return newNode;
newNode->next = *top;
*top = newNode;
if (*top == NULL) {
return;
}
*top = temp->next;
free(temp);
if (top == NULL) {
printf("Stack is empty.\n");
return;
top = top->next;
printf("\n");
}
6) QUEUES using Arrays-
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int insertion();
int deletion();
int display();
int queue_array[MAX];
int main() {
int choice;
while (1) {
printf("4. Quit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
insertion();
break;
case 2:
deletion();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice\n");
return 0;
int insertion() {
int add_item;
if (rear == MAX - 1) {
printf("Queue Overflow\n");
} else {
front = 0;
printf("Insert the element in the queue: ");
scanf("%d", &add_item);
rear++;
queue_array[rear] = add_item;
int deletion() {
printf("Queue Underflow\n");
} else {
front++;
int display() {
int i;
if (front == -1) {
printf("Queue is empty\n");
} else {
printf("Queue is:\n");
printf("\n");
Output-
7) Queues Using LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
// Function declarations
void dequeue();
void display();
void menu();
// Global variables
int main() {
while (1) {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
return 0;
if (newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
newNode->data = data;
newNode->next = NULL;
if (front == NULL) {
} else {
rear->next = newNode;
rear = newNode;
void dequeue() {
if (front == NULL) {
} else {
front = front->next;
free(temp);
if (front == NULL) {
rear = NULL;
void display() {
if (front == NULL) {
printf("Queue is empty.\n");
} else {
temp = temp->next;
printf("\n");
void menu() {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
}
8) Circular Queue
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
struct CircularQueue {
int arr[MAX_SIZE];
};
// Function declarations
void menu();
int main() {
CIRCULAR_QUEUE cq;
initializeQueue(&cq);
while (1) {
menu();
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &data);
enqueue(&cq, data);
break;
case 2:
dequeue(&cq);
break;
case 3:
display(&cq);
break;
case 4:
exit(0);
default:
return 0;
} else {
if (cq->front == -1) {
cq->front = cq->rear = 0;
} else {
cq->arr[cq->rear] = data;
if (cq->front == -1) {
printf("Circular Queue is empty. Cannot dequeue.\n");
} else {
if (cq->front == cq->rear) {
initializeQueue(cq);
} else {
if (cq->front == -1) {
} else {
int i = cq->front;
do {
i = (i + 1) % MAX_SIZE;
printf("\n");
void menu() {
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
}
9) Infix to Postfix
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Stack {
int top;
char items[MAX_SIZE];
};
if (stack->top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
exit(1);
stack->items[++stack->top] = item;
if (isEmpty(stack)) {
printf("Stack Underflow\n");
exit(1);
return stack->items[stack->top--];
return 1;
return 2;
return 0;
STACK stack;
stack.top = -1;
int i, j;
ch = infix[i];
if (isalnum(ch)) {
postfix[j++] = ch;
push(&stack, ch);
postfix[j++] = popped;
} else {
postfix[j++] = pop(&stack);
push(&stack, ch);
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
postfix[j] = '\0';
int main() {
infixToPostfix(infix, postfix);
return 0;
}
10) Postfix expression evaluation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct Stack {
int top;
int items[MAX_SIZE];
};
if (stack->top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
exit(1);
stack->items[++stack->top] = item;
if (isEmpty(stack)) {
printf("Stack Underflow\n");
exit(1);
return stack->items[stack->top--];
}
STACK stack;
stack.top = -1;
char ch = postfix[i];
if (isdigit(ch)) {
push(&stack, ch - '0');
} else if (isspace(ch)) {
} else {
switch (ch) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
exit(1);
return pop(&stack);
}
int main() {
char postfix[MAX_SIZE];
scanf("%s", postfix);
return 0;
OUTPUT-
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
printf("\n");
int main() {
printArray(arr, arr_size);
printArray(arr, arr_size);
return 0;
}
12) Bubble Sort
#include <stdio.h>
arr[j + 1] = temp;
printf("\n");
int main() {
printArray(arr, n);
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
13) DFS
#include <stdio.h>
int graph[MAX_VERTICES][MAX_VERTICES];
int visited[MAX_VERTICES];
void initializeGraph() {
visited[i] = 0;
graph[i][j] = 0;
graph[start][end] = 1;
graph[end][start] = 1;
visited[vertex] = 1;
dfs(i);
int main() {
printf("Enter the number of vertices and edges: ");
initializeGraph();
addEdge(start, end);
int startVertex;
scanf("%d", &startVertex);
dfs(startVertex);
return 0;
}
14) BFS
#include <stdio.h>
int graph[MAX_VERTICES][MAX_VERTICES];
int visited[MAX_VERTICES];
int queue[MAX_QUEUE_SIZE];
void initializeGraph() {
visited[i] = 0;
graph[i][j] = 0;
graph[start][end] = 1;
graph[end][start] = 1;
if (rear == MAX_QUEUE_SIZE - 1) {
printf("Queue is full\n");
return;
if (front == -1) {
front = 0;
queue[++rear] = vertex;
int dequeue() {
if (front == -1) {
printf("Queue is empty\n");
return -1;
return vertex;
visited[startVertex] = 1;
enqueue(startVertex);
visited[i] = 1;
enqueue(i);
int main() {
initializeGraph();
addEdge(start, end);
}
int startVertex;
scanf("%d", &startVertex);
bfs(startVertex);
return 0;
#include <stdlib.h>
*a = *b;
*b = temp;
}
void selectionSort(int *arr, int size) {
int min_index = i;
// Find the index of the minimum element in the remaining unsorted array
min_index = j;
swap(&arr[i], &arr[min_index]);
int main() {
int num;
scanf("%d", &num);
scanf("%d", &arr[i]);
selectionSort(arr, num);
free(arr);
printf("\n");
return 0;
}
18) Insertion Sort
#include <stdio.h>
#include <stdlib.h>
int i, key, j;
key = arr[i];
j = i - 1;
// Move elements that are greater than key to one position ahead of their current position
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int main() {
int num;
scanf("%d", &num);
scanf("%d", &arr[i]);
}
insertionSort(arr, num);
free(arr);
printf("\n");
return 0;