C++链式队列基本操作
2015-04-01 15:31
375 查看
#include <iostream> #define QUEUEELEMENTTYPE int using namespace std; /*结点*/ typedef struct Node { QUEUEELEMENTTYPE data; /*数据域*/ Node * next; /*指针域*/ }LinkQueueNode; /*队列*/ struct LinkQueue { LinkQueueNode * front; /*头指针*/ LinkQueueNode * rear; /*尾指针*/ }; /*将Q初始化为一个空的链队列*/ int InitQueue(LinkQueue * Q) { Q->front=(LinkQueueNode * )malloc(sizeof(LinkQueueNode)); if(Q->front!=0) { Q->rear = Q->front; Q->front->next=0; return true; } else return false; } /*判断队列Q是否为空*/ int IsEmpty(LinkQueue * Q) { if(Q->front==Q->rear) return true; return false; } /*将数据元素x插入到队列中去*/ int EnterQueue(LinkQueue * Q , QUEUEELEMENTTYPE x) { Node * temp = (Node * )malloc(sizeof(Node)); temp->next = 0; temp->data = x; Q->rear->next = temp; Q->rear = temp; return true; } /*将队列Q的队头元素出队,并存到x所指的存储空间中*/ int DeleteQueue(LinkQueue * Q , QUEUEELEMENTTYPE &x) { if(Q->rear!=Q->front) { x = Q->front->next->data; Q->front=Q->front->next; return true; } return false; } /*获取队头的元素,并存到x所指的存储空间中*/ int GetHead(LinkQueue * Q , QUEUEELEMENTTYPE &x) { if(Q->rear==Q->front) return false; else x = Q->front->next->data; return true; } /*将队列Q置为空*/ void ClearQueue(LinkQueue * Q) { Q->front=Q->rear; Q->front->next=0; } int main() { LinkQueue * queue=new LinkQueue; InitQueue(queue); cout << IsEmpty(queue) << endl; for(int i=0;i<1;i++) EnterQueue(queue,i); cout << IsEmpty(queue) << endl; int x = -1; DeleteQueue(queue,x); cout << "x = " << x << endl; DeleteQueue(queue,x); int y = -1; GetHead(queue,x); cout << "y = " << y << endl; cout << IsEmpty(queue) << endl; ClearQueue(queue); cout << IsEmpty(queue) << endl; return 0; }
相关文章推荐
- 链式队列的基本操作实现c++
- 【数据结构】链式队列的基本操作
- 栈和队列的基本操作(分顺序和链式,5种基本操作有创建,判空,判满,入,出)
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 【队列】链式结构实现队列的基本操作
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 队列的基本操作(顺序结构)C/C++
- PHP数据结构之七_队列的链式存储和队列的基本操作
- 队列之链式队列基本操作
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- (C语言版)栈和队列(一)——实现链式栈和链式队列的基本操作以及遇到的问题
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 循环队列和链式队列的结构及其基本操作(入队、出队、取队头、查看对内元素)
- C++循环队列基本操作
- 【数据结构】用C++编写队列及基本操作(包括插入,出队列,摧毁,清空等等)
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- C++ STL 有关容器部分 priority_queue 优先级队列的基本操作以及认识案例
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- C++循环队列基本操作