Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <vector>
- #include <string>
- using namespace std;
- struct Node {
- string name, gift;
- bool isnaughty;
- Node *prev, *next;
- };
- class list{
- private :
- Node *head;
- Node *tail;
- Node *pn;
- public :
- void constructList();
- void insertBefore();
- void insertBack();
- Node* makeNode(string namee, string giftt);
- void showList();
- void findMove();
- void updatepn();
- };
- void list::updatepn() {
- Node *cur=head;
- if (head->isnaughty)
- pn = head;
- while (cur) {
- if(cur->isnaughty) {
- pn = cur;
- return;
- }
- cur=cur->next;
- }
- }
- Node* list::makeNode(string namee, string giftt) {
- Node *np = new Node;
- np->gift = giftt;
- np->name = namee;
- np->isnaughty = false;
- np->prev = np->next = NULL;
- return np;
- }
- void list::insertBack() {
- string gift, name;
- cin>>name>>gift;
- Node *np = makeNode(name, gift);
- if (head == NULL){
- head = tail =np;
- }
- else {
- tail->next = np;
- np->prev = tail;
- tail = np;
- }
- }
- void list::showList() {
- if (head == NULL)
- cout << "Empty\n";
- else {
- Node *cur=head;
- cout << "SantaFinalList\n";
- cout <<"("<<cur->name<<','<<cur->gift<<")";
- cur = cur->next;
- while (cur) {
- printf("->");
- cout <<"("<<cur->name<<','<<cur->gift<<")";
- cur = cur->next;
- }
- cout << endl;
- }
- }
- void list::insertBefore() {
- int num;
- string name , gift;
- Node *np =NULL;
- cin >> num;
- for(int i=0; i<num; i++){
- if(pn == NULL) {
- insertBack();
- } else {
- cin >> name >> gift;
- np = makeNode(name,gift);
- np->next = pn;
- pn->prev->next = np;
- np->prev = pn->prev;
- pn->prev = np;
- /* if (pn->prev) {
- pn->prev->next = np;
- } else {
- head = np;
- }*/
- }
- }
- }
- void list:: constructList() {
- head = NULL;
- tail = NULL;
- pn = NULL;
- };
- void list::findMove() {
- int num;
- string findname;
- Node **pp=&head, *cur=head, *p;
- cin >> findname;
- while (cur) {
- if (cur->name == findname) {
- cur->gift = "coal";
- *pp = cur->next;
- if(cur->next) cur->next->prev=cur->prev;
- else tail = cur->prev;
- p = cur;
- tail->next = p;
- p->prev = tail;
- tail = p;
- p->next = NULL;
- cur->isnaughty = true;
- updatepn();
- return;
- }
- pp = &(cur->next);
- cur = cur->next;
- }
- }
- int main () {
- list A;
- string command;
- A.constructList();
- int num;
- cin >> command >> num;
- for (int i=0; i<num; i++) {
- A.insertBack();
- }
- for (;;) {
- cin >> command;
- if(command == "AppendList") {
- A.insertBefore();
- }
- else if(command == "End") {
- A.showList();
- return 0;
- }
- else if(command == "NaughtyKid") {
- cin >> num;
- for (int i=0; i<num; i++)
- A.findMove();
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement