My road to c++
C++博客
首页
新随笔
联系
聚合
管理
随笔-13 评论-0 文章-2 trackbacks-0
单向链表的实现(Alpha 1.0)
1
/**/
/*
*
2
Linked List base on dynamic array
3
Version: 1.0
4
Member function as follow:
5
size()
6
push_back(T) // inset an elm
7
erase(T) // delete an elm
8
empty() // if it is an empty list
9
print()
10
find(T&) // find an elm
11
12
Use C++ template
13
*
*/
14
#include
<
iostream
>
15
#include
<
string
>
16
using
namespace
std;
17
18
template
<
typename T
>
19
class
Node
20
{
21
public
:
22
T data;
23
Node
<
T
>*
next;
24
Node()
{ next
=
NULL;}
25
}
;
26
27
template
<
typename T
>
28
class
LinkedList
29
{
30
private
:
31
Node
<
T
>*
head;
32
int
size;
33
public
:
34
LinkedList();
35
~
LinkedList();
36
int
Size()
{
return
this
->
size; }
37
bool
empty()
{
return
size
==
0
; }
38
void
push_back(
const
T
&
);
39
const
T front()
const
{
return
head
->
data; }
40
void
find(
const
T
&
);
41
void
erase(
const
T
&
);
42
void
print()
const
;
43
}
;
44
45
template
<
typename T
>
LinkedList
<
T
>
::LinkedList()
46
{
47
size
=
0
;
48
head
=
new
Node
<
T
>
;
49
head
->
next
=
NULL;
50
}
51
52
template
<
typename T
>
LinkedList
<
T
>
::
~
LinkedList()
53
{
54
size
=
0
;
55
delete head;
56
}
57
58
template
<
typename T
>
void
LinkedList
<
T
>
::push_back(
const
T
&
one)
59
{
60
Node
<
T
>*
p
=
head;
61
if
( size
==
0
) head
->
data
=
one;
62
else
{
63
while
(p
->
next
!=
NULL) p
=
p
->
next;
64
Node
<
T
>*
new_node
=
new
Node
<
T
>
;
65
new_node
->
data
=
one;
66
new_node
->
next
=
NULL;
67
p
->
next
=
new_node;
68
}
69
size
++
;
70
}
71
72
template
<
typename T
>
void
LinkedList
<
T
>
::find(
const
T
&
s)
73
{
74
Node
<
T
>*
p
=
head;
75
int
index
=
-
1
;
76
while
(p
->
data
!=
s
&&
p
->
next)
{ p
=
p
->
next; index
++
; }
77
if
(p
->
next
&&
p
->
data
!=
s)
{ cout
<<
"
No such an element!
"
<<
endl;}
78
else
if
( p
->
data
==
s ) cout
<<
"
It's in the position of
"
<<
++
index
<<
endl;
79
else
cout
<<
"
It's in the position of
"
<<
index
<<
endl;
80
}
81
82
template
<
typename T
>
void
LinkedList
<
T
>
::erase(
const
T
&
s)
83
{
84
Node
<
T
>*
p
=
head;
85
Node
<
T
>*
temp
=
head;
86
while
( p
->
data
!=
s
&&
p
->
next )
87
{
88
temp
=
p;
89
p
=
p
->
next;
90
}
91
if
(p
->
next
==
NULL)
92
{
93
if
( p
->
data
==
s )
94
{
95
p
=
temp;
96
p
->
next
=
NULL;
97
size
--
;
98
}
99
else
100
cout
<<
"
No such an element!
"
<<
endl;
101
}
102
else
103
{
104
p
=
temp;
105
p
=
p
->
next
->
next;
106
size
--
;
107
}
108
}
109
110
template
<
typename T
>
void
LinkedList
<
T
>
::print()
const
111
{
112
Node
<
T
>*
p
=
head;
113
while
( p
->
next
!=
NULL )
114
{
115
cout
<<
p
->
data
<<
"
"
;
116
p
=
p
->
next;
117
}
118
cout
<<
p
->
data
<<
endl;
119
}
120
121
//
Test Function
122
#include
<
iostream
>
123
int
main()
124
{
125
LinkedList
<
int
>
list;
126
int
n;
127
cout
<<
"
Please input the number which you want to create!
"
<<
endl;
128
cin
>>
n;
129
cout
<<
"
Input the number you want to add in the array.\n
"
;
130
cout
<<
"
----------------------------------------------------------------\n\n
"
;
131
while
(n
--
)
132
{
133
int
a;
134
cin
>>
a;
135
list.push_back(a);
136
}
137
cout
<<
"
You input the array as follow
"
<<
endl;
138
list.print();
139
cout
<<
"
Do you want to delete an element ?
"
<<
endl;
140
cout
<<
"
Yes/No?
"
<<
endl;
141
string
s;
142
cin
>>
s;
143
if
( s
==
"
yes
"
||
s
==
"
YES
"
)
144
{
145
cout
<<
"
which number?
"
<<
endl;
146
int
tem;
147
cin
>>
tem;
148
list.erase(tem);
149
cout
<<
"
Now the array as follow
"
<<
endl;
150
list.print();
151
}
152
cout
<<
"
Do you want to find an element?
"
<<
endl;
153
cout
<<
"
Yes/No?
"
<<
endl;
154
string
s2;
155
cin
>>
s2;
156
if
( s2
==
"
yes
"
||
s2
==
"
YES
"
)
157
{
158
cout
<<
"
which number?
"
<<
endl;
159
int
tem;
160
cin
>>
tem;
161
list.find(tem);
162
}
163
164
cout
<<
"
Now the size of the array is
"
<<
list.Size()
<<
endl;
165
cout
<<
"
The first number is :
"
<<
list.front()
<<
endl;
166
cout
<<
endl;
167
cout
<<
"
----------------------------------------------------------------\n\n
"
;
168
system(
"
pause
"
);
169
return
0
;
170
171
}
172
posted on 2009-02-22 21:57
亦夏
阅读(169)
评论(0)
编辑
收藏
引用
所属分类:
DataStruct
只有注册用户
登录
后才能发表评论。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
相关文章:
栈的实现(顺序结构)Alpha 2.0
二查查找树的实现(链式) Alpha1.0
队列的实现(链式结构) Alpha 1.0
队列的实现(数组结构) Alpha 1.0
栈的实现(链式结构)Alpha 1.0
栈的实现(顺序结构)Alpha 1.0
循环链表的实现(Alpha)
双向链表的实现(Alpha1.0 )
单向链表的实现(Alpha 1.0)
线性表的顺序表示和实现(静态数组方式)(Alpha1.0)
网站导航:
博客园
IT新闻
BlogJava
知识库
博问
管理
<
2024年11月
>
日
一
二
三
四
五
六
27
28
29
30
31
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
1
2
3
4
5
6
7
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔分类
(13)
ACM (2)
DataStruct(10)
TopCoder(1)
随笔档案
(13)
2009年5月 (1)
2009年4月 (3)
2009年3月 (5)
2009年2月 (4)
文章分类
(2)
ACM(1)
Alogrithm
C part of c++
C++0X
Datestruct
Game of c++
General(1)
Object-orient of c++
Others
Puzzle
STL
Template c++
Visual c++
文章档案
(2)
2009年2月 (1)
2009年1月 (1)
搜索
最新随笔
1. Search and DP
2. 栈的实现(顺序结构)Alpha 2.0
3. HowEasy
4. HDOJ_1010.Tempter of the Bone(DFS)
5. 二查查找树的实现(链式) Alpha1.0
6. 队列的实现(链式结构) Alpha 1.0
7. 队列的实现(数组结构) Alpha 1.0
8. 栈的实现(链式结构)Alpha 1.0
9. 栈的实现(顺序结构)Alpha 1.0
10. 循环链表的实现(Alpha)
最新评论
阅读排行榜
1. HDOJ_1010.Tempter of the Bone(DFS)(906)
2. 队列的实现(链式结构) Alpha 1.0(464)
3. HowEasy(435)
4. Search and DP(399)
5. 线性表的顺序表示和实现(静态数组方式)(Alpha1.0)(338)
评论排行榜
1. 线性表的顺序表示和实现(静态数组方式)(Alpha1.0)(0)
2. 单向链表的实现(Alpha 1.0)(0)
3. 双向链表的实现(Alpha1.0 )(0)
4. 循环链表的实现(Alpha)(0)
5. 栈的实现(顺序结构)Alpha 1.0(0)