Sparse and Triangular Matrix Conversion
Sparse and Triangular Matrix Conversion
Write a program to convert the Sparse Matrix into non-zero form and
vice versa.
1. Start
2. Prompt user to enter the number of rows and columns.
3. Create a 2D matrix and read values from the user.
4. Display the original matrix.
5. For each element in the matrix:
If the element is not zero:
Print the row index, column index, and the value.
6. End
1. Start
2. Prompt user to enter the number of rows and columns.
3. Create a 2D matrix and initialize all elements to zero.
4. Ask user to input the number of non-zero elements.
5. For each non-zero element:
Read row index, column index, and value.
Place the value at the correct position in the matrix.
6. Display the reconstructed matrix.
7. End
CODE:
#include<iostream>
Page | 1
using namespace std;
class SparseMatrix {
public:
int matrix[10][10];
void getSparseMatrix() {
cout << "Enter the elements of the sparse matrix:" << endl;
void printSparseMatrix()
Page | 2
cout << matrix[i][j] << " ";
void toNonZeroForm()
if (matrix[i][j] != 0)
cout << i << "\t" << j << "\t" << matrix[i][j] << endl;
Page | 3
for (int i = 0; i < row; i++)
matrix[i][j] = 0;
matrix[nonZero[i][0]][nonZero[i][1]] = nonZero[i][2];
printSparseMatrix();
};
int main() {
SparseMatrix sm;
int choice;
cout << "Enter your choice (1 for sparse to non-zero, 2 for non-zero to
sparse): ";
if (choice == 1)
Page | 4
{
[Link]();
[Link]();
[Link]();
else if (choice == 2)
int nonZero[10][3];
int size;
cout << "Enter the non-zero elements (row, column, value):" <<
endl;
[Link](nonZero, size);
} else {
Page | 5
cout << "Invalid choice." << endl;
return 0;
OUTPUT:
1 0 0
0 0 2
0 3 0
Sparse matrix:
1 0 0
0 0 2
0 3 0
Non-zero form:
0 0 1
1 2 2
2 1 3
Page | 6
Enter your choice (1 for sparse to non-zero, 2 for non-zero to
sparse): 2
0 0 1
1 2 2
2 1 3
Sparse matrix:
1 0 0
0 0 2
0 3 0
BRIEF DISCUSSION:
A sparse matrix typically contains a high number of zero values. To save space and
improve processing time, we can convert the matrix to a triplet format, which only
stores the row index, column index, and non-zero value. This is useful in memory-
constrained systems or in applications like graphics and scientific computations.
Given the triplet data, the original matrix can be reconstructed by creating a matrix of
appropriate size and inserting the non-zero values at the specified positions. This is
crucial when we want to perform matrix operations using the actual 2D structure after
compact storage.
Page | 7
2. Write a program to implement Lower Triangular Matrix using one-
dimensional array.
ALGORITHM :
1. Start
5. inputMatrix():
6. displayMatrix():
7. End.
Code:
#include<iostream>
class LowerTriangularMatrix
Page | 8
{
private:
int n;
int matrix[10];
public:
LowerTriangularMatrix(int size)
n = size;
matrix[i] = 0;
void inputMatrix()
cout << "Enter the elements of the lower triangular matrix:" << endl;
cout << "Enter element [" << i << "][" << j << "]: ";
void displayMatrix()
Page | 9
for (int i = 0; i < n; i++)
if (i >= j)
} else
};
int main() {
int n;
cin >> n;
LowerTriangularMatrix matrix(n);
[Link]();
[Link]();
return 0;
Page | 10
OUTPUT:
8 0 0 0
6 2 0 0
4 7 5 0
3 9 5 6
BRIEF DISCUSSION:
This C++ program works with lower triangular matrices — matrices where all elements
above the main diagonal are zero.
Instead of using a full 2D array, it saves space by storing only the needed elements in a
1D array.
This method saves memory and is useful when working with large matrices that have a
lot of zero values.
Page | 11
3. Write a program to implement Upper Triangular Matrix using one-
dimensional array .
ALGORITHM :
Store n.
Initialize the 1D array of size n(n+1)/2 elements with 0.
7. End
CODE:
#include <iostream>
class UpperTriangularMatrix
private:
int n;
Page | 12
int matrix[10];
public:
UpperTriangularMatrix(int size)
n = size;
matrix[i] = 0;
void inputMatrix() {
cout << "Enter the elements of the upper triangular matrix:" << endl;
cout << "Enter element [" << i << "][" << j << "]: ";
void displayMatrix() {
if (i <= j)
Page | 13
{
} else {
};
int main() {
int n;
cin >> n;
UpperTriangularMatrix matrix(n);
[Link]();
[Link]();
return 0;
OUTPUT:
Page | 14
Enter element [1][1]: 9
5 8 7
0 9 6
0 0 2
BRIEF DISCUSSION:
This program is used to store and display an upper triangular matrix more efficiently
using a 1D array instead of a full 2D array.
In an upper triangular matrix, all the elements below the diagonal are 0.
To save space, the program only stores the non-zero elements (those on or above
the diagonal).
It uses a formula to convert 2D positions to 1D indexes, so it knows where to
store or find each element.
When displaying the matrix, it prints the stored numbers in the right positions,
and prints 0 for the elements below the diagonal.
This method reduces memory usage and works well for square matrices where many
elements are zero.
Page | 15
4. Write a program to implement Symmetric Matrix using one-dimensional
array .
ALGORITHM :
1. Start
2. Input the size n of the matrix.
3. Calculate the required size of the 1D array:
Size=n(n+1)2
index=j(j+1)2+i
6. For output:
Loop through all rows i and columns j
If i ≤ j:
Use index = j(j+1)/2 + i
Else:
Use index = i(i+1)/2 + j
Display the value.
7. End
CODE :
#include <iostream>
class SymmetricMatrix
Page | 16
{
private:
int n;
int matrix[10];
public:
SymmetricMatrix(int size)
n = size;
matrix[i] = 0;
void inputMatrix()
cout << "Enter the elements of the symmetric matrix:" << endl;
cout << "Enter element [" << i << "][" << j << "]: ";
Page | 17
}
void displayMatrix()
if (i <= j)
} else {
};
int main() {
Page | 18
int n;
cin >> n;
SymmetricMatrix matrix(n);
[Link]();
[Link]();
return 0;
OUTPUT:
Page | 19
Symmetric Matrix:
5 9 5 2
9 4 3 6
5 3 8 7
2 6 7 9
BRIEF DISCUSSION :
This program demonstrates how to efficiently store and display a symmetric matrix
using a one-dimensional array instead of a two-dimensional one. It’s a smart way to
save memory and understand how symmetric matrices work in programming.
Page | 20
5. Write a program to represent a polynomial using array. Also, add two
polynomials represented using array.
ALGORITHM :
[Link]
Declare:
Return r.
Page | 21
Step 6: In main()
Step 7: End
CODE :
#include<iostream>
class poly
private:
int a[20],n;
public:
poly r;
if(n>obj.n)
for(int i=0;i<=obj.n;i++)
r.a[i]=a[i]+obj.a[i];
for(int i=obj.n+1;i<=n;i++)
r.a[i]=a[i];
r.n=n;
Page | 22
{
r.a[i] = obj.a[i];
r.n = obj.n;
else
r.n = obj.n;
return (r);
void input()
cin >> n;
void display()
for(int i=n;i>=0;i--)
Page | 23
{
if(a[i]!=0)
if(i==0)
cout<<a[i]<<" + " ;
else if(i==1)
cout<<a[i]<<"x + ";
else
cout<<a[i]<<"x^"<<i<<" + ";
else
continue;
cout<<"\b\b ";
};
int main()
[Link]();
[Link]();
Page | 24
cout << "\n Enter second polynomial:\n";
[Link]();
[Link]();
p= [Link](p2);
[Link]();
return 0;
OUTPUT:
Enter first polynomial:
Enter degree of polynomial: 3
Enter 4 coefficients:1
2
3
4
Polynomial = 4x^3 + 3x^2 + 2x + 1
Enter second polynomial:
Enter degree of polynomial: 4
Enter 5 coefficients:4
5
6
4
3
Polynomial = 3x^4 + 4x^3 + 6x^2 + 5x + 4
Page | 25
BRIEF DISCUSSION :
This program adds two polynomials using a class in C++. Each polynomial is stored in
an array where the index shows the power of x, and the value is the coefficient. The user
enters the degree and coefficients of two polynomials. The program adds them term by
term using the addpoly() function and stores the result in a new polynomial. The
display() function prints the polynomials in a readable form like 5x^2 + 3x + 2. This
program shows how object-oriented programming can be used to handle polynomial
operations in a simple way.
Page | 26
6. Implement the following using a Singly Linked List.
i) Insertion-in the beginning, at the end, after a specific position and after a
specific element of the list.
ii) Deletion- from the beginning, from the end, from a specific position and
a specific element of the list.
ALGORITHM :
1. Start the Program .
Members:
Constructor:
Private Member:
4. Insertion Operations
At Beginning:
Page | 27
Set new node’s next to head.
Update head to new node.
At End:
After Position:
After Element:
5. Deletion Operations
From Beginning:
From End:
Find the node before the one containing the target value.
Adjust pointers to skip and delete that node.
6. Search Operation
Page | 28
Traverse from head and compare each node’s data.
If match found, print position.
Else, show "not found" message.
7. Reverse Operation
8. Display Operation
Traverse list from head and print each data value until NULL.
9. End of Program .
CODE :
#include <iostream>
class Node
public:
int data;
Node* next;
Node(int val)
data = val;
next = NULL;
};
class SinglyLinkedList
Page | 29
{
private:
Node* head;
public:
SinglyLinkedList()
head = NULL;
// Insert at beginning
newNode->next = head;
head = newNode;
// Insert at end
if (head == NULL)
head = newNode;
return;
Page | 30
temp = temp->next;
temp->next = newNode;
delete newNode;
return;
if (temp == NULL)
delete newNode;
return;
temp = temp->next;
if (temp == NULL)
Page | 31
cout << "Position out of range.\n";
delete newNode;
return;
newNode->next = temp->next;
temp->next = newNode;
temp = temp->next;
if (temp == NULL)
return;
newNode->next = temp->next;
temp->next = newNode;
void deleteFromBeginning()
Page | 32
{
if (head == NULL)
return;
head = head->next;
delete temp;
void deleteFromEnd()
if (head == NULL)
return;
if (head->next == NULL)
delete head;
head = NULL;
return;
Page | 33
temp = temp->next;
delete temp->next;
temp->next = NULL;
return;
if (pos == 1)
deleteFromBeginning();
return;
if (temp == NULL)
return;
temp = temp->next;
Page | 34
}
return;
temp->next = del->next;
delete del;
if (head == NULL)
return;
if (head->data == val)
deleteFromBeginning();
return;
temp = temp->next;
Page | 35
if (temp->next == NULL)
return;
temp->next = del->next;
delete del;
// Search an element
int pos = 1;
if (temp->data == val)
cout << "Element found at position: " << pos << endl;
return;
temp = temp->next;
++pos;
void reverseList()
Page | 36
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
head = prev;
void display()
temp = temp->next;
};
Page | 37
// Main function
int main() {
SinglyLinkedList list;
do {
switch (choice)
case 1:
[Link](value);
Page | 38
break;
case 2:
[Link](value);
break;
case 3:
[Link](value, pos);
break;
case 4:
[Link](value, element);
break;
case 5:
[Link]();
break;
case 6:
[Link]();
break;
case 7:
[Link](pos);
break;
case 8:
Page | 39
cout << "Enter element to delete: ";
[Link](element);
break;
case 9:
[Link](value);
break;
case 10:
[Link]();
break;
case 11:
[Link]();
break;
case 12:
break;
default:
return 0;
OUTPUT:
1. Insert at Beginning
2. Insert at End
Page | 40
4. Insert after Element
8. Delete Element
9. Search Element
12. Exit
Enter choice: 1
Enter value: 10
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
12. Exit
Enter choice: 2
Enter value: 20
1. Insert at Beginning
Page | 41
2. Insert at End
8. Delete Element
9. Search Element
12. Exit
Enter choice: 1
Enter value: 30
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
12. Exit
Enter choice: 3
Page | 42
Enter value and position: 40 2
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
12. Exit
Enter choice: 11
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
Page | 43
12. Exit
Enter choice: 10
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
12. Exit
Enter choice: 11
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
Page | 44
11. Display List
12. Exit
Enter choice: 9
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
12. Exit
Enter choice: 5
1. Insert at Beginning
2. Insert at End
8. Delete Element
Page | 45
9. Search Element
12. Exit
Enter choice: 7
Enter position: 3
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Search Element
12. Exit
Enter choice: 11
1. Insert at Beginning
2. Insert at End
Page | 46
7. Delete from Position
8. Delete Element
9. Search Element
12. Exit
Enter choice: 12
Exiting...
BRIEF DISCUSSION :
This C++ program implements a singly linked list using object-oriented principles with
two classes: Node and SinglyLinkedList.
The SinglyLinkedList class encapsulates the operations on the list such as insertions,
deletions, search, reverse, and display.
The program supports interactive menu-driven operations, allowing the user to test and
visualize how the list changes dynamically with each operation.
All memory management is handled properly using new and delete. Edge cases such as
operations on an empty list or invalid positions are also checked and handled gracefully.
Page | 47
7. Implement the following using a Doubly Linked List.
i) ) Insertion-in the beginning, at the end, after a specific position and after
a specific element of the list.
ii) Deletion- from the beginning, from the end, from a specific position and
a specific element of the list.
ALGORITHM :
Data Members:
Constructor:
Private Member:
4. Insertion Operations
a) Insert at Beginning
Page | 48
If head is not NULL, set head->prev to the new node and new Node-> next to
head.
Update head to the new node.
b) Insert at End
5. Deletion Operations
Page | 49
Input the position.
Traverse to the node at the given position.
Adjust the previous and next pointers to unlink the node.
Delete the node.
CODE:
#include <iostream>
class Node
public:
int data;
Node* prev;
Node* next;
Node(int value)
data=value;
prev=NULL;
next=NULL;
Page | 50
}
};
class DoublyLinkedList
private:
Node* head;
public:
DoublyLinkedList()
head=NULL;
void insertAtBeginning()
int data;
cin>>data;
if(head)
head->prev=newNode;
newNode->next=head;
head=newNode;
void insertAtEnd()
int data;
Page | 51
cin>>data;
if(!head)
head=newNode;
return;
Node* temp=head;
while(temp->next)
temp=temp->next;
temp->next=newNode;
newNode->prev=temp;
void insertAfterPosition()
int position,data;
cin>>position;
cin>>data;
if(position<0)
return;
Node* temp=head;
int index=0;
while(temp&&index<position)
temp=temp->next;
index++;
Page | 52
}
if(!temp)
return;
newNode->next=temp->next;
newNode->prev=temp;
if(temp->next)
temp->next->prev=newNode;
temp->next=newNode;
void insertAfterElement()
int element,data;
cin>>element;
cin>>data;
Node* temp=head;
while(temp&&temp->data!=element)
temp=temp->next;
if(!temp)
return;
newNode->next=temp->next;
newNode->prev=temp;
if (temp->next)
temp->next->prev=newNode;
temp->next=newNode;
Page | 53
}
void deleteFromBeginning()
if(!head)
return;
Node* temp=head;
head=head->next;
if(head)
head->prev=NULL;
delete temp;
void deleteFromEnd()
if(!head)
return;
Node* temp=head;
if(!temp->next)
delete temp;
head=NULL;
return;
while(temp->next)
temp=temp->next;
temp->prev->next=NULL;
delete temp;
Page | 54
}
void deleteFromPosition()
int position;
cin>>position;
if(position<0||!head)
return;
Node* temp=head;
int index=0;
while(temp&&index<position)
temp=temp->next;
index++;
if(!temp)
return;
if(temp->prev)
temp->prev->next=temp->next;
else
head=temp->next;
if(temp->next)
temp->next->prev=temp->prev;
delete temp;
void deleteElement()
Page | 55
int element;
cin>>element;
Node* temp=head;
while(temp&&temp->data!=element)
temp=temp->next;
if(!temp)
return;
if(temp->prev)
temp->prev->next=temp->next;
else
head=temp->next;
if(temp->next)
temp->next->prev=temp->prev;
delete temp;
void display()
Node* temp=head;
if(!temp)
cout<<"List is empty!\n";
return;
while(temp)
Page | 56
temp=temp->next;
cout<<"NULL\n";
};
int main()
DoublyLinkedList dll;
int ch;
do {
switch (ch)
case 1:
[Link]();
break;
case 2:
Page | 57
[Link]();
break;
case 3:
[Link]();
break;
case 4:
[Link]();
break;
case 5:
[Link]();
break;
case 6:
[Link]();
break;
case 7:
[Link]();
break;
case 8:
[Link]();
break;
case 9:
[Link]();
break;
break;
} while (ch!=10);
Page | 58
return 0;
OUTPUT:
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
Page | 59
Enter data to insert at end: 20
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
Page | 60
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
1. Insert at Beginning
2. Insert at End
Page | 61
3. Insert after Position
8. Delete Element
9. Display List
10. Exit
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
1. Insert at Beginning
2. Insert at End
Page | 62
4. Insert after Element
8. Delete Element
9. Display List
10. Exit
1. Insert at Beginning
2. Insert at End
8. Delete Element
9. Display List
10. Exit
Exiting...
BRIEF DISCUSSION :
This C++ program implements a doubly linked list using two classes: Node and
DoublyLinkedList. Each node stores data along with pointers to both the previous and
next nodes, allowing bidirectional traversal. The list supports various operations
including insertion (at beginning, end, after a position or element), deletion (from
beginning, end, a position or by value), and displaying the list. All operations are
Page | 63
handled using proper pointer updates, and edge cases like empty lists or invalid
positions are managed safely. The program uses a menu-driven approach, making it
interactive and easy to test.
Page | 64
8. Write a program to perform Stack operations using Array .
ALGORITHM :
1. Start
2. Initialize Stack
1: Push
2: Pop
3: Display
4: Exit
Increment top by 1.
Page | 65
Decrement top.
Check if top == -1
Else:
CODE:
#include<iostream>
#include<stdlib.h>
#define SIZE 5
class Stack
public:
Stack()
top=-1;
void push()
if(top==SIZE-1)
Page | 66
}
else
int item;
cin>>item;
top=top+1;
A[top]=item;
void pop()
if(top==-1)
else
top=top-1;
void display()
if(top==-1)
Page | 67
else
int i;
for(i=top;i>=0;i--)
cout<<A[i]<<"\n";
};
int main()
Stack s;
int ch;
while(1)
cin>>ch;
switch(ch)
case 1:[Link]();
break;
case 2:[Link]();
Page | 68
break;
case 3:[Link]();
break;
case 4:exit(0);
return 0;
OUTPUT:
1 for push
2 for pop
3 for display
4 for exit
1 for push
2 for pop
3 for display
4 for exit
1 for push
2 for pop
3 for display
4 for exit
Page | 69
Enter the item to push:30
1 for push
2 for pop
3 for display
4 for exit
30
20
10
1 for push
2 for pop
3 for display
4 for exit
1 for push
2 for pop
3 for display
4 for exit
1 for push
2 for pop
3 for display
4 for exit
Page | 70
Enter your choice:3
10
1 for push
2 for pop
3 for display
4 for exit
1 for push
2 for pop
3 for display
4 for exit
1 for push
2 for pop
3 for display
4 for exit
1 for push
2 for pop
3 for display
4 for exit
Page | 71
BRIEF DISCUSSION :
This program demonstrates a basic static stack implementation using an array in C++.
Class Structure :
Stack class encapsulates the stack data and operations.
Array A[SIZE] holds the stack elements.
top indicates the index of the topmost element.
Operations :
Push(): Adds an element at the top. It checks for overflow.
Pop(): Removes the top element and checks for underflow.
Display(): Prints all elements from top to bottom.
Page | 72
9. Write a program to perform Queue operations using Array .
ALGORITHM :
1. Initialization :
rear = -1
front = -1
2. Insert Operation :
else
3. Delete Operation :
Step 1: If front == -1
else
Step 4: If front > rear, set both front and rear back to -1 (queue becomes empty).
4. Display Operation :
Step 1: If front == -1
Page | 73
Queue is empty, return.
Display A[i]
CODE :
#include<iostream>
#include<stdlib.h>
#define SIZE 5
class Queue
public:
Queue()
rear=-1;
front=-1;
void insert()
if(rear==SIZE-1)
Page | 74
cout<<"\nQueue overflow,item cannot be inserted";
else
int item;
cin>>item;
rear=rear+1;
A[rear]=item;
if(front==-1)
front=front+1;
void Delete()
if(front==-1)
else
front=front+1;
if(front>rear)
Page | 75
front=-1;
rear=-1;
void display()
if(front==-1)
else
int i;
for(i=front;i<=rear;i++)
cout<<A[i]<<" ";
};
int main()
Queue q;
int ch;
while(1)
Page | 76
cout<<"\n2 for delete";
cin>>ch;
switch(ch)
case 1:[Link]();
break;
case 2:[Link]();
break;
case 3:[Link]();
break;
case 4:exit(0);
return 0;
OUTPUT :
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
Page | 77
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
Page | 78
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
Page | 79
Queue underflow,item cannot be displayed
1 for insert
2 for delete
3 for display
4 for exit
BRIEF DISCUSSION :
Page | 80
10. Write a program to perform Circular Queue operations using Array .
ALGORITHM :
Step 1: Initialization :
Else:
Else:
Page | 81
Else:
Print each element in the range using % SIZE for wrapping around.
1. Insert
2. Delete
3. Display
4. Exit
CODE :
#include<iostream>
#include<stdlib.h>
#define SIZE 5
class circularq
public:
circularq()
rear=-1;
front=-1;
void insert()
if((rear+1)%SIZE==front)
Page | 82
{
inserted";
else
int item;
cout<<"\nEnter value:";
cin>>item;
if((rear==-1)&&(front==-1))
rear=0;
front=0;
else
rear=(rear+1)%SIZE;
A[rear]=item;
void Delete()
if((rear==-1)&&(front==-1))
deleted";
Page | 83
else
int del;
del=A[front];
cout<<"\nDeleted:"<<del;
if(rear==front)
rear=-1;
front=-1;
else
front=(front+1)%SIZE;
void display()
if((rear==-1)&&(front==-1))
displayed";
else
int i;
i=front;
Page | 84
while(1)
cout<<"["<<A[i]<<"]"<<" ";
if(i==rear)
break;
i=(i+1)%SIZE;
};
int main()
circularq cq;
int ch;
while(1)
cin>>ch;
switch(ch)
case 1:[Link]();
break;
Page | 85
case 2:[Link]();
break;
case 3:[Link]();
break;
case 4:exit(0);
return 0;
OUTPUT :
1 for insert
2 for delete
3 for display
4 for exit
Enter value:10
1 for insert
2 for delete
3 for display
4 for exit
Enter value:20
1 for insert
2 for delete
3 for display
4 for exit
Page | 86
Enter value:30
1 for insert
2 for delete
3 for display
4 for exit
Enter value:40
1 for insert
2 for delete
3 for display
4 for exit
Enter value:50
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
Deleted:10
1 for insert
Page | 87
2 for delete
3 for display
4 for exit
Enter value:60
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
BRIEF DISCUSSION :
A circular queue prevents unused space that occurs in a linear queue after multiple
insertions and deletions.
% SIZE is used to wrap around the array indices when the end is reached.
front and rear are managed carefully to distinguish between empty and full states.
Page | 88
11. Write a program to perform Stack operations using Linked List .
ALGORITHM :
1. Initialize Stack:
Create a class stack with a private member top, initialized to NULL
in the constructor.
2. Push Operation:
Create a new node.
Input data from the user.
Set new node’s data to the input and its next pointer to current top.
Update top to point to the new node.
3. Pop Operation:
If top is NULL, display "Stack is empty".
Else:
Store top in a temporary pointer.
Print the data of top.
Update top to the next node.
Delete the temporary node.
4. Display Operation:
If top is NULL, display "Stack is empty".
Else:
Traverse the linked list from top to NULL, printing each node's
data.
5. Menu-driven Main Function:
Repeatedly display menu options: push, pop, display, and exit.
Perform operation based on user’s choice using switch.
CODE :
#include<iostream>
#include<stdlib.h>
Page | 89
class Node
public:
int data;
Node *next;
};
class stack
Node *top;
public:
stack()
top=NULL;
void push()
Node *temp;
int v;
temp=new Node();
cout<<"\nEnter data:";
cin>>v;
temp->data=v;
temp->next=top;
top=temp;
void pop()
if(top==NULL)
cout<<"\nStack is empty";
else
Node *p=top;
Page | 90
cout<<top->data<<" is deleted";
top=top->next;
delete p;
void display()
if(top==NULL)
cout<<"\nStack is empty";
else
Node *i=top;
while(i!=0)
cout<<"\n["<<i->data<<"]";
i=i->next;
};
int main()
stack s;
int ch;
while(1)
cin>>ch;
Page | 91
switch(ch)
case 1:[Link]();
break;
case 2:[Link]();
break;
case 3:[Link]();
break;
case 4:exit(0);
return 0;
OUTPUT :
1 for push
2 for pop
3 for display
4 for exit
Enter data:10
1 for push
2 for pop
3 for display
4 for exit
Enter data:20
1 for push
2 for pop
3 for display
Page | 92
4 for exit
Enter data:30
1 for push
2 for pop
3 for display
4 for exit
[30]
[20]
[10]
1 for push
2 for pop
3 for display
4 for exit
30 is deleted
1 for push
2 for pop
3 for display
4 for exit
[20]
[10]
1 for push
2 for pop
Page | 93
3 for display
4 for exit
20 is deleted
1 for push
2 for pop
3 for display
4 for exit
10 is deleted
1 for push
2 for pop
3 for display
4 for exit
Stack is empty
1 for push
2 for pop
3 for display
4 for exit
Stack is empty
1 for push
2 for pop
3 for display
4 for exit
Page | 94
Enter your choice:4
BRIEF DISCUSSION :
It uses a Node structure (as a class) to hold integer data and a pointer to the next node.
The stack class encapsulates stack operations: push(), pop(), and display().
Page | 95
12. Write a program to perform Queue operations using Linked List.
ALGORITHM :
1. Insert Operation
Else:
2. Delete Operation
Else:
3. Display Operation
Else:
Page | 96
While temp is not NULL:
Print temp->data
Loop infinitely
Display menu
Read user’s choice
Use switch-case to call corresponding function:
Case 1: Insert
Case 2: Delete
Case 3: Display
CODE :
#include<iostream>
#include<stdlib.h>
class Node
public:
int data;
Node *next;
};
class Queue
Node *front;
Node *rear;
public:
Page | 97
Queue()
front=NULL;
rear=NULL;
void insert()
int v;
cout<<"\nEnter data:";
cin>>v;
temp->data=v;
temp->next=NULL;
if(rear==NULL)
front=rear=temp;
else
rear->next=temp;
rear=temp;
void Delete()
if(front==NULL)
Page | 98
}
else
Node *temp;
temp=front;
front=front->next;
if(front==NULL)
rear=NULL;
cout<<temp->data<<" is deleted";
delete temp;
void display()
if(front==NULL)
else
Node *temp=front;
cout<<"\nQueue:-";
while(temp!=NULL)
cout<<temp->data<<" ";
temp=temp->next;
Page | 99
}
};
int main()
Queue q;
int ch;
while(1)
cin>>ch;
switch(ch)
case 1:[Link]();
break;
case 2:[Link]();
break;
case 3:[Link]();
break;
case 4:exit(0);
return 0;
Page | 100
}
OUTPUT:
1 for insert
2 for delete
3 for display
4 for exit
Enter data:10
1 for insert
2 for delete
3 for display
4 for exit
Enter data:20
1 for insert
2 for delete
3 for display
4 for exit
Enter data:30
1 for insert
2 for delete
3 for display
4 for exit
Queue:-10 20 30
Page | 101
1 for insert
2 for delete
3 for display
4 for exit
10 is deleted
1 for insert
2 for delete
3 for display
4 for exit
20 is deleted
1 for insert
2 for delete
3 for display
4 for exit
Queue:-30
1 for insert
2 for delete
3 for display
4 for exit
30 is deleted
1 for insert
2 for delete
Page | 102
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
1 for insert
2 for delete
3 for display
4 for exit
BRIEF DISCUSSION :
This program demonstrates a basic Queue implementation using a singly linked list in
C++. The advantage of using linked lists is dynamic memory allocation, which allows
the queue to grow or shrink in size during runtime without wasting memory.
Page | 103
13. Write a program to perform Double ended queue operations using
Array.
ALGORITHM :
1. Initialization :
Set front = -1 and rear = -1.
2. Insert at Rear
Function: insertrear()
Check if rear == SIZE - 1:
Overflow, cannot insert.
If both front and rear == -1:
Set both to 0.
Else:
Increment rear and insert the item.
3. Delete from Rear
Function: deleterear()
If both front and rear == -1:
Underflow, nothing to delete.
Save the item at rear.
If front == rear:
Reset both to -1 (deque becomes empty).
Else:
Decrement rear.
4. Insert at Front
Function: insertfront()
If front == 0:
Overflow, no space at front.
If both front and rear == -1:
Set both to 0.
Else:
Page | 104
Decrement front and insert the item.
5. Delete from Front
Function: deletefront()
If both front and rear == -1:
Underflow, nothing to delete.
Save the item at front.
If front == rear:
Reset both to -1 (deque becomes empty).
Else:
Increment front.
6. Display
Function: display()
If deque is empty (front == -1 and rear == -1):
Print underflow.
Else:
Loop from front to rear and print all elements.
7. Main Menu Loop
Function: main()
Show menu with options to perform all deque operations.
Use a loop to allow repeated user interaction until exit is
chosen.
CODE :
#include<iostream>
#include<stdlib.h>
#define SIZE 5
class Deque
Page | 105
public:
Deque()
rear=-1;
front=-1;
void insertrear()
if((rear==(SIZE-1)))
from rear";
else
int item;
cout<<"\nEnter data:";
cin>>item;
if((rear==-1)&&(front==-1))
rear=0;
front=0;
else
rear=rear+1;
A[rear]=item;
Page | 106
}
void deleterear()
if((rear==-1)&&(front==-1))
from rear";
else
int del;
del=A[rear];
if(rear==front)
rear=-1;
front=-1;
else
rear=rear-1;
void insertfront()
if(front==0)
Page | 107
{
from front";
else
int item;
cout<<"\nEnter value:";
cin>>item;
if((rear==-1)&&(front==-1))
rear=0;
front=0;
else
front=front-1;
A[front]=item;
void deletefront()
if((rear==-1)&&(front==-1))
from front";
Page | 108
else
int del;
del=A[front];
if(rear==front)
rear=-1;
front=-1;
else
front=front+1;
void display()
if((rear==-1)&&(front==-1))
else
int i;
for(i=front;i<=rear;i++)
Page | 109
cout<<"["<<A[i]<<"]"<<" ";
};
int main()
Deque dq;
int ch;
while(1)
cin>>ch;
switch(ch)
case 1:[Link]();
break;
case 2:[Link]();
break;
case 3:[Link]();
break;
case 4:[Link]();
Page | 110
break;
case 5:[Link]();
break;
case 6:exit(0);
return 0;
OUTPUT :
5 for display
6 for exit
Enter data:10
5 for display
6 for exit
Enter data:20
Page | 111
2 for delete from rear
5 for display
6 for exit
5 for display
6 for exit
5 for display
6 for exit
Enter value:30
Page | 112
4 for delete from front
5 for display
6 for exit
5 for display
6 for exit
5 for display
6 for exit
5 for display
Page | 113
6 for exit
BRIEF DISCUSSION :
This C++ program demonstrates a basic double-ended queue (Deque) using an array of
fixed size (SIZE = 5). It allows:
Page | 114