两个队列实现一个栈
全部代码
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4
5 typedef struct node
6 {
7 int nValue;
8 struct node *pNext;
9 }MyQueue;
10
11 typedef struct node2
12 {
13 int nCount;
14 MyQueue *pHead;
15 MyQueue *pTail;
16 }Queue;
17
18 void q_Init(Queue **ppQueue)
19 {
20 assert(ppQueue!=NULL);
21
22 *ppQueue = (Queue *)malloc(sizeof(Queue));
23 if(NULL == *ppQueue)
24 {
25 printf("*ppQueue分配空间失败!\n");
26 exit(-1);
27 }
28 (*ppQueue)->nCount = 0;
29 (*ppQueue)->pHead = NULL;
30 (*ppQueue)->pTail = NULL;
31 }
32
33 //尾添加
34 void q_Push(Queue *pQueue, int nNum)
35 {
36 MyQueue *pTemp = NULL;
37
38 //队列存在
39 assert(pQueue!=NULL);
40
41 pTemp = (MyQueue *)malloc(sizeof(MyQueue));
42 if(NULL == pTemp)
43 {
44 printf("pTemp空间分配失败!\n");
45 exit(-1);
46 }
47 pTemp->nValue = nNum;
48 pTemp->pNext = NULL;
49
50 //队列空
51 if(NULL == pQueue->pHead)
52 {
53 pQueue->pHead = pTemp;
54 }
55 else
56 {
57 pQueue->pTail->pNext = pTemp;
58 }
59 pQueue->pTail = pTemp;
60
61 //更新队列元素
62 ++pQueue->nCount;
63 }
64
65 //头删除
66 int q_Pop(Queue *pQueue)
67 {
68 int nNum;
69 MyQueue *pDel = NULL;
70
71 //队列存在且队列非空
72 assert(pQueue!=NULL && pQueue->pHead!=NULL);
73
74 pDel = pQueue->pHead;
75 nNum = pDel->nValue;
76 pQueue->pHead = pQueue->pHead->pNext;
77 free(pDel);
78 pDel = NULL;
79
80 //更新队列元素
81 --pQueue->nCount;
82
83 //队列在元素删除后为空 尾置空
84 if(0 == pQueue->nCount)
85 {
86 pQueue->pTail = NULL;
87 }
88
89 return nNum;
90 }
91
92 int q_IsEmpty(Queue *pQueue)
93 {
94 //队列存在
95 assert(pQueue!=NULL);
96
97 return 0==pQueue->nCount?1 : 0;
98 }
99
100 typedef struct node3
101 {
102 int nCount;
103 Queue *pQueue1;
104 Queue *pQueue2;
105 }Stack;
106
107 void s_Init(Stack **ppStack)
108 {
109 assert(ppStack!=NULL);
110
111 *ppStack = (Stack *)malloc(sizeof(Stack));
112 if(NULL == *ppStack)
113 {
114 printf("*ppStack空间分配失败!\n");
115 exit(-1);
116 }
117 (*ppStack)->nCount = 0;
118 (*ppStack)->pQueue1 = NULL;
119 (*ppStack)->pQueue2 = NULL;
120
121 //申请两个队列
122 q_Init(&(*ppStack)->pQueue1);
123 q_Init(&(*ppStack)->pQueue2);
124 }
125
126 void s_Push(Stack *pStack, int nNum)
127 {
128 //栈存在
129 assert(pStack!=NULL && pStack->pQueue1!=NULL && pStack->pQueue2!=NULL);
130
131 if(!q_IsEmpty(pStack->pQueue1))
132 {
133 q_Push(pStack->pQueue1, nNum);
134 }
135 else
136 {
137 q_Push(pStack->pQueue2, nNum);
138 }
139
140 //更新栈内元素
141 ++pStack->nCount;
142 }
143
144 int s_Pop(Stack *pStack)
145 {
146 int nNum;
147
148 //栈存在且栈非空
149 assert(pStack!=NULL && pStack->nCount!=0 && pStack->pQueue1!=NULL && pStack->pQueue2!=NULL);
150
151 if(!q_IsEmpty(pStack->pQueue1))
152 {
153 while(pStack->pQueue1->nCount > 1)
154 {
155 q_Push(pStack->pQueue2, q_Pop(pStack->pQueue1));
156 }
157 nNum = q_Pop(pStack->pQueue1);
158 }
159 else
160 {
161 while(pStack->pQueue2->nCount > 1)
162 {
163 q_Push(pStack->pQueue1, q_Pop(pStack->pQueue2));
164 }
165 nNum = q_Pop(pStack->pQueue2);
166 }
167
168 //更新栈内元素
169 --pStack->nCount;
170
171 return nNum;
172 }
173
174 int main(void)
175 {
176 Stack *pStack = NULL;
177
178 s_Init(&pStack);
179 //添加
180 s_Push(pStack, 1);
181 s_Push(pStack, 2);
182 s_Push(pStack, 3);
183 s_Push(pStack, 4);
184 //弹出
185 printf("%d ", s_Pop(pStack));
186 printf("%d ", s_Pop(pStack));
187 printf("%d ", s_Pop(pStack));
188 printf("%d ", s_Pop(pStack));
189
190 return 0;
191 }