-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
69 lines (61 loc) · 2.08 KB
/
Copy pathstack.h
File metadata and controls
69 lines (61 loc) · 2.08 KB
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
#include <iostream>
using namespace std;
#define NMAX 50
template<typename T>
class Stack {
private:
T stackArray[NMAX]; // an array of NMAX dimension
int topLevel; // the top of the stack, representing the INDEX of last element of the
//stackArray:0, 1, 3,....
public:
void push(T x) // puts an element in the stack array
{
if (topLevel >= NMAX-1) //check if the stack array has the maximum dimension
{
cout<<"The stack is full: we have already NMAX elements!\n";
//exit the function without making anything
return;
}
/* add an element=> the index of the last element of the stack Array
increases and put the value of the new element in the stack array */
stackArray[++topLevel] = x;
}
int isEmpty()
{
//returns 1, if topLevel>=0, meaning the stack array has elements
// returns 0, otherwise
return (topLevel < 0);
}
T pop() // extracts and element from the stack array and returns the new top
{
if (isEmpty())
{
// the extraction is made only if the array is not empty
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel--]; // the topLevel decreases and the new top is changed
//difference return stackArray[--topLevel] ?
}
T peek()
{
// returns the top of the stack
if (isEmpty())
{
// the extraction is made only if the array is not empty
cout<<"The stack is empty! \n";
T x;
return x;
}
return stackArray[topLevel];
}
int getTopLevel()
{
return topLevel;
}
Stack()
{ // constructor
topLevel = -1; //the stack is empty in the beginning
}
};