Professional Documents
Culture Documents
1 2
g; g
3 4
Bounded Stack Example (cont'd) Bounded Stack Example (cont'd)
Implementation (cont'd) Use of a bounded stack to reverse a name
#include <stream.h>
void Stack::push (ELEMENT TYPE new item) #include "stack.h"
f
this->stack[this->stack top++] = new item; int main (int argc, char *argv[]) f
g const int MAX NAME LEN = 80;
ELEMENT TYPE Stack::pop (void) char name[MAX NAME LEN];
f Stack stack;
return this->stack[--this->stack top];
g cout << "please enter your name..: ";
ELEMENT TYPE Stack::top (void) cin.getline (name, MAX NAME LEN);
f
return this->stack[this->stack top , 1]; for (int i = 0; name[i] != '\n' && !stack.is full (); i++)
g stack.push (ELEMENT TYPE (name[i]));
int Stack::is empty (void)
f
return this->stack top == Stack::BOTTOM; cout << "your name backwards is..: ";
g
int Stack::is full (void) while (!stack.is empty ())
f cout << char (stack.pop ());
return this->stack top >= Stack::SIZE; cout << '\n';
g g
5 6
13
44