DDS Practical
DDS Practical
Implement Stack and its operations like (creation push pop traverse peek search) using linear data
structure
#include <stdio.h>
#define z 5
int stack[z];
void push(int n) {
if(top == z - 1)
printf("\nStack Overflow.");
else {
top++;
stack[top] = n;
void pop() {
if(top == -1)
printf("\nStack Underflow.");
else
top--;
void peek() {
printf("%d", stack[top]);
}
void display() {
while(top >= 0) {
printf("%d\t", stack[top]);
top--;
int main() {
int ch, p;
scanf("%d", &ch);
switch(ch) {
scanf("%d", &p);
push(p);
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: display();
break;
return 0;
}
Experiment 2:
#include <stdio.h>
#include <ctype.h>
char stack[20];
void push(char x) {
stack[++top] = x;
char pop() {
if (top == -1)
return -1;
return stack[top--];
int priority(char x) {
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/')
return 2;
if (x == '^')
return 3;
return -1;
}
int main() {
char exp[50];
char *e, x;
scanf("%s", exp);
e = exp;
if (isalnum(*e)) {
printf("%c", *e);
push(*e);
if (x == -1) {
return 1;
printf("%c", x);
if (x == -1) {
return 1;
} else {
printf("%c", pop());
push(*e);
}
e++;
x = pop();
if (x == '(' || x == ')') {
return 1;
printf("%c", x);
printf("\n");
return 0;
Experiment 3:
#include <stdio.h>
#include <ctype.h>
int stack[20];
stack[++top] = x;
int pop() {
return stack[top--];
int main() {
char exp[20];
char *e;
scanf("%s", exp);
e = exp;
if (isdigit(*e)) {
num = *e - 48;
push(num);
} else {
n1 = pop();
n2 = pop();
switch(*e) {
break;
break;
break;
push(n3);
e++;
return 0;
Experiment 4:
#include <stdio.h>
#include <stdlib.h>
#define MAX_DISKS 64
void solveHanoi(int n, int* sourcePeg, int* targetPeg, int* auxiliaryPeg, int* sourceTop, int*
targetTop, int* auxiliaryTop, int sourceNum, int targetNum);
void displayTowers(int* sourcePeg, int sourceTop, int* auxiliaryPeg, int auxiliaryTop, int* targetPeg,
int targetTop);
scanf("%d", &numDisks);
return 1;
int sourcePeg[MAX_DISKS];
int auxiliaryPeg[MAX_DISKS];
int targetPeg[MAX_DISKS];
return 0;
}
void solveHanoi(int numDisks, int* sourcePeg, int* targetPeg, int* auxiliaryPeg, int* sourceTop, int*
targetTop, int* auxiliaryTop, int sourceNum, int targetNum) {
if (numDisks == 1) {
return;
peg[++(*top)] = disk;
if (*top == -1) {
printf("Peg underflow\n");
return -1;
return peg[(*top)--];
}
void displayTowers(int* sourcePeg, int sourceTop, int* auxiliaryPeg, int auxiliaryTop, int* targetPeg,
int targetTop) {
displayPeg(sourcePeg, sourceTop);
displayPeg(auxiliaryPeg, auxiliaryTop);
displayPeg(targetPeg, targetTop);
printf("\n");
printf("\n");
Experiment 5:
Implement queue and its operations like enqueue, dequeue, traverse, search.
#include <stdio.h>
#define N 4
int queue[N];
void enqueue (int x) {
if (rear == N - 1)
printf("\nOverflow.");
rear = front = 0;
queue[rear] = x;
}else {
queue[++rear] = x;
void dequeue () {
printf("\nUnderflow.");
else {
front++;
void traversal() {
printf("\nTraversal: ");
printf("%d\t", queue[i]);
void search(int x) {
int main() {
enqueue(5);
enqueue(4);
enqueue(3);
dequeue();
traversal();
search(4);
return 0;
Experiment 6:
Implement Single Linked lists and its operations(creation insertion deletion traversal search reverse)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
void linkedListTraversal(struct Node *ptr) {
ptr = ptr->next;
newNode->data = data;
newNode->next = head;
return newNode;
head = head->next;
free(temp);
return head;
if (current->data == value) {
return 1;
current = current->next;
return 0;
}
next = current->next;
current->next = prev;
prev = current;
current = next;
return prev;
int main() {
head->data = 7;
head->next = second;
second->data = 11;
second->next = third;
third->data = 41;
third->next = fourth;
fourth->data = 66;
fourth->next = NULL;
linkedListTraversal(head);
linkedListTraversal(head);
head = deleteFirst(head);
linkedListTraversal(head);
if (search(head, searchValue)) {
} else {
head = reverse(head);
linkedListTraversal(head);
head = head->next;
free(temp);
return 0;
Experiment 7:
Implement Double Linked lists and its operations(creation insertion deletion traversal search reverse)
#include <stdio.h>
#include <stdlib.h>
int data;
} Node;
if(!newNode) {
printf("Memory error\n");
return NULL;
return newNode;
if(!newNode) return;
if(*head != NULL) {
*head = newNode;
if(!newNode) return;
if(*head == NULL) {
*head = newNode;
return;
}
temp -> next = newNode;
if(*head == NULL) {
printf("List is empty\n");
return;
if(*head != NULL) {
free(temp);
return;
if(temp == NULL) {
return;
}
if(temp -> next != NULL) {
free(temp);
if(head == NULL) {
printf("List is empty\n");
return;
while(temp != NULL) {
printf("\n");
int position = 0;
return position;
position++;
return -1;
while(current != NULL) {
if(temp != NULL) {
printf("List reversed\n");
int main() {
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
traverse(head);
deleteNode(&head, 2);
traverse(head);
search(head, 3);
search(head, 5);
reverse(&head);
traverse(head);
return 0;
Experiment 8:
#include <stdio.h>
return -1;
while (low <= high && key >= arr[low] && key <= arr[high]) {
if (low == high) {
return -1;
return -1;
int main() {
scanf("%d", &search);
scanf("%d", &n);
switch (n) {
break;
break;
result == -1 ? printf("Element not found in array.") : printf("Element found at index %d.", result);
return 0;
Experiment 9:
Implement Bubble sort,selection sort, Insertion sort, quick sort ,merge sort.
#include <stdio.h>
arr[j] = arr[j+1];
arr[j+1] = temp;
}
void selectionSort(int arr[], int arr_size) {
int minIndex = i;
minIndex = j;
if (minIndex != i) {
arr[i] = arr[minIndex];
arr[minIndex] = temp;
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = temp;
start++;
end--;
arr[start] = arr[end];
arr[end] = temp;
arr[lb] = arr[end];
arr[end] = temp;
return end;
}
void merge(int arr[], int lb, int mid, int ub) {
int n1 = mid - lb + 1;
int n2 = ub - mid;
int i = 0, j = 0, k = lb;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
int main() {
int arr[5];
scanf("%d", &arr[i]);
int size_arr = 5;
int n;
printf("\nSelect the required operation:\n1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4.
Quick Sort\n5. Merge Sort: ");
scanf("%d", &n);
switch(n) {
break;
case 2: selectionSort(arr, size_arr);
break;
break;
break;
break;
printf("\n");
return 0;
Experiment 10:
Implement Binary search Tree and its operations ( creation, insertion, deletion).
Experiment 11:
Implement a Binary Search Tree(BST) along with its operations, including creation, insertion, and
deletion, and perform traversals such as Preorder, Inorder, and Postorder on BST.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
newNode->data = data;
return newNode;
if (root == NULL) {
return createNode(data);
} else {
return root;
}
struct Node* findMin(struct Node* root) {
root = root->left;
return root;
if (root == NULL) {
return root;
} else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
root->data = temp->data;
}
return root;
if (root != NULL) {
inorderTraversal(root->left);
inorderTraversal(root->right);
if (root != NULL) {
preorderTraversal(root->left);
preorderTraversal(root->right);
if (root != NULL) {
postorderTraversal(root->left);
postorderTraversal(root->right);
int main() {
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorderTraversal(root);
printf("\n");
printf("Deleting 20\n");
inorderTraversal(root);
printf("\n");
printf("Deleting 30\n");
inorderTraversal(root);
printf("\n");
printf("Deleting 50\n");
inorderTraversal(root);
printf("\n");
preorderTraversal(root);
printf("\n");
postorderTraversal(root);
printf("\n");
return 0;
Experiment 12:
Implement Graphs and represent using adjaceny list and adjacency matrix and implement basic
operations with traversals (BFS and DFS).
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAX_NODES 5
int adjMatrix[MAX_NODES][MAX_NODES];
int queue[MAX_NODES];
adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;
queue[rear++] = startNode;
visited[startNode] = true;
queue[rear++] = i;
visited[i] = true;
printf("\n");
visited[node] = true;
dfs(i, visited);
}
struct Node {
int vertex;
};
newNode->vertex = v;
newNode->next = NULL;
return newNode;
newNode->next = adjList[u];
adjList[u] = newNode;
newNode = createNode(u);
newNode->next = adjList[v];
adjList[v] = newNode;
void printGraph() {
while (temp) {
printf("\n");
int main() {
adjMatrix[i][j] = 0;
adjList[i] = NULL;
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 3);
addEdge(2, 4);
addEdge(3, 4);
bfs(0);
dfs(0, visited);
printf("\n");
addEdgeToList(0, 1);
addEdgeToList(0, 2);
addEdgeToList(1, 3);
addEdgeToList(1, 4);
addEdgeToList(2, 3);
addEdgeToList(3, 4);
printGraph();
return 0;