鄙人虽然学过数据结构,无奈上课划水实验不好好做。兹此暑假决心痛改前非,重新实现一遍书上的代码,这次实现的是双链表,只是大概地实现了某些功能,如有错误欢迎批评加粗样式斧正
#include
#include
class node {
public:
int val;
node* pre;
node* next;
node(const int i, node* a, node* b) {
val = i;
pre = a;
next = b;
}
node(node* a = NULL, node* b = NULL) {
pre = a;
next = b;
}
};
class list:public node {
private:
node* head; node* tail; node* cur;
public:
list() {
cur = tail = head = new node;
}
~list() {
while (head != NULL)
{
cur = head;
head = head->next;
delete cur;
}
}
void Create(int n);
void Prev();
void Next();
void Insert(const int a);
void delet();
void print();
};
void list::Create(int n) {
node* t = new node;
head = t;
for (int i = 0; i < n; i++)
{
std::cin >> t->val;
node* p = new node;
t->next = p;
p->pre = t;
p->next = NULL;
t = p;
}
tail = t;
std::cin >> t->val;
}
void list::Prev() {
cur = cur->pre;
}
void list::Next() {
cur = cur->next;
}
void list::Insert(const int a) {
node* p,q;
q = cur->next;
q->pre = cur;
p =new node(a, cur, q);
cur->next = p;
q->pre = p;
}
void list::delet() {
node p = cur;
cur->pre->next = cur->next;
cur->next->pre = cur->pre;
cur = cur->next;
delete p;
}
void list::print() {
node* p = head;
while (p != tail)
{
std::cout << p->val << std::endl; p = p->next;
}
std::cout << tail->val;
}