常用数据结构----队列

// 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值