用队列实现栈和用栈实现队列
用队列实现栈
typedef int QDataType;
typedef struct QListNode
{
struct QListNode* next;
QDataType data;
}QNode;
typedef struct Queue
{
QNode* front;
QNode* rear;
}Queue;
void QueueInit(Queue* q)
{
assert(q);
q->front = NULL;
q->rear = NULL;
}
int QueueEmpty(Queue* q)
{
return q->front == NULL;
}
void QueuePush(Queue* q, QDataType data)
{
assert(q);
QNode* newnode = (QNode*)malloc(sizeof(QNode));
newnode->data = data;
newnode->next = NULL;
if (q->front == NULL)
{
q->front = q->rear = newnode;
}
q->rear->next = newnode;
q->rear = newnode;
}
void QueuePop(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
QNode* next = q->front->next;
free(q->front);
q->front = next;
if (q->front == NULL)
{
q->rear = NULL;
}
}
QDataType QueueFront(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->front->data;
}
QDataType QueueBack(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
return q->rear->data;
}
int QueueSize(Queue* q)
{
assert(q);
QNode* cur = q->front;
int size = 0;
while (cur)
{
cur = cur->next;
size++;
}
return size;
}
void QueueDestroy(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
QNode* cur = q->front;
while (cur)
{
QNode* next = cur->next;
free(cur);
cur = next;
}
q->front = q->rear = NULL;
}
void QueuePrint(Queue* q)
{
assert(q);
assert(!QueueEmpty(q));
QNode* cur = q->front;
while (cur)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
typedef struct {
Queue q1;
Queue q2;
} MyStack;
MyStack* myStackCreate() {
MyStack*st=(MyStack*)malloc(sizeof(MyStack));
QueueInit(&st->q1);
QueueInit(&st->q2);
return st;
}
void myStackPush(MyStack* obj, int x) {
if(!QueueEmpty(&obj->q1))
{
QueuePush(&obj->q1,x);
}
else
{
QueuePush(&obj->q2,x);
}
}
int myStackPop(MyStack* obj) {
Queue *nonemptyQ=&obj->q2;
Queue*emptyQ=&obj->q1;
if(!QueueEmpty(&obj->q1))
{
nonemptyQ=&obj->q2;
emptyQ=&obj->q2;
}
while(QueueSize(nonemptyQ)>1)
{
QueuePush(emptyQ,QueueFront(nonemptyQ));
QueuePop(nonemptyQ);
}
int top=QueueFront(nonemptyQ);
QueuePop(nonemptyQ);
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) {
QueueDestroy(&obj->q1);
QueueDestroy(&obj->q2);
free(obj);
}
用栈实现队列
ypedef int STDataType;
typedef struct Stack
{
STDataType *a;
int top;
int capacity;
}ST;
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = realloc(ps->a, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity = newcapacity;
}
}
ps->a[ps->top] = x;
ps->top++;
}
void StackDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackPop(ST* ps)
{
assert(ps);
assert(ps->top>0);
ps->top--;
}
STDataType StackTop(ST* ps)
{
assert(ps);
assert(ps->top>0);
return ps->a[ps->top - 1];
}
bool StackEmpty(ST* ps)
{
return ps->top == 0;
}
void StackPrint(ST* ps)
{
assert(ps);
while (!StackEmpty(ps))
{
printf("%d ", StackTop(ps));
StackPop(ps);
}
}
typedef struct {
ST PushST;
ST PopST;
} MyQueue;
MyQueue* myQueueCreate() {
MyQueue*q=(MyQueue*)malloc(sizeof(MyQueue));
StackInit(&q->PopST);
StackInit(&q->PushST);
return q;
}
void myQueuePush(MyQueue* obj, int x) {
StackPush(&obj->PushST, x);
}
int myQueuePop(MyQueue* obj) {
if(StackEmpty(&obj->PopST))
{
while(!StackEmpty(&obj->PushST))
{
StackPush(&obj->PopST,StackTop(&obj->PushST));
StackPop(&obj->PushST);
}
}
int top=StackTop(&obj->PopST);
StackPop(&obj->PopST);
return top;
}
int myQueuePeek(MyQueue* obj) {
if(StackEmpty(&obj->PopST))
{
while(!StackEmpty(&obj->PushST))
{
StackPush(&obj->PopST,StackTop(&obj->PushST));
StackPop(&obj->PushST);
}
}
return StackTop(&obj->PopST);
}
bool myQueueEmpty(MyQueue* obj) {
return StackEmpty(&obj->PopST)&&StackEmpty(&obj->PushST);
}
void myQueueFree(MyQueue* obj) {
StackDestroy(&obj->PushST);
StackDestroy(&obj->PopST);
free(obj);
}
循环队列
typedef struct {
int *a;
int front;
int rear;
int k;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
MyCircularQueue *q=( MyCircularQueue*)malloc(sizeof( MyCircularQueue));
q->a=(int*)malloc(sizeof(int)*(k+1));
q->front=q->rear=0;
q->k=k;
return q;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if((obj->rear+1)%(obj->k+1)==obj->front)
{
return false;
}
obj->a[obj->rear]=value;
obj->rear++;
obj->rear=(obj->rear)%(obj->k+1);
return true;
}
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
if((obj->front)==(obj->rear) )
{
return false;
}
obj->front++;
obj->front=(obj->front)%(obj->k+1);
return true;
}
int myCircularQueueFront(MyCircularQueue* obj) {
if((obj->front)==(obj->rear))
{
return -1;
}
return obj->a[obj->front];
}
int myCircularQueueRear(MyCircularQueue* obj) {
if((obj->front)==(obj->rear))
{
return -1;
}
if(obj->rear==0)
{
return obj->a[obj->k];
}
else
{
return obj->a[obj->rear-1];
}
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
return (obj->front)==(obj->rear);
}
bool myCircularQueueIsFull(MyCircularQueue* obj) {
return (obj->rear+1)%(obj->k+1)==obj->front;
}
void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
free(obj);
}