0% found this document useful (0 votes)
3 views61 pages

DFS File

The document is a practical file from DAV Institute of Management for BCA students, detailing various programming tasks related to data structures. It includes programs for array operations, linked lists, matrix operations, and sorting algorithms, along with their implementations in C. Each program is accompanied by an index, code snippets, and expected outputs.

Uploaded by

nirajkumar0bkt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views61 pages

DFS File

The document is a practical file from DAV Institute of Management for BCA students, detailing various programming tasks related to data structures. It includes programs for array operations, linked lists, matrix operations, and sorting algorithms, along with their implementations in C. Each program is accompanied by an index, code snippets, and expected outputs.

Uploaded by

nirajkumar0bkt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DAV INSTITUTE OF MANAGEMENT

NIT 3, Faridabad

Practical File

Data File Structure Practical


File
Submitted To: Submitted By:
Ms. Deepika Kamboj Priyanshu Sharma
[Assistant Professor] Course and semester
BCA DEPARTMENT BCA & 2ND Semester
Addmission No:-
DAVIM/2025/04725
INDEX
TABLE
TABLEOF
OFCONTENT
CONTENT
S NO. NAME OF PROGRAM PAGE REMARKS
NO.
1 Write a program for insertion and deletion 1 to 3
operation in an array.
2 Write a program to search for an element in an 4 to 5
array using linear Search and Binary Search.
3 Write a program to sort an array using Bubble 6 to 8
Sort, Selection Sort and Insertion Sort.
4 Write a program to merge two array. 9 to 10
5 Write a program to add and subtract two 11 to 12
matrices.
6 Write a program to multiply two matrices. 13 to 14
7 write a program to insert an element into a 15 to 18
singly linked list: (a) At a beginning (b) At the
end (c) At a specific position.
8 write a program to deletion an element into a 19 to 21
singly linked list: (a) At a beginning (b) At the
end (c) At a specific position.
9 Write a program to perform the following 22 to 24
operation in a Doubly Linked List:
(a)Create (b)Search for a element.
10 Write a program to perform the following 25 to 27
operation in a Circular Linked List:
(a)Create (b)Deletion an element from the end.
11 Write a program to implement stack operation 28 to 31
using an array.
12 Write a program to implement stack operation 32 to 34
using a linked list.
13 Write a program to add two polynomials using 35 to 38
a linked list.
14 Write a program to evaluate a postfix 39 to 41
expression using a stack.
15 write a program to perform the following using 42 to 44
recursion.( a )find the factorial of a number (b)
find the GCD of two number (C) solve Tower of
Hanoi problems.
16 Write a program to implement simple queue 45 to 47
operation using a array.
17 Write a program to implement Circular queue 48 to 50
operation using a array.
18 Write a program to implement Circular queue 51 to 53
operation using a linked list.
19 Write a program to perform the following 54 to 56
operations on a binary search tree.
(a)Preorder Traversal (b) Inorder Traversal (c)
Postorder Traversal 47.
20 Write a program to perform insertion 57 to 59
operation in a binary search.
Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 1:
Write a program for insertion and deletion operation in an array.
#include <stdio.h>

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

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

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

printf("\n");

int main() {

int arr[100], n, i, pos, val, choice;

printf("Enter number of elements in array: ");

scanf("%d", &n);

printf("Enter %d elements:\n", n);

for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("\n1. Insertion\n2. Deletion\nEnter choice: ");

scanf("%d", &choice);

if (choice == 1) {

printf("Enter position to insert (1 to %d): ", n + 1);

scanf("%d", &pos);

printf("Enter value to insert: ");

scanf("%d", &val);

if (pos < 1 || pos > n + 1) {

printf("Invalid position!\n");

} else {

1|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

for (i = n; i >= pos; i--) {

arr[i] = arr[i - 1];

arr[pos - 1] = val;

n++;

printf("Array after insertion: ");

display(arr, n);

} else if (choice == 2) {

printf("Enter position to delete (1 to %d): ", n);

scanf("%d", &pos);

if (pos < 1 || pos > n) {

printf("Invalid position!\n");

} else {

for (i = pos - 1; i < n - 1; i++) {

arr[i] = arr[i + 1];

n--;

printf("Array after deletion: ");

display(arr, n);

} else {

printf("Invalid choice!\n");

return 0;

2|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

OUTPUT:

3|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 2:
Write a program to search for an element in an array using linear Search and
Binary Search.
#include <stdio.h>

int main() {

int arr[100], n, i, key, low, high, mid, found = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter sorted elements:\n");

for(i = 0; i < n; i++) {

scanf("%d", &arr[i]);

printf("Enter element to search: ");

scanf("%d", &key);

// Linear Search

for(i = 0; i < n; i++) {

if(arr[i] == key) {

printf("Linear Search: Found at position %d\n", i + 1);

found = 1;

break;

if(!found)

printf("Linear Search: Not found\n");

// Binary Search

low = 0;

high = n - 1;

found = 0;

4|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

while(low <= high) {

mid = (low + high) / 2;

if(arr[mid] == key) {

printf("Binary Search: Found at position %d\n", mid + 1);

found = 1;

break;

else if(arr[mid] < key)

low = mid + 1;

else

high = mid - 1;

if(!found)

printf("Binary Search: Not found\n");

return 0;

Output:

5|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 3:
Write a program to sort an array using Bubble Sort, Selection Sort and
Insertion Sort.
#include <stdio.h>

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

int i, j, temp;

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

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

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

temp = arr[j];

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

arr[j+1] = temp;

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

int i, j, min, temp;

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

min = i;

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

if(arr[j] < arr[min])

min = j;

temp = arr[i];

arr[i] = arr[min];

arr[min] = temp;

6|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

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

int i, j, key;

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

key = arr[i];

j = i - 1;

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

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

j--;

arr[j+1] = key;

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

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

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

int main() {

int arr[100], n, choice;

printf("Enter number of elements: ");

scanf("%d", &n);

printf("Enter elements:\n");

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

scanf("%d", &arr[i]);

printf("[Link] [Link] [Link]\nEnter choice: ");

scanf("%d", &choice);

if(choice == 1)

bubbleSort(arr, n);

7|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

else if(choice == 2)

selectionSort(arr, n);

else

insertionSort(arr, n);

printf("Sorted array:\n");

printArray(arr, n);

return 0;

Output:

8|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 4:
Write a program to merge two array.
#include <stdio.h>

int main() {

int a[50], b[50], c[100];

int n1, n2, i;

printf("Enter size of first array: ");

scanf("%d", &n1);

printf("Enter elements:\n");

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

scanf("%d", &a[i]);

printf("Enter size of second array: ");

scanf("%d", &n2);

printf("Enter elements:\n");

for(i = 0; i < n2; i++)

scanf("%d", &b[i]);

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

c[i] = a[i];

for(i = 0; i < n2; i++)

c[n1 + i] = b[i];

printf("Merged array:\n");

for(i = 0; i < n1 + n2; i++)

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

return 0;

9|Page Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Output:

10 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 5:
Write a program to add and subtract two matrices.
#include <stdio.h>

int main() {

int a[10][10], b[10][10], sum[10][10], sub[10][10];

int r, c, i, j;

printf("Enter rows and columns: ");

scanf("%d%d", &r, &c);

printf("Enter first matrix:\n");

for(i = 0; i < r; i++)

for(j = 0; j < c; j++)

scanf("%d", &a[i][j]);

printf("Enter second matrix:\n");

for(i = 0; i < r; i++)

for(j = 0; j < c; j++)

scanf("%d", &b[i][j]);

for(i = 0; i < r; i++) {

for(j = 0; j < c; j++) {

sum[i][j] = a[i][j] + b[i][j];

sub[i][j] = a[i][j] - b[i][j];

printf("\nAddition:\n");

for(i = 0; i < r; i++) {

for(j = 0; j < c; j++)

printf("%d ", sum[i][j]);

printf("\n");

11 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

printf("\nSubtraction:\n");

for(i = 0; i < r; i++) {

for(j = 0; j < c; j++)

printf("%d ", sub[i][j]);

printf("\n");

return 0;

Output:

12 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 6:
Write a program to multiply two matrices.
#include <stdio.h>

int main() {

int a[10][10], b[10][10], result[10][10];

int r1, c1, r2, c2, i, j, k;

printf("Enter rows and cols of first matrix: ");

scanf("%d%d", &r1, &c1);

printf("Enter rows and cols of second matrix: ");

scanf("%d%d", &r2, &c2);

if(c1 != r2) {

printf("Multiplication not possible");

return 0;

printf("Enter first matrix:\n");

for(i = 0; i < r1; i++)

for(j = 0; j < c1; j++)

scanf("%d", &a[i][j]);

printf("Enter second matrix:\n");

for(i = 0; i < r2; i++)

for(j = 0; j < c2; j++)

scanf("%d", &b[i][j]);

for(i = 0; i < r1; i++) {

for(j = 0; j < c2; j++) {

result[i][j] = 0;

for(k = 0; k < c1; k++) {

result[i][j] += a[i][k] * b[k][j];

13 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

printf("Result matrix:\n");

for(i = 0; i < r1; i++) {

for(j = 0; j < c2; j++)

printf("%d ", result[i][j]);

printf("\n");

return 0;

}
output:

14 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 7:
write a program to insert an element into a singly linked list: (a) At a
beginning (b) At the end (c) At a specific position.
#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

};

struct node *head = NULL;

// Insert at beginning

void insert_begin(int value) {

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

newNode->data = value;

newNode->next = head;

head = newNode;

// Insert at end

void insert_end(int value) {

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

newNode->data = value;

newNode->next = NULL;

if(head == NULL) {

head = newNode;

return;

struct node *temp = head;

while(temp->next != NULL)

15 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

temp = temp->next;

temp->next = newNode;

// Insert at specific position

void insert_position(int value, int pos) {

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

newNode->data = value;

if(pos == 1) {

newNode->next = head;

head = newNode;

return;

struct node *temp = head;

for(int i = 1; i < pos - 1 && temp != NULL; i++)

temp = temp->next;

if(temp == NULL) {

printf("Invalid position\n");

return;

newNode->next = temp->next;

temp->next = newNode;

// Display list

void display() {

struct node *temp = head;

if(head == NULL) {

printf("List is empty\n");

return;

16 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

while(temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

// Main function

int main() {

int choice, value, pos;

while(1) {

printf("\[Link] Beginning\[Link] End\[Link] Position\[Link]\[Link]\n");

printf("Enter choice: ");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

insert_begin(value);

break;

case 2:

printf("Enter value: ");

scanf("%d", &value);

insert_end(value);

break;

case 3:

printf("Enter value and position: ");

scanf("%d%d", &value, &pos);

17 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

insert_position(value, pos);

break;

case 4:

display();

break;

case 5:

exit(0);

default:

printf("Invalid choice\n");

return 0;

Output:

18 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 8:
write a program to deletion an element into a singly linked list: (a) At a
beginning (b) At the end (c) At a specific position.
#include <stdio.h>

#define MAX 5

int stack[MAX], top = -1;

void push(int value) {

if(top == MAX - 1) {

printf("Stack Overflow\n");

return;

stack[++top] = value;

void pop() {

if(top == -1) {

printf("Stack Underflow\n");

return;

printf("Popped: %d\n", stack[top--]);

void display() {

if(top == -1) {

printf("Stack is empty\n");

return;

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

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

19 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

int main() {

int choice, value;

while(1) {

printf("\[Link] [Link] [Link] [Link]\n");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter value: ");

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice");

20 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Output:

21 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 9:
Write a program to perform the following operation in a Doubly Linked List:
(a)Create (b)Search for a element.
#include <stdio.h>

#include <stdlib.h>

// 1. Structure definition for a Doubly Linked List Node

struct Node {

int data;

struct Node* prev;

struct Node* next;

};

// (a) Create operation: Adds a node to the end of the list

void create(struct Node** head, int data) {

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

newNode->data = data;

newNode->next = NULL;

if (*head == NULL) {

newNode->prev = NULL;

*head = newNode;

return;

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

22 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

// (b) Search operation: Finds the position of an element

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

struct Node* temp = head;

int position = 1;

while (temp != NULL) {

if (temp->data == key) return position;

temp = temp->next;

position++;

return -1;

int main() {

struct Node* head = NULL;

int n, value, key;

printf("How many nodes? ");

scanf("%d", &n);

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

printf("Enter value: ");

scanf("%d", &value);

create(&head, value);

printf("Enter search value: ");

scanf("%d", &key);

int result = search(head, key);

if (result != -1) printf("Found at position %d.\n", result);

else printf("Not found.\n");

return 0;

23 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Output:

Program 10:
Write a program to perform the following operation in a Circular Linked List:
(a)Create (b)Deletion an element from the end.
24 | P a g e Submitted by Rajkumar Kaushik
Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

// (a) Create / Insert at End

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

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

newNode->data = value;

if (head == NULL) {

newNode->next = newNode; // Points to itself

return newNode;

struct Node* temp = head;

while (temp->next != head) {

temp = temp->next;

temp->next = newNode;

newNode->next = head;

return head;

// (b) Deletion from the end

struct Node* deleteEnd(struct Node* head) {

if (head == NULL) {

printf("List is empty.\n");

return NULL;

25 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

struct Node *curr = head, *prev = NULL;

// Case: Only one node in the list

if (curr->next == head) {

free(curr);

return NULL;

// Traverse to find the last and second-to-last nodes

while (curr->next != head) {

prev = curr;

curr = curr->next;

// Update second-to-last node to point to head and free last node

prev->next = head;

free(curr);

return head;

void display(struct Node* head) {

if (head == NULL) {

printf("List is empty.\n");

return;

struct Node* temp = head;

do {

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

temp = temp->next;

} while (temp != head);

printf("(head)\n");

26 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

int main() {

struct Node* head = NULL;

// (a) Create

head = create(head, 10);

head = create(head, 20);

head = create(head, 30);

printf("Original List: ");

display(head);

// (b) Delete from end

head = deleteEnd(head);

printf("After deleting from end: ");

display(head);

return 0;

Output:

27 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 11:
Write a program to implement stack operation using an array.
#include <stdio.h>

#include <stdlib.h>

#define MAX 5 // Define maximum size of stack

int stack[MAX];

int top = -1;

// Function to add an element (Push)

void push(int val) {

if (top == MAX - 1) {

printf("Stack Overflow! Cannot push %d\n", val);

} else {

top++;

stack[top] = val;

printf("%d pushed into stack\n", val);

// Function to remove an element (Pop)

void pop() {

if (top == -1) {

printf("Stack Underflow! No elements to pop\n");

} else {

printf("Popped element: %d\n", stack[top]);

top--;

// Function to see the top element (Peek)

void peek() {

28 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

if (top == -1) {

printf("Stack is empty\n");

} else {

printf("Top element is: %d\n", stack[top]);

// Function to display all elements

void display() {

if (top == -1) {

printf("Stack is empty\n");

} else {

printf("Stack elements: ");

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

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

printf("\n");

int main() {

int choice, value;

while (1) {

printf("\n--- Stack Operations ---");

printf("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit");

printf("\nEnter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to push: ");

29 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

scanf("%d", &value);

push(value);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

display();

break;

case 5:

exit(0);

default:

printf("Invalid choice! Please try again.\n");

return 0;

30 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Output:

31 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 12:
Write a program to implement stack operation using a linked list.
#include <stdio.h>

#include <stdlib.h>

// Define the Node structure

struct Node {

int data;

struct Node* next;

};

// Push: Adds a new element to the top of the stack

void push(struct Node** top, int value) {

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

if (!newNode) {

printf("Heap Overflow\n"); // Memory allocation failure

return;

newNode->data = value;

newNode->next = *top; // Link new node to current top

*top = newNode; // Update top pointer

printf("%d pushed to stack\n", value);

// Pop: Removes and returns the top element

int pop(struct Node** top) {

if (*top == NULL) {

printf("Stack Underflow\n"); // Cannot pop from empty stack

return -1;

struct Node* temp = *top;

32 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

int poppedValue = temp->data;

*top = (*top)->next; // Move top to the next node

free(temp); // Release memory

return poppedValue;

// Peek: Returns the top element without removing it

int peek(struct Node* top) {

if (top == NULL) {

printf("Stack is empty\n");

return -1;

return top->data; // Returns head->data

int main() {

struct Node* top = NULL; // Initialize empty stack

push(&top, 10);

push(&top, 20);

printf("Popped: %d\n", pop(&top));

return 0;

33 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Output:

34 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 13:
Write a program to add two polynomials using a linked list.
#include <stdio.h>

#include <stdlib.h>

// Structure for a polynomial term

struct Node {

int coeff;

int exp;

struct Node* next;

};

// Function to create a new node

struct Node* createNode(int c, int e) {

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

newNode->coeff = c;

newNode->exp = e;

newNode->next = NULL;

return newNode;

// Function to insert a term at the end of the polynomial

void insertTerm(struct Node** head, int c, int e) {

struct Node* newNode = createNode(c, e);

if (*head == NULL) {

*head = newNode;

return;

struct Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

35 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

temp->next = newNode;

// Function to add two polynomials

struct Node* addPolynomials(struct Node* p1, struct Node* p2) {

struct Node* result = NULL;

while (p1 != NULL && p2 != NULL) {

if (p1->exp > p2->exp) {

insertTerm(&result, p1->coeff, p1->exp);

p1 = p1->next;

} else if (p1->exp < p2->exp) {

insertTerm(&result, p2->coeff, p2->exp);

p2 = p2->next;

} else {

int sumCoeff = p1->coeff + p2->coeff;

if (sumCoeff != 0) {

insertTerm(&result, sumCoeff, p1->exp);

p1 = p1->next;

p2 = p2->next;

// Append remaining terms from p1 or p2

while (p1 != NULL) {

insertTerm(&result, p1->coeff, p1->exp);

p1 = p1->next;

while (p2 != NULL) {

36 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

insertTerm(&result, p2->coeff, p2->exp);

p2 = p2->next;

return result;

// Function to display the polynomial

void display(struct Node* head) {

if (head == NULL) {

printf("0\n");

return;

while (head != NULL) {

printf("%dx^%d", head->coeff, head->exp);

head = head->next;

if (head != NULL && head->coeff >= 0) printf(" + ");

printf("\n");

int main() {

struct Node *poly1 = NULL, *poly2 = NULL;

// Example 1: 5x^2 + 4x^1 + 2x^0

insertTerm(&poly1, 5, 2);

insertTerm(&poly1, 4, 1);

insertTerm(&poly1, 2, 0);

// Example 2: 5x^1 + 5x^0

insertTerm(&poly2, 5, 1);

insertTerm(&poly2, 5, 0);

printf("Polynomial 1: ");

37 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

display(poly1);

printf("Polynomial 2: ");

display(poly2);

struct Node* result = addPolynomials(poly1, poly2);

printf("Sum: ");

display(result);

return 0;

Output:

38 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 14:
Write a program to evaluate a postfix expression using a stack.
#include <stdio.h>

#include <ctype.h> // For isdigit()

#include <stdlib.h> // For exit()

#define MAX 100

int stack[MAX];

int top = -1;

// Function to push an element onto the stack

void push(int val) {

if (top >= MAX - 1) {

printf("Stack Overflow\n");

exit(1);

stack[++top] = val;

// Function to pop an element from the stack

int pop() {

if (top < 0) {

printf("Stack Underflow\n");

exit(1);

return stack[top--];

// Function to evaluate the postfix expression

int evaluatePostfix(char* exp) {

for (int i = 0; exp[i] != '\0'; i++) {

// If character is an operand, push it to stack

39 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

if (isdigit(exp[i])) {

push(exp[i] - '0'); // Convert char to int

// If character is an operator, pop two elements and apply operator

else {

int val1 = pop();

int val2 = pop();

switch (exp[i]) {

case '+': push(val2 + val1); break;

case '-': push(val2 - val1); break;

case '*': push(val2 * val1); break;

case '/': push(val2 / val1); break;

return pop();

int main() {

char exp[] = "231*+9-"; // Example expression

printf("Postfix expression: %s\n", exp);

printf("Evaluation result: %d\n", evaluatePostfix(exp));

return 0;

40 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Output:

41 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 15:
write a program to perform the following using recursion.( a )find the
factorial of a number (b) find the GCD of two number (C) solve Tower of
Hanoi problems.
#include <stdio.h>

// (a) Function to find Factorial using recursion

long long factorial(int n) {

if (n == 0 || n == 1) // Base case

return 1;

return n * factorial(n - 1); // Recursive case

// (b) Function to find GCD using recursion (Euclidean Algorithm)

int gcd(int a, int b) {

if (b == 0) // Base case

return a;

return gcd(b, a % b); // Recursive case

// (c) Function to solve Tower of Hanoi using recursion

void towerOfHanoi(int n, char from_rod, char aux_rod, char to_rod) {

if (n == 1) { // Base case

printf("Move disk 1 from %c to %c\n", from_rod, to_rod);

return;

// Step 1: Move n-1 disks from Source to Auxiliary

towerOfHanoi(n - 1, from_rod, to_rod, aux_rod);

// Step 2: Move the nth disk from Source to Target

printf("Move disk %d from %c to %c\n", n, from_rod, to_rod);

// Step 3: Move n-1 disks from Auxiliary to Target

42 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

towerOfHanoi(n - 1, aux_rod, from_rod, to_rod);

int main() {

int choice, n, a, b;

printf("Choose an operation:\n1. Factorial\n2. GCD\n3. Tower of Hanoi\nEnter choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter a number: ");

scanf("%d", &n);

if (n < 0) printf("Factorial not defined for negative numbers.\n");

else printf("Factorial of %d is %lld\n", n, factorial(n));

break;

case 2:

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

printf("GCD of %d and %d is %d\n", a, b, gcd(a, b));

break;

case 3:

printf("Enter number of disks: ");

scanf("%d", &n);

printf("Steps to solve Tower of Hanoi with %d disks:\n", n);

towerOfHanoi(n, 'A', 'B', 'C'); // A: Source, B: Auxiliary, C: Target

break;

default:

printf("Invalid choice!\n");

43 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

return 0;

Output:

44 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 16:
Write a program to implement simple queue operation using a array.
#include <stdio.h>

#include <stdlib.h>

#define MAX 5 // Maximum size of the queue

int queue[MAX];

int front = -1;

int rear = -1;

// Function to add an element (Enqueue)

void enqueue() {

int item;

if (rear == MAX - 1) {

printf("Queue Overflow\n");

} else {

if (front == -1) front = 0; // Initialize front if first element

printf("Enter element: ");

scanf("%d", &item);

rear++;

queue[rear] = item;

// Function to remove an element (Dequeue)

void dequeue() {

if (front == -1 || front > rear) {

printf("Queue Underflow\n");

} else {

printf("Deleted: %d\n", queue[front]);

front++;

45 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

// Reset pointers if queue becomes empty

if (front > rear) {

front = rear = -1;

// Function to display the queue

void display() {

if (front == -1) {

printf("Queue is empty\n");

} else {

printf("Queue elements: ");

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

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

printf("\n");

int main() {

int choice;

while (1) {

printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\nChoice: ");

scanf("%d", &choice);

switch (choice) {

case 1: enqueue(); break;

case 2: dequeue(); break;

case 3: display(); break;

case 4: exit(0);

46 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

default: printf("Invalid choice!\n");

return 0;

Output:

47 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 17:
Write a program to implement Circular queue operation using a array.
#include <stdio.h>

#include <limits.h> // For INT_MIN

#define MAX_SIZE 5

int queue[MAX_SIZE];

int front = -1, rear = -1;

int isFull() {

return (rear + 1) % MAX_SIZE == front;

int isEmpty() {

return front == -1;

void enqueue(int data) {

if (isFull()) {

printf("Queue Overflow: Cannot insert %d\n", data);

return;

if (front == -1) front = 0; // First element

rear = (rear + 1) % MAX_SIZE;

queue[rear] = data;

int dequeue() {

if (isEmpty()) {

printf("Queue Underflow: Nothing to delete\n");

return INT_MIN;

int data = queue[front];

48 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

if (front == rear) {

front = rear = -1; // Reset queue

} else {

front = (front + 1) % MAX_SIZE;

return data;

int peek() {

if (isEmpty()) {

printf("Queue is empty\n");

return INT_MIN;

return queue[front];

void display() {

if (isEmpty()) {

printf("Queue is empty\n");

return;

int i = front;

while (i != rear) {

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

i = (i + 1) % MAX_SIZE;

printf("%d\n", queue[rear]);

int main() {

49 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

enqueue(10);

enqueue(20);

enqueue(30);

printf("Queue elements : "); display();

printf("Front element (peek): %d\n", peek());

printf("Dequeued element: %d\n", dequeue());

printf("Queue elements : "); display();

return 0;

Output:

50 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 18:
Write a program to implement Circular queue operation using a linked list.
#include <stdio.h>

#define MAX 5

int queue[MAX], front = -1, rear = -1;

// Enqueue

void enqueue(int value) {

if((rear + 1) % MAX == front) {

printf("Queue Overflow\n");

return;

if(front == -1)

front = 0;

rear = (rear + 1) % MAX;

queue[rear] = value;

// Dequeue

void dequeue() {

if(front == -1) {

printf("Queue Underflow\n");

return;

printf("Deleted: %d\n", queue[front]);

if(front == rear)

front = rear = -1;

else

front = (front + 1) % MAX;

51 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

// Display

void display() {

if(front == -1) {

printf("Queue is empty\n");

return;

int i = front;

while(1) {

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

if(i == rear)

break;

i = (i + 1) % MAX;

// Main

int main() {

int choice, val;

while(1) {

printf("\[Link] [Link] [Link] [Link]\n");

scanf("%d", &choice);

switch(choice) {

case 1:

printf("Enter value: ");

scanf("%d", &val);

enqueue(val);

break;

case 2:

dequeue();

52 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

break;

case 3:

display();

break;

case 4:

return 0;

default:

printf("Invalid choice");

Output:

53 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 19:
Write a program to perform the following operations on a binary search tree.
(a)Preorder Traversal (b) Inorder Traversal (c) Postorder Traversal 47.
#include <stdio.h>

#include <stdlib.h>

// Define the structure for a BST node

struct Node {

int data;

struct Node *left, *right;

};

// Create a new BST node

struct Node* newNode(int data) {

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

node->data = data;

node->left = node->right = NULL;

return node;

// Insert a node into the BST

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

if (node == NULL) return newNode(data);

if (data < node->data) node->left = insert(node->left, data);

else if (data > node->data) node->right = insert(node->right, data);

return node;

// (a) Preorder Traversal (Root, Left, Right)

void preorder(struct Node* root) {

if (root == NULL) return;

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

54 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

preorder(root->left);

preorder(root->right);

// (b) Inorder Traversal (Left, Root, Right)

void inorder(struct Node* root) {

if (root == NULL) return;

inorder(root->left);

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

inorder(root->right);

// (c) Postorder Traversal (Left, Right, Root)

void postorder(struct Node* root) {

if (root == NULL) return;

postorder(root->left);

postorder(root->right);

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

int main() {

struct Node* root = NULL;

root = insert(root, 47);

insert(root, 30); insert(root, 70);

insert(root, 20); insert(root, 40);

printf("Preorder: "); preorder(root);

printf("\nInorder: "); inorder(root);

printf("\nPostorder: "); postorder(root);

return 0;

55 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Output:

56 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

Program 20:
Write a program to perform insertion operation in a binary search.
#include <stdio.h>

#include <stdlib.h>

// Node structure definition

struct node {

int data;

struct node *left, *right;

};

// Function to create a new BST node

struct node* createNode(int value) {

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

newNode->data = value;

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

return newNode;

// Function to insert a new key in BST

struct node* insert(struct node* node, int key) {

if (node == NULL) return createNode(key);

if (key < node->data)

node->left = insert(node->left, key);

else if (key > node->data)

node->right = insert(node->right, key);

return node;

// Inorder traversal to display BST

void inorder(struct node* root) {

if (root != NULL) {

57 | P a g e Submitted by Rajkumar Kaushik


Data File Structure Practical File Submitted to: Ms. Deepika Kamboj

inorder(root->left);

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

inorder(root->right);

int main() {

struct node* root = NULL;

root = insert(root, 50);

insert(root, 30);

insert(root, 20);

insert(root, 40);

insert(root, 70);

printf("Inorder traversal: ");

inorder(root);

return 0;

Output:

58 | P a g e Submitted by Rajkumar Kaushik

You might also like