-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
147 lines (124 loc) · 4.18 KB
/
Stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "Stack.h"
/*********************************************************************************************
S t a c k . c p p (GIVEN)
**********************************************************************************************
Description: To hold the function definitions that were decared in the class declaration
*********************************************************************************************/
/*************************************************************************************
Name: Default constructor
Precondition: new stack object created in the main
Postcondition: new stack object created in main is initialized as stack with top = 0
Description: default constructor will set top of stack to 0.
*************************************************************************************/
stack::stack()
{
//cout<<"Inside Default Constructor\n";
s_top = 0;
}
/*************************************************************************************
Name: Copy constructor
Precondition: stack one has been created and initialized.
Postcondition: stack two will have a copy of contents in stack one
Description: copy constructor will initialize this top to 0 and transverse through
stack one pushing each data term into stack temp, it then transverses this stack (stack two)
and it pushed each term into the new stack.
*************************************************************************************/
stack::stack(const stack& Org)
{
//cout<<"Inside the Copy Constructor\n";
(*this).s_top = 0;
stack temp;
stack_node* p = Org.s_top;
while (p != 0)
{
temp.push(p->data);
p = p->next;
}
p = temp.s_top;
while (p != 0)
{
(*this).push(p->data);
p = p->next;
}
}
/*************************************************************************************
Name: stack destructor
Precondition: stack has been created and initialized
Postcondition: stack has been deleted
Description: function will transverse through the stack and pop each term.
*************************************************************************************/
stack::~stack()
{
//cout<<"Inside !stack (destructor) \n";
while (s_top != 0)
{
pop();
}
}
/*************************************************************************************
Name: pop
Precondition: stack is created and initialized
Postcondition: removes the top element and returns the result.
Description: removes the top element
*************************************************************************************/
string stack::pop()
{
//cout<<"Inside pop \n";
string my_result;
stack_node* p;
if (s_top != 0)
{
p = s_top;
my_result=p->data;
s_top = s_top->next;
delete p;
}
return my_result;
}
/*************************************************************************************
Name: push
Precondition: stack is created and initialized
Postcondition: push element to the top of the stack
Description: push element to the top if linked list not full increment top by 1,
and store value in linked list [top]
*************************************************************************************/
void stack::push(const stack_element& item)
{
//cout<<"Inside push \n";
stack_node* p = new stack_node;
p->data = item;
p->next = s_top;
s_top = p;
}
/*************************************************************************************
Name: print
Precondition: stack is created and initialized
Postcondition: stack is printed onto the screen
Description: function will transverse through the stack and cout every term.
*************************************************************************************/
void stack::print()
{
//cout<<"Inside print \n";
for (stack_node* p = s_top; p != 0; p = p->next)
{
cout << p->data << endl;
}
}
/*************************************************************************************
Name: top
Precondition: stack is created and initialized
Postcondition: top data of the stack is returned if the stack is not empty.
Description: top data of the stack is returned if the stack is not empty.
*************************************************************************************/
stack_element stack::top()
{
//cout<<"Inside top \n";
if (s_top == 0)
{
exit(1);
}
else
{
return s_top->data;
}
}