1 /***************************************************************************
2                           strstack.cpp  -  description
3                              -------------------
4     begin                : Wed Oct 3 2001
5     copyright            : (C) 2001 by Dannel Albert
6     email                : dalbert@capitol-college.edu
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include <cassert>
19 #include "strstack.h"
20 
21 using namespace std;
22 
StrStack()23 StrStack::StrStack()
24 {
25   numItems = 0;
26   top = NULL;
27 }
28 
~StrStack()29 StrStack::~StrStack()
30 {
31   StrNode* nd;
32   for(unsigned int i=0; i<numItems; i++)
33   {
34     nd = top;
35     top->SetNext(top->GetNext());
36     delete nd;
37   }
38 }
39 
Peek() const40 string StrStack::Peek() const
41 {
42   if (numItems == 0)
43     return (string)"";
44   return top->GetData();
45 }
46 
Pop()47 string StrStack::Pop()
48 {
49   string data;
50   if (numItems == 0)
51     return (string)"";
52 
53   StrNode* nd = top;
54   data = nd->GetData();
55   top = top->GetNext();
56   delete nd;
57   numItems--;
58   return data;
59 }
60 
Push(string s)61 void StrStack::Push(string s)
62 {
63   StrNode* nd = new StrNode(s, top);
64   assert(nd);
65   top = nd;
66   numItems++;
67 }