第七周项目4 队列数组

来源:互联网 发布:琵琶 知乎 编辑:程序博客网 时间:2024/06/09 23:20
/* *Copyright (c) 2015,烟台大学计算机学院 *All rights reserved. *文件名称:duilieshuzu.cpp *作者:朱希康 *完成日期:2015年10月27日 *版本号:vc++6.0 * *问题描述:队列数组 *输入描述:实数 *程序输出:输入实数的尾数与队列数组下标相同的队列*/


#ifndef LIQUEUE_H_INCLUDED#define LIQUEUE_H_INCLUDED#include<malloc.h>typedef int ElemType;typedef struct qnode{    ElemType data;    struct qnode *next;} QNode;        //链队数据结点类型定义typedef struct{    QNode *front;    QNode *rear;} LiQueue;          //链队类型定义void InitQueue(LiQueue *&q);  //初始化链队void DestroyQueue(LiQueue *&q);  //销毁链队bool QueueEmpty(LiQueue *q);  //判断链队是否为空int QueueLength(LiQueue *q);  //返回队列中数据元素个数void enQueue(LiQueue *&q,ElemType e);  //入队bool deQueue(LiQueue *&q,ElemType &e);   //出队#endif // LIQUEUE_H_INCLUDED


#include <stdio.h>#include "head.h"#define N 10int main(){  LiQueue *qu[N];  int a,i;  for(i=0;i<N;i++)    InitQueue(qu[i]);    printf("输入若干正整数,以0结束: ");  while(1)  {      scanf("%d",&a);      enQueue(qu[a%10],a);      if(a==0)        break;  }  printf("按个位数整理到各个队列中后,各队列出队的结果是: \n");   for(i=0;i<N;i++)   {       printf("qu[%d]:",i);       while(!QueueEmpty(qu[i]))        {            deQueue(qu[i],a);            printf("%d ",a);        }        printf("\n");   }   for(i=0;i<N;i++)    DestroyQueue(qu[i]);   return 0;}


#include "head.h"void InitQueue(LiQueue *&q){    q=(LiQueue *)malloc(sizeof(LiQueue));    q->front=q->rear=NULL;}void DestroyQueue(LiQueue *&q){    QNode *r,*p=q->front;    if(p!=NULL)    {        r=p->next;        while(r!=NULL)        {            free(p);            p=r;            r=p->next;        }    }    free(p);    free(q);}bool QueueEmpty(LiQueue *q){    return q->rear==NULL;}int QueueLength(LiQueue *q){    return q->rear->data;}void enQueue(LiQueue *&q,ElemType e){    QNode *p;    p=(QNode *)malloc(sizeof(QNode));    p->data=e;    p->next=NULL;    if(q->rear==NULL)        q->rear=q->front=p;    else    {        q->rear->next=p;        q->rear=p;    }}bool deQueue(LiQueue *&q,ElemType &e){    QNode *t;    t=q->front;    if(q->rear==NULL)        return false;    if(q->rear==q->front)        q->rear=q->front=NULL;        else        {            q->front=q->front->next;        }    e=t->data;    free(t);    return true;}


运行结果:

知识点总结:

在链式队列的基础上,对主函数进行修改,即定义一个指针数组,将输入的数据相对的保存在数组中并形成队列,最后输出队列即可。

0 0