summaryrefslogtreecommitdiffstats
path: root/4.00.stack.cc
blob: 89e2df532efb9bede9704a6b478590a3cd185d9c (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * =====================================================================================
 *
 *       Filename:  4.00.stack.cc
 *
 *    Description:  Implementation of stack as Class
 *
 *        Version:  1.0
 *        Created:  05.05.2014 09:49:24
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Stefan Suhren (SSuhren), suhren.stefan@fh-swf.de
 *   Organization:  FH Südwestfalen, Iserlohn
 *
 * =====================================================================================
 */

#include	<cstdlib>
#include	<iostream>
#include	<iomanip>
#include	<cassert>

using namespace std;

const int MAXARRAY = 100;

// =====================================================================================
//        Class:  Stack
//  Description:  Simple implementation of Stack
// =====================================================================================
class Stack
{
	public:
		// ====================  LIFECYCLE     ======================================= 
		Stack();								// constructor 
		Stack(int);								// constructor 
		~Stack();								// destructor

		// ====================  ACCESSORS     ======================================= 
		void push(int);
		int pop();
		bool isEmpty();
		int top();
		int height();
		void dup();
		void exch();
		void copy(const Stack&);
		void print(string msg);

	private:
		// ====================  METHODS       ======================================= 
		void init( int );
		// ====================  DATA MEMBERS  ======================================= 
		int *stack;
		int next;
		int max;

}; // -----  end of class Stack  ----- 

void belege (int);

// ===  FUNCTION  ======================================================================
//         Name:  main
//  Description:  Tests stack
// =====================================================================================
int main ( int argc, char *argv[] ){
	//-----------------------------------------------------------------------------
	//  4.1
	//-----------------------------------------------------------------------------

	Stack test1(MAXARRAY);

	// Fill test1
	for(int i = 0; i < 99; i++){
		test1.push(i);
	}
	
	// some testcases for the functions
	cout << "\nTest1.isEmpty() = " << boolalpha << test1.isEmpty() << "\n";
	cout << "Test1.top() = " << test1.top() << "\n";
	cout << "Test1.height() = " << test1.height() << "\n";
	test1.exch();
	test1.dup();
	cout << "Test1.height() = " << test1.height() << "\n";

	// Empty test1 and verfiy
	for(int i = 0; i < 100; i++){
		cout << test1.pop() << " ";
	}

	cout << "\nTest1.isEmpty() = " << boolalpha << test1.isEmpty() << "\n";

	//-----------------------------------------------------------------------------
	//  4.2
	//-----------------------------------------------------------------------------

	system("free");

	belege(1024);

	system("free");

	//-----------------------------------------------------------------------------
	//  4.3
	//-----------------------------------------------------------------------------

	Stack stapel2(30), stapel3(20);
	// Fill stack2
	for( int i = 0; i < 15; i++ ){
		stapel2.push(i);
	}

	// Fill stack3
	for( int i = 0; i < 10; i++ ){
		stapel3.push(i);
	}

	// Now print both stacks
	cout << "\n";
	stapel2.print( "Stapel2" );
	cout << "\n";
	stapel3.print( "Stapel3" );

	// Copy 2 on 3
	stapel3.copy(stapel2);

	// Now print both stacks again
	cout << "\n";
	stapel2.print( "Stapel2" );
	cout << "\n";
	stapel3.print( "Stapel3" );

	return EXIT_SUCCESS;
}				// ----------  end of function main  ---------- 

// ===  FUNCTION  ======================================================================
//         Name:  belege
//  Description:  Recursive function for testing the stack
// =====================================================================================
void belege ( int n ){
	if( n <= 0 ){
		system("free");
	}
	else{
		Stack test;
		belege( n-1 );
	}
}		// -----  end of function belege  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::Stack
//  Description:  Create a dynamic stack with 1024 items
// =====================================================================================
Stack::Stack (  ){
	init( 1024 );
}		// -----  end of function Stack::Stack  ----- 
// ===  FUNCTION  ======================================================================
//         Name:  Stack::Stack
//  Description:  Create a dynamic stack with length items
// =====================================================================================
Stack::Stack ( int length ){
	assert( length > 0 );
	init( length );
}		// -----  end of function Stack::Stack  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::~Stack
//  Description:  Free memory
// =====================================================================================
Stack::~Stack (  ){
	delete[] stack;
}		// -----  end of function Stack::~Stack  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::push
//  Description:  Push value onto stack
// =====================================================================================
void Stack::push ( int value ){
	assert( next < max );

	stack[next++] = value; 
}		// -----  end of function Stack::push  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::pop
//  Description:  Pop element from stack
// =====================================================================================
int Stack::pop (  ){
	assert( next > 0 );

	return stack[--next];
}		// -----  end of function Stack::pop  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::isEmpty
//  Description:  Determine if stack is empty
// =====================================================================================
bool Stack::isEmpty (  ){
	return next <= 0;
}		// -----  end of function Stack::isEmpty  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::top
//  Description:  Get topmost value without poping it
// =====================================================================================
int Stack::top (  ){
	assert( next > 0 );

	return stack[next-1];
}		// -----  end of function Stack::top  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::height
//  Description:  Get the actual hight of the stack
// =====================================================================================
int Stack::height (  ){
	return next;
}		// -----  end of function Stack::height  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::dup
//  Description:  Duplicate the topmost item
// =====================================================================================
void Stack::dup (  ){
	push(top());
}		// -----  end of function Stack::dup  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::exch
//  Description:  Swap the two topmost items
// =====================================================================================
void Stack::exch (  ){
	assert( next > 1 );

	int tmp = stack[next-2];
	stack[next-2] = stack[next-1];
	stack[next-1] = tmp;

}		// -----  end of function Stack::exch  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack:copy
//  Description:  Copys a stack s onto this
// =====================================================================================
void Stack::copy ( const Stack &s ){
	if( &s != this ){
		// As a safty
		int *temp = stack;

		// New Stack
		stack = new int[s.max];

		// Deep Copy
		for(int i = 0; i < max; i++){
			stack[i] = s.stack[i];
		}

		// Now make the object consistent
		max = s.max;
		next = s.next;

		// And free memory
		delete[] temp;
	}
}		// -----  end of function Stack:copy  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack:print
//  Description:  
// =====================================================================================
void Stack::print ( string msg ){
	cout << msg;

	for( int i = 0; i < next; i++ ){
		// Every ten values a new line
		if( (i % 10)  == 0 ){
			cout << "\n" << setw(3) << i << " : ";
		}
		cout << setw(5) << stack[i];
	}

	cout << "\nStack-Länge : " << max << " Belegung : " << next << "\n";
}		// -----  end of function Stack:print  ----- 

// ===  FUNCTION  ======================================================================
//         Name:  Stack::init
//  Description:  Initialise next "pointer"
// =====================================================================================
void Stack::init ( int length ){
	next = 0;

	max = length;
	stack = new int[max];
}		// -----  end of function Stack::init  -----