-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmystack.cpp
More file actions
65 lines (58 loc) · 1.32 KB
/
mystack.cpp
File metadata and controls
65 lines (58 loc) · 1.32 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
#include "mystack.h"
#include <qDebug.h>
mystack::mystack() {
this->capacity = 1000000;
this->data = (char**)malloc(this->capacity * sizeof(char*));
this->Nodearr = (TreeNode**)malloc(this->capacity * sizeof(TreeNode*));
this->size = 0;
}
void mystack::push(const char *value,TreeNode *Node){
if (this->size < this->capacity) {
this->data[this->getsize()] = _strdup(value); // 使用 _strdup 复制字符串
this->Nodearr[this->getsize()] = Node;
this->size++;
} else {
fprintf(stderr, "Stack overflow\n");
exit(EXIT_FAILURE);
}
}
void mystack::pop(){
if(this->size>0)
{
this->size--;
}
else
{
qDebug()<<"stack is empty";
}
}
char *mystack::getit(){
if(this->size>0)
{
return this->data[this->size-1];
}
else
{
qDebug()<<"There is no item in stack";
}
}
TreeNode *mystack::getnode(){
if(this->size>0)
{
return this->Nodearr[this->size-1];
}
else
{
qDebug()<<"There is no item in stack";
}
}
void mystack::freestack(){
for(size_t i = 0; i< this->capacity;i++)
{
free(this->data[i]);
}
free(this->data);
}
size_t mystack::getsize(){
return this->size;
}