[置顶] 数据结构之 队列的操作与实现
2013-09-27 23:49
323 查看
// 队列.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "malloc.h" #define maxSize 10 //循环队列 typedef struct { int data[maxSize]; int front; int rear; }SqQueue; void initQueue(SqQueue &qu)//初始队 { qu.front=qu.rear=0; } int isEmpty(SqQueue qu)//判断队是否为空 { if(qu.front==qu.rear) return 1; else return 0; } void enQueue(SqQueue &qu,int x)//进队 { if((qu.rear+1)%maxSize==qu.front) return; else qu.rear=(qu.rear+1)%maxSize; qu.data[qu.rear]=x; } void delQueue(SqQueue &qu,int &x)//出队 { if(qu.front==qu.rear) return; qu.front=(qu.front+1)%maxSize; x=qu.data[qu.front]; } void show(SqQueue &qu) { int x; while(qu.front!=qu.rear) { delQueue(qu,x); printf("%d",x); } } //链队 typedef struct QNode { int data; struct QNode *next; }QNode; typedef struct { QNode *front; QNode *rear; }LiQueue; void Li_initQueue(LiQueue *&L) { L=(LiQueue*)malloc(sizeof(LiQueue)); L->front=L->rear=NULL; } void Li_enQueue(LiQueue *&L,int x) { QNode *p; p=(QNode*)malloc(sizeof(QNode)); p->data=x; p->next=NULL; if(L->rear==NULL) L->front=L->rear=p; else { L->rear->next=p; L->rear=p; } } void Li_delQueue(LiQueue *&L,int &x) { QNode *p; if(L->front==NULL) return; else { p=L->front; if(L->front==L->rear) { L->front=L->rear=NULL; } else { L->front=L->front->next; } x=p->data; free(p); } } void Li_show(LiQueue *&L) { int x; while(L->front!=NULL) { Li_delQueue(L,x); printf("%d",x); } } int _tmain(int argc, _TCHAR* argv[]) { /* //顺序队 SqQueue qu; initQueue(qu); for(int i=1;i<10;i++) { enQueue(qu,i); } show(qu); */ //链队 LiQueue *Li; Li_initQueue(Li); for(int j=1;j<5;j++) { Li_enQueue(Li,j); } Li_show(Li); }
相关文章推荐
- [置顶] 数据结构之 单链表的实现与操作
- [置顶] 数据结构之 顺序表的实现与操作
- [置顶] Redis应用3-基于Redis消息队列实现的异步操作
- 两个栈实现一个队列,并实现队列入队、出队、取队头、取队尾相关操作
- [置顶] 【Excel_To_DB】SpringBoot+EasyPoi+Redis消息队列实现Excel批量异步导入数据库(一)
- C++:利用两个栈实现队列操作
- 链队列的综合操作(详解、演示)C语言实现
- JAVA实现用两个栈来实现一个队列,完成队列的Push和Pop操作(《剑指offer》)
- 数据结构 编写一个类,用两个栈实现队列,支持队列的基本操作(add 、poll、peek)
- 队列基本操作及银行简易排号的实现
- 用两个栈实现队列操作
- (java)两个队列实现栈的操作
- (C语言版)栈和队列(一)——实现链式栈和链式队列的基本操作以及遇到的问题
- jquery实现表格的上下移动和置顶操作
- JavaScript中栈和队列相关操作和实现
- 数据结构(9)--链队列的定义以及相关操作的实现
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- 用两个栈实现队列 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
- 【算法设计-优先队列】优先队列的实现与操作
- 数据结构之 单链表的实现与操作