Advertisement
yungyao

neoj20 link list ver

Mar 6th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3. #include <queue>
  4.  
  5. struct Node{
  6.     int member;
  7.     short int name;
  8.     Node *next;
  9. }*head,*nowAt,*last,*temp;
  10.  
  11. int main(){
  12.     string op;
  13.     int opOn;
  14.     short int opIn;
  15.     int n,t;
  16.     int LINE = 0;
  17.     short int groupSize;
  18.     short int groupBelong[1000050] ={0};
  19.     queue <int> group[1010];
  20.  
  21.     while (cin >> n){
  22.         head = new Node;
  23.         for (int i=0;i<1005;++i)
  24.             while (!group[i].empty())
  25.                 group[i].pop();
  26.  
  27.         cout << "Line #" << ++LINE << '\n';
  28.  
  29.         for (int i=1;i<=n;++i){
  30.             cin >> groupSize;
  31.             while (groupSize--){
  32.                 cin >> t;
  33.                 groupBelong[t] = i;
  34.             }
  35.         }
  36.  
  37.         nowAt = head;
  38.         last = head;
  39.         head->next = 0;
  40.         cin >> op >> opOn; //�o�榳�i��X�ơA�p�G����ܾ��T����stop����
  41.  
  42.         if (!groupBelong[opOn]){
  43.             nowAt->name = 0;
  44.             nowAt->member = opOn;
  45.         }
  46.         else{
  47.             nowAt->name = groupBelong[opOn];
  48.             group[groupBelong[opOn]].push(opOn);
  49.         }
  50.  
  51.         while (cin >> op){
  52.             if (op == "STOP")
  53.                 break;
  54.             else if (op == "ENQUEUE"){
  55.                 cin >> opOn;
  56.                 opIn = groupBelong[opOn];
  57.                 if (head->name == -1){
  58.                     head->name = groupBelong[opOn];
  59.  
  60.                     if (opIn){
  61.                         group[opIn].push(opOn);
  62.                     }
  63.                     else{
  64.                         head->member = opOn;
  65.                     }
  66.  
  67.                     continue;
  68.                 }
  69.  
  70.                 if (!opIn){
  71.                     nowAt = last;
  72.                     nowAt->next = new Node;
  73.                     nowAt = nowAt->next;
  74.                     nowAt->name = 0;
  75.                     nowAt->member = opOn;
  76.  
  77.                     last = nowAt;
  78.                     last->next = 0;
  79.                 }
  80.                 else{
  81.                     if (group[opIn].empty()){
  82.                         nowAt = last;
  83.  
  84.                         nowAt->next = new Node;
  85.                         nowAt = nowAt->next;
  86.  
  87.                         nowAt->next = 0;
  88.                         nowAt->name = groupBelong[opOn];
  89.                         group[opIn].push(opOn);
  90.  
  91.                         last = nowAt;
  92.                     }
  93.                     else{
  94.                         group[opIn].push(opOn);
  95.                     }
  96.                 }
  97.             }
  98.             else{
  99.                 nowAt = head;
  100.  
  101.                 if (!head->name){
  102.                     cout << head->member << '\n';
  103.                     head = head->next;
  104.  
  105.                     delete nowAt;
  106.                 }
  107.                 else{
  108.                     opIn = head->name;
  109.  
  110.                     cout << group[opIn].front() << '\n';
  111.                     group[opIn].pop();
  112.  
  113.                     if (group[opIn].empty()){
  114.                         head = head->next;
  115.  
  116.                         delete nowAt;
  117.                     }
  118.                 }
  119.  
  120.                 if (!head){
  121.                     head = new Node;
  122.                     head->next = 0;
  123.                     head->name = -1;
  124.                     last = head;
  125.                 }
  126.             }
  127.         }
  128.  
  129.  
  130.         nowAt = head;
  131.         while (nowAt != last){
  132.             temp = nowAt;
  133.             nowAt = nowAt->next;
  134.  
  135.             delete temp;
  136.         }delete last;
  137.  
  138.         for (int i=0;i<1000050;++i)
  139.             groupBelong[i] = 0;
  140.     }
  141.  
  142.     return 0;
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement