Question #29387

1.using an array of size 10, implement the PUSH and POP operations of the stack. NB: Let your TOP and BOTTOM be -1 when that STACK is EMPTY
2. Convert your algorithm above into a c++ code

Expert's answer

//1.using an array of size 10, implement the PUSH and POP operations of

//the stack. NB: Let your TOP and BOTTOM be -1 when that STACK is EMPTY

//2. Convert your algorithm above into a c++ code

# include <iostream>

using namespace std;

class stck{

int ar[10];

int ptr;

public:

stck(){ptr=-1;}

void push(int val){ if (ptr == 8) cout<<"Stack is full!\n"; ptr++;ar[ptr] = val;}

void pop(){if (ptr == -1) cout<<"Stack is Empty!\n"; else ptr--;}

};

int main(){

stck s;

s.push(1);

s.pop();

s.pop();

for (int i=0;i<11;i++)

s.push(1);

system("PAUSE");

return 0;

}

//the stack. NB: Let your TOP and BOTTOM be -1 when that STACK is EMPTY

//2. Convert your algorithm above into a c++ code

# include <iostream>

using namespace std;

class stck{

int ar[10];

int ptr;

public:

stck(){ptr=-1;}

void push(int val){ if (ptr == 8) cout<<"Stack is full!\n"; ptr++;ar[ptr] = val;}

void pop(){if (ptr == -1) cout<<"Stack is Empty!\n"; else ptr--;}

};

int main(){

stck s;

s.push(1);

s.pop();

s.pop();

for (int i=0;i<11;i++)

s.push(1);

system("PAUSE");

return 0;

}

## Comments

## Leave a comment