码迷,mamicode.com
首页 > 其他好文 > 详细

两个栈实现一个队列

时间:2017-11-18 16:01:28      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:else   stdio.h   span   两个栈   sem   struct   null   tac   基本   

全部代码

  基本思想:栈1用来添加(直接添加即可),栈2用来删除(先判断栈2是否是空,如果是空,把栈1的元素弹出,添加到栈2中,然后弹出栈2的栈顶元素,作为出队的元素;如果栈2非空,直接弹出栈2的栈顶元素,作为出队的元素即可)。

  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 }MyStack;
 10 
 11 typedef struct node2
 12 {
 13     int nCount;
 14     MyStack *pTop;
 15 }Stack;
 16 
 17 void s_Init(Stack **ppStack)
 18 {
 19     assert(ppStack!=NULL);
 20 
 21     *ppStack = (Stack *)malloc(sizeof(Stack));
 22     if(NULL == *ppStack)
 23     {
 24         printf("*ppStack空间分配失败!\n");
 25         exit(-1);
 26     }
 27     (*ppStack)->nCount = 0;
 28     (*ppStack)->pTop = NULL;
 29 }
 30 
 31 void s_Push(Stack *pStack, int nNum)
 32 {
 33     MyStack *pTemp = NULL;
 34 
 35     //栈存在
 36     assert(pStack!=NULL);
 37 
 38     //临时节点申请空间
 39     pTemp = (MyStack *)malloc(sizeof(MyStack));
 40     if(NULL == pTemp)
 41     {
 42         printf("pTemp空间分配失败!\n");
 43         exit(-1);
 44     }
 45     pTemp->nValue = nNum;
 46     pTemp->pNext = NULL;
 47 
 48     //头添加
 49     //临时节点的下一个是头节点
 50     pTemp->pNext = pStack->pTop;
 51     //新节点是新栈顶
 52     pStack->pTop = pTemp;
 53 
 54     //更新栈内元素
 55     ++pStack->nCount;
 56 }
 57 
 58 int s_Pop(Stack *pStack)
 59 {
 60     int nNum;
 61     MyStack *pDel = NULL;
 62 
 63     assert(pStack!=NULL && pStack->pTop!=NULL);
 64 
 65     //标记  要删除的节点
 66     pDel = pStack->pTop;
 67     nNum = pStack->pTop->nValue;
 68     //栈顶指针下移
 69     pStack->pTop = pStack->pTop->pNext;
 70     //释放空间
 71     free(pDel);
 72     pDel = NULL;
 73 
 74     //更新栈内元素
 75     --pStack->nCount;
 76 
 77     return nNum;
 78 }
 79 
 80 int s_IsEmpty(Stack *pStack)
 81 {
 82     assert(pStack!=NULL);
 83 
 84     return 0==pStack->nCount ? 1:0;
 85 }
 86 
 87 typedef struct node3
 88 {
 89     int nCount;
 90     Stack *pStack1;
 91     Stack *pStack2;
 92 }Queue;
 93 
 94 void q_Init(Queue **ppQueue)
 95 {
 96     assert(ppQueue!=NULL);
 97 
 98     *ppQueue = (Queue *)malloc(sizeof(Queue));
 99     if(NULL == *ppQueue)
100     {
101         printf("*ppQueue分配空间失败!\n");
102         exit(-1);
103     }
104     (*ppQueue)->nCount = 0;
105     (*ppQueue)->pStack1 = NULL;
106     (*ppQueue)->pStack2 = NULL;
107 
108     //申请辅助栈
109     s_Init(&(*ppQueue)->pStack1);
110     s_Init(&(*ppQueue)->pStack2);
111 }
112 
113 void q_Push(Queue *pQueue, int nNum)
114 {
115     //队列存在
116     assert(pQueue!=NULL && pQueue->pStack1!=NULL && pQueue->pStack2!=NULL);
117 
118     s_Push(pQueue->pStack1, nNum);
119 
120     //更新队列元素
121     ++pQueue->nCount;
122 }
123 
124 int q_Pop(Queue *pQueue)
125 {
126     int nNum;
127 
128     //队列存在且队列非空
129     assert(pQueue!=NULL && pQueue->nCount!=0 && pQueue->pStack1!=NULL && pQueue->pStack2!=NULL);
130 
131     if(!s_IsEmpty(pQueue->pStack2))
132     {
133         nNum = s_Pop(pQueue->pStack2);
134     }
135     else
136     {
137         while(pQueue->pStack1->nCount > 0)
138         {
139             s_Push(pQueue->pStack2, s_Pop(pQueue->pStack1));
140         }
141         nNum = s_Pop(pQueue->pStack2);
142     }
143 
144     //更新队列元素
145     --pQueue->nCount;
146 
147     return nNum;
148 }
149 
150 int main(void)
151 {
152     Queue *pQueue = NULL;
153 
154     //初始化
155     q_Init(&pQueue);
156 
157     //添加
158     q_Push(pQueue, 1);
159     q_Push(pQueue, 2);
160     q_Push(pQueue, 3);
161     q_Push(pQueue, 4);
162 
163     //弹出
164     printf("%d ", q_Pop(pQueue));
165     printf("%d ", q_Pop(pQueue));
166     printf("%d ", q_Pop(pQueue));
167     printf("%d ", q_Pop(pQueue));
168 
169     return 0;
170 }

 

两个栈实现一个队列

标签:else   stdio.h   span   两个栈   sem   struct   null   tac   基本   

原文地址:http://www.cnblogs.com/chen-cai/p/7856774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!