栈和队列基本接口的实现
栈
Stack.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int _STdatatype;
typedef struct Stack
{
_STdatatype * _a;
int _top;
int _capacity;
}Stack;
void InitStack(Stack * ps);
void DestoryStack(Stack * ps);
void PushStack(Stack * ps, _STdatatype x);
void PopStack(Stack * ps);
_STdatatype TopStack(Stack * ps);
int isEmptyStack(Stack * ps);
int SizeStack(Stack * ps);
Stack.c
#include"Stack.h"
void InitStack(Stack * ps)
{
assert(ps);
ps->_a = (_STdatatype *)malloc(sizeof(_STdatatype) * 4);
assert(ps->_a);
ps->_top = 0;
ps->_capacity = 4;
}
void DestoryStack(Stack * ps)
{
assert(ps);
if (ps->_a)
{
free(ps->_a);
ps->_a = NULL;
ps->_top = 0;
ps->_capacity = 0;
}
}
void PushStack(Stack * ps, _STdatatype x)
{
assert(ps);
if (ps->_top == ps->_capacity)
{
ps->_a = (_STdatatype *)realloc(ps->_a, sizeof(_STdatatype) * 2 * ps->_capacity);
ps->_capacity *= 2;
}
ps->_a[ps->_top] = x;
ps->_top++;
}
void PopStack(Stack * ps)
{
assert(ps && ps->_top > 0);
ps->_top--;
}
_STdatatype TopStack(Stack * ps)
{
assert(ps && ps->_top > 0);
return ps->_a[ps->_top - 1];
}
int isEmptyStack(Stack * ps)
{
assert(ps);
return ps->_top == 0 ? 0 : 1;
}
int SizeStack(Stack * ps)
{
assert(ps);
return ps->_top;
}
队列
Queue.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int _QTdatatype;
typedef struct QueueNode
{
_QTdatatype _data;
struct QueueNode * next;
}QueueNode;
typedef struct Queue
{
QueueNode * _front;
QueueNode * _back;
}Queue;
QueueNode * BuyQueueNode(_QTdatatype x);
void InitQueue(Queue * pq);
void DestoryQueue(Queue * pq);
void PushQueue(Queue * pq, _QTdatatype x);
void PopQueue(Queue * pq);
_QTdatatype FrontQueue(Queue * pq);
_QTdatatype BackQueue(Queue * pq);
int isEmptyQueue(Queue * pq);
int SizeQueue(Queue * pq);
Queue.c
#include"Queue.h"
QueueNode * BuyQueueNode(_QTdatatype x)
{
QueueNode * node = (QueueNode *)malloc(sizeof(QueueNode));
assert(node);
node->next = NULL;
node->_data = x;
return node;
}
void InitQueue(Queue * pq)
{
assert(pq);
pq->_front = NULL;
pq->_back = NULL;
}
void DestoryQueue(Queue * pq)
{
QueueNode * cur;
assert(pq);
cur = pq->_front;
while (cur)
{
free(cur);
cur = cur->next;
}
free(cur);
pq->_front = pq->_back = NULL;
}
void PushQueue(Queue * pq, _QTdatatype x)
{
QueueNode * node = BuyQueueNode(x);
assert(pq);
if (pq->_front == NULL)
{
pq->_front = node;
pq->_back = node;
}
else
{
pq->_back->next = node;
pq->_back = node;
}
}
void PopQueue(Queue * pq)
{
QueueNode * next;
assert(pq);
next = pq->_front->next;
free(pq->_front);
pq->_front = next;
if (next == NULL)
{
pq->_back = NULL;
}
}
_QTdatatype FrontQueue(Queue * pq)
{
assert(pq);
return pq->_front->_data;
}
_QTdatatype BackQueue(Queue * pq)
{
assert(pq);
return pq->_back->_data;
}
int isEmptyQueue(Queue * pq)
{
assert(pq);
return pq->_front == NULL ? 0 : 1;
}
int SizeQueue(Queue * pq)
{
int size = 0;
QueueNode * cur;
assert(pq);
cur = pq->_front;
while (cur)
{
size++;
cur = cur->next;
}
return size;
}
用两个栈实现一个队列
QueueByTwoStack.h
#pragma once
#include"Stack.h"
typedef struct QueueByTwoStack
{
Stack s1;
Stack s2;
}QueueByTwoStack;
void QueueByTwoStackInit(QueueByTwoStack * qts);
void QueueByTwoStackDestory(QueueByTwoStack * qts);
void QueueByTwoStackPush(QueueByTwoStack * qts, _STdatatype x);
void QueueByTwoStackPop(QueueByTwoStack * qts);
_STdatatype QueueByTwoStackFront(QueueByTwoStack * qts);
int QueueByTwoStackEmpty(QueueByTwoStack * qts);
int QueueByTwoStackSize(QueueByTwoStack * qts);
QueueByTwoStack.c
#include"QueueByTwoStack.h"
void QueueByTwoStackInit(QueueByTwoStack * qts)
{
assert(qts);
InitStack(&qts->s1);
InitStack(&qts->s2);
}
void QueueByTwoStackDestory(QueueByTwoStack * qts)
{
assert(qts);
DestoryStack(&qts->s1);
DestoryStack(&qts->s2);
}
void QueueByTwoStackPush(QueueByTwoStack * qts, _STdatatype x)
{
assert(qts);
PushStack(&qts->s1, x);
}
void QueueByTwoStackPop(QueueByTwoStack * qts)
{
assert(qts);
if (isEmptyStack(&qts->s2) == 0)
{
while (isEmptyStack(&qts->s1))
{
PushStack(&qts->s2, TopStack(&qts->s1));
PopStack(&qts->s1);
}
}
PopStack(&qts->s2);
}
_STdatatype QueueByTwoStackFront(QueueByTwoStack * qts)
{
assert(qts);
if (isEmptyStack(&qts->s2) == 0)
{
while (isEmptyStack(&qts->s1))
{
PushStack(&qts->s2, TopStack(&qts->s1));
PopStack(&qts->s1);
}
}
return TopStack(&qts->s2);
}
int QueueByTwoStackEmpty(QueueByTwoStack * qts)
{
assert(qts);
return isEmptyStack(&qts->s1) | isEmptyStack(&qts->s2);
}
int QueueByTwoStackSize(QueueByTwoStack * qts)
{
assert(qts);
return SizeStack(&qts->s1) + SizeStack(&qts->s2);
}
用两个队列实现一个栈
StackByTwoQueue.h
#pragma once
#include"Queue.h"
typedef struct StackByTwoQueue
{
Queue q1;
Queue q2;
}StackByTwoQueue;
void StackByTwoQueueInit(StackByTwoQueue * stq);
void StackByTwoQueueDestory(StackByTwoQueue * stq);
void StackByTwoQueuePush(StackByTwoQueue * stq, _QTdatatype x);
void StackByTwoQueuePop(StackByTwoQueue * stq);
_QTdatatype StackByTwoQueueTop(StackByTwoQueue * stq);
int StackByTwoQueueEmpty(StackByTwoQueue * stq);
int StackByTwoQueueSize(StackByTwoQueue * stq);
StackByTwoQueue.c
#include"StackByTwoQueue.h"
void StackByTwoQueueInit(StackByTwoQueue * stq)
{
assert(stq);
InitQueue(&stq->q1);
InitQueue(&stq->q2);
}
void StackByTwoQueueDestory(StackByTwoQueue * stq)
{
assert(stq);
DestoryQueue(&stq->q1);
DestoryQueue(&stq->q2);
}
void StackByTwoQueuePush(StackByTwoQueue * stq, _QTdatatype x)
{
assert(stq);
if (isEmptyQueue(&stq->q1) != 0)
{
PushQueue(&stq->q1, x);
}
else
{
PushQueue(&stq->q2, x);
}
}
void StackByTwoQueuePop(StackByTwoQueue * stq)
{
Queue * empty = &stq->q1, *noempty = &stq->q2;
assert(stq);
if (isEmptyQueue(&stq->q1) != 0)
{
empty = &stq->q2;
noempty = &stq->q1;
}
while (SizeQueue(noempty) > 1)
{
PushQueue(empty, FrontQueue(noempty));
PopQueue(noempty);
}
PopQueue(noempty);
}
_QTdatatype StackByTwoQueueTop(StackByTwoQueue * stq)
{
assert(stq);
if (isEmptyQueue(&stq->q1))
{
return BackQueue(&stq->q1);
}
else
{
return BackQueue(&stq->q2);
}
}
int StackByTwoQueueEmpty(StackByTwoQueue * stq)
{
assert(stq);
return isEmptyQueue(&stq->q1) | isEmptyQueue(&stq->q2);
}
int StackByTwoQueueSize(StackByTwoQueue * stq)
{
assert(stq);
return SizeQueue(&stq->q1) + SizeQueue(&stq->q2);
}
测试代码
test.c
#include"Stack.h"
#include"Queue.h"
#include"QueueByTwoStack.h"
#include"StackByTwoQueue.h"
void testStack()
{
Stack s;
InitStack(&s);
PushStack(&s, 1);
PushStack(&s, 2);
PushStack(&s, 3);
PushStack(&s, 4);
PushStack(&s, 5);
PushStack(&s, 6);
printf("%d %d\n", isEmptyStack(&s), SizeStack(&s));
while (isEmptyStack(&s))
{
printf("%d ",TopStack(&s));
PopStack(&s);
}
printf("\n");
printf("%d %d\n", isEmptyStack(&s), SizeStack(&s));
DestoryStack(&s);
}
void testQueue()
{
Queue q;
InitQueue(&q);
PushQueue(&q, 1);
PushQueue(&q, 2);
PushQueue(&q, 3);
PushQueue(&q, 4);
PushQueue(&q, 5);
PushQueue(&q, 6);
printf("%d %d\n", isEmptyQueue(&q), SizeQueue(&q));
while (isEmptyQueue(&q))
{
printf("%d ", FrontQueue(&q));
PopQueue(&q);
}
printf("\n");
printf("%d %d\n", isEmptyQueue(&q), SizeQueue(&q));
DestoryQueue(&q);
}
void testStackByTwoQueue()
{
QueueByTwoStack qts;
QueueByTwoStackInit(&qts);
QueueByTwoStackPush(&qts, 1);
QueueByTwoStackPush(&qts, 2);
QueueByTwoStackPush(&qts, 3);
QueueByTwoStackPush(&qts, 4);
printf("%d %d\n", QueueByTwoStackEmpty(&qts), QueueByTwoStackSize(&qts));
while (QueueByTwoStackEmpty(&qts))
{
printf("%d ",QueueByTwoStackFront(&qts));
QueueByTwoStackPop(&qts);
}
printf("\n");
QueueByTwoStackPush(&qts, 5);
QueueByTwoStackPush(&qts, 6);
printf("%d %d\n", QueueByTwoStackEmpty(&qts), QueueByTwoStackSize(&qts));
while (QueueByTwoStackEmpty(&qts))
{
printf("%d ", QueueByTwoStackFront(&qts));
QueueByTwoStackPop(&qts);
}
printf("\n");
printf("%d %d\n", QueueByTwoStackEmpty(&qts), QueueByTwoStackSize(&qts));
QueueByTwoStackDestory(&qts);
}
void testQueueByTwoStack()
{
StackByTwoQueue stq;
StackByTwoQueueInit(&stq);
StackByTwoQueuePush(&stq, 1);
StackByTwoQueuePush(&stq, 2);
StackByTwoQueuePush(&stq, 3);
StackByTwoQueuePush(&stq, 4);
printf("%d %d\n",StackByTwoQueueEmpty(&stq),StackByTwoQueueSize(&stq));
while (StackByTwoQueueEmpty(&stq))
{
printf("%d ", StackByTwoQueueTop(&stq));
StackByTwoQueuePop(&stq);
}
printf("\n");
StackByTwoQueuePush(&stq, 5);
StackByTwoQueuePush(&stq, 6);
printf("%d %d\n", StackByTwoQueueEmpty(&stq), StackByTwoQueueSize(&stq));
while (StackByTwoQueueEmpty(&stq))
{
printf("%d ", StackByTwoQueueTop(&stq));
StackByTwoQueuePop(&stq);
}
printf("\n");
printf("%d %d\n", StackByTwoQueueEmpty(&stq), StackByTwoQueueSize(&stq));
StackByTwoQueueDestory(&stq);
}
int main()
{
testStack();
testQueue();
testStackByTwoQueue();
testQueueByTwoStack();
return 0;
}