-
Notifications
You must be signed in to change notification settings - Fork 0
/
The_stack_and_queue.cpp
129 lines (110 loc) · 2.36 KB
/
The_stack_and_queue.cpp
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//the queue and stack
#define MAXSIZE 1000
#define ERROR false
#define OK true
#include <iostream>
#include <string>
using namespace std;
//define ElemType as int,
//but it can be any type such as double long
typedef int ElemType;
typedef bool Status;
void Initstack(SqStack *S);
bool isempty(SqStack S);
Status Push(SqStack *S,ElemType e);
Status Pop(SqStack *S,ElemType e);
Status GetTop(SqStack *S,ElemType *e);
Status InitQueue(SqQueue *Q);
bool whetherempty(SqQueue *Q);
int Queuelenth(SqQueue Q){
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
typedef struct{
ElemType data[MAXSIZE];
int front;
int rear;
}SqQueue;
//top means the number of the top of stack
typedef struct{
ElemType data[MAXSIZE];
int top;
}SqStack;
//make a stack NODE
typedef struct StackNode{
ElemType data;
struct StackNode *next;
}StackNode,*LinkStackPrt;
//make a stack link chain
typedef struct LinkStack{
LinkStackPrt top;
int count;
}LinkStack;
//these are of sighficant importance
//the link push the node together
//start the stack
void Initstack(SqStack *S){
S->top=-1;
}
//is stack empty
bool isempty(SqStack S){
if(S.top==-1){
return true;
}
else{
return false;
}
}
//push the elem into the stack
Status Push(SqStack *S,ElemType e){
if(S->top==MAXSIZE-1){
return ERROR;
}
++S->top;
S->data[S->top]=e;
return OK;
}
//pop the elem of stack out
Status Pop(SqStack *S,ElemType *e){
if(S->top==-1){
return ERROR;
}
*e=S->data[S->top];
--S->top;
return OK;
}
//read the top of stack
Status GetTop(SqStack *S,ElemType *e){
if(S->top==-1){
return ERROR;
}
*e=S->data[S->top];
return OK;
}
//push the stack into the linknode
Status Push(LinkStack *S,ElemType e){
LinkStackPrt p=(LinkStackPrt)malloc(sizeof(StackNode));
p->data=e;
p->next=S->top;
S->top=p;
S->count++;
return OK;
}
//pop the stacknode out of stacklink
Status Pop(LinkStack *S,ElemType *e)
Status InitQueue(SqQueue *Q){
Q->front=0;
Q->rear=0;
return OK;
}
bool whetherempty(SqQueue *Q){
if(Q->front==Q->rear){
return true;
}
else{
return false;
}
}
int main(){
cout<<"hello world"<<endl;
return 0;
}