把书上的链表搬到电脑上时,想要在main中检验但是不知道怎么开始
用代码块功能插入代码,请勿粘贴截图
template <typename E> class Link {
public:
E element;
Link* next;
Link(const E& elemval, Link* nextVal = NULL) {
element = elemval;
next = nextVal;
}
Link(Link* nextval = NULL) {
next = nextval;
}
};
template <typename E> class LList : public E::template List<E>
{
private:
/* data */
Link<E>* head;
Link<E>* tail;
Link<E>* curr;
int cnt;
void init() {
curr = tail = head = new Link<E>;
cnt = 0;
}
void removeall() {
while (head != NULL) {
curr = head;
head = head->next;
delete curr;
}
}
public:
LList(int size/* = defaultSize*/) {
init();
}
~LList() {
removeall();
}
void print() const;
void clear() {
removeall();
init();
}
void insert(const E& it) {
curr->next = new Link<E>(it, curr->next);
if (tail == curr) {
tail = curr->next;
}
cnt++;
}
void append(const E& it) {
tail = tail->next = new Link<E>(it, NULL);
cnt++;
}
E remove() {
// Assert(curr->next != NULL, "No element");
E it = curr->next->element;
Link<E>* ltemp = curr->next;
if (tail == curr->next) {
tail = curr;
}
curr->next = curr->next->next;
delete ltemp;
cnt--;
return it;
}
void moveToStart() {
curr = head;
}
void moveToEnd() {
curr = tail;
}
void prev() {
if (curr == head) {
return;
}
Link<E>* temp = head;
while (temp->next != curr) {
temp = temp->next;
}
curr = temp;
}
void next() {
if (curr != tail) {
curr = curr->next;
}
}
int length() const {
return cnt;
}
int currPos() const {
Link<E>* temp = head;
int i;
for (i = 0; curr != temp; i++) {
temp = temp->next;
}
return i;
}
void moveToPos(int pos) {
//assert((pos >= 0) && (pos <= cnt), "Position out of range");
curr = head;
for (int i = 0; i < pos; i++) {
curr = curr->next;
}
}
const E& getValue() const {
// assert(curr->next != NULL, "No value");
return curr->next->element;
}
void reserve() {
int i = 0;
Link<E>* pre = head;
Link<E>* it = head->next;
for (; it->next != NULL; it = it->next, pre = pre->next) {
it = new Link<E>(it->element, pre);
}
Link<E>* temp = tail;
tail->next = it;
tail->element = head->elememt;
head = tail;
}
};