# 14205: 【原4205】Turing Award

### 题目描述

author: Shuhao Li 原OJ链接：https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4205

# Description

## Sample Input

``````10
insert 1982 Stephen A. Cook
insert 2002 Adi Shamir
list 1
list 2
delete 2
insert 2014 Michael Stonebraker
insert 2004 Robert E. Kahn
list 3
delete 1
list 1
``````

## Sample Output

``````1982 Stephen A. Cook
2014 Michael Stonebraker
2004 Robert E. Kahn
``````

## BugenZhao's solution

``````//
// Created by BugenZhao on 2019/3/16.
//
// 链表基本练习

#include <iostream>
#include <string>

using namespace std;

class Node {
public:
int year = 0;
string name;
Node *next = nullptr;
};

static inline bool less(const Node &a, const Node &b) {
return a.year == b.year ? a.name < b.name : a.year < b.year;
}

int size;

public:
size = 0;
head = new Node{0, "666", nullptr};
}

void insert(int year, const string &name) {
Node *toInsert = new Node{year, name, nullptr};
Node *p = head;
while (p != nullptr) {
if (p->next == nullptr) {
p->next = toInsert;
break;
}
if (less(*toInsert, *p->next)) {
Node *tmp = p->next;
p->next = toInsert;
toInsert->next = tmp;
break;
}
p = p->next;
}
++size;
}

void del(int i) {
i -= 1;
Node *p = head;
while (i--) {
p = p->next;
}
Node *tmp = p->next;
p->next = tmp->next;
delete tmp;
--size;
}

void list(int i) {
Node *p = head;
while (i--) {
p = p->next;
}
cout << p->year << " " << p->name << endl;
}

Node *p = head->next;
while (p != nullptr) {
Node *tmp = p->next;
delete p;
p = tmp;
}
}
};

int main() {
int N;
string op;
cin >> N;
while (N--) {
cin >> op;
if (op == "insert") {
int year;
string name;
cin >> year;
getline(cin, name);
linkedList.insert(year, name.substr(1, name.size() - 1));
} else if (op == "delete") {
int i;
cin >> i;
} else if (op == "list") {
int i;
cin >> i;
}
}
return 0;
}
``````

## skyzh's solution

``````#include <iostream>
#include <cstring>
using namespace std;

public:
struct Node {
int yrs;
char name[40];
Node* next;
head = new Node;
}
Node* list(int i) const {
Node* ptr = head;
while (i > 0) {
--i;
ptr = ptr->next;
}
return ptr;
}
void insert(Node* data) {
Node* ptr = head->next, *prev = head;
while (ptr &&
(ptr->yrs < data->yrs ||
(ptr->yrs == data->yrs) && strcmp(ptr->name, data->name) < 0)) {
prev = ptr;
ptr = ptr->next;
}
prev->next = data;
data->next = ptr;
}
void del(int i) {
Node* ptr = head;
while (i > 1) {
--i;
ptr = ptr->next;
}
Node *tmp = ptr->next;
ptr->next = tmp->next;
delete tmp;
}
Node* ptr = head;
while (ptr) {
Node* next = ptr->next;
delete ptr;
ptr = next;
}
}
};

int main() {
char cmd[100];
int N;
cin >> N;
for (int i = 0; i < N; i++) {
cin >> cmd;
if (strcmp(cmd, "insert") == 0) {
ptr->next = NULL;
cin >> ptr->yrs;
cin.getline(ptr->name, 40);
lst.insert(ptr);
} else if (strcmp(cmd, "delete") == 0) {
int cnt;
cin >> cnt;
lst.del(cnt);
} else if (strcmp(cmd, "list") == 0) {
int cnt;
cin >> cnt;
LinkedList::Node* ptr = lst.list(cnt);
cout << ptr->yrs << " " << ptr->name << endl;
}
}
return 0;
}
``````