关于经典停车场问题指针输出乱码问题?

邀请好友加入腾讯云自媒体分享计划
给好友发送邀请链接,好友成功加入计划后你和好友都分别获得 30 / 100 / 180 元云服务器代金
#include
#include
#include
#include
struct stackstruct /栈的结构体/
{
int id;
int time;
struct stackstruct pre;
struct stackstruct
next;
};

创新互联公司服务项目包括阳西网站建设、阳西网站制作、阳西网页制作以及阳西网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,阳西网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到阳西省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

struct queuestruct //队列结构体
{
int id;
struct queuestruct *next;
};

struct stackstruct stackhead1, stackend1;
struct stackstruct stackhead2, stackend2;
struct queuestruct queuehead, queueend;
int stack1count, stack2count; /栈中元素总数/
int queuecount; /队列中元素总数/

void push(int flag, struct stackstruct p)
{
struct stackstruct
stack;
if (flag == 0) /栈1进栈操作/
{
if (stack1count == 0)//栈1空
{
stackhead1 = (struct stackstruct )malloc(sizeof(struct stackstruct));
stackhead1->id = p->id;
stackhead1->time = p->time;
stackhead1->next = NULL;
stackhead1->pre = NULL;
stackend1 = stackhead1;
}
else
{
stack = (struct stackstruct
)malloc(sizeof(struct stackstruct));
stack->id = p->id;
stack->time = p->time;
stackend1->next = stack;
stack->pre = stackend1;
stack->next = NULL;
stackend1 = stack;
}
stack1count++;
}
else if (flag == 1) /栈2进栈操作,栈1出栈/
{
if (stack2count == 0)//栈2空
{
stackhead2 = (struct stackstruct )malloc(sizeof(struct stackstruct));
stackhead2->id = p->id;
stackhead2->time = p->time;
stackhead2->next = NULL;
stackhead2->pre = NULL;
stackend2 = stackhead2;
}
else
{
stack = (struct stackstruct
)malloc(sizeof(struct stackstruct));
stack->id = p->id;
stack->time = p->time;
stackend2->next = stack;
stack->pre = stackend2;
stack->next = NULL;
stackend2 = stack;
}
stack2count++;
}
}

struct stackstruct pop(int id, int time)
{
struct stackstruct
stack;
stack = (struct stackstruct *)malloc(sizeof(struct stackstruct));

if (stackend1->id != id)
{
    stack->id = stackend1->id;
    stack->time = stackend1->time;
    stack->pre = stackend1->pre;
    stackend1=NULL;
    stackend1 = stack->pre;
    stackend1->next = NULL;
    stack1count--;
}
else
{
    stack->id = stackend1->id;
    stack->time = stackend1->time;
    stack->pre = stackend1->pre;
    printf("%d号汽车出停车场\n",id);
    printf("停车场停留时间: %d\n",time - stack->time);
    printf("应该缴纳的费用(单价: 5): %d\n", 5 * (time - stack->time));
    stackend1=NULL;
    if (--stack1count == 0)
        stackend1 = stackhead1 = NULL;
    else
    {
        stackend1 = stack->pre;
        stackend1->next = NULL;
    }
    stack = NULL;
}
return stack;

}

struct stackstruct pop1()//栈2元素出栈
{
struct stackstruct
stack;
stack = (struct stackstruct *)malloc(sizeof(struct stackstruct));

stack->id = stackend2->id;
stack->time = stackend2->time;
stack->pre = stackend2->pre;
free(stackend2);
stackend2 = stack->pre;
stack2count--;

return stack;

}

void Enqueue(struct stackstruct p)//入队
{
struct queuestruct
queue;
if (queuecount == 0)
{
queuehead = (struct queuestruct )malloc(sizeof(struct queuestruct));
queuehead->id = p->id;
queuehead->next = NULL;
queueend = queuehead;
}
else
{
queue = (struct queuestruct
)malloc(sizeof(struct queuestruct));
queue->id = p->id;
queue->next = NULL;
queueend->next = queue;
queueend = queue;
}
queuecount++;
}

struct stackstruct Dequeue(int time)//出队
{
struct stackstruct
stack;
stack = (struct stackstruct *)malloc(sizeof(struct stackstruct));

stack->id = queuehead->id;
stack->time = time;
if (--queuecount == 0)
{
    queuehead = NULL;
    queueend = NULL;
}
else
    queuehead = queuehead->next;
return stack;

}
int main()
{
int n;
char s = {0};
struct stackstruct p;
printf("输入狭长通道可停放汽车数量: ");
scanf_s("%d", &n);
getchar();
stack1count = stack2count = queuecount = 0;
p = (struct stackstruct
)malloc(sizeof(struct stackstruct));
printf("输入停车信息:(动作,车牌号,时间)\n");
while (scanf_s("%c,%d%d", &s,1, &p->id, &p->time) != EOF)
{
if (s =='E')
{
printf("End");
break;
}
if (s =='A') /汽车到达/
{
if (stack1count < n) /栈未满,进栈操作/
{
push(0, p);
printf("%d号汽车进入停车场\n",p->id);
printf("进入停车场时间: %d\n",stackend1->time);
printf("停车位置: %d\n",stack1count);
}
else /栈满,进队列操作/
{
Enqueue(p);
printf("%d号汽车进入便道\n",p->id);
printf("进入便道时间: %d\n",p->time);
printf("便道位置: %d\n",queuecount);
}
}
if (s =='D') /汽车离去/
{
struct stackstruct *temp;
while ((temp = pop(p->id, p->time)) != NULL)
{
push(1, temp);
}
while (stack2count != 0)
{
push(0, pop1());
}
if (queuecount != 0)
{
push(0, Dequeue(p->time));
printf("%d号汽车进入停车场\n",stackend1->id);
printf("进入时间: %d\n",stackend1->time);
printf("停车位置: %d\n",stack1count);
}
}
}
return 0;
}
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=hiyc3ueo3ntk


当前文章:关于经典停车场问题指针输出乱码问题?
转载来源:http://pwwzsj.com/article/gihiio.html