第一范文网 - 专业文章范例文档资料分享平台

编写链接队列的基本操作函数

来源:用户分享 时间:2025/6/5 23:18:57 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

编写链接队列的基本操作函数

(1)编写链接队列的基本操作函数 1.进队函数 EnQueue(LinkQueue *Q,QElemType e)

2.出队函数 ,队空 DeQueue(LinkQueue *Q,QElemType e) 3.输出队列中元素 OutputQueue(LinkQueue *Q)

(2)调用上述函数实现下列操作,操作步骤如下 1.调用进队函数建立一个队列 2.读取队列中的第一个元素 2.从队列中删除元素 4.输出队列中的所有元素

(3)编写环形队列的基本操作函数

1.进队函数 EnQueue(SqQueue *Q,QElemType e)

2.出队函数 ,队空 DeQueue(SqQueue *Q,QElemType e) 3.输出队列中元素 OutputQueue(SqQueue *Q) (4)调用上述函数实现下列操作,操作步骤如下 1.调用进队函数建立一个队列 2.读取队列中的第一个元素 2.从队列中删除元素 4.输出队列中的所有元素

1.链接队列:

#include<stdio.h> #include<malloc.h> typedef struct node {int data;

struct node *next; };

typedef struct {struct node *front; struct node *rear; }LinkQueue;

InitQueue(LinkQueue *Q)

{ Q->front=(struct node *)malloc(sizeof( struct node)); Q->rear=Q->front; Q->front->next=NULL; }

EnQueue(LinkQueue *Q,int e) {struct node *p;

p=(struct node *)malloc(sizeof(struct node )); p->data=e; p->next=NULL;

Q->rear->next=p; Q->rear=p; }

DeQueue(LinkQueue *Q,int e) { struct node *p;

if(Q->front==Q->rear) return 0; else{

p=Q->front->next;

Q->front->next=p->next;

if(p->next=NULL)Q->rear=Q->front; return(p->data); free(p); } }

OutputQueue(LinkQueue *Q) { struct node *p;

p=Q->front->next; while(p!=NULL)

{ printf("%d ",p->data); p=p->next; } }

GetFront(LinkQueue *Q) { struct node *p;

p=Q->front->next;

printf("%d",p->data); }

void main() { LinkQueue s; int i,max,e,item; InitQueue(&s);

printf("put a max:"); scanf("%d",&max); printf("shu ru yuan su"); for(i=0;i<max;i++){

scanf("%d",&e); EnQueue(&s,e);} OutputQueue(&s); printf("\\n");

printf("di yi ge yuan su wei:"); GetFront(&s);

printf("\\n");

printf("shu ru shan chu yuan su :"); scanf("%d",&item); DeQueue(&s,item); OutputQueue(&s); printf("\\n"); }

2.环形队列:

#define MAXQSIZE 100 #include<stdio.h> #include<malloc.h> typedef struct{ int *base; int front; int rear; }SqQueue;

InitQueue(SqQueue *Q)

{ Q->base=(int *)malloc(MAXQSIZE * sizeof(int)); if(!Q->base)exit(1); Q->front=Q->rear=0;

搜索更多关于: 编写链接队列的基本操作函数 的文档
编写链接队列的基本操作函数.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c3ckwd2kf5a8mpoj7ocb09o8y29wt5t00yzo_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top