zoukankan      html  css  js  c++  java
  • 数据结构实验5

    题目:假设以数组 sequ[MaxSize]存放环形队列的元素,同时 Rear 和 Len 分别指示 环形队列中队尾元素的位置和内含元素的个数。设计相应的入队和出队算法。

    test.h

    #pragma once
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<conio.h>
    
    #define MaxSize 5
    typedef int elemtype;
    typedef struct SequenQueue
    {
        elemtype sequ[MaxSize];
        int Len;        //内含元素的个数
        int Rear;        //队尾元素的位置
    }SequenQueue;
    SequenQueue *Init_SequenQueue()
    {
        SequenQueue * Q;
        Q = (SequenQueue *)malloc(sizeof(SequenQueue));
        if (Q == NULL)
        {
            printf("申请空间失败
    ");
            exit(0);
        }
    
        if (Q != NULL)
        {
            Q->Rear = 0;
            Q->Len = 0;
        }
        return Q;
    }
    void entryQueue(SequenQueue *Q)
    {
        int x,First;
        if (Q->Len == MaxSize)
        {
            printf("队列已满,不能入列。
    ");
            return;
        }
        printf("输入x:");
        scanf("%d", &x);
    
        Q->sequ[Q->Rear] = x;    //入队
        Q->Rear = (Q->Rear + 1) % MaxSize;        //移动队尾指针
        Q->Len++;
        First = ((Q->Rear + MaxSize) - Q->Len + 1) % MaxSize-1;        //计算队头元素的下标
        printf("%d已入列
    ",x);
        printf("尾部位置%d
    ", Q->Rear);
        printf("头部位置%d
    ", First);
        printf("
    ");
    }
    
    int exitQueue(SequenQueue *Q)
    {
        int x, First;//队头元素的下标
        if (Q->Len == 0)
        {
            printf("队列已空,不能出队
    ");
            return -1;
        }
        First = ((Q->Rear + MaxSize) - Q->Len + 1) % MaxSize-1;        //计算队头元素的下标
        x = Q->sequ[First];            //获得队头元素
        Q->Len--;                    //长度减一
                First = ((Q->Rear + MaxSize) - Q->Len + 1) % MaxSize;        //计算队头元素的下标
        printf("已将%d出列
    ", x);
        printf("尾部位置%d
    ", Q->Rear);
        printf("头部位置%d
    ", First);
        printf("
    ");
        return x;
    }
    void menu()
    {
        printf("--菜单栏--
    ");
        printf("1.入列
    ");
        printf("2.出列
    ");
        printf("3.输出列的内容
    ");
        printf("0.退出
    ");
        printf("
    ");
    }
    
    void Print(SequenQueue *Q)
    {
        int a, First,i = 0;
        a = Q->Len;
        First = ((Q->Rear + MaxSize) - Q->Len + 1) % MaxSize-1;        //计算队头元素的下标
        printf("列内的数据元素分别为:");
        while (a != 0)
        {
            printf("%d  ", Q->sequ[(First + i) % MaxSize]);
            a--;
            i++;
        }
        printf("
    
    ");
    }

    test.c

    #include"test.h"
    void main()
    {
        char i;
        SequenQueue *Q;
        Q = Init_SequenQueue();
        menu();
    //    scanf("%d", &i);
        i = _getch();
        while (i)
        {
            switch (i)
            {
            case '0':
                exit(0);
                break;
            case '1':
                entryQueue(Q);
                break;
            case '2':
                exitQueue(Q);
                break;
            case '3':
                Print(Q);
                break;
            default:
                printf("请输入正确的序号!
    ");
                printf("
    ");
                break;
            }
        //    scanf("%d", &i);
            printf("继续输入序号进行操作
    ");
            i = _getch();
        }
    }
  • 相关阅读:
    c++ 为自定义类添加stl遍历器风格的遍历方式
    C++ 生成随机数
    c/c++ 函数说明以及技巧总结
    XSLT 教程
    C# 高效过滤DataTable 中重复数据方法
    xml获取指定节点的路径
    TreeView控件
    推荐一些C#相关的网站、资源和书籍
    C#多线程操作
    C#二进制序列化
  • 原文地址:https://www.cnblogs.com/wuyibb/p/6930622.html
Copyright © 2011-2022 走看看