在入栈时发生了写入访问权限冲突,具体是在执行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;
}
错误提示如图
我是半路出家转专业来计算机的,可能有很多基础的东西不太懂,希望大家不要嫌弃