同时使用一个栈和一个队列
1: #include "stdio.h"
2: #define STACK_INIT_SIZE 20
3: #define STACKINCREMENT 10
4:
5:
6: typedef char ElemType; /*将char类型定义为ElemType*/
7:
8: typedef struct QNode { /*定义队列结点类*/
9: ElemType data;
10: struct QNode *next;
11: } QNode , *QueuePtr;
12:
13: typedef struct { /*定义一个链队列*/
14: QueuePtr front; /*队头指针*/
15: QueuePtr rear; /*队尾指针*/
16: } LinkQueue;
17:
18: typedef struct { /*定义一个栈类型*/
19: ElemType *base;
20: ElemType *top;
21: int stacksize;
22: } sqStack;
23:
24:
25: void initQueue(LinkQueue *q) {
26: /*初始化一个空队列*/
27: q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); /*创建一个头结点,队头队尾指针 指向该结点*/
28:
29: if( !q->front) exit(0); /*创建头结点失败*/
30:
31: q->front->next = NULL; /*头结点指针域置NULL*/
32: }
33:
34: void EnQueue(LinkQueue *q, ElemType e) { /*入队列操作*/
35: QueuePtr p;
36: p = (QueuePtr)malloc(sizeof(QNode)); /*创建一个队列元素结点*/
37:
38: if( !q->front) exit(0); /*创建头结点失败*/
39:
40: p->data = e; /*将元素e入队列*/
41: p->next = NULL; /*修改队尾指针*/
42: q->rear ->next = p;
43: q->rear = p;
44: }
45:
46: void DeQueue(LinkQueue *q, ElemType *e) {
47: /*如果队列q不为空,删除q的队头元素,用e返回其值*/
48: QueuePtr p;
49:
50: if(q->front == q->rear) return; /*队列为空,返回*/
51:
52: p = q->front->next;
53: *e = p->data;
54: q->front->next = p->next;
55:
56: if(q->rear == p) q->rear = q->front; /*如果队头就是队尾,则修改队尾指针*/
57:
58: free(p);
59: }
60:
61: void initStack(sqStack *s)
62: {
63: /*内存中开辟一段连续空间作为栈空间,首地址赋值给s->base*/
64: s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
65:
66: if(!s->base) exit(0); /*分配空间失败*/
67:
68: s->top = s->base; /*最开始,栈顶就是栈底*/
69: s->stacksize = STACK_INIT_SIZE; /*最大容量为STACK_INIT_SIZE */
70: }
71:
72: void Push(sqStack *s, ElemType e) { /*入栈操作*/
73: if(s->top - s->base >= s->stacksize) {
74: /*栈满,追加空间*/
75: s->base = (ElemType *)realloc(s->base, (s->stacksize +
76: STACKINCREMENT) * sizeof(ElemType));
77:
78: if(!s->base) exit(0); /*存储分配失败*/
79:
80: s->top = s->base + s->stacksize;
81: s->stacksize = s->stacksize + STACKINCREMENT; /*设置栈的最大容量*/
82: }
83:
84: *(s->top) = e; /*放入数据*/
85: s->top++;
86: }
87:
88: void Pop(sqStack *s , ElemType *e) { /*出栈操作*/
89: if(s->top == s->base) return; /*将栈顶元素弹出*/
90:
91: *e = *--(s->top); /*修改栈顶指针*/
92: }
93:
94: int StackLen(sqStack s) { /*获得栈s的大小*/
95: return (s.top - s.base) ;
96: }
97:
98:
99: int main()
100: {
101: sqStack s;
102: LinkQueue q;
103: ElemType e, r1, r2;
104: int flag = 1, i, len;
105:
106: initQueue(&q); /*初始化一个队列*/
107: initStack(&s); /*初始化一个栈*/
108: printf("Please input a string ,type '@' for end\n");
109: scanf("%c", &e);
110:
111: while(e != '@') { /*输入待判断的字符序列*/
112: Push(&s, e);
113: EnQueue(&q, e);
114: scanf("%c", &e);
115: }
116:
117: len = StackLen(s) / 2; /*获得字符序列的长度*/
118:
119: for(i = 0; i < len; i++)
120: {
121: Pop(&s, &r1); /*出栈操作,由r1将栈顶元素返回*/
122: DeQueue(&q, &r2); /*出队列操作,由r2将队头元素返回*/
123:
124: if(r1 != r2) {
125: flag = 0;
126: break;
127: }
128: }
129:
130: if(flag == 1)printf("It is a circle string.\n");
131: else printf("It is not a circle string.\n") ;
132:
133: return 0;
134:
135: }
136: