两个栈实现一个队列

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

全部代码

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct node
{
    int nValue;
    struct node *pNext;
}MyStack;

typedef struct node2
{
    int nCount;
    MyStack *pTop;
}Stack;

void s_Init(Stack **ppStack)
{
    assert(ppStack!=NULL);

    *ppStack = (Stack *)malloc(sizeof(Stack));
    if(NULL == *ppStack)
    {
        printf("*ppStack空间分配失败!
");
        exit(-1);
    }
    (*ppStack)->nCount = 0;
    (*ppStack)->pTop = NULL;
}

void s_Push(Stack *pStack, int nNum)
{
    MyStack *pTemp = NULL;

    //栈存在
    assert(pStack!=NULL);

    //临时节点申请空间
    pTemp = (MyStack *)malloc(sizeof(MyStack));
    if(NULL == pTemp)
    {
        printf("pTemp空间分配失败!
");
        exit(-1);
    }
    pTemp->nValue = nNum;
    pTemp->pNext = NULL;

    //头添加
    //临时节点的下一个是头节点
    pTemp->pNext = pStack->pTop;
    //新节点是新栈顶
    pStack->pTop = pTemp;

    //更新栈内元素
    ++pStack->nCount;
}

int s_Pop(Stack *pStack)
{
    int nNum;
    MyStack *pDel = NULL;

    assert(pStack!=NULL && pStack->pTop!=NULL);

    //标记  要删除的节点
    pDel = pStack->pTop;
    nNum = pStack->pTop->nValue;
    //栈顶指针下移
    pStack->pTop = pStack->pTop->pNext;
    //释放空间
    free(pDel);
    pDel = NULL;

    //更新栈内元素
    --pStack->nCount;

    return nNum;
}

int s_IsEmpty(Stack *pStack)
{
    assert(pStack!=NULL);

    return 0==pStack->nCount ? 1:0;
}

typedef struct node3
{
    int nCount;
    Stack *pStack1;
    Stack *pStack2;
}Queue;

void q_Init(Queue **ppQueue)
{
    assert(ppQueue!=NULL);

    *ppQueue = (Queue *)malloc(sizeof(Queue));
    if(NULL == *ppQueue)
    {
        printf("*ppQueue分配空间失败!
");
        exit(-1);
    }
    (*ppQueue)->nCount = 0;
    (*ppQueue)->pStack1 = NULL;
    (*ppQueue)->pStack2 = NULL;

    //申请辅助栈
    s_Init(&(*ppQueue)->pStack1);
    s_Init(&(*ppQueue)->pStack2);
}

void q_Push(Queue *pQueue, int nNum)
{
    //队列存在
    assert(pQueue!=NULL && pQueue->pStack1!=NULL && pQueue->pStack2!=NULL);

    s_Push(pQueue->pStack1, nNum);

    //更新队列元素
    ++pQueue->nCount;
}

int q_Pop(Queue *pQueue)
{
    int nNum;

    //队列存在且队列非空
    assert(pQueue!=NULL && pQueue->nCount!=0 && pQueue->pStack1!=NULL && pQueue->pStack2!=NULL);

    if(!s_IsEmpty(pQueue->pStack2))
    {
        nNum = s_Pop(pQueue->pStack2);
    }
    else
    {
        while(pQueue->pStack1->nCount > 0)
        {
            s_Push(pQueue->pStack2, s_Pop(pQueue->pStack1));
        }
        nNum = s_Pop(pQueue->pStack2);
    }

    //更新队列元素
    --pQueue->nCount;

    return nNum;
}

int main(void)
{
    Queue *pQueue = NULL;

    //初始化
    q_Init(&pQueue);

    //添加
    q_Push(pQueue, 1);
    q_Push(pQueue, 2);
    q_Push(pQueue, 3);
    q_Push(pQueue, 4);

    //弹出
    printf("%d ", q_Pop(pQueue));
    printf("%d ", q_Pop(pQueue));
    printf("%d ", q_Pop(pQueue));
    printf("%d ", q_Pop(pQueue));

    return 0;
}
原文地址:https://www.cnblogs.com/chen-cai/p/7856774.html