c语言队列的基本操作实验报告,数据结构(c语言版)队列基本操作的实现

满意答案

02ae427d08e371d7e90d5b995e828d6d.png

wudawu

推荐于 2016.03.29

02ae427d08e371d7e90d5b995e828d6d.png

采纳率:44%    等级:6

已帮助:1008人

/***************/

/* 链式队列 */

/***************/

#include "stdlib.h"

#include "stdio.h"

/* 定义链式队列类型 */

typedef int ElemType;

typedef struct QNode

{ ElemType data;

struct QNode *next;

} QNode, *QueuePtr;

typedef struct

{ QueuePtr front;

QueuePtr rear;

} LinkQueue;

/* 1、初始化链式队列 */

void InitQueue(LinkQueue *Q)

{ Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));

if (!(Q->front)) exit(0);

Q->front->next=NULL; }

/* 2、销毁链式队列 */

void DestroyQueue(LinkQueue *Q)

{ while (Q->front)

{ Q->rear=Q->front->next;

free(Q->front);

Q->front=Q->rear; }

}

/* 3、清空链式队列 */

void ClearQueue(LinkQueue *Q)

{ QueuePtr p;

p=Q->front->next;

while (p)

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

free(p); }

Q->rear=Q->front;

}

/* 4、判断空队列 */

int QueueEmpty(LinkQueue Q)

{ if (Q.front==Q.rear)

return 1;

else

return 0; }

/* 5、求链式队列长度 */

int QueueLength(LinkQueue Q)

{ QueuePtr p; int n=0;

p=Q.front;

while (p!=Q.rear)

{ n++; p=p->next; }

return n;

}

/* 6、取队头元素 */

ElemType GetHead(LinkQueue Q)

{ if (Q.front!=Q.rear)

return Q.front->next->data;

}

/* 7、入队列 */

void EnQueue(LinkQueue *Q, ElemType e)

{ QueuePtr p;

p=(QueuePtr)malloc(sizeof(QNode));

if (!p) exit(0);

p->data=e; p->next=NULL;

Q->rear->next=p;

Q->rear=p; }

/* 8、出队列 */

void DeQueue(LinkQueue *Q, ElemType *e)

{ QueuePtr p;

if (Q->front!=Q->rear)

{ p=Q->front->next;

*e=p->data;

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

if (Q->rear==p) Q->rear=Q->front;

free(p); }

}

/* 9、遍历链式队列并输出元素 */

void QueueTraverse(LinkQueue Q)

{ QueuePtr p;

printf("\nQueue: ");

p=Q.front->next;

while (p)

{ printf("%d\t",p->data);

p=p->next;}

}

/* 约瑟夫问题 */

void Joseffer(int n)

{ LinkQueue Q; int i; ElemType x;

InitQueue(&Q);

for(i=1; i<=n; i++)

EnQueue(&Q,i);

while (!QueueEmpty(Q))

{ for(i=1; i<=3; i++)

{ DeQueue(&Q,&x);

if (i!=3)

EnQueue(&Q,x);

else

printf("%5d",x);

}

}

}

/* 主函数 */

main()

{ LinkQueue Q; int i; ElemType x;

InitQueue(&Q);

for(i=2; i<=5; i++)

EnQueue(&Q,i);

printf("len:%d\n",QueueLength(Q));

while (!QueueEmpty(Q))

{ DeQueue(&Q,&x);

printf("%d\t",x); }

//QueueTraverse(Q);

//Joseffer(6);

}

自己去调试吧,这个是C语言版的链式队列,如果看不懂或者调不出来就去看书吧。否则你这门是白学了。

注:这里是链式队列

/***************/

/* 循环队列 */

/***************/

#include "stdlib.h"

#include "stdio.h"

#define N 100

/* 定义循环队列类型 */

typedef int ElemType;

typedef struct

{ ElemType *base;

int front;

int rear;

} SqQueue;

/* 1、初始化循环队列 */

void InitQueue(SqQueue *Q)

{ Q->base=(ElemType*)malloc(N*sizeof(ElemType));

Q->front=Q->rear=0; }

/* 2、销毁循环队列 */

void DestroyQueue(SqQueue *Q)

{ free(Q->base); }

/* 3、清空循环队列 */

void ClearQueue(SqQueue *Q)

{ Q->front=Q->rear=0; }

/* 4、判断空队列 */

int QueueEmpty(SqQueue Q)

{ if (Q.front==Q.rear)

return 1;

else

return 0; }

/* 5、求循环队列长度 */

int QueueLength(SqQueue Q)

{ return (Q.rear+N-Q.front)%N; }

/* 6、取队头元素 */

void GetHead(SqQueue Q, ElemType *e)

{ if (Q.front!=Q.rear)

*e=Q.base[Q.front];

}

/* 7、入队列 */

int EnQueue(SqQueue *Q, ElemType e)

{ if ((Q->rear+1)%N==Q->front)

return 0;

Q->base[Q->rear]=e;

Q->rear=(Q->rear+1)%N;

return 1; }

/* 8、出队列 */

int DeQueue(SqQueue *Q, ElemType *e)

{ if (Q->front==Q->rear)

return 0;

*e=Q->base[Q->front];

Q->front=(Q->front+1)%N;

return 1; }

/* 9、遍历循环队列并输出元素 */

void QueueTraverse(SqQueue Q)

{ int i;

printf("\nQueue: ");

if (Q.rear

for(i=Q.front; i

printf("%d\t",Q.base[i%N]); }

/* 主函数 */

main()

{ SqQueue Q; int i; ElemType x;

InitQueue(&Q);

for(i=2; i<=5; i++)

EnQueue(&Q,i);

printf("len:%d\n",QueueLength(Q));

while (!QueueEmpty(Q))

{ DeQueue(&Q,&x);

printf("%d\t",x); }

QueueTraverse(Q);

}

在给你个循环队列吧

00分享举报

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值