DSA Practical File
DSA Practical File
a) Stack ADT
b) Queue ADT
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
98
24
12
Press Next Choice
Enter the Choice:2
24
12
Press Next Choice
Enter the Choice:4
EXIT POINT
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
Output:
Enter no 1:10
Enter no 2:54
Enter no 3:98
Enter no 4:234
Deleted Element is 10
Enter the Choice:3
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
if (top1 == NULL)
{
printf("Stack is empty");
return;
}
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}
void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
void destroy()
{
top1 = top;
Output:
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Display
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
907856
Enter choice : 7
Stack is empty
Enter choice : 5
b) Queue ADT
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;
int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
Output:
1 - Enque
2 - Deque
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size
Enter choice : 1
Enter data : 14
Enter choice : 1
Enter data : 85
Enter choice : 1
Enter data : 38
Enter choice : 3
Front element : 14
Enter choice : 6
148538
Enter choice : 7
Queue size : 3
Enter choice : 2
Dequed value : 14
Enter choice : 6
8538
Enter choice : 7
Queue size : 2
Enter choice : 4
Queue not empty
Enter choice : 5
Ans.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *prev, *next;
};
struct node *head = NULL, *tail = NULL;
struct node * createNode(int data)
{
struct node *newnode = (struct node *)malloc(sizeof (struct node));
newnode->data = data;
newnode->next = newnode->prev = NULL;
return (newnode);
}
void createSentinels()
{
head = createNode(0);
tail = createNode(0);
head->next = tail;
tail->prev = head;
}
void enqueueAtFront(int data)
{
struct node *newnode, *temp;
newnode = createNode(data);
temp = head->next;
head->next = newnode;
newnode->prev = head;
newnode->next = temp;
temp->prev = newnode;
}
if (head->next == tail)
{
printf("Queue is empty\n");
return;
}
temp = head->next;
while (temp != tail)
{
printf("%-3d", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
int data, ch;
createSentinels();
while (1)
#include<stdio.h>
#include<stdlib.h>
struct node {
int key;
struct node *left, *right;
};
struct node *newNode(int item) {
struct node *temp = (struct node *) malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
struct node* insert(struct node* node, int key)
{
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
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);
inorder(root);
return 0;
}
Output:
20 30 40 50 60 70 80
#include<stdio.h>
#include<stdlib.h>
struct node {
int key;
struct node *left, *right;
};
struct node *newNode(int item) {
struct node *temp = (struct node *) malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
struct node* insert(struct node* node, int key) {
if (node == NULL)
return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
struct node * minValueNode(struct node* node) {
struct node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node* deleteNode(struct node* root, int key) {
if (root == NULL)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
Output:
#include <stdio.h>
#include <stdlib.h>
struct TreeNode {
int data;
struct TreeNode *leftChildNode;
struct TreeNode *rightChildNode;
};
typedef struct TreeNode node;
node *rootNode = NULL;
void insertNode(int i, node **n) {
if (*n == NULL) {
(*n) = (node*)malloc(sizeof(node));
(*n)->leftChildNode = NULL;
(*n)->rightChildNode = NULL;
(*n)->data = i;
}
else if ((*n)->data == i)
printf("\nThis value already exists in the tree!");
else if (i> (*n)->data)
insertNode(i, &((*n)->rightChildNode));
else
insertNode(i, &((*n)->leftChildNode));
}
void searchNode(int i, node **n) {
if (*n == NULL)
printf("\nValue does not exist in tree!");
else if((*n)->data == i)
printf("\nValue found!");
else if(i> (*n)->data)
searchNode(i, &((*n)->rightChildNode));
else
searchNode(i, &((*n)->leftChildNode));
}
int main()
{
int ch, num, num1;
do {
printf("\nSelect a choice from the menu below.");
printf("\n1. Insert a node.");
printf("\n2. Search for a node.");
printf("\nChoice: ");
scanf("%d", &ch);
switch(ch) {
case 1:
printf("\nEnter an element: ");
scanf("%d", &num);
insertNode(num, &rootNode);
break;
case 2:
Output:
Ans.
#include<stdio.h>
#define capacity 6
int queue[capacity];
int front = -1, rear = -1;
int checkFull ()
{
if ((front == rear + 1) || (front == 0 && rear == capacity - 1))
{
return 1;
}
return 0;
}
int checkEmpty ()
{
if (front == -1)
{
return 1;
}
return 0;
}
void enqueue (int value)
{
if (checkFull ())
printf ("Overflow condition\n");
else
{
if (front == -1)
front = 0;
rear = (rear + 1) % capacity;
queue[rear] = value;
printf ("%d was enqueued to circular queue\n", value);
}
}
int dequeue ()
{
int variable;
if (checkEmpty ())
{
printf ("Underflow condition\n");
return -1;
}
else
{
variable = queue[front];
if (front == rear)
{
front = rear = -1;
Output:
Underflow condition
15 was enqueued to circular queue
20 was enqueued to circular queue
25 was enqueued to circular queue
30 was enqueued to circular queue
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
typedef struct Node {
char key[50];
char value[100];
struct Node* next;
} Node;
typedef struct Dictionary {
Node* table[SIZE];
} Dictionary;
unsigned int hashFunction(char* key) {
unsigned int hash = 0;
for (int i = 0; key[i] != '\0'; i++) {
hash = hash * 31 + key[i];
}
return hash % SIZE;
}
Dictionary* createDictionary() {
Dictionary* dict = (Dictionary*)malloc(sizeof(Dictionary));
for (int i = 0; i< SIZE; i++) {
dict->table[i] = NULL;
}
return dict;
}
void insert(Dictionary* dict, char* key, char* value) {
unsigned int index = hashFunction(key);
Node* newNode = (Node*)malloc(sizeof(Node));
strcpy(newNode->key, key);
strcpy(newNode->value, value);
newNode->next = dict->table[index];
dict->table[index] = newNode;
}
Node* search(Dictionary* dict, char* key) {
unsigned int index = hashFunction(key);
Node* temp = dict->table[index];
while (temp != NULL) {
if (strcmp(temp->key, key) == 0) {
return temp;
}
temp = temp->next;
}
return NULL;
}
void deleteNode(Dictionary* dict, char* key) {
unsigned int index = hashFunction(key);
Node* current = dict->table[index];
Ans.
a) Insertion
B Tree Insertion
#include <stdio.h>
#include <stdlib.h>
#define MAX_KEYS 3
typedef struct BTreeNode {
int keys[MAX_KEYS];
struct BTreeNode* children[MAX_KEYS + 1];
int num_keys;
} BTreeNode;
BTreeNode* createNode() {
BTreeNode* newNode = (BTreeNode*)malloc(sizeof(BTreeNode));
newNode->num_keys = 0;
for (int i = 0; i<= MAX_KEYS; ++i) {
newNode->children[i] = NULL;
}
return newNode;
}
int main() {
BTreeNode* root = NULL;
int keys[] = {3, 7, 2, 1, 9, 5, 6};
int i, n = sizeof(keys) / sizeof(keys[0]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
return node;
}
AVLNode* leftRotate(AVLNode* y) {
AVLNode* x = y->right;
AVLNode* T2 = x->left;
x->left = y;
y->right = T2;
return x;
AVLNode* rightRotate(AVLNode* x) {
AVLNode* y = x->left;
AVLNode* T2 = y->right;
y->right = x;
x->left = T2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
int main() {
AVLNode* root = NULL;
int keys[] = {3, 7, 2, 1, 9, 5, 6};
int i, n = sizeof(keys) / sizeof(keys[0]);
Output:
b) Deletion
B Tree Deletion
int main() {
AVLNode* root = NULL;
int keys[] = {3, 7, 2, 1, 9, 5, 6};
int i, n = sizeof(keys) / sizeof(keys[0]);
int key_to_delete = 5;
printf("\nDeleting key %d from AVL Tree:\n", key_to_delete);
root = deleteAVL(root, key_to_delete);
printf("Preorder traversal of AVL Tree after deletion:\n");
preOrder(root);
printf("\n");
return 0;
}
Output:
Ans.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_VERTICES 100
typedef struct {
int vertices[MAX_VERTICES];
int front, rear;
} Queue;
typedef struct {
int vertices[MAX_VERTICES];
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} Graph;
Queue* createQueue() {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->front = -1;
queue->rear = -1;
return queue;
}
void enqueue(Queue* queue, int vertex) {
if (queue->rear == MAX_VERTICES - 1) {
printf("Queue is full\n");
} else {
if (queue->front == -1) {
queue->front = 0;
}
queue->rear++;
queue->vertices[queue->rear] = vertex;
}
}
int dequeue(Queue* queue) {
if (queue->front == -1) {
printf("Queue is empty\n");
return -1;
} else {
int vertex = queue->vertices[queue->front];
queue->front++;
if (queue->front > queue->rear) {
queue->front = queue->rear = -1;
}
return vertex;
}
}
bool isEmpty(Queue* queue) {
return queue->front == -1;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
int vertices[MAX_VERTICES];
int top;
} Stack;
typedef struct {
int vertices[MAX_VERTICES];
int adjacencyMatrix[MAX_VERTICES][MAX_VERTICES];
int numVertices;
} Graph;
Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = -1;
return stack;
}
void push(Stack* stack, int vertex) {
stack->top++;
stack->vertices[stack->top] = vertex;
}
int pop(Stack* stack) {
if (stack->top == -1) {
return -1;
} else {
int vertex = stack->vertices[stack->top];
stack->top--;
return vertex;
}
}
bool isEmpty(Stack* stack) {
return stack->top == -1;
}
Graph* createGraph(int numVertices) {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->numVertices = numVertices;
for (int i = 0; i<numVertices; i++) {
for (int j = 0; j <numVertices; j++) {
graph->adjacencyMatrix[i][j] = 0;
}
}
return graph;
}
void addEdge(Graph* graph, int src, int dest) {
int startVertex = 0;
DFS(graph, startVertex);
return 0;
}
Output:
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
key[0] = 0;
parent[0] = -1;
printMST(parent, graph);
}
int main() {
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};
return 0;
}
Output:
b) Kruskal’s algorithm
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int src, dest, weight;
} Edge;
typedef struct {
Edge* edges[MAX_EDGES];
int numEdges;
} Graph;
Graph* createGraph() {
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->numEdges = 0;
return graph;
}
int parent[V];
for (i = 0; i< V; i++)
parent[i] = -1;
i = 0;
while (e < V - 1) {
Edge nextEdge = *(graph->edges[i++]);
if (x != y) {
resultMST[e++] = nextEdge;
unionSets(parent, x, y);
}
}
int main() {
Graph* graph = createGraph();
addEdge(graph, 0, 1, 2);
addEdge(graph, 0, 3, 6);
addEdge(graph, 1, 2, 3);
addEdge(graph, 1, 4, 5);
addEdge(graph, 2, 4, 7);
kruskalMST(graph);
return 0;
}
Output:
Ans.
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
int main() {
int graph[V][V] = {{0, 4, 0, 0, 0},
{4, 0, 8, 0, 0},
{0, 8, 0, 7, 0},
{0, 0, 7, 0, 9},
{0, 0, 0, 9, 0}};
return 0;
}
Output:
Ans.a) Pre-order
#include <stdio.h>
#include <stdlib.h>
typedef struct {
StackNode* top;
} Stack;
Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
return stack;
}
if (node->right != NULL) {
push(stack, node->right);
}
if (node->left != NULL) {
push(stack, node->left);
}
}
}
int main() {
// Constructing the binary tree
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
printf("Pre-order traversal of the binary tree: ");
preOrderTraversal(root);
printf("\n");
// Clean up the tree nodes if needed
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
StackNode* top;
} Stack;
Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
return stack;
}
current = pop(stack);
printf("%d ", current->data);
current = current->right;
}
}
int main() {
// Constructing the binary tree
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return 0;
}
Output:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
StackNode* top;
} Stack;
Stack* createStack() {
Stack* stack = (Stack*)malloc(sizeof(Stack));
stack->top = NULL;
return stack;
}
push(stack1, root);
Node* node;
if (node->left != NULL) {
push(stack1, node->left);
}
if (node->right != NULL) {
push(stack1, node->right);
}
}
int main() {
// Constructing the binary tree
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
return 0;
}
Output:
#include <stdio.h>
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
Output:
Original array: 64 34 25 12 22 11 90
Sorted array in ascending order: 11 12 22 25 34 64 90
b) Merge sort
#include <stdio.h>
#include <stdlib.h>
int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
return 0;
}
Output:
Original array: 38 27 43 3 9 82 10
Sorted array in ascending order: 3 9 10 27 38 43 82