0% found this document useful (0 votes)
29 views8 pages

Lab 3

The document describes implementing two stacks in a single array data structure. It defines a Stack class with private data members for the top indices of each stack and a shared array. Member functions are included to push, pop, and peek from either stack by manipulating the appropriate top index and checking for empty/full conditions. The main function tests the class by prompting the user to push, pop, or peek from stack 1 or 2 and calling the corresponding member functions.

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)
29 views8 pages

Lab 3

The document describes implementing two stacks in a single array data structure. It defines a Stack class with private data members for the top indices of each stack and a shared array. Member functions are included to push, pop, and peek from either stack by manipulating the appropriate top index and checking for empty/full conditions. The main function tests the class by prompting the user to push, pop, or peek from stack 1 or 2 and calling the corresponding member functions.

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/ 8

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

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