-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizeArrayStack.cpp
More file actions
112 lines (95 loc) · 2.18 KB
/
ResizeArrayStack.cpp
File metadata and controls
112 lines (95 loc) · 2.18 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
using namespace std;
template <class Type>
class ResizeArrayStack
{
private:
Type *pItem;
int ArrayNum;
int ItemNum;
public:
ResizeArrayStack();
~ResizeArrayStack();
void ResizeArray(int size);
bool IsEmpty();
void Push(Type item);
Type Pop();
Type GetTop();
void Display();
};
template <class Type>
ResizeArrayStack<Type>::ResizeArrayStack()
{
pItem = new Type[1];
ArrayNum = 1;
ItemNum = 0;
}
template<class Type>
ResizeArrayStack<Type>::~ResizeArrayStack()
{
delete[] pItem;
}
template<class Type>
void ResizeArrayStack<Type>::ResizeArray(int size)
{
Type *pCopyItem = new Type[size];
for(int i=0;i<ItemNum;i++)
pCopyItem[i] = pItem[i];
pItem = pCopyItem;
ArrayNum = size;
}
template<class Type>
bool ResizeArrayStack<Type>::IsEmpty()
{
return (ItemNum == 0);
}
template<class Type>
void ResizeArrayStack<Type>::Push(Type item)
{
if(ItemNum == ArrayNum)
ResizeArray(ArrayNum*2);
pItem[ItemNum++] = item;
}
template<class Type>
Type ResizeArrayStack<Type>::Pop()
{
if((ItemNum > 0) && (ItemNum == ArrayNum/4))
ResizeArray(ArrayNum/2);
Type item = pItem[--ItemNum];
//pItem[ItemNum] = NULL;
return item;
}
template<class Type>
Type ResizeArrayStack<Type>::GetTop()
{
return pItem[ItemNum-1];
}
template <class Type>
void ResizeArrayStack<Type>::Display()
{
for(int i=0;i<ItemNum;i++)
cout << "current node is " << pItem[i] << endl;
}
int main()
{
ResizeArrayStack<int> oResizeArrayStack;
oResizeArrayStack.Push(13);
oResizeArrayStack.Push(15);
oResizeArrayStack.Push(22);
oResizeArrayStack.Pop();
oResizeArrayStack.Display();
ResizeArrayStack<float> oFloatStack;
oFloatStack.Push(2.3);
oFloatStack.Push(2.5);
oFloatStack.Pop();
oFloatStack.Display();
ResizeArrayStack<string> oStringStack;
oStringStack.Push("hello ");
oStringStack.Push("world ");
oStringStack.Push("newbee");
oStringStack.GetTop();
oStringStack.Pop();
oStringStack.Display();
return 1;
}