Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. struct Node {
  9.     string name, gift;
  10.     bool isnaughty;
  11.     Node *prev, *next;
  12. };
  13.  
  14.  
  15. class list{
  16.     private :
  17.         Node *head;
  18.         Node *tail;
  19.         Node *pn;
  20.     public :
  21.         void constructList();
  22.         void insertBefore();
  23.         void insertBack();
  24.         Node* makeNode(string namee, string giftt);
  25.         void showList();
  26.         void findMove();
  27. void updatepn();
  28. };
  29.  
  30.  
  31. void list::updatepn() {
  32.     Node *cur=head;
  33.     if (head->isnaughty)
  34.         pn = head;
  35.     while (cur) {
  36.         if(cur->isnaughty) {
  37.             pn = cur;
  38.         return;
  39.         }
  40. cur=cur->next;
  41. }
  42. }
  43.  
  44. Node* list::makeNode(string namee, string giftt) {
  45.     Node *np = new Node;
  46.     np->gift = giftt;
  47.     np->name = namee;
  48.     np->isnaughty = false;
  49.     np->prev = np->next = NULL;
  50.     return np;
  51. }
  52.  
  53. void list::insertBack() {
  54.     string gift, name;
  55.     cin>>name>>gift;
  56.     Node *np = makeNode(name, gift);
  57.     if (head == NULL){
  58.         head = tail =np;
  59.     }
  60.     else {
  61.         tail->next = np;
  62.         np->prev = tail;
  63.         tail = np;
  64.     }
  65. }
  66.     void list::showList() {
  67.         if (head == NULL)
  68.             cout << "Empty\n";
  69.         else {
  70.             Node *cur=head;
  71.             cout << "SantaFinalList\n";
  72.             cout <<"("<<cur->name<<','<<cur->gift<<")";
  73.             cur = cur->next;
  74.             while (cur) {
  75.                 printf("->");
  76.                 cout <<"("<<cur->name<<','<<cur->gift<<")";
  77.                 cur = cur->next;
  78.             }
  79.             cout << endl;
  80.         }
  81.     }
  82.  
  83. void list::insertBefore() {
  84.     int num;
  85.     string name , gift;
  86.     Node *np =NULL;
  87.     cin >> num;
  88.     for(int i=0; i<num; i++){
  89.         if(pn == NULL) {
  90.             insertBack();
  91.         } else {
  92.             cin >> name >> gift;
  93.             np = makeNode(name,gift);
  94.             np->next = pn;
  95.             pn->prev->next = np;
  96.             np->prev = pn->prev;
  97.             pn->prev = np;
  98.    
  99.         /*  if (pn->prev) {
  100.                 pn->prev->next = np;
  101.             } else {
  102.                 head = np;
  103.             }*/
  104.         }
  105.     }
  106. }
  107. void list:: constructList() {
  108.     head = NULL;
  109.     tail = NULL;
  110.     pn = NULL;
  111. };
  112.  
  113. void list::findMove() {
  114.     int num;
  115.     string findname;
  116.     Node **pp=&head, *cur=head, *p;
  117.         cin >> findname;
  118.         while (cur) {
  119.             if (cur->name == findname) {
  120.                 cur->gift = "coal";
  121.                 *pp = cur->next;
  122.                 if(cur->next) cur->next->prev=cur->prev;
  123.                 else tail = cur->prev;
  124.                 p = cur;
  125.                 tail->next = p;
  126.                 p->prev = tail;
  127.                 tail = p;
  128.                 p->next = NULL;
  129.                 cur->isnaughty = true;
  130.                 updatepn();
  131.         return;
  132.             }
  133.             pp = &(cur->next);
  134.             cur = cur->next;
  135.         }
  136. }
  137. int main () {
  138.     list A;
  139.     string command;
  140.     A.constructList();
  141.     int num;
  142.     cin >> command >> num;
  143.     for (int i=0; i<num; i++) {
  144.         A.insertBack();
  145.     }
  146.     for (;;) {
  147.         cin >> command;
  148.         if(command == "AppendList") {
  149.             A.insertBefore();
  150.         }
  151.         else if(command == "End") {
  152.             A.showList();
  153.             return 0;
  154.         }
  155.         else if(command == "NaughtyKid") {
  156.         cin >> num;
  157.     for (int i=0; i<num; i++)
  158.             A.findMove();
  159. }
  160.     }
  161.     return 0;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement