AC!
This commit is contained in:
114
2023202/main.cpp
114
2023202/main.cpp
@@ -1,121 +1,47 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
template <class E> class Node {
|
struct Node{
|
||||||
private:
|
int prev, next;
|
||||||
E content;
|
|
||||||
Node<E> *next;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Node(const E &_content) : content(_content), next(this){};
|
|
||||||
Node(const E &_content, Node<E> *nextPtr)
|
|
||||||
: content(_content), next(nextPtr){};
|
|
||||||
void setNext(Node<E> *_ptr) {
|
|
||||||
this->next = _ptr;
|
|
||||||
}
|
|
||||||
E &getContent() {
|
|
||||||
return this->content;
|
|
||||||
}
|
|
||||||
Node<E> *getNextPtr() {
|
|
||||||
return this->next;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class E> class Iterator {
|
Node map[100000];
|
||||||
private:
|
|
||||||
Node<E> *current;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Iterator(Node<E> *start) : current(start){};
|
|
||||||
E &peekNext() {
|
|
||||||
return this->current->getNextPtr()->getContent();
|
|
||||||
}
|
|
||||||
void next() {
|
|
||||||
this->current = this->current->getNextPtr();
|
|
||||||
}
|
|
||||||
E &peek() {
|
|
||||||
return this->current->getContent();
|
|
||||||
}
|
|
||||||
Node<E> *peekNode() {
|
|
||||||
return this->current;
|
|
||||||
}
|
|
||||||
void insertHere(const E &newItem) {
|
|
||||||
this->current->setNext(
|
|
||||||
new Node<E>(newItem, this->current->getNextPtr()));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class E> class List {
|
|
||||||
private:
|
|
||||||
// int _length;
|
|
||||||
Node<E> head;
|
|
||||||
|
|
||||||
public:
|
|
||||||
List(const E &dummyValue)
|
|
||||||
: head(Node<E>(dummyValue)){
|
|
||||||
// this->_length = 0;
|
|
||||||
};
|
|
||||||
void insert(const E &target, const E &newItem) {
|
|
||||||
Iterator<E> iter = Iterator<E>(&this->head);
|
|
||||||
// Remember to add a loop to stop at length!!;
|
|
||||||
while (iter.peek() != target) {
|
|
||||||
iter.next();
|
|
||||||
}
|
|
||||||
iter.insertHere(newItem);
|
|
||||||
// this->_length++;
|
|
||||||
}
|
|
||||||
void remove(const E &target) {
|
|
||||||
Iterator<E> iter = Iterator<E>(&this->head);
|
|
||||||
while (iter.peekNext() != target) {
|
|
||||||
iter.next();
|
|
||||||
}
|
|
||||||
Node<E> *toBeDeleted = iter.peekNode()->getNextPtr();
|
|
||||||
iter.peekNode()->setNext(toBeDeleted->getNextPtr());
|
|
||||||
delete toBeDeleted;
|
|
||||||
// this->_length--;
|
|
||||||
}
|
|
||||||
Iterator<E> iterate() {
|
|
||||||
return Iterator<E>(&this->head);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
List<int> aList(0);
|
map[0].prev = 1;
|
||||||
aList.insert(0, 1);
|
map[0].next = 1;
|
||||||
|
map[1].prev = 0;
|
||||||
|
map[1].next = 0;
|
||||||
int n;
|
int n;
|
||||||
int cmd, x, y;
|
int cmd, x, y;
|
||||||
scanf("%d", &n);
|
scanf("%d", &n);
|
||||||
while (n) {
|
while (n) {
|
||||||
scanf("%d %d", &cmd, &x);
|
scanf("%d %d", &cmd, &x);
|
||||||
switch (cmd) {
|
switch(cmd) {
|
||||||
case 1: {
|
case 1: {
|
||||||
scanf("%d", &y);
|
scanf("%d", &y);
|
||||||
aList.insert(x, y);
|
map[y].prev = x;
|
||||||
|
map[y].next = map[x].next;
|
||||||
|
map[x].next = y;
|
||||||
|
map[map[y].next].prev = y;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 2: {
|
case 2: {
|
||||||
Iterator<int> iter = aList.iterate();
|
printf("%d\n", map[x].next);
|
||||||
while (iter.peek() != x) {
|
|
||||||
iter.next();
|
|
||||||
}
|
|
||||||
printf("%d\n", iter.peekNext());
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 3: {
|
case 3: {
|
||||||
aList.remove(x);
|
map[map[x].prev].next = map[x].next;
|
||||||
|
map[map[x].next].prev = map[x].prev;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
n--;
|
n--;
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator<int> iter = aList.iterate();
|
int cur = map[0].next;
|
||||||
iter.next();
|
while (cur != 0) {
|
||||||
while (true) {
|
printf("%d\n", cur);
|
||||||
int content = iter.peek();
|
cur = map[cur].next;
|
||||||
if (content == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
printf("%d\n", content);
|
|
||||||
iter.next();
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user