-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-stack.h
More file actions
88 lines (64 loc) · 3 KB
/
dynamic-stack.h
File metadata and controls
88 lines (64 loc) · 3 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef LAB2_DYNAMIC_STACK_H
#define LAB2_DYNAMIC_STACK_H
class DynamicStack {
public:
// Defines the kind of data that the stack will contain
typedef int StackItem;
// Defines a constant that will be used to indicate an empty stack
static const StackItem EMPTY_STACK;
private:
// Befriend so tests have access to variables.
friend class DynamicStackTest;
// MEMBER VARIABLES
// An array of stack items.
StackItem* items_;
// Maximum number of elements allowed in the stack.
unsigned int capacity_;
// Current number of elements in the stack.
unsigned int size_;
// Initial capacity of the array (i.e., the capacity set in the constructor).
// This is used by pop() to determine if we should decrease the capacity.
unsigned int init_capacity_;
// Copy constructor. Declared private so we don't use it incorrectly.
DynamicStack(const DynamicStack& other) {}
// Assignment operator. Declared private so we don't use it incorrectly.
DynamicStack operator=(const DynamicStack& other) {}
public:
// CONSTRUCTORS/DESTRUCTOR
// Default constructor of the class DynamicStack. It uses 16 as the initial
// capacity of the array, and allocates the required memory space for the
// stack. The function appropriately initializes the fields of the created
// empty stack.
DynamicStack();
// Parametric constructor of the class DynamicStack. It allocates the required
// memory space for the stack of the given capacity. The function
// appropriately initializes the fields of the created empty stack.
DynamicStack(unsigned int capacity);
// Destructor of the class DynamicStack. It deallocates the memory space
// allocated for the stack.
~DynamicStack();
// ACCESSORS
// Returns the number of items in the stack.
unsigned int size() const;
// Returns true if the stack is empty and false otherwise.
bool empty() const;
// Returns the value at the top of the stack without removing it. If the
// stack is empty, it returns the EMPTY_STACK constant instead.
StackItem peek() const;
// MUTATORS
// Takes as an argument a StackItem value. If the stack is not full, the value
// is pushed onto the stack. Otherwise, the capacity of the stack is doubled,
// and the item is then pushed onto the resized stack.
void push(StackItem value);
// Removes and returns the top item from the stack as long as the stack is
// not empty. If the number of items remaining in the stack after popping
// is less than or equal to one quarter of the capacity of the array, then
// the array is halved. However, if the new halved capacity is less than
// the initial capacity, then no resizing takes place. Finally, If the stack
// is empty before the pop, the EMPTY STACK constant is returned.
StackItem pop();
// Prints the stack items sequentially and in order, from the top to the
// bottom of the stack.
void print() const;
};
#endif // LAB2_DYNAMIC_STACK_H