Program 1
Obj :- To perform insertion and deletion operations on array.
#include <iostream>
using namespace std;
int main() {
//n is the size of the array
int n;
cout << "Enter the size of the array: " << endl;
cin >> n;
cout << endl;
int arr[n];
cout << "Choose the operation you want to perform" << endl;
cout << "1. Insertion" << endl;
cout << "2. Deletion" << endl << endl;
//choice for type of operation to perform
int choice;
cout << "Enter the operation type: ";
cin >> choice;
cout << endl;
switch (choice) {
case 1:
//Insertion Operation
cout << "Enter " << n - 1 << " elements to insert:" << endl;
for (int i = 0; i < n - 1; i++) {
cin >> arr[i];
} cout << endl;
Program 2
Obj :- To perform multiplication operation on matrix.
#include <iostream>
using namespace std;
int main() {
// number of rows and columns for matrix 1
int r1, c1;
cout << "Enter the row 1 and column 1 for matrix 1: " << endl;
cout << "row 1: ";
cin >> r1;
cout << "column 1: ";
cin >> c1;
cout << endl;
// number of rows and columns for matrix 2
int r2, c2;
cout << "Enter the row 2 and column 2 for matrix 2: " << endl;
cout << "row 2: ";
cin >> r2;
cout << "column 2: ";
cin >> c2;
cout << endl;
//c1 and r2 should be equal for multiplication
if (c1 == r2) {
// matrix 1 initialization by user
int A[r1][c1];
Program 3
Obj :- To implement single linked list.
#include <iostream>
using namespace std;
// Node class for single linked list
class Node {
public:
int data;
Node *next;
} *head;
// function for creating the linked list
void createSLL(int n) {
// size invalid case
if (n <= 0) {
cout << "The list size is invalid " << endl;
return;
head = (Node *) malloc (sizeof(Node));
// if head node does not allocated the memory
if (head == NULL) {
cout << "Overflow" << endl;
return;
// data of head Node
int data;
cout << "Enter data for Head Node: ";
cin >> data;
Program 4
Obj :- To implement doubly linked list.
#include <iostream>
using namespace std;
// Node class for doubly linked list
class Node {
public:
int data;
Node *next;
Node *prev;
} *head;
// function for creating the linked list
void createDLL(int n) {
// size invalid case
if (n <= 0) {
cout << "The list size is invalid " << endl;
return;
head = (Node *) malloc (sizeof(Node));
// if head node does not allocated the memory
if (head == NULL) {
cout << "Overflow" << endl;
return;
// data of head Node
int data;
cout << "Enter data for Head Node: ";
Program 5
Obj :- To calculate factorial of number using recursion.
#include <iostream>
using namespace std;
//factorial using recursion function
int factorial (int n) {
//base case
if (n == 1) {
return 1;
// assuption
return n * factorial (n - 1);
int main() {
//n for factorial we need
int n;
cout << "Enter the number for factorial you need: " << endl;
cin >> n;
//calling the factorial function and inserting the return value of function in result variable
int result = factorial(n);
//printing the factorial of n
cout << "The factorial of " << n << " is" << endl << result;
return 0;
}
Program 6
Obj :- To demonstrate static implementation of stack.
#include <iostream>
#define MAX 100
using namespace std;
//Stack class for static implementation
class Stack {
public:
int arr[MAX];
int capacity;
int top;
Stack (int size) {
top = -1;
capacity = size - 1;
//push function to insert element on top of the stack
void push (int data) {
cout << "Push the data: " << data << endl;
//if top reach the max size or above
if (this->top >= this->capacity) {
cout << "Overflow" << endl;
return;
arr[++this->top] = data;
}
//pop function to delete element from top of the stack
int pop() {
cout << "Pop the top of the Element: "<<endl;
//if Stack is NULL
if (this->top < 0) {
cout << "NULL" << endl << "Underflow" << endl;
return -1;
cout << arr[top] << endl;
return arr[this->top--];
//size function to know the current size of the Stack
int size() {
return this->top + 1;
//empty check function to know the stack is empty or not
bool isEmpty() {
return (this->top < 0);
//top function to get the top element of the stack
int topOf() {
// if stack is empty
if (this->top < 0) {
cout << "Empty Stack" << endl;
return -1;
}
return arr[this->top];
//display function to print the Stack element from top to bottom
void display() {
cout << endl;
//if stack is empty
if (isEmpty()) {
cout << "Stack is Empty" << endl;
return;
cout << "The Stack elements are:" << endl;
for (int i = top; i > (-1); i--) {
cout << arr[i] << " ";
cout << endl << endl;
};
int main() {
Stack st(4);
st.push(5);
st.push(4);
st.push(3);
st.display();
st.pop();
st.display();
cout<<"The size of the Stack is: "<<st.size()<<endl;
cout<<"The top element of the Stack is: "<<st.topOf()<<endl;
cout<<"The Stack is Empty: "<<st.size()<<endl;
return 0;
Output:
Program 7
Obj :- To demonstrate dynamic implementation of stack.
#include <iostream>
using namespace std;
// Node class for single linked list
class Node {
public:
int data;
Node *next;
};
//Stack class
class Stack {
private:
Node* head;
public:
int top;
int capacity;
Stack (int size) {
top = -1;
capacity = size;
head = NULL;
//pushing elements in the Stack
void createStack() {
int size;
cout << "Enter number of element to push in stack: " << endl;
cin >> size;
// if size is greater than capacity
if (this->capacity < size) {
cout << "Invalid size (size should be equal or smaller than capacity)" << endl;
return;
//if size is smaller than the zero
if (size <= 0) {
cout << "The stack size is invalid" << endl;
return;
head = (Node *) malloc (sizeof(Node));
//if memory not allocated to the head
if (head == NULL) {
cout << "Overflow" << endl;
return;
int val , n = 0;
cout << "Enter the data for element " << ++n << ": ";
cin >> val;
head->data = val;
head->next = NULL;
++this->top;
for (int i = 2; i <= size; i++) {
Node *newNode = (Node *) malloc (sizeof(Node));
//if memory not allocated to the newNode
if (newNode == NULL) {
cout << "Overflow" << endl;
return;
int val;
cout << "Enter the data for element " << ++n << ": ";
cin >> val;
newNode->data = val;
newNode->next = head;
head = newNode;
++this->top;
cout << endl;
cout << "The stack is created (top to bottom) " << endl;
//push function to insert element on top of the stack
void push() {
//if size reach more than the copacity
if (top + 1 >= capacity) {
cout << "Stack Overflow" << endl;
return;
int val;
cout << "Enter data for push element in stack: ";
cin >> val;
cout<<endl;
Node *newNode = (Node *) malloc (sizeof(Node));
//if memory not allocated to the newNode
if (newNode == NULL) {
cout << "Overflow" << endl;
return;
newNode->data = val;
newNode->next = head;
head = newNode;
++this->top;
cout<<"After pushing the element in stack the New Stack is: "<<endl;
//pop function to delete top element from the stack
void pop() {
// if stack is empty
if (head == NULL) {
cout << "Underflow"<<endl;
return;
Node *temp = head;
head = head->next;
free(temp);
--this->top;
cout<<"After poping the element from stack the New Stack is: "<<endl;
//printing the stack elements from top to bottom
void display() {
// if list is empty
if (head == NULL) {
cout << "The linked list is empty" << endl << endl;
return;
// printing the list
Node *ptr = head;
while (ptr != NULL) {
cout << ptr->data << "->";
ptr = ptr->next;
cout << "NULL" << endl << endl;
//to know the size of the stack
int size() {
return this->top + 1;
//to know the stack is empty or not
bool isEmpty() {
return (this->top < 0);
//to know the top elements of the stack
int topOf() {
//if stack is empty
if (head == NULL) {
cout << "Stack is empty" << endl;
return -1;
return head->data;
// function for modify the Stack
void modifyStack() {
// flag used for asking for modify or not
int flag;
cout << "Do you want to do changes in the Stack" << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
cout << endl;
cout << "Enter 1 for Yes and 2 for No: ";
cin >> flag;
cout << endl;
// modifing till flag is 1 (Yes)
while (flag == 1) {
cout << "Choose the Operation you want to perform" << endl;
cout << "1. Push" << endl;
cout << "2. POP" << endl;
cout << "3. Size of the Stack" << endl;
cout << "4. Top element fo the Stack" << endl;
cout << "5. Empty" << endl << endl;
int choice;
cout << "Enter the operation type: ";
cin >> choice;
cout << endl;
switch (choice) {
// calling push operation function
case 1:
push();
display();
break;
// calling pop operation function
case 2:
pop();
display();
break;
// calling the size operation function
case 3:
cout<<"The size of the Stack is "<<endl;
cout<<size()<<endl <<endl;
break;
// calling the top operation function
case 4:
cout<<"The top of the element of Stack is "<<endl;
cout<<topOf()<<endl;
cout<<endl;
break;
// calling the empty check operation function
case 5:
cout<<"The Stack is Empty "<<endl;
cout<<isEmpty()<<endl;
cout<<endl;
break;
default:
break;
// asking for modification to continue the loop
cout << "Do you want to do changes in the Stack" << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl << endl;
cout << "Enter 1 for Yes and 2 for No: ";
cin >> flag;
cout << endl;
};
int main() {
//max size or capacity of the stack
int size;
cout << "Enter the max size(capacity) of the stack: " << endl;
cin >> size;
//object of class Stack
Stack st(size);
st.createStack();
st.display();
//calling modifyStack for performing operation on the Stack
st.modifyStack();
return 0;
Output: