NBCA-206P — Data Structure Lab Practical File
NBCA-206P
DATA STRUCTURE LAB
LTP:0 0 3
PRACTICAL FILE
Name _______________________________
Roll No. _______________________________
Semester / Section _______________________________
Submitted To _______________________________
Page 1 of 24
NBCA-206P — Data Structure Lab Practical File
INDEX
[Link]. Name of the Program Date Page No. Signature
1 To implement addition and multiplication of two 2D arrays.
2 To transpose a 2D array.
3 To implement stack using array.
4 To implement stack using linked list.
5 To implement queue using array.
6 To implement queue using linked list.
7 To implement circular queue using array.
8 To implement circular queue using linked list.
Page 2 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 1
Aim: To implement addition and multiplication of two 2D arrays.
Program:
#include <stdio.h>
int main() {
int r1, c1, r2, c2;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
int a[10][10];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < r1; i++)
for (int j = 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
int b[10][10];
printf("Enter elements of second matrix:\n");
for (int i = 0; i < r2; i++)
for (int j = 0; j < c2; j++)
scanf("%d", &b[i][j]);
/* Addition - requires same dimensions */
if (r1 == r2 && c1 == c2) {
int sum[10][10];
printf("\nSum of the two matrices:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
} else {
printf("\nAddition not possible: dimension mismatch.\n");
}
/* Multiplication - requires columns of first = rows of second */
if (c1 == r2) {
int mul[10][10];
printf("\nProduct of the two matrices:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
mul[i][j] = 0;
for (int k = 0; k < c1; k++)
Page 3 of 24
NBCA-206P — Data Structure Lab Practical File
mul[i][j] += a[i][k] * b[k][j];
printf("%d ", mul[i][j]);
}
printf("\n");
}
} else {
printf("\nMultiplication not possible: column of first != row of
second.\n");
}
return 0;
}
Sample Output:
Enter rows and columns of first matrix: 2 2
Enter elements of first matrix:
1 2
3 4
Enter rows and columns of second matrix: 2 2
Enter elements of second matrix:
5 6
7 8
Sum of the two matrices:
6 8
10 12
Product of the two matrices:
19 22
43 50
Result: The C program to implement Addition and Multiplication of two 2D arrays was executed
successfully and the output was verified.
Page 4 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 2
Aim: To transpose a 2D array.
Program:
#include <stdio.h>
int main() {
int r, c;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
int a[10][10], t[10][10];
printf("Enter elements of matrix:\n");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &a[i][j]);
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
t[j][i] = a[i][j];
printf("\nOriginal Matrix:\n");
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++)
printf("%d ", a[i][j]);
printf("\n");
}
printf("\nTranspose Matrix:\n");
for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++)
printf("%d ", t[i][j]);
printf("\n");
}
return 0;
}
Sample Output:
Enter rows and columns of matrix: 2 3
Enter elements of matrix:
1 2 3
4 5 6
Original Matrix:
1 2 3
4 5 6
Transpose Matrix:
Page 5 of 24
NBCA-206P — Data Structure Lab Practical File
1 4
2 5
3 6
Result: The C program to implement Transpose of a 2D array was executed successfully and the
output was verified.
Page 6 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 3
Aim: To implement stack using array.
Program:
#include <stdio.h>
#define MAX 5
int stack[MAX], top = -1;
void push(int val) {
if (top == MAX - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = val;
printf("%d pushed to stack\n", val);
}
void pop() {
if (top == -1) {
printf("Stack Underflow\n");
return;
}
printf("%d popped from stack\n", stack[top--]);
}
void display() {
if (top == -1) {
printf("Stack is empty\n");
return;
}
printf("Stack elements: ");
for (int i = top; i >= 0; i--)
printf("%d ", stack[i]);
printf("\n");
}
int main() {
int choice, val;
do {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &val);
push(val);
Page 7 of 24
NBCA-206P — Data Structure Lab Practical File
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
Sample Output:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 10
10 pushed to stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 20
20 pushed to stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 30
30 pushed to stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements: 30 20 10
Page 8 of 24
NBCA-206P — Data Structure Lab Practical File
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
30 popped from stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements: 20 10
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4
Exiting...
Result: The C program to implement Stack using Array was executed successfully and the output was
verified.
Page 9 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 4
Aim: To implement stack using linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *top = NULL;
void push(int val) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = top;
top = newNode;
printf("%d pushed to stack\n", val);
}
void pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return;
}
struct Node *temp = top;
printf("%d popped from stack\n", temp->data);
top = top->next;
free(temp);
}
void display() {
if (top == NULL) {
printf("Stack is empty\n");
return;
}
struct Node *temp = top;
printf("Stack elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, val;
Page 10 of 24
NBCA-206P — Data Structure Lab Practical File
do {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to push: ");
scanf("%d", &val);
push(val);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
Sample Output:
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 10
10 pushed to stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Enter value to push: 20
20 pushed to stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 1
Page 11 of 24
NBCA-206P — Data Structure Lab Practical File
Enter value to push: 30
30 pushed to stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements: 30 20 10
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 2
30 popped from stack
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 3
Stack elements: 20 10
1. Push
2. Pop
3. Display
4. Exit
Enter your choice: 4
Exiting...
Result: The C program to implement Stack using Linked List was executed successfully and the
output was verified.
Page 12 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 5
Aim: To implement queue using array.
Program:
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
void enqueue(int val) {
if (rear == MAX - 1) {
printf("Queue Overflow\n");
return;
}
if (front == -1)
front = 0;
queue[++rear] = val;
printf("%d enqueued to queue\n", val);
}
void dequeue() {
if (front == -1 || front > rear) {
printf("Queue Underflow\n");
return;
}
printf("%d dequeued from queue\n", queue[front++]);
}
void display() {
if (front == -1 || front > rear) {
printf("Queue is empty\n");
return;
}
printf("Queue elements: ");
for (int i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
int main() {
int choice, val;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
Page 13 of 24
NBCA-206P — Data Structure Lab Practical File
scanf("%d", &val);
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
Sample Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 10
10 enqueued to queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 20
20 enqueued to queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 30
30 enqueued to queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Page 14 of 24
NBCA-206P — Data Structure Lab Practical File
Queue elements: 10 20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
10 dequeued from queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements: 20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...
Result: The C program to implement Queue using Array was executed successfully and the output
was verified.
Page 15 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 6
Aim: To implement queue using linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *front = NULL, *rear = NULL;
void enqueue(int val) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
printf("%d enqueued to queue\n", val);
}
void dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return;
}
struct Node *temp = front;
printf("%d dequeued from queue\n", temp->data);
front = front->next;
if (front == NULL)
rear = NULL;
free(temp);
}
void display() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
struct Node *temp = front;
printf("Queue elements: ");
while (temp != NULL) {
printf("%d ", temp->data);
Page 16 of 24
NBCA-206P — Data Structure Lab Practical File
temp = temp->next;
}
printf("\n");
}
int main() {
int choice, val;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &val);
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
Sample Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 10
10 enqueued to queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Page 17 of 24
NBCA-206P — Data Structure Lab Practical File
Enter value to enqueue: 20
20 enqueued to queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 30
30 enqueued to queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements: 10 20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
10 dequeued from queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements: 20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...
Result: The C program to implement Queue using Linked List was executed successfully and the
output was verified.
Page 18 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 7
Aim: To implement circular queue using array.
Program:
#include <stdio.h>
#define MAX 5
int cqueue[MAX], front = -1, rear = -1;
void enqueue(int val) {
if ((rear + 1) % MAX == front) {
printf("Queue Overflow\n");
return;
}
if (front == -1)
front = 0;
rear = (rear + 1) % MAX;
cqueue[rear] = val;
printf("%d enqueued to circular queue\n", val);
}
void dequeue() {
if (front == -1) {
printf("Queue Underflow\n");
return;
}
printf("%d dequeued from circular queue\n", cqueue[front]);
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
}
void display() {
if (front == -1) {
printf("Queue is empty\n");
return;
}
printf("Circular Queue elements: ");
int i = front;
while (1) {
printf("%d ", cqueue[i]);
if (i == rear)
break;
i = (i + 1) % MAX;
}
printf("\n");
}
Page 19 of 24
NBCA-206P — Data Structure Lab Practical File
int main() {
int choice, val;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &val);
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
Sample Output:
(Queue size MAX = 5, demonstrating wrap-around)
Enter choice: 1 Enter value: 10 -> 10 enqueued
Enter choice: 1 Enter value: 20 -> 20 enqueued
Enter choice: 1 Enter value: 30 -> 30 enqueued
Enter choice: 1 Enter value: 40 -> 40 enqueued
Enter choice: 1 Enter value: 50 -> 50 enqueued
Enter choice: 2 -> 10 dequeued from circular queue
Enter choice: 2 -> 20 dequeued from circular queue
Enter choice: 1 Enter value: 60 -> 60 enqueued (wraps to index 0)
Enter choice: 1 Enter value: 70 -> 70 enqueued (wraps to index 1)
Enter choice: 3
Circular Queue elements: 30 40 50 60 70
Enter choice: 4
Exiting...
Result: The C program to implement Circular Queue using Array was executed successfully and the
output was verified.
Page 20 of 24
NBCA-206P — Data Structure Lab Practical File
Page 21 of 24
NBCA-206P — Data Structure Lab Practical File
Experiment No. 8
Aim: To implement circular queue using linked list.
Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *last = NULL; /* last points to rear node; last->next is the
front */
void enqueue(int val) {
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = val;
if (last == NULL) {
newNode->next = newNode;
last = newNode;
} else {
newNode->next = last->next;
last->next = newNode;
last = newNode;
}
printf("%d enqueued to circular queue\n", val);
}
void dequeue() {
if (last == NULL) {
printf("Queue Underflow\n");
return;
}
struct Node *head = last->next;
printf("%d dequeued from circular queue\n", head->data);
if (head == last) {
last = NULL;
} else {
last->next = head->next;
}
free(head);
}
void display() {
if (last == NULL) {
printf("Queue is empty\n");
return;
Page 22 of 24
NBCA-206P — Data Structure Lab Practical File
}
struct Node *temp = last->next;
printf("Circular Queue elements: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != last->next);
printf("\n");
}
int main() {
int choice, val;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &val);
enqueue(val);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
Sample Output:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 10
10 enqueued to circular queue
Page 23 of 24
NBCA-206P — Data Structure Lab Practical File
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 20
20 enqueued to circular queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter value to enqueue: 30
30 enqueued to circular queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
10 dequeued from circular queue
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Circular Queue elements: 20 30
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...
Result: The C program to implement Circular Queue using Linked List was executed successfully
and the output was verified.
Page 24 of 24