-
Notifications
You must be signed in to change notification settings - Fork 0
/
test03.cpp
76 lines (66 loc) · 1.33 KB
/
test03.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<vector>
#include<stdlib.h>
#include<algorithm>
using namespace std;
struct ListNode {
int val;
ListNode* next;
};
struct dList
{
int data;
dList* prior; //指向前驱的指针
dList* next; //指向后继的指针
};
void dlistReverse(dList* head) //循环双链表的倒置
{
dList* p = head->next,
* q = head->prior;
//p、q不重合并且不交换
while (q != p && p->prior != q)
{
//交换两节点的数据元素
int t; t = p->data;
p->data = q->data;
q->data = t;
//p指针从头到尾遍历,q指针从尾到头遍历
p = p->next;
q = q->prior;
}
}
void add_dList(dList* head, int n)
{
dList* p = head->next;
dList* newNode = new dList{ n, nullptr, head };
do
{
p = p->next;
}while (p->next != head);
p->next = newNode;
newNode->prior = p;
head->prior = newNode;
}
void printNode(dList* head)
{
dList* p = head;
while (p->next != head)
{
cout << p->data << ' ';
p = p->next;
}
cout << p->data << ' ';
cout << endl;
}
int main()
{
dList* l = new dList;
l->data = 0;
l->next = l;
l->prior = l;
for (int i = 1; i < 6; i++)
add_dList(l, i * 2);
printNode(l);
dlistReverse(l);
printNode(l);
}