顺序队列,链队列的基本操作

深碍√TFBOYSˉ_ 2022-11-25 13:24 244阅读 0赞

顺序队列,链队列的基本操作

一、实验目的

1.深入了解队列的定义和特性。
2.掌握队列的数组表示、链表表示以及相应操作的实现,巩固对这两种结构的构造方法的掌握。

  1. 会灵活运用队列结构解决某些实际问题。

二、实验内容

  1. 顺序队列的基本操作的实现(初始化、赋值、取值、插入、删除等)。
  2. 链队列的基本操作的实现(初始化、赋值、取值、插入、删除等)。
  3. 舞伴问题(参加教材相关描述)。

三、实验要求

  1. 在本题下面提交源程序和实验运行结果截图。
  2. 顺序队列的基本操作的实现(初始化、赋值、取值、插入、删除等)。

    include

    include

    using namespace std;

    define ERROR 0

    define OK 1

    define OVERFLOW -1

    define MAXSIZE 100

    typedef int QElemType;
    typedef int Status;
    typedef struct {

    1. QElemType *base;
    2. int front;
    3. int rear;

    } SqQueue;
    Status InitQueue(SqQueue &Q) {

    1. Q.base = new QElemType[MAXSIZE];
    2. if(!Q.base) {
    3. cout<<"储存分配失败"<<endl;
    4. exit(OVERFLOW);
    5. }
    6. Q.front=Q.rear=0;
    7. return OK;

    }//初始化
    Status EnQueue(SqQueue &Q,QElemType e) {

    1. if((Q.rear+1)%MAXSIZE==Q.front)
    2. return ERROR;
    3. Q.base[Q.rear]=e;
    4. Q.rear=(Q.rear+1)%MAXSIZE;
    5. return OK;

    }//插入新元素
    QElemType GetHead(SqQueue Q) {

    1. if(Q.front!=Q.rear)
    2. return Q.base[Q.front];

    }//取元素
    Status DeQueue(SqQueue &Q,QElemType &e) {

    1. if(Q.front==Q.rear) return ERROR;
    2. e=Q.base[Q.front];
    3. Q.front=(Q.front+1)%MAXSIZE;
    4. return OK;

    }//出队
    int QueueLenght(SqQueue Q) {

    1. return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;

    }//求长度
    int main() {

    1. SqQueue Q;
    2. int n,m;
    3. QElemType e;
    4. if(InitQueue(Q)) cout<<"初始化成功"<<endl;
    5. cout<<"请输入队列中元素个数"<<endl;
    6. cin>>n;
    7. for(int i=0; i<n; i++) {
    8. cin>>e;
    9. EnQueue(Q,e);
    10. }
    11. cout<<"1,取队头元素"<<endl;
    12. cout<<"2,向队尾插入元素"<<endl;
    13. cout<<"3,出队"<<endl;
    14. cout<<"4,退出"<<endl;
    15. while(~scanf("%d",&m)) {
    16. if(m==4)
    17. break;
    18. switch(m) {
    19. case 1:
    20. cout<<"队头元素为"<<GetHead(Q)<<endl;
    21. break;
    22. case 2:
    23. cout<<"请输入要插入的队尾元素:"<<endl;
    24. cin>>e;
    25. EnQueue(Q,e);
    26. cout<<"当前对内元素有:"<<QueueLenght(Q)<<"个"<<endl;
    27. break;
    28. case 3:
    29. DeQueue(Q,e);
    30. cout<<"出队元素为" <<e<<endl;
    31. cout<<"当前对内元素有:"<<QueueLenght(Q)<<"个"<<endl;
    32. break;
    33. default:
    34. break;
    35. }
    36. }
    37. return 0;

    }

在这里插入图片描述

  1. 舞伴问题

    include

    define MAXQSIZE 100

    define QueueSize 20

    define OK 1

    define ERROR 0

    define OVERFLOW 0

    include

    include

    using namespace std;
    typedef char QElemType;
    typedef int Status;
    //typedef char SElemType;
    typedef struct
    {

    1. char name[QueueSize];
    2. char sex;

    }person;
    typedef struct
    {

    1. person *dancer;
    2. person *base; //存储空间的基地址
    3. int front; //头指针
    4. int rear; //尾指针

    }SqQueue;

    Status InitQueue(SqQueue &Q)
    {

    1. //构造一个空队列Q
    2. Q.base=new person[MAXQSIZE]; //为队列分配一个最大容量为MAXQSIZE的数组空间
    3. if(!Q.base) exit(OVERFLOW); //存储分配失败
    4. Q.front=Q.rear=0; //头指针和尾指针为零,队列为空
    5. return OK;

    }
    Status EnQueue(SqQueue &Q,person e)
    {

    1. //插入元素e为Q的新的队尾元素
    2. if((Q.rear+1)%MAXQSIZE==Q.front) //尾指针在循环意义上加1后等于头指针,表明队满
    3. return ERROR;
    4. Q.base[Q.rear]=e; //新元素插入队尾
    5. Q.rear=(Q.rear+1)%MAXQSIZE; //队尾指针加1
    6. return OK;

    }
    int QueueEmpty(SqQueue &Q)
    {

    1. if (Q.front==Q.rear) return OK;
    2. else return ERROR;

    }
    Status DeQueue(SqQueue &Q,person &e)
    {

    1. //删除Q的队头元素,用e返回其值
    2. if(Q.front==Q.rear) return ERROR; //队空
    3. e=Q.base[Q.front]; //保存队头元素
    4. Q.front=(Q.front+1)%MAXQSIZE; //队头指针加1
    5. return OK;

    }
    person GetHead(SqQueue Q)
    {

    1. //返回Q的队列元素,不修改队头指针
    2. if(Q.front!=Q.rear) //队列非空
    3. return Q.base[Q.front]; //返回队头元素的值,队头指针不变

    }
    void DancePartner(person dancer[],int num)
    {

    1. //结构数组dancer中存放跳舞的男女,num是跳舞的人数
    2. person p;
    3. int i;
    4. SqQueue Mdancers,Fdancers;
    5. InitQueue(Mdancers); //男士队列初始化
    6. InitQueue(Fdancers); //女士队列初始化
    7. for (i=0;i<num;i++) //根据性别依次将跳舞的人插入相应队列
    8. {
    9. p=dancer[i];
    10. if (p.sex=='F') EnQueue(Fdancers,p); //插入男队
    11. else EnQueue(Mdancers,p); //插入女队
    12. }
    13. cout<<"The dancing partner are:\n";
    14. while(!QueueEmpty(Fdancers)&&!QueueEmpty(Mdancers))
    15. {
    16. //依次输出男女舞伴的姓名
    17. DeQueue(Fdancers,p); //女士出队
    18. cout<<p.name<<" "; //输出出队女士姓名
    19. DeQueue(Mdancers,p); //男士出队
    20. cout<<p.name<<endl; //输出出队男士姓名
    21. }
    22. if (!QueueEmpty(Fdancers)) //女士队非空,输出队头女士的姓名
    23. {
    24. p=GetHead(Fdancers); // 取女队的头
    25. cout<<p.name<<" is waiting for a partner."<<endl;
    26. }
    27. else if (!QueueEmpty(Mdancers)) //男士队非空,输出男士队头的姓名
    28. {
    29. p=GetHead(Mdancers); // 取男队的头
    30. cout<<p.name<<" is waiting for a partner."<<endl;
    31. }

    }
    int main()
    {

    1. int i,j;
    2. person dancer[QueueSize];
    3. cout<<"请输入跳舞的人数:";
    4. cin>>j;
    5. while(j<=0)
    6. {
    7. cout<<"输入错误,请重新输入跳舞的人数:";
    8. cin>>j;
    9. }
    10. for(i=1;i<=j;i++)
    11. {
    12. cout<<"请输入第"<<i<<"舞者的名字:"<<endl;
    13. cin>>dancer[i-1].name;
    14. cout<<"请输入第"<<i<<"个人的性别(F/M):"<<endl;
    15. cin>>dancer[i-1].sex;
    16. while(dancer[i-1].sex!='F'&&dancer[i-1].sex!='M')
    17. {
    18. cout<<"*******输入错误,请重新输入:\n";
    19. cout<<dancer[i-1].sex;
    20. cout<<"请输入第"<<i<<"个人的性别(F/M):"<<endl;
    21. cin>>dancer[i-1].sex;
    22. break;
    23. }
    24. }
    25. DancePartner(dancer,j);

    }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读