zoukankan      html  css  js  c++  java
  • 数据结构实验之栈与队列十一:refresh的停车场

    Problem Description

     refresh最近发了一笔横财,开了一家停车场。由于土地有限,停车场内停车数量有限,但是要求进停车场的车辆过多。当停车场满时,要进入的车辆会进入便道等待,最先进入便道的车辆会优先

    进入停车场,而且停车场的结构要求只出去的车辆必须是停车场中最后进去的车辆。现告诉你停车场容量N以及命令数M,以及一些命令(Add num 表示车牌号为num的车辆要进入停车场或便道,

    Del 表示停车场中出去了一辆车,Out 表示便道最前面的车辆不再等待,放弃进入停车场)。假设便道内的车辆不超过1000000.

    Input

     输入为多组数据,每组数据首先输入N和M(0< n,m <200000),接下来输入M条命令。

    Output

     输入结束后,如果出现停车场内无车辆而出现Del或者便道内无车辆而出现Out,则输出Error,否则输出停车场内的车辆,最后进入的最先输出,无车辆不输出。

    Sample Input

    2 6
    Add 18353364208
    Add 18353365550
    Add 18353365558
    Add 18353365559
    Del
    Out

    Sample Output

    18353365558
    18353364208

    Hint

     

    Source

    注:原来做的时候,栈没有用base指针,一直错误,还没有找到出现问题的原因

      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 
      5 
      6 typedef struct node
      7 {
      8     char num[25];
      9     struct node *next;
     10 } Node;
     11 
     12 typedef struct
     13 {
     14     Node *top;
     15     Node *base;
     16     int len;
     17 } SeqStack;
     18 
     19 typedef struct
     20 {
     21     Node *front;
     22     Node *rear;
     23     int len;
     24 } SeqQueue;
     25 
     26 Node* NewNode()
     27 {
     28     Node *p;
     29     p = (Node*)malloc(sizeof(Node));
     30     p->next = NULL;
     31     return p;
     32 }
     33 
     34 // 接下来是对栈的操作
     35 
     36 SeqStack* CreateStack()
     37 {
     38     SeqStack *t;
     39     t = (SeqStack*)malloc(sizeof(SeqStack));
     40     t ->top =NewNode();
     41     t->base = t->top;
     42     t->len = 0;
     43     return t;
     44 }
     45 
     46 int IsEmptyS(SeqStack *s)
     47 {
     48     if (s->top->next == NULL)
     49         return 1;
     50     return 0;
     51 }
     52 
     53 void PushS(SeqStack *s, char num[])
     54 {
     55     Node *p = NewNode();
     56     strcpy(p->num,num);
     57     p->next = s->top->next;
     58     s->top->next = p;
     59     s->base = p;
     60     s->len++;
     61 }
     62 
     63 void PopS(SeqStack *s)
     64 {
     65     Node *t;
     66     t = s->top->next;
     67     s->top->next = s->top->next->next;
     68     free(t);
     69     s->len--;
     70 }
     71 
     72 void ClearS(SeqStack* s)
     73 {
     74     while(!IsEmptyS(s))
     75     {
     76         PopS(s);
     77     }
     78 }
     79 // 接下来是对队列的操作
     80 
     81 SeqQueue* CreateQueue()
     82 {
     83     SeqQueue *q;
     84     q = (SeqQueue*)malloc(sizeof(SeqQueue));
     85     q->front = NewNode();
     86     q->rear = q->front;
     87     q->len = 0;
     88     return q;
     89 }
     90 
     91 int IsEmptyQ(SeqQueue *q)
     92 {
     93     if (q->front->next == NULL)
     94         return 1;
     95     return 0;
     96 }
     97 
     98 void PushQ(SeqQueue *q, char num[])
     99 {
    100     Node *t = NewNode();
    101     strcpy(t->num, num);
    102     q->rear->next = t;
    103     q->rear = t;
    104     q->len++;
    105 }
    106 
    107 void PopQ(SeqQueue *q)
    108 {
    109     Node *t = q->front->nexxt;
    110     q->front->next = q->front->next->next;
    111     free(t);
    112     q->len--;
    113 }
    114 
    115 void show(Node *t)
    116 {
    117     Node *p = t->next;
    118     while(p)
    119     {
    120         printf("%s
    ", p->num);
    121         p = p->next;
    122     }
    123 }
    124 
    125 void ClearQ(SeqQueue *q)
    126 {
    127     if(!IsEmptyQ(q))
    128     {
    129         PopQ(q);
    130     }
    131 }
    132 
    133 int main()
    134 {
    135     int n, m;
    136     char str[25];
    137     char num[25];
    138     SeqQueue *q;
    139     SeqStack *s;
    140     s = CreateStack();
    141     q = CreateQueue();
    142     while(~scanf("%d%d", &n, &m))
    143     {
    144         int flag = 1;
    145         ClearS(s);
    146         ClearQ(q);
    147         while(m--)
    148         {
    149             scanf("%s", str);
    150             if (strcmp(str, "Add") == 0)  // 如果栈没满,入栈,否则入队
    151             {
    152                 scanf("%s", num);
    153                 if (s->len >= n)
    154                 {
    155                     PushQ(q, num);
    156                 }
    157                 else
    158                 {
    159                     PushS(s, num);
    160                 }
    161             }
    162             else if (strcmp(str, "Del") == 0)  // 如果栈没空,则出栈,同时后面的车出队入栈
    163             {
    164                 if (!IsEmptyS(s))
    165                 {
    166                     PopS(s);
    167                     if (!IsEmptyQ(q))
    168                     {
    169                         strcpy(num,q->front->next->num);
    170                         PopQ(q);
    171                         PushS(s, num);
    172                     }
    173                 }
    174                 else
    175                 {
    176                     flag = 0;
    177                 }
    178             }
    179             else if (strcmp(str, "Out") == 0)  // 不排队,出队
    180             {
    181                 if (!IsEmptyQ(q))
    182                 {
    183                     PopQ(q);
    184                 }
    185                 else
    186                 {
    187                     flag = 0;
    188                 }
    189             }
    190         }
    191         if (flag == 0)
    192         {
    193             printf("Error
    ");
    194         }
    195         else
    196         {
    197             show(s->top);
    198         }
    199     }
    200     return 0;
    201 }
  • 相关阅读:
    从Oracle提供两种cube产品说开
    Sql Server DWBI的几个学习资料
    Unload Oracle data into text file
    初学Java的几个tips
    我常用的Oracle知识点汇总
    benefits by using svn
    如何在windows上使用putty来显示远端linux的桌面
    building commercial website using Microsoft tech stack
    Understand Thread and Lock
    Update google calendar by sunbird
  • 原文地址:https://www.cnblogs.com/sugerandmaster/p/11562044.html
Copyright © 2011-2022 走看看