-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp2.cpp
More file actions
84 lines (73 loc) · 1.84 KB
/
Copy pathp2.cpp
File metadata and controls
84 lines (73 loc) · 1.84 KB
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
77
78
79
80
81
82
83
84
#include "ADTList.cpp"
// 顺序表实现
template<typename T>
void reverse_SquList(SquList<T> *list){
for(auto l=list->begin(),r=list->end()-1;r-l>0;++l,--r)
swap(*l,*r);
}
// 双向循环链表实现
template<typename T>
void reverse_LinkList(LinkList<T> *list){
auto cur=list->end().getNode();
do{
swap(cur->pre,cur->nxt);
cur=cur->pre;
}while(cur!=list->end().getNode());
}
// 单链表实现
template<typename T>
struct Node{
T elem;
Node *nxt;
Node(T elem=T()):elem(elem),nxt(nullptr) {}
};
template<typename T>
Node<T> *insert(Node<T> *cur,T elem){
Node<T> *now=new Node<T>(elem);
now->nxt=cur->nxt;
cur->nxt=now;
return now;
}
template<typename T>
void traverse(Node<T> *head){
head=head->nxt;
while(head!=nullptr)
cout<<(head->elem)<<' ',
head=head->nxt;
cout<<'\n';
}
template<typename T>
void reverse_LinkList(Node<T> *head){
Node<T> *p=head->nxt,*q;
head->nxt=nullptr;
for(;p!=nullptr;p=q){
q=p->nxt;
p->nxt=head->nxt;
head->nxt=p;
}
}
int main(){
int n; cin>>n;
SquList<int> *list1=new SquList<int>(n);
LinkList<int> *list2=new LinkList<int>;
Node<int> *list3=new Node<int>,*rear=list3;
for(int i=0,x;i<n;i++){
cin>>x;
list2->push_back(x);
rear=insert(rear,x);
list1->setElem(i,x);
}
cout<<"SeqList:"<<endl;
cout<<"Before reverse(): "; list1->traverse();
reverse_SquList(list1);
cout<<"After reverse(): "; list1->traverse();
cout<<"LinkList:"<<endl;
cout<<"Before reverse(): "; list2->traverse();
reverse_LinkList(list2);
cout<<"After reverse(): "; list2->traverse();
cout<<"LinkList:"<<endl;
cout<<"Before reverse(): "; traverse(list3);
reverse_LinkList(list3);
cout<<"After reverse(): "; traverse(list3);
return 0;
}