Data Structure Practical Programs in C++ (Turbo C++)
1. Square Matrix and Diagonal Elements
Write a program to create a square matrix, fill the data inside and print the diagonal
elements.
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int mat[10][10], n, i, j;
cout << "Enter the size of square matrix (max 10): ";
cin >> n;
cout << "Enter elements of " << n << "x" << n << " matrix:\n";
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> mat[i][j];
}
}
cout << "\nPrimary Diagonal Elements: ";
for (i = 0; i < n; i++) {
cout << mat[i][i] << " ";
}
cout << "\nSecondary Diagonal Elements: ";
for (i = 0; i < n; i++) {
cout << mat[i][n - i - 1] << " ";
}
getch();
}
Sample Output:
Sample Input:
Enter the size of square matrix (max 10): 3
Element [0][0]: 1
Element [0][1]: 2
Element [0][2]: 3
Element [1][0]: 4
Element [1][1]: 5
Element [1][2]: 6
Element [2][0]: 7
Element [2][1]: 8
Element [2][2]: 9
Output:
Primary Diagonal Elements: 1 5 9
Secondary Diagonal Elements: 3 5 7
2. Matrix Addition and Subtraction
Write a program to perform addition and subtraction on two matrices.
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int mat1[10][10], mat2[10][10], sum[10][10], diff[10][10];
int i, j, n, m;
cout << "Enter number of rows and columns (max 10): ";
cin >> n >> m;
cout << "Enter elements of first matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
cout << "mat1[" << i << "][" << j << "]: ";
cin >> mat1[i][j];
}
cout << "Enter elements of second matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
cout << "mat2[" << i << "][" << j << "]: ";
cin >> mat2[i][j];
}
cout << "\nMatrix Addition:\n";
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
cout << sum[i][j] << " ";
}
cout << "\n";
}
cout << "\nMatrix Subtraction:\n";
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
diff[i][j] = mat1[i][j] - mat2[i][j];
cout << diff[i][j] << " ";
}
cout << "\n";
}
getch();
}
Sample Output:
Input:
Matrix 1:
1 2
3 4
Matrix 2:
5 6
7 8
Output:
Addition:
6 8
10 12
Subtraction:
-4 -4
-4 -4
3. Matrix Multiplication
Write a program to perform multiplication on two matrices.
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int mat1[10][10], mat2[10][10], mul[10][10];
int i, j, k, r1, c1, r2, c2;
cout << "Enter rows and columns of first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns of second matrix: ";
cin >> r2 >> c2;
if (c1 != r2) {
cout << "Matrix multiplication not possible!";
getch();
return;
}
cout << "Enter elements of first matrix:\n";
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++) {
cout << "mat1[" << i << "][" << j << "]: ";
cin >> mat1[i][j];
}
cout << "Enter elements of second matrix:\n";
for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++) {
cout << "mat2[" << i << "][" << j << "]: ";
cin >> mat2[i][j];
}
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
mul[i][j] = 0;
for (k = 0; k < c1; k++) {
mul[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
cout << "\nMatrix Multiplication Result:\n";
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
cout << mul[i][j] << " ";
}
cout << "\n";
}
getch();
}
Sample Output:
Input:
Matrix 1:
1 2
3 4
Matrix 2:
5 6
7 8
Output:
19 22
43 50
4. Singly Linked List - Insertion and Deletion at End
Write a program to perform insertion and deletion of nodes from the end in singly linked
list.
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct Node {
int data;
Node *next;
};
Node *head = NULL;
void insertEnd(int val) {
Node *temp = new Node;
temp->data = val;
temp->next = NULL;
if (head == NULL) {
head = temp;
} else {
Node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
void deleteEnd() {
if (head == NULL) {
cout << "List is empty\n";
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
Node *ptr = head;
while (ptr->next->next != NULL)
ptr = ptr->next;
delete ptr->next;
ptr->next = NULL;
}
void display() {
Node *ptr = head;
while (ptr != NULL) {
cout << ptr->data << " -> ";
ptr = ptr->next;
}
cout << "NULL\n";
}
void main() {
clrscr();
insertEnd(10);
insertEnd(20);
insertEnd(30);
cout << "List after insertion: ";
display();
deleteEnd();
cout << "List after deletion: ";
display();
getch();
}
Sample Output:
Output:
List after insertion: 10 -> 20 -> 30 -> NULL
List after deletion: 10 -> 20 -> NULL
5. Doubly Linked List - Insertion and Deletion at End
Write a program to perform insertion and deletion of nodes from the end in doubly linked
list.
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct Node {
int data;
Node *prev, *next;
};
Node *head = NULL;
void insertEnd(int val) {
Node *temp = new Node;
temp->data = val;
temp->prev = temp->next = NULL;
if (head == NULL) {
head = temp;
} else {
Node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
temp->prev = ptr;
}
}
void deleteEnd() {
if (head == NULL) {
cout << "List is empty\n";
return;
}
if (head->next == NULL) {
delete head;
head = NULL;
return;
}
Node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->prev->next = NULL;
delete ptr;
}
void display() {
Node *ptr = head;
while (ptr != NULL) {
cout << ptr->data << " <-> ";
ptr = ptr->next;
}
cout << "NULL\n";
}
void main() {
clrscr();
insertEnd(5);
insertEnd(15);
insertEnd(25);
cout << "List after insertion: ";
display();
deleteEnd();
cout << "List after deletion: ";
display();
getch();
}
Sample Output:
Output:
List after insertion: 5 <-> 15 <-> 25 <-> NULL
List after deletion: 5 <-> 15 <-> NULL