DSP Lab
DSP Lab
a)
#include <stdio.h>
// Stack structure
typedef struct {
int data[MAX];
int top;
} Stack;
stack->top = -1;
if (isFull(stack)) {
printf("Stack overflow!\n");
} else {
stack->data[++stack->top] = value;
if (isEmpty(stack)) {
printf("Stack underflow!\n");
return -1;
} else {
return stack->data[stack->top--];
}
// Function to display the stack elements
if (isEmpty(stack)) {
printf("Stack is empty!\n");
} else {
printf("\n");
int main() {
Stack stack;
initializeStack(&stack);
while (1) {
printf("\nStack Operations:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
push(&stack, value);
break;
case 2:
value = pop(&stack);
if (value != -1) {
break;
case 3:
display(&stack);
break;
case 4:
printf("Exiting...\n");
return 0;
default:
}
}
Output:
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Stack elements: 2
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Stack elements: 2 4
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Popped value: 4
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Stack elements: 2
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Exiting...
b)
#include <stdio.h>
typedef struct {
} Queue;
q->front = -1;
q->rear = -1;
if (isFull(q)) {
return;
if (isEmpty(q)) {
q->rear++;
q->data[q->rear] = value;
}
// Dequeue operation: Remove an element from the front of the queue
if (isEmpty(q)) {
if (q->front == q->rear) {
q->front = -1;
q->rear = -1;
} else {
q->front++;
return value;
if (isEmpty(q)) {
printf("Queue is empty!\n");
return;
printf("\n");
int main() {
Queue q;
initQueue(&q);
while (1) {
printf("\nQueue Operations:\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
dequeue(&q);
break;
case 3:
display(&q);
break;
case 4:
printf("Exiting...\n");
return 0;
default:
Output:
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue elements: 23
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue is empty!
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Queue is empty!
Queue Operations:
1. Enqueue
2. Dequeue
3. Display
4. Exit
2. Write a C++ program to implement the following using a singly linked list a. Stack ADT b. Queue ADT.
a)
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
typedef struct {
Node *top;
} Stack;
s->top = NULL;
if (newNode == NULL) {
return;
}
newNode->data = value;
newNode->next = s->top;
s->top = newNode;
// Pop operation: Remove and return the top element of the stack
if (isEmpty(s)) {
s->top = s->top->next;
free(temp);
return value;
if (isEmpty(s)) {
printf("Stack is empty!\n");
return;
current = current->next;
printf("\n");
int main() {
Stack s;
initStack(&s);
while (1) {
printf("\nStack Operations:\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) {
case 1:
scanf("%d", &value);
push(&s, value);
break;
case 2:
pop(&s);
break;
case 3:
display(&s);
break;
case 4:
printf("Exiting...\n");
return 0;
default:
Output:
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
Stack elements: 40
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
1. Push
2. Pop
3. Display
4. Exit
Stack is empty!
Stack Operations:
1. Push
2. Pop
3. Display
4. Exit
b)
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
};
// Queue structure
struct Queue {
};
newNode->data = data;
newNode->next = NULL;
return newNode;
return queue;
// Enqueue operation
return;
queue->rear->next = newNode;
queue->rear = newNode;
// Dequeue operation
return -1;
queue->front = queue->front->next;
if (queue->front == NULL) {
queue->rear = NULL;
free(temp);
return data;
if (queue->front == NULL) {
printf("Queue is empty.\n");
return;
printf("Queue: ");
temp = temp->next;
printf("\n");
// Main function
int main() {
struct Queue* queue = createQueue();
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
displayQueue(queue);
dequeue(queue);
dequeue(queue);
displayQueue(queue);
dequeue(queue);
return 0;
Output:
10 added to the queue.
Queue: 10 20 30
Queue: 30
3. Write C Program to implement the DEQUE (double ended queue) ADT using arrays.
#include <stdio.h>
#define MAX 10
typedef struct {
int data[MAX];
} Deque;
dq->front = -1;
dq->rear = -1;
}
// Check if the deque is full
if (isFull(dq)) {
return;
if (isEmpty(dq)) {
dq->front = dq->rear = 0;
} else if (dq->front == 0) {
dq->front = MAX - 1;
} else {
dq->front--;
dq->data[dq->front] = value;
if (isFull(dq)) {
return;
if (isEmpty(dq)) {
dq->front = dq->rear = 0;
dq->rear = 0;
} else {
dq->rear++;
dq->data[dq->rear] = value;
if (isEmpty(dq)) {
return;
}
printf("Deleted %d from the front.\n", dq->data[dq->front]);
if (dq->front == dq->rear) {
dq->front = 0;
} else {
dq->front++;
if (isEmpty(dq)) {
return;
if (dq->front == dq->rear) {
} else if (dq->rear == 0) {
dq->rear = MAX - 1;
} else {
dq->rear--;
if (isEmpty(dq)) {
printf("Deque is empty.\n");
return;
int i = dq->front;
while (1) {
if (i == dq->rear) break;
i = (i + 1) % MAX;
printf("\n");
int main() {
Deque dq;
initializeDeque(&dq);
insertRear(&dq, 10);
insertRear(&dq, 20);
displayDeque(&dq);
insertFront(&dq, 5);
displayDeque(&dq);
deleteFront(&dq);
displayDeque(&dq);
deleteRear(&dq);
displayDeque(&dq);
return 0;
Output:
Inserted 10 at the rear.
Deque elements: 10 20
Deque elements: 5 10 20
Deque elements: 10 20
Deque elements: 10
4. Write a C program to perform the following operations: a) Insert an element into a binary search tree. b) Delete an element from a binary search tree.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = value;
return newNode;
if (root == NULL) {
root = root->left;
return root;
if (root == NULL) {
} else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
// Node with two children: get the inorder successor (smallest in the right subtree)
return root;
}
if (root != NULL) {
inorder(root->left);
inorder(root->right);
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &value);
break;
case 2:
scanf("%d", &value);
break;
case 3:
inorder(root);
printf("\n");
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
}
return 0;
Output:
Binary Search Tree Operations:
1. Insert
2. Delete
4. Exit
Inorder Traversal: 12 13 15
Inorder Traversal: 13 15
Exiting program.
5. Write a C program that use recursive functions to traverse the given binary tree in a) Preorder b) Inorder and c) Postorder.
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int val;
};
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
preorderTraversal(root->left);
preorderTraversal(root->right);
inorderTraversal(root->left);
inorderTraversal(root->right);
postorderTraversal(root->left);
postorderTraversal(root->right);
int main() {
// 1
// / \
// 2 3
// / \
// 4 5
root->left = newTreeNode(2);
root->right = newTreeNode(3);
root->left->left = newTreeNode(4);
root->left->right = newTreeNode(5);
preorderTraversal(root);
printf("\n");
inorderTraversal(root);
printf("\n");
printf("\n");
return 0;
Output:
Preorder traversal: 1 2 4 5 3
Inorder traversal: 4 2 5 1 3
Postorder traversal: 4 5 2 3 1
L.S
#include <stdio.h>
if(arr[i] == target) {
int main() {
int target = 6;
if(result != -1) {
} else {
return 0;
B.S
#include <stdio.h>
if(arr[mid] == target) {
left = mid + 1;
} else {
right = mid - 1;
int main() {
int target = 6;
if(result != -1) {
} else {
return 0;
Output:
Element found at index 3
7. Write C programs for the implementation of BFS and DFS for a given graph.
#include <stdio.h>
#include <stdlib.h>
struct AdjListNode {
int dest;
};
struct AdjList {
struct AdjListNode *head;
};
struct Graph {
int V;
};
newNode->dest = dest;
newNode->next = NULL;
return newNode;
graph->V = V;
graph->array[i].head = NULL;
return graph;
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
newNode = newAdjListNode(src);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
visited[i] = 0;
int queue[graph->V];
queue[rear++] = startVertex;
while (adjList) {
if (!visited[adjVertex]) {
queue[rear++] = adjVertex;
visited[adjVertex] = 1;
adjList = adjList->next;
free(visited);
visited[vertex] = 1;
while (adjList) {
if (!visited[adjVertex]) {
adjList = adjList->next;
visited[i] = 0;
free(visited);
int main() {
int V = 5;
addEdge(graph, 0, 1);
addEdge(graph, 0, 4);
addEdge(graph, 1, 2);
addEdge(graph, 1, 3);
addEdge(graph, 1, 4);
addEdge(graph, 2, 3);
addEdge(graph, 3, 4);
BFS(graph, 0);
printf("\n");
DFS(graph, 0);
printf("\n");
return 0;
Output:
Breadth-First Search starting from vertex 0:
04132
04321
8. Write C programs for implementing the following sorting methods: a) Merge Sort b) Heap Sort
a)
#include <stdio.h>
int i = 0, j = 0, k = left;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
printf("\n");
int main() {
printArray(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printArray(arr, arr_size);
return 0;
b)
#include <stdio.h>
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
largest = left;
largest = right;
if (largest != i) {
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
heapify(arr, n, i);
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
printf("\n");
int main() {
printArray(arr, n);
heapSort(arr, n);
printArray(arr, n);
return 0;
Output:
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
9. Write a C program to perform the following operations. a) Insertion into a B-tree b) Deletion from a B-tree.
a)
b)
#include <stdio.h>
*a = *b;
*b = temp;
// Partition function
int i = low - 1;
i++;
swap(&arr[i], &arr[j]);
}
return i + 1;
quickSort(arr, pi + 1, high);
printf("\n");
int main() {
printArray(arr, n);
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
Output:
Unsorted array:
10 7 8 9 1 5
Sorted array:
1 5 7 8 9 10