1 //实验课内容
2 #include<stdio.h>
3 #include<stdlib.h>
4 #include<conio.h>
5 #define TRUE 1
6 #define FALSE 0
7 #define OK 1
8 #define ERROR 0
9 #define INFEASIBLE -1
10 #define OVERFLOW -2
11 #define INIT_SIZE_STACK 100
12 #define STACKINCREMENT 10
13 typedef int Status;
14 typedef char ElemType;
15
16 typedef struct
17 {
18 ElemType *top;
19 ElemType *base;
20 int stacksize;
21 }SqStack;
22
23 //构造一个空栈
24 Status InitStack(SqStack &S)
25 {
26 S.base = (ElemType *)malloc(INIT_SIZE_STACK*sizeof(ElemType));
27 if(!S.base) exit(OVERFLOW);
28 S.top = S.base; //栈空的标志
29 S.stacksize = INIT_SIZE_STACK;
30 return OK;
31 }
32
33 //入栈
34 Status Push(SqStack &S,ElemType e)
35 {//向栈顶插入元素e
36 if(S.top - S.base >= S.stacksize)
37 {//栈已满,增加空间分配
38 S.base = (ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType));
39 if(!S.base) exit(OVERFLOW);
40 S.top = S.base+S.stacksize;
41 S.stacksize +=STACKINCREMENT;
42 }
43 //*S.top = e;
44 //S.top++;
45 *(S.top++) = e;
46 printf("s.[%d]=%c\n",S.top-S.base-1,e);
47 return OK;
48 }
49
50 //删除栈顶元素
51 Status Pop(SqStack &S,ElemType &e)
52 {
53 if(S.top == S.base) return ERROR;
54 //S.top--;
55 //e = *S.top;
56 e = *(--S.top);
57 return OK;
58 }
59
60 //判断栈是否为空
61 int StackEmpty(SqStack S)
62 {
63 if(S.top == S.base)
64 return 1;
65 else
66 return 0;
67 }
68
69 //出栈
70 Status GetTop(SqStack S)
71 {
72 ElemType e;
73 if(S.top == S.base) return ERROR;
74 e = *(S.top-1);
75 return e;
76 }
77
78 #define QueueSize 100
79 //循环队列的存储结构
80 typedef struct
81 {
82 ElemType *base;
83 int front,rear;
84 }SeqQueue;
85
86 //循环队列的创建
87 Status InitQueue(SeqQueue &Q)
88 {
89 Q.base = (ElemType *)malloc(QueueSize*sizeof(ElemType));
90 if(!Q.base)
91 exit(OVERFLOW);
92 Q.front = Q.rear = 0;
93 return OK;
94 }
95
96 //循环队列的插入
97 Status EnQueue(SeqQueue &Q,ElemType e)
98 {
99 if((Q.rear+1)%QueueSize==Q.front)
100 {
101 printf("Queue overflow");
102 return ERROR;
103 }
104 Q.base[Q.rear] = e;
105 Q.rear = (Q.rear+1)%QueueSize;
106 return OK;
107 }
108
109 //循环队列的删除
110 Status DeQueue(SeqQueue &Q,ElemType &e)
111 {
112 if(Q.front == Q.rear)
113 {
114 printf("Queue empty");
115 return ERROR;
116 }
117 e = Q.base[Q.front];
118 Q.front = (Q.front+1)%QueueSize;
119 return OK;
120 }
121
122 //取出循环队列的第一个元素
123 ElemType GetHead(SeqQueue Q)
124 {
125 if(Q.front == Q.rear)
126 {
127 printf("Queue empty");
128 exit(ERROR);
129 }
130 else
131 return Q.base[Q.front];
132 }
133
134 //遍历循环队列
135 Status QueueTraverse(SeqQueue Q)
136 {
137 int p;
138 if(Q.front == Q.rear)
139 {
140 printf("Queue empty");
141 return ERROR;
142 }
143 p = Q.front;
144 do
145 {
146 printf("%2c",Q.base[p]);
147 p = (p+1)%QueueSize;
148 }while(p!=Q.rear);
149 return OK;
150 }
151
152 void main()
153 {
154 SqStack s;
155 SeqQueue q;
156 ElemType ch,e1,e2;
157 int state;
158 InitStack(s); InitQueue(q);
159 printf("input a string endding by#:");
160 scanf("%c",&ch);
161 while(ch!='#')
162 {
163 Push(s,ch);
164 EnQueue(q,ch);
165 scanf("%c",&ch);
166 }
167 printf("\nThe Queue is;");
168 QueueTraverse(q);
169 printf("\n");
170 state = TRUE;
171 while(!StackEmpty(s) && state)
172 {
173 if(GetTop(s)==GetHead(q))
174 {
175 Pop(s,e1);
176 DeQueue(q,e2);
177 }
178 else
179 state = FALSE;
180 }
181 if(state)
182 printf("This string is HuiWen!\n");
183 else
184 printf("The string is not HuiWen!\n");
185 }