0% found this document useful (0 votes)
35 views

DDS Practical

Uploaded by

blab41333
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)
35 views

DDS Practical

Uploaded by

blab41333
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/ 37

Experiment 1:

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];

int top = -1;

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() {

printf("\nFrom top to bottom: ");

while(top >= 0) {

printf("%d\t", stack[top]);

top--;

int main() {

printf("\nStack Operations: \n1. Push\n2. Pop\n3. Peek\n4. Display\nChoose option: ");

int ch, p;

scanf("%d", &ch);

switch(ch) {

case 1: printf("\nEnter number to push: ");

scanf("%d", &p);

push(p);

break;

case 2: pop();

break;

case 3: peek();

break;

case 4: display();

break;

return 0;

}
Experiment 2:

Implement Infix to Postfix Expression Conversion using Stack

#include <stdio.h>

#include <ctype.h>

char stack[20];

int top = -1;

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;

printf("Enter the expression: ");

scanf("%s", exp);

e = exp;

printf("Postfix expression: ");

while (*e != '\0') {

if (isalnum(*e)) {

printf("%c", *e);

} else if (*e == '(') {

push(*e);

} else if (*e == ')') {

while (top != -1 && (x = pop()) != '(') {

if (x == -1) {

printf("\nError: Unmatched parenthesis. \n");

return 1;

printf("%c", x);

if (x == -1) {

printf("\nError: Unmatched parenthesis. \n");

return 1;

} else {

while (top != -1 && priority(stack[top]) >= priority(*e)) {

printf("%c", pop());

push(*e);
}

e++;

while (top != -1) {

x = pop();

if (x == '(' || x == ')') {

printf("\nError: Unmatched parenthesis.\n");

return 1;

printf("%c", x);

printf("\n");

return 0;

Experiment 3:

Implement Postfix evaluation using Stack.

#include <stdio.h>

#include <ctype.h>

int stack[20];

int top = -1;


void push(int x) {

stack[++top] = x;

int pop() {

return stack[top--];

int main() {

char exp[20];

char *e;

int n1, n2, n3, num;

printf("Enter the expression: ");

scanf("%s", exp);

e = exp;

printf("Postfix expression: ");

while (*e != '\0') {

if (isdigit(*e)) {

num = *e - 48;

push(num);

} else {

n1 = pop();

n2 = pop();

switch(*e) {

case '+': n3 = n1 + n2;

break;

case '-': n3 = n2 - n1;

break;

case '*': n3 = n1 * n2;


break;

case '/': n3 = n2 / n1;

break;

push(n3);

e++;

printf("\nThe result of the expression is %s = %d.\n\n", exp, pop());

return 0;

Experiment 4:

Implement Towers of Hanoi using Stack.

#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 pushDisk(int* peg, int* top, int disk);

int popDisk(int* peg, int* top);

void displayTowers(int* sourcePeg, int sourceTop, int* auxiliaryPeg, int auxiliaryTop, int* targetPeg,
int targetTop);

void displayPeg(int* peg, int top);


int main() {

int numDisks; // Number of disks

printf("Enter the number of disks: ");

scanf("%d", &numDisks);

if (numDisks > MAX_DISKS) {

printf("Number of disks exceeds the maximum allowed (%d).\n", MAX_DISKS);

return 1;

int sourcePeg[MAX_DISKS];

int auxiliaryPeg[MAX_DISKS];

int targetPeg[MAX_DISKS];

int sourceTop = -1;

int auxiliaryTop = -1;

int targetTop = -1;

for (int i = numDisks; i >= 1; i--) {

pushDisk(sourcePeg, &sourceTop, i);

printf("Initial state of towers:\n");

displayTowers(sourcePeg, sourceTop, auxiliaryPeg, auxiliaryTop, targetPeg, targetTop);

solveHanoi(numDisks, sourcePeg, targetPeg, auxiliaryPeg, &sourceTop, &targetTop, &auxiliaryTop,


1, 3);

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) {

int disk = popDisk(sourcePeg, sourceTop);

pushDisk(targetPeg, targetTop, disk);

printf("Move disk %d from peg %d to peg %d\n", disk, sourceNum, targetNum);

displayTowers(sourcePeg, *sourceTop, auxiliaryPeg, *auxiliaryTop, targetPeg, *targetTop);

return;

solveHanoi(numDisks - 1, sourcePeg, auxiliaryPeg, targetPeg, sourceTop, auxiliaryTop, targetTop,


sourceNum, targetNum);

int disk = popDisk(sourcePeg, sourceTop);

pushDisk(targetPeg, targetTop, disk);

printf("Move disk %d from peg %d to peg %d\n", disk, sourceNum, targetNum);

displayTowers(sourcePeg, *sourceTop, auxiliaryPeg, *auxiliaryTop, targetPeg, *targetTop);

solveHanoi(numDisks - 1, auxiliaryPeg, targetPeg, sourcePeg, auxiliaryTop, targetTop, sourceTop,


sourceNum, targetNum);

void pushDisk(int* peg, int* top, int disk) {

peg[++(*top)] = disk;

int popDisk(int* peg, int* top) {

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) {

printf("Source Peg: ");

displayPeg(sourcePeg, sourceTop);

printf("Auxiliary Peg: ");

displayPeg(auxiliaryPeg, auxiliaryTop);

printf("Target Peg: ");

displayPeg(targetPeg, targetTop);

printf("\n");

void displayPeg(int* peg, int top) {

for (int i = top; i >= 0; i--) {

printf("%d ", peg[i]);

printf("\n");

Experiment 5:

Implement queue and its operations like enqueue, dequeue, traverse, search.

#include <stdio.h>

#define N 4

int rear = -1, front = -1;

int queue[N];
void enqueue (int x) {

if (rear == N - 1)

printf("\nOverflow.");

else if(rear == -1 && front == -1) {

rear = front = 0;

queue[rear] = x;

}else {

queue[++rear] = x;

void dequeue () {

if (front == -1 && rear == -1)

printf("\nUnderflow.");

else if (front == rear)

front = rear = -1;

else {

printf("%d is deleted", queue[front]);

front++;

void traversal() {

printf("\nTraversal: ");

for (int i = 0; i <= rear; i++)

printf("%d\t", queue[i]);

void search(int x) {

for (int i = 0; i <= rear; i++) {


if (queue[i] == x) {

printf("\n%d found in index %d.", x, i);

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;

struct Node *next;

};
void linkedListTraversal(struct Node *ptr) {

while (ptr != NULL) {

printf("Element: %d\n", ptr->data);

ptr = ptr->next;

struct Node *insertAtFirst(struct Node *head, int data) {

struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = head;

return newNode;

struct Node *deleteFirst(struct Node *head) {

struct Node *temp = head;

head = head->next;

free(temp);

return head;

int search(struct Node *head, int value) {

struct Node *current = head;

while (current != NULL) {

if (current->data == value) {

return 1;

current = current->next;

return 0;
}

struct Node *reverse(struct Node *head) {

struct Node *prev = NULL;

struct Node *current = head;

struct Node *next = NULL;

while (current != NULL) {

next = current->next;

current->next = prev;

prev = current;

current = next;

return prev;

int main() {

struct Node *head;

struct Node *second;

struct Node *third;

struct Node *fourth;

head = (struct Node *)malloc(sizeof(struct Node));

second = (struct Node *)malloc(sizeof(struct Node));

third = (struct Node *)malloc(sizeof(struct Node));

fourth = (struct Node *)malloc(sizeof(struct Node));

head->data = 7;

head->next = second;

second->data = 11;
second->next = third;

third->data = 41;

third->next = fourth;

fourth->data = 66;

fourth->next = NULL;

printf("Linked list before insertion and deletion:\n");

linkedListTraversal(head);

head = insertAtFirst(head, 56);

printf("\nLinked list after insertion:\n");

linkedListTraversal(head);

head = deleteFirst(head);

printf("\nLinked list after deletion:\n");

linkedListTraversal(head);

int searchValue = 41;

if (search(head, searchValue)) {

printf("\nValue %d found in the linked list.\n", searchValue);

} else {

printf("\nValue %d not found in the linked list.\n", searchValue);

head = reverse(head);

printf("\nLinked list after reversal:\n");

linkedListTraversal(head);

while (head != NULL) {


struct Node *temp = 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>

typedef struct Node {

int data;

struct Node* next;

struct Node* prev;

} Node;

Node* createNode(int data) {

Node* newNode = (Node*)malloc(sizeof(Node));

if(!newNode) {

printf("Memory error\n");

return NULL;

newNode -> data = data;


newNode -> next = NULL;

newNode -> prev = NULL;

return newNode;

void insertAtBeginning(Node** head, int data) {

Node* newNode = createNode(data);

if(!newNode) return;

if(*head != NULL) {

newNode -> next = *head;

(*head) -> prev = newNode;

*head = newNode;

printf("Inserted %d at the beginning\n", data);

void insertAtEnd(Node** head, int data) {

Node* newNode = createNode(data);

if(!newNode) return;

if(*head == NULL) {

*head = newNode;

printf("Inserted %d at the end\n", data);

return;

Node* temp = *head;

while (temp -> next != NULL) {

temp = temp -> next;

}
temp -> next = newNode;

newNode -> prev = temp;

printf("Inserted %d at the end\n", data);

void deleteNode(Node** head, int key) {

if(*head == NULL) {

printf("List is empty\n");

return;

Node* temp = *head;

if(temp != NULL && temp -> data == key) {

*head = temp -> next;

if(*head != NULL) {

(*head) -> prev = NULL;

free(temp);

printf("Deleted %d from the list\n", key);

return;

while (temp != NULL && temp -> data != key) {

temp = temp -> next;

if(temp == NULL) {

printf("Key %d not found in the list\n", key);

return;

}
if(temp -> next != NULL) {

temp -> next -> prev = temp -> prev;

if(temp -> prev != NULL) {

temp -> prev -> next = temp -> next;

free(temp);

printf("Deleted %d from the list\n", key);

void traverse(Node* head) {

if(head == NULL) {

printf("List is empty\n");

return;

Node* temp = head;

printf("List elements: ");

while(temp != NULL) {

printf("%d ", temp -> data);

temp = temp -> next;

printf("\n");

int search(Node* head, int key) {

Node* temp = head;

int position = 0;

while (temp != NULL) {

if(temp -> data == key) {


printf("Element %d found at position %d\n", key, position);

return position;

temp = temp -> next;

position++;

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

return -1;

void reverse(Node** head) {

Node* temp = NULL;

Node* current = *head;

while(current != NULL) {

temp = current -> prev;

current -> prev = current -> next;

current -> next = temp;

current = current -> prev;

if(temp != NULL) {

*head = temp -> prev;

printf("List reversed\n");

int main() {

Node* head = NULL;

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:

Implement binary search and interpolation search.

#include <stdio.h>

int binarySearch(int arr[], int size, int key) {

int low = 0, high = size - 1;

while (low <= high) {


int mid = low + (high - low) / 2;

if (arr[mid] == key) return mid;

else if (arr[mid] < key) low = mid + 1;

else high = mid - 1;

return -1;

int interpolationSearch(int arr[], int size, int key) {

int low = 0, high = size - 1;

while (low <= high && key >= arr[low] && key <= arr[high]) {

if (low == high) {

if (arr[low] == key) return low;

return -1;

int pos = low + (((double)(high - low) / (arr[high] - arr[low])) * (key - arr[low]));

if (arr[pos] == key) return pos;

if (arr[pos] < key) low = pos + 1;

else high = pos - 1;

return -1;

int main() {

int search, n, result, arr[5];

printf("Enter 5 elements for array(In sorted order): ");

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

printf("Enter elements to search: ");

scanf("%d", &search);

printf("Enter Choice: \n1. Binary Search.\n2. Interpolation Search: ");

scanf("%d", &n);
switch (n) {

case 1: result = binarySearch(arr, 5, search);

break;

case 2: result = interpolationSearch(arr, 5, search);

break;

default: printf("Invalid Option!");

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>

void bubbleSort(int arr[], int size_arr) {

for(int i = 0; i < size_arr - 1; i++) {

for(int j = 0; j < size_arr - i - 1; j++) {

if (arr[j] > arr[j+1]) {

int temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

}
void selectionSort(int arr[], int arr_size) {

for (int i = 0; i < arr_size - 1; i++) {

int minIndex = i;

for (int j = i + 1; j < arr_size; j++) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

if (minIndex != i) {

int temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

void insertionSort(int arr[], int n) {

for(int i = 1; i < n; i++) {

int temp = arr[i];

int j = i - 1;

while(j >= 0 && arr[j] > temp) {

arr[j + 1] = arr[j];

j--;

arr[j + 1] = temp;

int partition(int arr[], int lb, int ub) {

int pivot = arr[lb];


int start = lb;

int end = ub;

while (start < end) {

while (start < end && arr[start] <= pivot) {

start++;

while (arr[end] > pivot) {

end--;

if (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

int temp = arr[lb];

arr[lb] = arr[end];

arr[end] = temp;

return end;

void quickSort(int arr[], int lb, int ub) {

if (lb < ub) {

int pivot = partition(arr, lb, ub);

quickSort(arr, lb, pivot - 1);

quickSort(arr, pivot + 1, ub);

}
void merge(int arr[], int lb, int mid, int ub) {

int n1 = mid - lb + 1;

int n2 = ub - mid;

int L[n1], R[n2];

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

L[i] = arr[lb + i];

for (int j = 0; j < n2; j++) {

R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = lb;

while (i < n1 && j < n2) {

if (L[i] <= R[j]) {

arr[k] = L[i];

i++;

} else {

arr[k] = R[j];

j++;

k++;

while (i < n1) {

arr[k] = L[i];

i++;

k++;

}
while (j < n2) {

arr[k] = R[j];

j++;

k++;

void mergeSort(int arr[], int lb, int ub) {

if (lb < ub) {

int mid = lb + (ub - lb) / 2;

mergeSort(arr, lb, mid);

mergeSort(arr, mid + 1, ub);

merge(arr, lb, mid, ub);

int main() {

int arr[5];

printf("Enter elements to sort (5 elements): ");

for(int i = 0; i < 5; i++) {

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) {

case 1: bubbleSort(arr, size_arr);

break;
case 2: selectionSort(arr, size_arr);

break;

case 3: insertionSort(arr, 5);

break;

case 4: quickSort(arr, 0, 4);

break;

case 5: mergeSort(arr, 0, 4);

break;

default: printf("\nInvalid Option!");

printf("Sorted array: \n");

for(int i = 0; i < size_arr; i++) {

printf("%d ", arr[i]);

printf("\n");

return 0;

Experiment 10:

Implement Binary search Tree and its operations ( creation, insertion, deletion).

Experiment 11:

Implement Traversals Preorder Inorder Postorder on BST.

Experiment 10 & 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;

struct Node* left;

struct Node* right;

};

struct Node* createNode(int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->left = newNode->right = NULL;

return newNode;

struct Node* insert(struct Node* root, int data) {

if (root == NULL) {

return createNode(data);

if (data < root->data) {

root->left = insert(root->left, data);

} else {

root->right = insert(root->right, data);

return root;

}
struct Node* findMin(struct Node* root) {

while (root->left != NULL) {

root = root->left;

return root;

struct Node* deleteNode(struct Node* root, int data) {

if (root == NULL) {

return root;

if (data < root->data) {

root->left = deleteNode(root->left, data);

} else if (data > root->data) {

root->right = deleteNode(root->right, data);

} else {

if (root->left == NULL) {

struct Node* temp = root->right;

free(root);

return temp;

} else if (root->right == NULL) {

struct Node* temp = root->left;

free(root);

return temp;

struct Node* temp = findMin(root->right);

root->data = temp->data;

root->right = deleteNode(root->right, temp->data);

}
return root;

void inorderTraversal(struct Node* root) {

if (root != NULL) {

inorderTraversal(root->left);

printf("%d ", root->data);

inorderTraversal(root->right);

void preorderTraversal(struct Node* root) {

if (root != NULL) {

printf("%d ", root->data);

preorderTraversal(root->left);

preorderTraversal(root->right);

void postorderTraversal(struct Node* root) {

if (root != NULL) {

postorderTraversal(root->left);

postorderTraversal(root->right);

printf("%d ", root->data);

int main() {

struct Node* root = NULL;

root = insert(root, 50);


insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

insert(root, 60);

insert(root, 80);

printf("In-order traversal: ");

inorderTraversal(root);

printf("\n");

printf("Deleting 20\n");

root = deleteNode(root, 20);

printf("In-order traversal after deletion: ");

inorderTraversal(root);

printf("\n");

printf("Deleting 30\n");

root = deleteNode(root, 30);

printf("In-order traversal after deletion: ");

inorderTraversal(root);

printf("\n");

printf("Deleting 50\n");

root = deleteNode(root, 50);

printf("In-order traversal after deletion: ");

inorderTraversal(root);

printf("\n");

printf("Pre-order traversal: ");

preorderTraversal(root);
printf("\n");

printf("Post-order traversal: ");

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];

int front = 0, rear = 0;

void addEdge(int u, int v) {

adjMatrix[u][v] = 1;
adjMatrix[v][u] = 1;

void bfs(int startNode) {

bool visited[MAX_NODES] = {false};

queue[rear++] = startNode;

visited[startNode] = true;

printf("BFS Traversal: ");

while (front < rear) {

int currentNode = queue[front++];

printf("%d ", currentNode);

for (int i = 0; i < MAX_NODES; i++) {

if (adjMatrix[currentNode][i] == 1 && !visited[i]) {

queue[rear++] = i;

visited[i] = true;

printf("\n");

void dfs(int node, bool visited[]) {

printf("%d ", node);

visited[node] = true;

for (int i = 0; i < MAX_NODES; i++) {

if (adjMatrix[node][i] == 1 && !visited[i]) {

dfs(i, visited);

}
struct Node {

int vertex;

struct Node* next;

};

struct Node* adjList[MAX_NODES];

struct Node* createNode(int v) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->vertex = v;

newNode->next = NULL;

return newNode;

void addEdgeToList(int u, int v) {

struct Node* newNode = createNode(v);

newNode->next = adjList[u];

adjList[u] = newNode;

newNode = createNode(u);

newNode->next = adjList[v];

adjList[v] = newNode;

void printGraph() {

for (int i = 0; i < MAX_NODES; i++) {

struct Node* temp = adjList[i];

printf("Node %d:", i);

while (temp) {

printf(" -> %d", temp->vertex);


temp = temp->next;

printf("\n");

int main() {

for (int i = 0; i < MAX_NODES; i++) {

for (int j = 0; j < MAX_NODES; j++) {

adjMatrix[i][j] = 0;

adjList[i] = NULL;

addEdge(0, 1);

addEdge(0, 2);

addEdge(1, 3);

addEdge(2, 4);

addEdge(3, 4);

bfs(0);

bool visited[MAX_NODES] = {false};

printf("DFS Traversal: ");

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;

You might also like