DS-Lab Programs
DS-Lab Programs
APPLICATIONS
Practical Record
on
Data Structures
Submitted in the partial fulfilment for the award of
degree in
Submitted by
Nithin V
P03ZW24S126004
1
Certificate
This is to certify that Smt/Sri NITHIN V Register Number
P03ZW24S126004 Class MCA 1ST semester have successfully
completed Data Structures Lab Practical Record as prescribed
by the College for the Academic year 2024-2025
Date: Seal
2
Index
Sl. Program Name
No
1 Linear Search and Binary Search
2 Sorting Procedures (Selection, Bubble,
Insertion Sort)
3 Polynomial Addition using Arrays
4 Sparse Matrix Manipulation using
Arrays
5 Stack using Arrays
6 Queue using Arrays
7 Circular Queue using Arrays
8 Singly Linked List
9 Polynomial Addition using Linked Lists
10 Queue using Linked Lists
11 Binary Search Tree Traversal using
Recursion
12 Graph Representation using Arrays
13 Infix to Postfix Conversion
14 Evaluation of Postfix Expressions
15 Doubly Linked List
16 Circular Linked List
17 Graph using Linked List
18 2D Array allocation Dynamically
19 Demonstrating Realloc Function
20 Binary Search Tree Traversal Without
Recursion
3
1. Program to represent Linear Search and Binary
Search.
Program:
#include <stdio.h>
void linearSearch(int arr[], int n, int key)
{
int i;
for (i = 0; i < n; i++)
{
if (arr[i] == key)
{
printf("\nThe element found at %d:\n", i);
return;
}
}
printf("Element not found!!!\n");
}
void binarySearch(int arr[], int n, int key)
{
int start, end, mid;
start = 0, end = n - 1;
while (start <= end)
{
mid = (start + end) / 2;
if (arr[mid] == key)
{
printf("The Element is found at: %d\n", mid);
return;
}
else if (arr[mid] > key)
{
end = mid - 1;
} else
{
start = mid + 1;
}
}
4
printf("Element not found!!!");
}
int main()
{
int choice, n, key,i;
printf("Enter the number of elements in the array:");
scanf("%d", &n);
int arr[n];
printf("Enter the Elements of the Array
(in Sorted Manner):”);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to be searched:");
scanf("%d", &key);
printf("\nEnter 1 for Linear Search\n
Enter 2 for Binary Search\n");
scanf("%d", &choice);
switch (choice) {
case 1:
linearSearch(arr, n, key);
break;
case 2:
binarySearch(arr, n, key);
break;
default:
printf("Invalid Choice!!!\n");
}
return 0;
}
Output:
5
5
Enter the element to be searched:6
Program:
#include <stdio.h>
6
array[j + 1] = temp;
}
}
}
}
7
void printArray(int array[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int n, i;
int choice;
int array[n];
switch (choice) {
case 1:
bubbleSort(array, n);
printf("\nSorted array (Bubble Sort): \n");
printArray(array, n);
8
break;
case 2:
selectionSort(array, n);
printf("\nSorted array (Selection Sort): \n");
printArray(array, n);
break;
case 3:
insertionSort(array, n);
printf("\nSorted array (Insertion Sort): \n");
printArray(array, n);
break;
default:
printf("\nInvalid choice.\n");
}
return 0;
}
Output:
Unsorted array:
59301
9
3. Polynomial addition using arrays.
Program:
#include <stdio.h>
void addPolynomials(int poly1[], int poly2[], int result[], int n)
{
for (int i = 0; i < n; i++) {
result[i] = poly1[i] + poly2[i];
}
}
int main() {
int n = 3;
int poly1[] = {1, 2, 3};
int poly2[] = {3, 4, 5};
int result[n];
return 0;
}
Output:
10
Polynomial 1: 1x^0 + 2x^1 + 3x^2
Polynomial 2: 3x^0 + 4x^1 + 5x^2
Sum: 4x^0 + 6x^1 + 8x^2
Program:
#include <stdio.h>
int main()
{
int sparse_matrix[4][5] =
{
{0 , 0 , 6 , 0 , 9 },
{0 , 0 , 4 , 6 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 1 , 2 , 0 , 0 }
};
int size = 0;
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
if(sparse_matrix[i][j]!=0)
{
size++;
}
}
}
int matrix[3][size];
int k=0;
for(int i=0; i<4; i++)
{
for(int j=0; j<5; j++)
{
if(sparse_matrix[i][j]!=0)
11
{
matrix[0][k] = i;
matrix[1][k] = j;
matrix[2][k] = sparse_matrix[i][j];
k++;
}
}
}
Output:
0 0 1 1 3 3
2 4 2 3 1 2
6 9 4 6 1 2
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
int A[MAX_SIZE];
int top = -1;
void Push(int x)
{
if (top == MAX_SIZE - 1)
12
{
printf("Error: stack overflow\n");
return;
}
A[++top] = x;
}
void Pop()
{
if (top == -1)
{
printf("Error: No element to pop\n");
return;
}
top--;
}
void Print()
{
int i;
printf("Stack: ");
for (i = 0; i <= top; i++)
printf("%d ", A[i]);
printf("\n");
}
int IsEmpty()
{
if (top == -1)
return 1;
else
return 0;
}
int main()
{
Push(2);
Print();
Push(5);
Print();
Push(10);
Print();
Pop();
Print();
Push(12);
13
Print();
return 0;
}
Output:
Stack: 2
Stack: 2 5
Stack: 2 5 10
Stack: 2 5
Stack: 2 5 12
Program:
#include <stdio.h>
#define MAX_SIZE 10
int A[MAX_SIZE];
int front = -1;
int rear = -1;
void Enqueue(int x)
{
if (rear == MAX_SIZE - 1)
{
printf("Error: Queue is full\n");
return;
}
if (front == -1 && rear == -1)
{
front = rear = 0;
}
else
{
rear++;
}
A[rear] = x;
}
void Dequeue()
{
14
if (front == -1 && rear == -1)
{
printf("Error: Queue is empty\n");
return;
}
else if (front == rear)
{
front = rear = -1;
}
else
{
front++;
}
}
}
void Print()
{
int i;
printf("Queue: ");
for (i = front; i <= rear; i++)
printf("%d ", A[i]);
printf("\n");
}
int IsEmpty()
{
if (front == -1 && rear == -1)
return 1;
else
return 0;
}
int main()
{
Enqueue(2);
Print();
Enqueue(5);
Print();
Enqueue(10);
Print();
Dequeue();
Print();
Enqueue(12);
15
Print();
return 0;
}
Output:
Queue: 2
Queue: 2 5
Queue: 2 5 10
Queue: 5 10
Queue: 5 10 12
Program:
#include <stdio.h>
#define MAX_SIZE 5
int A[MAX_SIZE];
int front = -1;
int rear = -1;
void Enqueue(int x)
{
if ((rear + 1) % MAX_SIZE == front)
{
printf("Error: Queue is full\n");
return;
}
else if (front == -1 && rear == -1)
{
front = rear = 0;
}
else
{
rear = (rear + 1) % MAX_SIZE;
}
A[rear] = x;
}
void Dequeue()
16
{
if (front == -1 && rear == -1)
{
printf("Error: Queue is empty\n");
return;
}
else if (front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % MAX_SIZE;
}
}
int Front()
{
if (front == -1 && rear == -1)
{
printf("Error: Queue is empty\n");
return -1;
}
return A[front];
}
void Print()
{
int i = front;
if (front == -1 && rear == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue: ");
while (i != rear)
{
printf("%d ", A[i]);
i = (i + 1) % MAX_SIZE;
}
printf("%d\n", A[rear]);
}
int IsEmpty()
17
{
if (front == -1 && rear == -1)
return 1;
else
return 0;
}
int main()
{
Enqueue(2);
Print();
Enqueue(5);
Print();
Enqueue(10);
Print();
Dequeue();
Print();
Enqueue(12);
Print();
Enqueue(15);
Print();
return 0;
}
Output:
Queue: 2
Queue: 2 5
Queue: 2 5 10
Queue: 5 10
Queue: 5 10 12
Queue: 5 10 12 15
Program:
#include <stdio.h>
#include <stdlib.h>
struct node {
18
int INFO;
struct node *LINK;
};
void create() {
char ch;
int i = 0;
NODE *CPTR, *NEWNODE;
start = CPTR;
while (1) {
printf("\nEnter the node %d: ", i + 1);
scanf("%d", &CPTR->INFO);
void display() {
NODE *CPTR = start;
if (start == NULL) {
printf("\nLinked list is empty\n");
return;
}
int length() {
int len = 0;
NODE *CPTR = start;
if (start == NULL) {
printf("The linked list is empty\n");
return 0;
}
20
printf("\nThe item %d is found at position no: %d\n",
ITEM, i);
return;
}
CPTR = CPTR->LINK;
}
int main() {
int ITEM;
return 0;
}
}
Output:
21
Do you wish to add one more node (Y/N): y
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int coeff;
int pow;
struct Node* next;
};
22
addPolynomial(head1->next, head2);
head1->next = nextPtr;
return head1;
}
printf("\n");
}
23
int main() {
printList(head);
return 0;
}
Output:
5 X ^ 2 -1 X ^ 1 -3 X ^ 0
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete_q();
void display();
void main ()
{
int choice;
24
while(choice != 4)
{
printf("\n**Main Menu\n");
printf("\[Link] an element\[Link] an
element\[Link] the queue\[Link]\n");
printf("\nEnter your choice ?");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete_q();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void insert()
{
struct node *ptr;
int item;
Output:
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
Enter value?
14
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
Enter value?
20
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
27
Enter your choice ?1
Enter value?
23
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
14
20
23
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
Enter value?
33
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
28
Enter your choice ?3
14
20
23
33
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
**Main Menu
[Link] an element
[Link] an element
[Link] the queue
[Link]
20
23
33
**Main Menu
29
[Link] an element
[Link] an element
[Link] the queue
[Link]
struct BinaryTreeNode {
int key;
struct BinaryTreeNode *left, *right;
};
struct BinaryTreeNode*
searchNode(struct BinaryTreeNode* root, int target)
{
if (root == NULL || root->key == target) {
return root;
}
if (root->key < target) {
return searchNode(root->right, target);
}
30
return searchNode(root->left, target);
}
struct BinaryTreeNode*
insertNode(struct BinaryTreeNode* node, int value)
{
if (node == NULL) {
return newNodeCreate(value);
}
if (value < node->key) {
node->left = insertNode(node->left, value);
}
else if (value > node->key) {
node->right = insertNode(node->right, value);
}
return node;
}
if (x > root->key) {
root->right = delete (root->right, x);
}
else if (x < root->key) {
root->left = delete (root->left, x);
}
else {
if (root->left == NULL && root->right == NULL) {
free(root);
return NULL;
}
else if (root->left == NULL
|| root->right == NULL) {
struct BinaryTreeNode* temp;
if (root->left == NULL) {
temp = root->right;
}
else {
32
temp = root->left;
}
free(root);
return temp;
}
else {
struct BinaryTreeNode* temp
= findMin(root->right);
root->key = temp->key;
root->right = delete (root->right, temp->key);
}
}
return root;
}
int main()
{
struct BinaryTreeNode* root = NULL;
printf("\n");
postOrder(root);
printf("\n");
preOrder(root);
printf("\n");
33
inOrder(root);
printf("\n");
return 0;
}
Output:
60 found
20 40 30 60 80 70 50
50 30 20 40 70 60 80
20 30 40 50 60 70 80
After Delete:
20 30 40 50 60 80
Program:
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
#define TRUE 1
#define FALSE 0
34
void DFSUtil(int graph[MAX][MAX], int start,
int visited[MAX]);
int main()
{
int graph[MAX][MAX] = { 0 };
insertEdge(graph, 0, 1);
insertEdge(graph, 0, 2);
insertEdge(graph, 1, 2);
insertEdge(graph, 2, 0);
insertEdge(graph, 2, 3);
return 0;
}
return graph[u][v];
}
35
void BFS(int graph[MAX][MAX], int start)
{
int visited[MAX] = { 0 };
int queue[MAX], front = 0, rear = 0;
visited[start] = TRUE;
queue[rear++] = start;
visited[start] = TRUE;
printf("%d ", start);
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int prec(char c) {
if (c == '^')
return 3;
else if (c == '/' || c == '*')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return -1;
}
37
char stack[len];
int j = 0;
int top = -1;
if (isalnum(c))
result[j++] = c;
else if (c == '(')
stack[++top] = '(';
else if (c == ')') {
while (top != -1 && stack[top] != '(') {
result[j++] = stack[top--];
}
top--;
}
else {
while (top != -1 && (prec(c) < prec(stack[top]) ||
prec(c) == prec(stack[top]))) {
result[j++] = stack[top--];
}
stack[++top] = c;
}
}
result[j] = '\0';
printf("%s\n", result);
}
int main() {
char exp[] = "a+b*(c^d-e)^(f+g*h)-i";
infixToPostfix(exp);
return 0;
38
}
Output:
abcd^e-fgh*+^*+i-
Program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define MAX 20
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return s[top--];
}
int main() {
char postfix[MAX], ch;
int i, op1, op2, res, len;
39
printf("\nProgram to Evaluate Postfix Expression\n");
printf("Enter the postfix expression: ");
scanf("%s", postfix);
len = strlen(postfix);
if (isdigit(ch))
push(ch - '0');
else {
op2 = pop();
op1 = pop();
switch (ch) {
case '+': res = op1 + op2; break;
case '-': res = op1 - op2; break;
case '*': res = op1 * op2; break;
case '/': res = op1 / op2; break;
case '^': res = pow(op1, op2); break;
default: printf("Invalid Character\n");
return 1;
}
push(res);
}
}
Output:
40
15. Program to represent Doubly Linked List.
Program:
#include <stdio.h>
#include <stdlib.h>
// defining a node
typedef struct Node {
int data;
struct Node* next;
struct Node* prev;
} Node;
41
{
// creating new node
Node* newNode = createNode(data);
if (position == 1) {
insertAtBeginning(head, data);
return;
}
Node* newNode = createNode(data);
Node* temp = *head;
for (int i = 1; temp != NULL && i < position - 1; i++) {
temp = temp->next;
}
if (temp == NULL) {
printf(
"Position greater than the number of nodes.\n");
return;
}
newNode->next = temp->next;
newNode->prev = temp;
42
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
}
int main()
{
Node* head = NULL;
insertAtEnd(&head, 10);
insertAtEnd(&head, 20);
insertAtBeginning(&head, 5);
insertAtPosition(&head, 15, 2); // List: 5 15 10 20
printf("After Insertions:\n");
printListForward(head);
printListReverse(head);
deleteAtBeginning(&head); // List: 15 10 20
deleteAtEnd(&head); // List: 15 10
deleteAtPosition(&head, 2); // List: 15
printf("After Deletions:\n");
printListForward(head);
return 0;
}
45
Output:
After Insertions:
Forward List: 5 15 10 20
Reverse List: 20 10 15 5
After Deletions:
Forward List: 15
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
46
struct Node *newNode = createNode(data);
if (pos == 1) {
newNode->next = curr;
last->next = newNode;
return last;
}
if (curr == last->next) {
printf("Invalid position!\n");
return last;
}
}
newNode->next = curr->next;
curr->next = newNode;
return last;
}
int main() {
return 0;
}
Output:
Original list: 2 3 4
List after insertions: 2 5 3 4
48
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int vertex;
struct Node* next;
};
struct Graph {
int numVertices;
struct Node** adjLists;
};
return graph;
}
49
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
int main() {
int vertices = 5;
struct Graph* graph = createGraph(vertices);
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);
printGraph(graph);
return 0;
}
Output:
50
Adjacency list of vertex 2: 3 -> 1 -> NULL
Adjacency list of vertex 3: 4 -> 2 -> 1 -> NULL
Adjacency list of vertex 4: 3 -> 1 -> 0 -> NULL
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows, cols;
if (array == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
51
}
}
printf("Array elements:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
return 0;
}
Output:
2
3
4
5
6
7
8
1
Array elements:
123
456
781
52
=== Code Execution Successful ===
19. Program to demonstrate the use of realloc function.
Program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr = (int *)malloc(sizeof(int)*2);
int i;
int *ptr_new;
*ptr = 10;
*(ptr + 1) = 20;
getchar();
return 0;
}
Output:
10 20 30
Program:
#include <stdio.h>
#include <stdlib.h>
current = stack[top--];
printf("%d ", current->data);
current = current->right;
}
}
54
if (current->right) stack[++top] = current->right;
if (current->left) stack[++top] = current->left;
}
}
stack1[++top1] = root;
int main() {
struct Node* root = createNode(10);
root->left = createNode(5);
root->right = createNode(20);
root->left->left = createNode(3);
root->left->right = createNode(7);
root->right->left = createNode(15);
root->right->right = createNode(25);
return 0;
}
Output:
Inorder Traversal : 3 5 7 10 15 20 25
Preorder Traversal : 10 5 3 7 20 15 25
Postorder Traversal : 3 7 5 15 25 20 10
56