// Queue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
enum Symbol
{
Success,
Failed
};
typedef struct tagNode
{
int nData;
struct tagNode *pNext;
}Node_S;
typedef struct tagQueue
{
Node_S *pFront;
Node_S *pRear;
}Queue_S;
//为了方便添加一个头结点。
int InitQueue(Queue_S &Queue)
{
Queue.pFront = Queue.pRear = (Node_S *)malloc(sizeof(Node_S)); //头结点
Queue.pFront->nData = 0;
Queue.pFront->pNext = NULL;
Queue.pRear->nData = 0;
Queue.pRear->pNext = NULL;
return Success;
}
int EnQueue(Queue_S &Queue, int m) //队尾入队
{
Node_S *pNew = (Node_S *)malloc(sizeof(Node_S));
pNew->nData = m;
pNew->pNext = NULL;
Queue.pRear->pNext = pNew;
Queue.pRear = pNew;
return Success;
}
int DeQueue(Queue_S &Queue, int &n)
{
if (Queue.pFront == Queue.pRear) //空队
{
return Failed;
}
Node_S *pDel = Queue.pFront->pNext;
n = pDel->nData;
Queue.pFront->pNext = pDel->pNext;
if (Queue.pRear == pDel) //pDel为队尾节点
{
Queue.pRear = Queue.pFront;
}
free(pDel);
pDel = NULL;
return Success;
}
int _tmain(int argc, _TCHAR* argv[])
{
int m = 5;
int k = 10;
int n = 0;
Queue_S Queue;
InitQueue(Queue);
EnQueue(Queue, m);
EnQueue(Queue, k);
DeQueue(Queue, n);
cout<<n<<endl;
DeQueue(Queue, n);
cout<<n<<endl;
while (Queue.pFront->pNext != NULL)
{
Queue.pRear = Queue.pFront->pNext;
free(Queue.pFront);
Queue.pFront = Queue.pRear;
}
free(Queue.pRear);
Queue.pFront = NULL;
Queue.pRear = NULL;
system("pause");
return 0;
}