-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInverse_stack.cpp
158 lines (139 loc) · 2.88 KB
/
Inverse_stack.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define N 100
typedef int BOOL;
typedef int ElemType;
typedef struct{
int top;
int maxSize;
ElemType *element;
}Stack;
/*
void Create1(Stack *S, int mSize);
BOOL IsEmpty1(Stack *S);
BOOL IsFULL1(Stack *S);
BOOL Top(Stack *S,ElemType *x);
BOOL Push(Stack *S,ElemType x);
BOOL Pop(Stack *S);
void Create2(Queue *Q,int mSize);
BOOL IsEmpty2(Queue *Q);
BOOL IsFULL2(Queue *Q);
BOOL Front2(Queue *Q,ElemType *x);
BOOL DeQueue(Queue *Q);
void Output(Stack *S,int n);
int Inverse(Stack *S,Queue *Q,int n);
*/
void Create1(Stack *S, int mSize){
S->maxSize=mSize-1;
S->element=(ElemType*)malloc(sizeof(ElemType)*mSize);
S->top=-1;
}
BOOL IsEmpty1(Stack *S){
return S->top==-1;
}
BOOL IsFULL1(Stack *S){
return S->top==S->maxSize;
}
BOOL Top(Stack *S,ElemType *x){
if(IsEmpty1(S))
return FALSE;
*x=S->element[S->top];
return TRUE;
}
BOOL Push(Stack *S,ElemType x){
if(IsFULL1(S))
return FALSE;
S->top++;
S->element[S->top]=x;
return TRUE;
}
BOOL Pop(Stack *S){
if(IsEmpty1(S))
return FALSE;
S->top--;
return TRUE;
}
typedef struct{
int front;
int rear;
int maxSize;
ElemType *element;
}Queue;
void Create2(Queue *Q,int mSize){
Q->maxSize=mSize;
Q->element=(ElemType*)malloc(sizeof(ElemType)*mSize);
Q->front=Q->rear=0;
}
BOOL IsEmpty2(Queue *Q){
return Q->front==Q->rear;
}
BOOL IsFULL2(Queue *Q){
return (Q->rear+1)%Q->maxSize==Q->front;
}
BOOL Front2(Queue *Q,ElemType *x){
if(IsEmpty2(Q))
return FALSE;
*x=Q->element[(Q->front+1)%Q->maxSize];
return TRUE;
}
BOOL EnQueue(Queue *Q,ElemType x){
if(IsFULL2(Q))
return FALSE;
Q->rear=(Q->rear+1)%Q->maxSize;
Q->element[Q->rear]=x;
return TRUE;
}
BOOL DeQueue(Queue *Q){
if(IsEmpty2(Q)){
return FALSE;
}
Q->front=(Q->front+1)%Q->maxSize;
return TRUE;
}
void Output(Stack *S,int n){
int i;
i=S->top;
if(S->top==-1){
printf("NULL");
}
while(S->top!=-1){
printf("%d",S[i--]);
}
printf("\n");
}
int Inverse(Stack *S,Queue *Q,int n){
int i;
ElemType a[N];
for(i=0;i<n;i++){
if(IsEmpty1(S)){
return FALSE;
}
a[i]=S->element[S->top--];
EnQueue(Q,a[i]);
}
for(i=0;i<n;i++){
if(IsEmpty2(Q)){
return FALSE;
}
a[i]=Q->element[Q->front++];
Push(S,a[i]);
}
}
int main(){
int n,i,x;
Stack S;
Queue Q;
scanf("%d",&n);
printf("\n");
Create1(&S,n);
Create2(&Q,n);
for(i=0;i<n;i++){
scanf("%d",&x);
Push(&S,x);
}
Output(&S,n);
Inverse(&S,&Q,n);
Output(&S,n);
}