weixin_62680436 2021-12-14 17:59 采纳率: 100%
浏览 74
已结题

写入访问权限冲突的问题

在入栈时发生了写入访问权限冲突,具体是在执行Function函数中switch语句的case2时发生的
具体代码如下

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>

#define STACKSIZE 10

typedef struct SeqStack
{
    int data[STACKSIZE];
    int top;
}SqStack;

typedef struct Qnode
{
    int data;
    struct Qnode* next;
}Qnode, * QueuePtr;

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

void InitStack(SqStack& S)
{
    S.top = 0;
}

int isFull(SqStack S)
{
    if (S.top == STACKSIZE)
        return 1;
    else
        return 0;
}

int GetTop(SqStack S)
{
    if (S.top != 0)
        return S.data[S.top];
}

int Judge(SqStack S, int num)//flag为0,不存在重复值,为1则有重复值
{
    int flag = 0;
    for (int i = 0; i < S.top; i++)
    {
        if (S.data[i] == num)
            flag = 1;
    }
    return flag;
}

void Push(SqStack& S, int x)
{
    S.data[S.top] = x;
    S.top++;
}

void Pop(SqStack& S,int &temp)
{
    S.top--;
    temp = S.data[S.top];
}

void ShowStack(SqStack S)
{
    int i;
    for (i = 0; i < S.top; i++)
    {
        printf("车位%d:%d\n", i+1, S.data[i]);
    }
}

#define LENG sizeof(Qnode)

LinkQueue InitQueue()
{
    LinkQueue Q;
    Q.front = Q.rear = (QueuePtr)malloc(LENG);
    Q.front->next = NULL;
    return Q;
}

int isEmpty(LinkQueue Q)
{
    if (Q.front == Q.rear)
        return 1;
    else
        return 0;
}

void EnQueue(LinkQueue& Q, int e)
{
    Qnode* p;
    p = (Qnode*)malloc(LENG);
    p->data = e;
    p->next = NULL;
    Q.rear->next = p;
    Q.rear = p;
    return;
}

int DeQueue(LinkQueue& Q)
{
    Qnode* p;
    if (Q.front == Q.rear)
    {
        printf("队列为空!\n");
        return -1;
    }
    p = Q.front->next;
    int e = p->data;
    Q.front->next = p->next;
    if (Q.rear == p)
        Q.rear = Q.front;
    free(p);
    return e;
}

void Function(SqStack &PARKING, SqStack &TRANSIENT, LinkQueue &Q, int demand, int num)
{
    int temp = 0;
    switch (demand)
    {
        case 1:
            if (!isFull(PARKING))
            {
                if (!Judge(PARKING, num))
                {
                    Push(PARKING, num);
                    printf("车辆已进入停车场!\n");
                    printf("当前停车场内状况为:\n");
                    ShowStack(PARKING);
                }
                else
                {
                    printf("该车已在停车场内,请输入正确车号!\n");
                    return;
                }
            }
            else
            {
                EnQueue(Q, num);
                printf("车辆在便道等候!\n");
            }
            break;
        case 2:
            int target = GetTop(PARKING);
            while (target != num)
            {
                Pop(PARKING, temp);
                Push(TRANSIENT,temp);
            }
            Pop(PARKING, temp);
            while (TRANSIENT.top != 0)
            {
                Pop(TRANSIENT, temp);
                Push(PARKING,temp);
            }
            if (!isEmpty(Q))
            {
                Push(PARKING, DeQueue(Q));
            }
            else
            {
                printf("当前无等候车辆!\n");
            }
            break;
    }
}

int main()
{
    SqStack PARKING, TRANSIENT;
    InitStack(PARKING);
    InitStack(TRANSIENT);
    LinkQueue Q = InitQueue();
    int demand, num;
    printf("请输入要处理的车号:\n");
    scanf_s("%d", &num);
    while (num > 0) 
    {
        printf("请输入要使用的功能:\n");
        printf("1:进入停车场 2:离开停车场\n");
        scanf_s("%d", &demand);
        if (demand != 1 && demand != 2)
            printf("请输入正确的选项!\n");
        Function(PARKING, TRANSIENT, Q, demand, num);
        printf("请输入要处理的车号:\n");
        scanf_s("%d", &num);
    }
    printf("程序结束!\n");
    return 0;
}

错误提示如图

img

我是半路出家转专业来计算机的,可能有很多基础的东西不太懂,希望大家不要嫌弃

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-12-14 18:44
    关注

    你这个pop和push函数不安全,应该检查top是否为0,以及top是否大于STACKSIZE

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月30日
  • 已采纳回答 12月22日
  • 创建了问题 12月14日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度