实现顺序队列各种基本运算的算法
来源:互联网 发布:亨廷顿 谈中国知乎 编辑:程序博客网 时间:2024/05/22 11:56
/*algo3-4.cpp*/
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct qnode
{
ElemType data;
struct qnode *next;
}QNode;
typedef struct
{
QNode *front;
QNode *rear;
}LiQueue;
void InitQueue(LiQueue * &q)
{
q=(LiQueue *)malloc(sizeof(LiQueue));
q->front=q->rear=NULL;
}
void ClearQueue(LiQueue * &q)
{
QNode *p=q->front,*r;
if(p!=NULL)/*释放数据结点占用空间*/
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;r=p->next;
}
}
free(q);/*释放数据结点占用空间*/
}
int QueueLength(LiQueue * &q)
{
int n=0;
QNode *p=q->front;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
int QueueEmpty(LiQueue *q)
{
if(q->rear==NULL)
return 1;
else
return 0;
}
void enQueue(LiQueue * &q,ElemType e)
{
QNode *s;
s=(QNode *)malloc(sizeof(QNode));
s->data=e;
s->next=NULL;
if(q->rear==NULL)/*若链队为空,则新结点是队首结点又是队尾结点*/
q->front=q->rear=s;
else
{
q->rear->next=s;/*将*s结点链到队尾,rear指向它*/
q->rear=s;
}
}
int deQueue(LiQueue * &q,ElemType &e)
{
QNode *t;
if(q->rear==NULL)/*队列为空*/
return 0;
if(q->front==q->rear)/*队列中只有一个结点时*/
{
t=q->front;
q->front=q->rear=NULL;
}
else/*队列中有多个结点时*/
{
t=q->front;
q->front=q->front->next;
}
e=t->data;
free(t);
return 1;
}
/*exp3-4.cpp*/
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct qnode
{
ElemType data;
struct qnode *next;
}QNode;
typedef struct
{
QNode *front;
QNode *rear;
}LiQueue;
extern void InitQueue(LiQueue * &q);
extern void ClearQueue(LiQueue * &q);
extern int QueueLength(LiQueue * &q);
extern int QueueEmpty(LiQueue *q);
extern void enQueue(LiQueue * &q,ElemType e);
extern int deQueue(LiQueue * &q,ElemType &e);
void main()
{
ElemType e;
LiQueue *q;
printf("(1)初始化链队\n");
InitQueue(q);
printf("(2)依次进链队元素a,b,c\n");
enQueue(q,'a');
enQueue(q,'b');
enQueue(q,'c');
printf("(3)链队为%s\n",(QueueEmpty(q)?"空":"非空"));
if(deQueue(q,e)==0)
printf("队宽,不能出队\n");
else
printf("(4)出队一个元素%c\n",e);
printf("(5)链队q的元素个数:%d\n",QueueLength(q));
printf("(6)依次进链队元素d,e,f\n");
enQueue(q,'d');
enQueue(q,'e');
enQueue(q,'f');
printf("(7)链队q的元素个数:%d\n",QueueLength(q));
printf("(8)出链队序列:");
while(!QueueEmpty(q))
{
deQueue(q,e);
printf("%c",e);
}
printf("\n");
printf("(9)释放链队\n");
ClearQueue(q);
}
- 实现顺序队列各种基本运算的算法
- 实现顺序队列各种基本运算的算法
- 顺序环形队列的各种基本运算
- 实现顺序栈的各种基本运算的算法
- 实现顺序栈各种基本运算算法
- 实现顺序表各种基本运算算法
- 实现顺序表各种基本运算的算法
- 实现顺序栈各种基本运算的算法
- 实现顺序串各种基本运算的算法
- 顺序表各种基本运算的算法
- 顺序栈各种基本运算的算法
- 顺序串各种基本运算的算法
- 环形队列各种基本运算的算法
- 编写一个程序,实现顺序环形队列的各种基本运算。
- 实现单链表各种基本运算的算法
- 队列的顺序存储结构及其基本运算的实现
- 队列的顺序存储结构及其基本运算的实现
- 编写一个程序,实现顺序表的各种基本运算
- 一句话搞定Frame局中,不必要获取屏幕大小
- 51中断
- C语言数据类型
- Codeforces #163 Div2 E
- 网络程序设计——ping命令的实现
- 实现顺序队列各种基本运算的算法
- linux 获取时间差
- 教你如何训练和提高自己的情商
- UML 概述
- Python入门解析篇
- 荷叶茶的制作
- AMPS:内存管理模块源码解读(二)
- Python多线程实例
- ISE Simulator综合后仿真 - How do you run Post Synthesis Simulation in ISE Project Navigator?