This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.h
99 lines (81 loc) · 2.31 KB
/
stack.h
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
/****************************************/
// QT Paint
// File: stack.h
// Author: Hong Joon Choi
// Date: 25.Mar.2015
// Description: the header file
// -- Stack class declaration + definition. Stack implemented by array.
// template implementation of stack data structure for multiple-level undo and redo
//
// NOTE: pop() function returns pointer of an object, and it DOES NOT automatically release memory. Attention is required when using pop()
// NOTE: push() function makes a copy of an object and stores inside the stack.
/****************************************/
#include <iostream>
#ifndef _STACK_H
#define _STACK_H
template <class Type>
class Stack
{
public:
/** constructor and destructor */
Stack() // 1 level for default
{
max_size=1;
items = new Type*[max_size];
top_index = -1;
}
Stack(int max)
{
max_size=max;
items = new Type*[max_size];
top_index = -1;
}
~Stack()
{
for (int i=0; i<= top_index; i++)
delete items[i];
delete[] items;
}
/** reset all data members */
void reset()
{
for (int i=0; i <= top_index; i++)
{
delete items[i];
items[i] = NULL;
//std::cout << " one item deleted from stack!" << std::endl;
}
top_index = -1;
}
/** push, pop, isEmpty, isFull */
void push(Type item) // add one object item to stack. IF STACK IS FULL, ITEM AT THE BOTTOM IS DELETED BEFORE NEW ONE GETS ADDED.
{
if (!isFull())
items[++top_index] = new Type(item);
else
{
//std::cout << " one item deleted from stack!" << std::endl;
delete items[0];
for (int i=0; i < top_index; i++)
items[i] = items[i+1];
items[top_index] = new Type(item);
}
//std::cout << " one item added to stack!" << std::endl;
}
Type* pop() // returns pointer of top object from the stack.
{
//std::cout << " stack.pop() called. Returned item as pointer" << std::endl;
if (!isEmpty())
return items[top_index--];
else
return NULL;
}
bool isEmpty() const{return top_index == -1;}
bool isFull() const{return top_index == max_size - 1;}
int size() const{ return top_index+1;}
private:
Type** items;
int top_index;
int max_size;
};
#endif