Maple's Blog.

链表

字数统计: 955阅读时长: 5 min
2019/07/06 Share

实现的代码如下:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cassert>
using namespace std;

template <typename T>
class LinkList{
private:
struct Node{
T e;
Node *next;
Node(T e){
this->e=e;
this->next=NULL;
}
Node(T e,Node *next){
this->e=e;
this->next=next;
}
Node(){
this->e=NULL;
this->next=NULL;
}
};
int size;
Node *dummyHead;
public:

LinkList(){
this->size=0;
dummyHead=new Node();
}


//获得链表中元素的个数
int getSize(){
return size;
}

//判断链表是否为空
bool isEmpty(){
return size==0;
}

//在索引为index的位置添加元素e
void add(int index,T e){
assert(index>=0&&index<=size);
Node *prev=dummyHead;
for(int i=0;i<index;i++){
prev=prev->next;
}
// Node *newnode=new Node();
// newnode->e=e;
// prev->next=newnode->next;
// prev->next=newnode;
prev->next=new Node(e,prev->next);
size++;
}

//在链表的第一个节点添加元素e
void addFirst(T e){
add(0,e);
}

//在链表的最后一个节点添加元素e
void addLast(T e){
add(size,e);
}

//获得索引为index位置的元素e
T get(int index){
assert(!isEmpty());
Node *node=dummyHead->next;
for(int i=0;i<index;i++){
node=node->next;
}
return node->e;
}

//获得第一个节点的元素
T getFirst(){
return get(0);
}

//获得最后一个节点的元素
T getLast(){
return get(size-1);
}

//修改index位置的值为e
void set(int index,T e){
assert(!isEmpty());
Node *node=dummyHead->next;
for(int i=0;i<index;i++){
node=node->next;
}
node->e=e;
}

//链表中是否包含元素为e的节点
bool contains(T e){
assert(!isEmpty());
Node *node=dummyHead->next;
while(node!=NULL){
if(node->e==e)
return true;
node=node->next;
}
return false;
}

//删除索引为index位置的元素
T remove(int index){
assert(!isEmpty());
Node *node=dummyHead;
for(int i=0;i<index;i++){
node=node->next;
}
Node *retnode=node->next;
node->next=retnode->next;
retnode->next=NULL;
size--;
return retnode->e;
}

//删除最后一个节点的元素
T removeLast(){
return remove(size-1);
}

//删除第一个节点的元素
T removeFirst(){
return remove(0);
}

//删除元素为e的节点
void removeElement(T e){
assert(!isEmpty());
Node *node=dummyHead;
while(node->next->e!=e)
{
node=node->next;
}
Node *retnode=node->next;
node->next=retnode->next;
retnode->next=NULL;
}

//打印链表
void PrintLinkList(){
assert(!isEmpty());
Node *node=dummyHead->next;
while(node!=NULL){
cout<<node->e;
if(node->next){
cout<<"->";
}
node=node->next;
}
cout<<endl;
}

};

int main()
{


LinkList <int>*arr=new LinkList<int>();
for(int i=0;i<5;i++){
arr->add(i,i+1);
}

cout<<"初始化的链表为:";
arr->PrintLinkList();
cout<<"链表的元素总数为:";
cout<<arr->getSize()<<endl;

arr->addFirst(12);
arr->addLast(666);
cout<<"在链表的第一个位置插入12这个元素,在链表的最后一个位置插入666这个元素:";
arr->PrintLinkList();

cout<<"得到索引为1的链表元素:";
cout<<arr->get(1)<<endl;
cout<<"得到链表中第一个节点的元素:";
cout<<arr->getFirst()<<endl;
cout<<"得到链表中最后一个节点的元素:";
cout<<arr->getLast()<<endl;

cout<<"修改索引为3的节点的元素为888:";
arr->set(3,888);
cout<<"打印数组:";
arr->PrintLinkList();

cout<<"判断链表中是否含有888这个元素,如果有返回1,没有返回0:";
cout<<arr->contains(888)<<endl;

cout<<"删除索引为2的节点:";
arr->remove(2);
arr->PrintLinkList();

cout<<"删除第一个节点和最后一个节点";
arr->removeFirst();
arr->removeLast();
arr->PrintLinkList();

arr->removeElement(888);
cout<<"删除888这个元素的节点,删除元素后的链表为:";
arr->PrintLinkList();


return 0;
}

原文作者:Maple

原文链接:http://yoursite.com/2019/07/06/链表/

发表日期:July 6th 2019, 10:43:49 pm

更新日期:July 6th 2019, 10:43:49 pm

版权声明:本文采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可

CATALOG