1 #include <iostream>
2
3 using namespace std;
4
5 class DblList;// 前置声明
6
7 class DblListNode
8 {
9 friend class DblList;
10 public:
11 int data;
12 DblListNode *llink, *rlink;
13
14 };
15
16 class DblList
17 {
18
19 public:
20 DblList()
21 {
22 first = new DblListNode();
23 first->rlink = first;
24 first->llink = first;
25
26
27 }
28 void Insert(DblListNode*,DblListNode*);//前参数节点插入到后参数节点的右边
29 void Delete(DblListNode*);
30
31 //private:
32
33 DblListNode *first;//first是个指针,写完这里再回去写DblList的构造函数
34 };
35
36
37 void DblList::Insert(DblListNode *p, DblListNode *x)//p、x分别是指针,指向各自节点
38 {
39
40 p->llink = x;
41 p->rlink = x->rlink;
42 x->rlink->llink = p;//注意顺序 要先写这个
43 x->rlink = p;
44 }
45
46 void DblList::Delete(DblListNode *x)
47 {
48 if(x == first) cerr << "Deletion of head node not permitted." << endl;
49
50 else
51 {
52
53 x->llink->rlink = x->rlink;
54 x->rlink->llink = x->llink;
55
56 }
57
58 }
59
60
61 int main()
62 {
63
64 cout << "Hello 双向链表!" << endl;
65
66 DblList intList;
67 DblListNode node1,node2,node3,node4,node5;
68 node1.data = 10;
69 node2.data = 20;
70 node3.data = 30;
71 node4.data = 40;
72 node5.data = 50;
73
74
75 intList.Insert( &node1,intList.first);
76 intList.Insert( &node2,intList.first);
77 intList.Insert( &node3,intList.first);
78 intList.Insert( &node4,intList.first);
79 intList.Insert( &node5,intList.first);
80 intList.Delete(&node4);
81 //intList.Delete(&node4);
82 //intList.Delete(&node3);
83
84 cout << intList.first->rlink->data << endl;
85 cout << intList.first->rlink->rlink->data<< endl;
86 cout << intList.first->rlink->rlink->rlink->data << endl;
87 cout << intList.first->rlink->rlink->rlink->rlink->data << endl;
88 cout << intList.first->rlink->rlink->rlink->rlink->rlink->data << endl;
89
90 return 0;
91 }