121 lines
2.7 KiB
C++
121 lines
2.7 KiB
C++
#include <stdio.h>
|
|
|
|
template <class E> class Node {
|
|
private:
|
|
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 {
|
|
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() {
|
|
List<int> aList(0);
|
|
aList.insert(0, 1);
|
|
int n;
|
|
int cmd, x, y;
|
|
scanf("%d", &n);
|
|
while (n) {
|
|
scanf("%d %d", &cmd, &x);
|
|
switch (cmd) {
|
|
case 1: {
|
|
scanf("%d", &y);
|
|
aList.insert(x, y);
|
|
break;
|
|
}
|
|
case 2: {
|
|
Iterator<int> iter = aList.iterate();
|
|
while (iter.peek() != x) {
|
|
iter.next();
|
|
}
|
|
printf("%d\n", iter.peekNext());
|
|
break;
|
|
}
|
|
case 3: {
|
|
aList.remove(x);
|
|
}
|
|
}
|
|
n--;
|
|
}
|
|
|
|
Iterator<int> iter = aList.iterate();
|
|
iter.next();
|
|
while (true) {
|
|
int content = iter.peek();
|
|
if (content == 0) {
|
|
break;
|
|
}
|
|
printf("%d\n", content);
|
|
iter.next();
|
|
}
|
|
return 0;
|
|
} |