0% found this document useful (0 votes)
20 views9 pages

Lab 2

The document contains code implementations of stacks and related algorithms: 1) A Stack class is defined with push(), pop(), peek(), isFull() and isEmpty() methods. 2) Main() demonstrates usage of the Stack class. 3) A function is defined to print a string in reverse order using a stack. 4) A TwoStacks class is defined using a single array to implement two stacks, with push1(), push2(), pop1(), pop2(), peek1(), peek2() and other methods. 5) Main() demonstrates usage of the TwoStacks class.

Uploaded by

sahardevjani635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Lab 2

The document contains code implementations of stacks and related algorithms: 1) A Stack class is defined with push(), pop(), peek(), isFull() and isEmpty() methods. 2) Main() demonstrates usage of the Stack class. 3) A function is defined to print a string in reverse order using a stack. 4) A TwoStacks class is defined using a single array to implement two stacks, with push1(), push2(), pop1(), pop2(), peek1(), peek2() and other methods. 5) Main() demonstrates usage of the TwoStacks class.

Uploaded by

sahardevjani635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

LAB 2

Sahar Fatima Devjani


FA20-BSCS-1025
DATA STRUCTURES AND ALGORITHM LAB
Implement Stack
#include <iostream>

using namespace std;

class Stack {

private:

int top;

int* arr_stack;

public:

int size;

Stack(){

cout<<"Enter size of Stack : ";

cin>>size;

arr_stack = new int [size];

top = -1;

void push(int value){

if(!isFull()){

arr_stack[++top] = value;

}else{

cout<<"Stack is Full"<<endl;
}

void pop(){

if(!isEmpty()){

cout << arr_stack[top--] <<endl;

}else{

cout<<"Stack is Empty"<<endl;

void peek(){

if(!isEmpty()){

cout << arr_stack[top] <<endl;

}else{

cout<<"Stack is Empty"<<endl;

bool isFull(){

return top == size-1;

bool isEmpty(){

return top < 0;

};

int main() {

Stack obj;

int option = 1;

while(option != 0){
cout<<"Choose an option"<<endl <<"1 to push"<<endl <<"2 to pop"<<endl <<"3 to peek"<<endl;

cin>>option;

if(option==1){

int value;

cout<<"Enter your value: ";

cin>>value;

obj.push(value);

else if(option==2){

obj.pop();

else if(option==3){

obj.peek();

else{

option = 0;

return 0;

}
Print Reverse String using Stack
#include <iostream>

using namespace std;

int main() {

int k;

char str[100];

cout<<"Enter string to reverse : ";

cin>>str;

for(int i=0;str[i]!='\0' ;i++){

k++;

for(int i=k;i>=0;i--){

cout<<str[i];

return 0;

Implement 2 Stacks in a Single Array


#include <iostream>

using namespace std;

class Stack {

private:

int top1;

int top2;

int* arr_stack;

public:

int size1;

int size2;

Stack() {

cout << "Enter size of Array 1: ";

cin >> size1;

cout << "Enter size of Array 2: ";

cin >> size2;

arr_stack = new int[size1+size2];

top1 = -1;

top2 = size1-1;

void push1(int value) {

if (!isFull1()) {

arr_stack[++top1] = value;

else {

cout << "Stack 1 is full" << endl;

}
void push2(int value) {

if (!isFull2()) {

arr_stack[++top2] = value;

else {

cout << "Stack 2 is full" << endl;

void pop1() {

if (!isEmpty1()) {

cout << arr_stack[top1--] << endl;

else {

cout << "Stack is empty" << endl;

void pop2() {

if (!isEmpty2()) {

cout << arr_stack[top2--] << endl;

else {

cout << "Stack is empty" << endl;

void peek1() {

if (!isEmpty1()) {

cout << arr_stack[top1] << endl;

else {
cout << "Stack is empty" << endl;

void peek2() {

if (!isEmpty2()) {

cout << arr_stack[top2] << endl;

else {

cout << "Stack is empty" << endl;

bool isFull1() {

return top1 == (size1-1);

bool isEmpty1() {

return top1 < 0;

bool isFull2() {

return top2 == ((size1+size2)-1);

bool isEmpty2() {

return top2 < size1;

};

int main() {

int option = 1;

Stack obj;

while (option != 0) {
int value;

cout << "Select an option\nPress 1 to push in Array 1\nPress 2 to push in Array 2\nPress 3 to pop in
Array 1\nPress 4 to pop in Array 2\nPress 5 to peek in Array 1\nPress 6 to peek in Array 2\nPress 0 to
exit" << endl;

cin >> option;

if (option == 1) {

cout << "Enter a value to push in Array1: ";

cin >> value;

obj.push1(value);

else if (option == 2) {

cout << "Enter a value to push Array2: ";

cin >> value;

obj.push2(value);

else if (option == 3) {

obj.pop1();

else if (option == 4) {

obj.pop2();

else if (option == 5) {

obj.peek1();

else if (option == 6) {

obj.peek2();

return 0;
}

You might also like