// Queue.cpp : 定义控制台应用程序的入口点。
//visual studio 2013
/*
@author:inc_cool
*/
#include "stdafx.h"
#include"iostream"
using namespace std;
#define ok 1
typedef int QueueDataType;
typedef int Status;
//定义队列里的节点
struct queueNode{
QueueDataType data;
queueNode *queueNodeNext;
};
//定义队列链表
struct queueList{
queueNode *queueHeadNode, *queueTailNode;
};
//初始化队列
Status InitQueue(queueList &queue){
queue.queueHeadNode = queue.queueTailNode=(queueNode*)malloc(sizeof(queueNode));
if (!queue.queueHeadNode&&!queue.queueTailNode)
{
cout << "分配失败" << endl;
exit(OVERFLOW);
}
queue.queueHeadNode->queueNodeNext = NULL;
return ok;
}
//插入队列元素(尾部)
Status InsertQueue(queueList &queue, QueueDataType insertData)
{
//给一个queueNode分配内存,装入数据
queueNode *insertQueueNode = (queueNode*)malloc(sizeof(queueNode));
if (!insertQueueNode)
{
cout << "节点内存分配失败" << endl;
//exit(OVERFLOW);
}
insertQueueNode->data = insertData;
insertQueueNode->queueNodeNext = NULL;
queue.queueTailNode->queueNodeNext = insertQueueNode;
queue.queueTailNode = insertQueueNode;
return ok;
}
//遍历队列
Status QueueTraverse(queueList queue)
{
queueNode* queueTraverseNode = (queueNode*)malloc(sizeof(queueNode));
if (!queueTraverseNode)
{
cout << "queueTraverseNode分配失败" << endl;
//exit(OVERFLOW);
}
queueTraverseNode = queue.queueHeadNode->queueNodeNext;
if (queue.queueHeadNode == queue.queueTailNode)
{
cout << "队列为空" << endl;
}
else{
while ( queueTraverseNode)
{
cout << queueTraverseNode->data << endl;
queueTraverseNode = queueTraverseNode->queueNodeNext;
}
}
free(queueTraverseNode);
return ok;
}
//出队列(头元素)
Status GetTopQueue(queueList &queue, QueueDataType &getTopElem)
{
queueNode* getTopQueueNode = (queueNode*)malloc(sizeof(queueNode));
if (!getTopQueueNode)
{
cout << "getTopQueueNode内存分配失败" << endl;
//exit(OVERFLOW);
}
getTopQueueNode = queue.queueHeadNode->queueNodeNext;
if (queue.queueHeadNode==queue.queueTailNode)
{
cout << "队列为空,没有头元素" << endl;
}
else{
getTopElem = getTopQueueNode->data;
}
free(getTopQueueNode);
return ok;
}
int _tmain(int argc, _TCHAR* argv[])
{
queueList queue;
QueueDataType elem=0;
InitQueue(queue);
for (int i = 0; i < 5;i++)
{
InsertQueue(queue,i);
}
QueueTraverse(queue);
GetTopQueue(queue, elem);
cout << "elem is:" << elem << endl;
system("pause");
return 0;
//visual studio 2013
/*
@author:inc_cool
*/
#include "stdafx.h"
#include"iostream"
using namespace std;
#define ok 1
typedef int QueueDataType;
typedef int Status;
//定义队列里的节点
struct queueNode{
QueueDataType data;
queueNode *queueNodeNext;
};
//定义队列链表
struct queueList{
queueNode *queueHeadNode, *queueTailNode;
};
//初始化队列
Status InitQueue(queueList &queue){
queue.queueHeadNode = queue.queueTailNode=(queueNode*)malloc(sizeof(queueNode));
if (!queue.queueHeadNode&&!queue.queueTailNode)
{
cout << "分配失败" << endl;
exit(OVERFLOW);
}
queue.queueHeadNode->queueNodeNext = NULL;
return ok;
}
//插入队列元素(尾部)
Status InsertQueue(queueList &queue, QueueDataType insertData)
{
//给一个queueNode分配内存,装入数据
queueNode *insertQueueNode = (queueNode*)malloc(sizeof(queueNode));
if (!insertQueueNode)
{
cout << "节点内存分配失败" << endl;
//exit(OVERFLOW);
}
insertQueueNode->data = insertData;
insertQueueNode->queueNodeNext = NULL;
queue.queueTailNode->queueNodeNext = insertQueueNode;
queue.queueTailNode = insertQueueNode;
return ok;
}
//遍历队列
Status QueueTraverse(queueList queue)
{
queueNode* queueTraverseNode = (queueNode*)malloc(sizeof(queueNode));
if (!queueTraverseNode)
{
cout << "queueTraverseNode分配失败" << endl;
//exit(OVERFLOW);
}
queueTraverseNode = queue.queueHeadNode->queueNodeNext;
if (queue.queueHeadNode == queue.queueTailNode)
{
cout << "队列为空" << endl;
}
else{
while ( queueTraverseNode)
{
cout << queueTraverseNode->data << endl;
queueTraverseNode = queueTraverseNode->queueNodeNext;
}
}
free(queueTraverseNode);
return ok;
}
//出队列(头元素)
Status GetTopQueue(queueList &queue, QueueDataType &getTopElem)
{
queueNode* getTopQueueNode = (queueNode*)malloc(sizeof(queueNode));
if (!getTopQueueNode)
{
cout << "getTopQueueNode内存分配失败" << endl;
//exit(OVERFLOW);
}
getTopQueueNode = queue.queueHeadNode->queueNodeNext;
if (queue.queueHeadNode==queue.queueTailNode)
{
cout << "队列为空,没有头元素" << endl;
}
else{
getTopElem = getTopQueueNode->data;
}
free(getTopQueueNode);
return ok;
}
int _tmain(int argc, _TCHAR* argv[])
{
queueList queue;
QueueDataType elem=0;
InitQueue(queue);
for (int i = 0; i < 5;i++)
{
InsertQueue(queue,i);
}
QueueTraverse(queue);
GetTopQueue(queue, elem);
cout << "elem is:" << elem << endl;
system("pause");
return 0;
}
感谢我的室友,由于他不是计算机专业的,他在自学数据结构。为了让他更好的了解数据结构,我答应他给他写数据结构的源代码。本来我感觉数据结构有什么好写的,等我真正写的时候,发现自己把数据结构的忘得差不多了。我感谢我的室友,让我有动力有毅力去写完正本数据结构的算法,也感谢严蔚敏老师。PS:收获不小!窃喜^.^