各位大神帮忙看一个程序吧。。。
#include"stdio.h"
#include"malloc.h"
#define maxstacksize 5
#define fee 5
typedef struct
{
char nub;
int time;
int flag;/* 用该结构体来存放车的状态,包括车牌号,到达或离去时间,flag中0表示到达状态 1表示离去状态*/
}state;
typedef struct stack /*用该栈来模拟停车场*/
{
state T[maxstacksize];
int top;
}seqstack;
typedef struct stack1 /*用该栈来存放临时让出的车*/
{
state T1[maxstacksize];
int top;
}seqstack1;
typedef struct qnode /*用该队列来模拟便道*/
{
char nub;
struct qnode * next;
}lqnode;
typedef struct
{
lqnode * front;
lqnode * rear;
}lqueue;
void arrive(lqueue * q, seqstack * s, state * tc)/*该算法实现对到达车辆的操作*/
{
if(s->top <= maxstacksize)
{
s->T[s->top].nub = tc->nub;
s->T[s->top].time = tc->time;
s->T[s->top].flag = tc->flag;
s->top++;
printf("汽车在停车场的位置为:%d号位\n",s->top);
}
else
{
printf("停车场已满,请进入便道等候\n");
lqnode * p;
p = (lqnode *)malloc(sizeof(lqnode));
p->nub = tc->nub;
p->next = NULL;
if(q->rear != NULL) q->rear->next = p;
q->rear = p;
if(q->front == NULL) q->front = p;
}
}
void leave(lqueue * q, seqstack * s,seqstack1 * s1, state * tc)/*该算法实现对离去车辆的操作*/
{
lqnode * p;
while(tc->nub != s->T[s->top].nub )
{
s->top--;
s1->T1[s->top].nub = s->T[s->top].nub ;
s1->T1[s->top].flag = s->T[s->top].flag;
s1->T1[s->top].time = s->T[s->top].time;/*车出栈放入另一堆栈*/
s1->top++;
}
printf("汽车在停车场停留时间为:%d ,应缴纳费用为:%d\n",tc->time-(s->T[s->top]).time, fee * (tc->time-(s->T[s->top]).time));
while(s1->top>0)
{
s1->top--;
s->T[s->top].nub = s1->T1[s->top].nub;
s->T[s->top].flag = s1->T1[s->top].flag;
s->T[s->top].time = s1->T1[s->top].time;
s->top++;
}
if(q->front != NULL)/*队列中第一个数据元素进入堆栈,便道上的第一辆车进入停车场*/
{
s->T[s->top].nub = q->front->nub;
p = q->front;
q->front = q->front->next;
if(q->front = NULL) q->rear = NULL;
free(p);
}
}
void main(void)
{
int i, n = 20;
state tc[20];
seqstack * s;
seqstack1 * s1;
lqueue * q;
s = (seqstack *)malloc(sizeof(seqstack));/*堆栈1初始化*/
s->top = 0;
s1 = (seqstack1 *)malloc(sizeof(seqstack1));/*堆栈2初始化*/
s1->top = 0;
q = (lqueue *)malloc(sizeof(lqueue));/*队列初始化*/
q->front = NULL;
q->rear = NULL;
for(i = 0; i<n; i++)
{
printf("请输入车辆信息:-车牌号-时间-状态(车牌号为字符,时间为整形,状态为:0到达1离开)\n ");
scanf("%c %d %d",&(tc[i].nub),&(tc[i].time), &(tc[i].flag));
if(tc[i].flag == 0)
arrive(q, s, &tc[i]);
else
leave(q, s, s1, &tc[i]);
}
}
此程序可以运行一次,第二次输入数据的时候就会崩溃,XP系统下显示xxxxxxx内存为只读,不知怎么回事求大神帮忙 |