用队列实现栈
225. 用队列实现栈 - 力扣(LeetCode) (leetcode-cn.com)
目的 :用队列实现栈,从先进先出——>先进后出,
1234这四个数据依次从队列1的队尾进入,要让4先出,一个队列是无法实现的,所以这里的队列2就排上用场了,我们可以利用队列2来进行导数据。
将123依次由队列2的队尾进入到队列2中,此时队列1中还剩一个4,将4弹出,同理,再将12依次进入到队列1中,将3弹出……
也就是说 。
出数据把不为空的 队列数据向为空的队列中导,知道剩最后一个。
入数据向不为空的队列入。
始终保持一个队列为空,一个不为空。
队列的实现 ——队列的实现——【线性表】之队列_半生瓜のblog-CSDN博客
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 typedef int QueueDataType;typedef struct QueueNode { struct QueueNode * next ; QueueDataType data; }QueueNode; typedef struct Queue { QueueNode* tail; QueueNode* head; }Queue; void QueueInit (Queue* pq) { assert(pq); pq->tail = pq->head = NULL ; } void QueueDestory (Queue* pq) { assert(pq); QueueNode* cur = pq->head; while (cur) { QueueNode* curNext = cur->next; free (cur); cur = curNext; } pq->head = pq->tail = NULL ; } void QueuePush (Queue* pq, QueueDataType x) { assert(pq); QueueNode* newnode = (QueueNode*)malloc (sizeof (QueueNode)); if (newnode == NULL ) { printf ("malloc is fail\n" ); exit (-1 ); } newnode->data = x; newnode->next = NULL ; if (pq->tail == NULL ) { pq->head = pq->tail = newnode; } else { pq->tail->next = newnode; pq->tail = newnode; } } void QueuePop (Queue* pq) { assert(pq); assert(pq->head); if (pq->head->next == NULL ) { free (pq->head); pq->head = pq->tail = NULL ; } else { QueueNode* nextNode = pq->head->next; free (pq->head); pq->head = nextNode; } } QueueDataType QueueFront (Queue* pq) { assert(pq); assert(pq->head); return pq->head->data; } QueueDataType QueueBack (Queue* pq) { assert(pq); assert(pq->head); return pq->tail->data; } bool QueueEmpty (Queue* pq) { assert(pq); return pq->head == NULL ; } int QueueSize (Queue* pq) { assert(pq); int size = 0 ; QueueNode* cur = pq->head; while (cur) { size++; cur = cur->next; } return size; } typedef struct { Queue q1; Queue q2; } MyStack; MyStack* myStackCreate () { MyStack* ps = (MyStack*)malloc (sizeof (MyStack)); if (ps == NULL ) { printf ("malloc is fail!\n" ); exit (-1 ); } QueueInit(&ps->q1); QueueInit(&ps->q2); return ps; } void myStackPush (MyStack* obj, int x) { if (!QueueEmpty(&obj->q1)) { QueuePush(&obj->q1,x); } else { QueuePush(&obj->q2,x); } } int myStackPop (MyStack* obj) { Queue* emptyQ = &obj->q1; Queue* noemptyQ = &obj->q2; if (!QueueEmpty(&obj->q1)) { emptyQ = &obj->q2; noemptyQ = &obj->q1; } while (QueueSize(noemptyQ)>1 ) { QueuePush(emptyQ,QueueFront(noemptyQ)); QueuePop(noemptyQ); } int top = QueueFront(noemptyQ); QueuePop(noemptyQ); return top; } int myStackTop (MyStack* obj) { if (!QueueEmpty(&obj->q1)) { return QueueBack(&obj->q1); } else { return QueueBack(&obj->q2); } } bool myStackEmpty (MyStack* obj) { return QueueEmpty(&obj->q1) && QueueEmpty(&obj->q2); } void myStackFree (MyStack* obj) { QueueDestory(&obj->q1); QueueDestory(&obj->q2); free (obj); }