Dsal 2
Dsal 2
Lab
Lab Journal – 02
(OC-Lab 02)
1
1 Objectives
This lab is intended to introduce students to the concept of Stacks and its
implementation.
The students will implement the Stack and employ it in solving the given problems
statically and using a linklist dynamically.
Ans,
For first three steps A , B and C will be added in the stack in order
C(2 index)
B(1 index)
2
A(0 index)
The pop function will delete the value inserted previously first in last out since it is
called twice B and C will be removed
A(0 index)
E(2 index)
D(1 index)
A(0 index)
3
A(0 index)
Task 2.2: Write a C++ program to implement the following static stack algorithm such
that:
Solution:
#include<iostream>
using namespace std;
4
}
void isEmpty() {
if (top == -1) {
cout << "the stack is empty." << endl;
}
}
void push(int a) {
if (top >= macro - 1) {
isFull();
}
else {
top++;
arr[top] = a;
}
}
void pop() {
if (top == -1) {
isEmpty();
}
else {
arr[top--];
}
}
void getTop() {
if (top != -1) {
cout << "Top element: " << arr[top] << endl;
}
else {
isEmpty();
}
}
void display() {
if (top == -1) {
isEmpty();
}
else {
cout << "Stack elements: ";
for (int i = 0; i <= top; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
}
int main() {
int choice, value;
do {
cout << "\nStack Operations Menu:"<<endl;
cout << "1. Push"<<endl;
cout << "2. Pop"<<endl;
cout << "3. Display Top Element"<<endl;
cout << "4. Display Stack"<<endl;
cout << "5. Exit"<<endl;
cout << "Enter your choice: "<<endl;
cin >> choice;
5
switch (choice) {
case 1:
cout << "Enter a value to push: ";
cin >> value;
push(value);
break;
case 2:
pop();
break;
case 3:
getTop();
break;
case 4:
display();
break;
case 5:
cout << "Exit" << endl;
break;
default:
cout << "Invalid choice" << endl;
}
} while (choice != 5);
return 0;
}
6
Task 2.3:
Write a program using struct to perform the push and pop operations on the stack such
that:
A stack structure to be declared with array and top variable as its members.
Perform the push, Pop and list_element operations by using pointer of type struct as
a parameter for all functions.
Solution: #include<iostream>
using namespace std;
struct stack {
int arr[10];
7
int top = -1;
};
void pop(stack*a) {
if (a->top == -1) {
cout << "stack is empty";
}
else {
a->arr[a->top--];
}
}
void listelement(stack* a) {
int main() {
stack a;
int n;
int c;
for (int i = 0; i < 10; i++) {
cout << "enter the values you want to add in the stack" << endl;
cin >> n;
push(&a, n);
}
cout << "enter the number of elements you want to pop: ";
cin >> c;
for (int i = 0; i < c; i++) {
pop(&a);
}
listelement(&a);
}
8
Task 2.4:
Calculate the sum of the marks of all the students of a group. In the program declare an
integer array dynamically and allocated it some space in memory equal to the size
which would be occupied by length number of integers. Note: value for length
variable shall also be taken at run time. Delete the array at the end.
Solution: #include<iostream>
using namespace std;
int main() {
int length;
int marks;
int sum=0;
cout << "Enter the amount of students: ";
cin >> length;
int* arr = new int[length];
for (int i = 0; i < length; i++) {
cout << "enter the marks of the students:" << endl;
cin >> marks;
arr[i] = marks;
sum += arr[i];
9
}
cout << "the sum of marks is: " << sum;
delete[]arr;
}
Exercise 01:
Write a program to implement a stack dynamically using a linklist with all the functions
representing different operations of stack. Define member functions push (), Pop () and
display outside the class.
Solution:
Exercise 02:
Write a C++ program that reverses the stack (using an array) elements.
Push elements in the stack:
Stack elements: 1 2 3 4 5 6 7
Display the reverse elements of the stack:
Stack elements: 7 6 5 4 3 2 1
Solution:
#include<iostream>
using namespace std;
const int n = 7;
class stack {
int arr[n];
int top = -1;
public:
void push(int a) {
if (top >= 6) { cout << "stack is full"; }
else
top++;
arr[top] = a;
}
void display() {
for (int i = 0; i < n; i++) {
cout << arr[i] << endl;
}
}
void reverse() {
for (int i = 0; i < n/2; i++) {
int temp = arr[i];
arr[i] = arr[n - 1 - i];
10
arr[n - 1 - i] = temp;
}
}
};
int main() {
stack s;
for (int i = 1; i <= 7; i++) {
s.push(i);
}
cout << "Normal order:" << endl;
s.display();
cout << endl << "reversed order:" << endl;
s.reverse();
s.display();
11