1. 程式人生 > >兩個棧實現一個隊列

兩個棧實現一個隊列

val spa stdio.h == sizeof 臨時 tac %d 節點

全部代碼

  基本思想:棧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 }

兩個棧實現一個隊列