My road to c++
C++博客
首页
新随笔
联系
聚合
管理
随笔-13 评论-0 文章-2 trackbacks-0
循环链表的实现(Alpha)
1
/**/
/*
*
2
Loop List (just like LinkedList)
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
struct
Node
20
{
21
T data;
22
Node
<
T
>*
next;
23
Node()
{ next
=
NULL;}
24
}
;
25
26
template
<
typename T
>
27
class
LoopList
28
{
29
private
:
30
Node
<
T
>*
head;
31
int
size;
32
public
:
33
LoopList()
{ head
=
new
Node
<
T
>
; size
=
0
;}
34
~
LoopList()
{ delete head; size
=
0
;}
35
bool
empty()
{
return
size
==
0
;}
36
int
Size()
{
return
size;}
37
T front()
{
return
head
->
data;}
38
void
push_back(
const
T
&
);
39
void
find(
const
T
&
);
40
void
erase(
const
T
&
);
41
void
print()
const
;
42
}
;
43
44
template
<
typename T
>
void
LoopList
<
T
>
::push_back(
const
T
&
one)
45
{
46
47
if
(size
==
0
)
{ head
->
data
=
one; head
->
next
=
head;}
48
else
49
{
50
Node
<
T
>*
p
=
new
Node
<
T
>
;
51
p
=
head;
52
while
( p
->
next
!=
head ) p
=
p
->
next;
53
Node
<
T
>*
new_node
=
new
Node
<
T
>
;
54
new_node
->
data
=
one;
55
new_node
->
next
=
head;
56
p
->
next
=
new_node;
57
}
58
size
++
;
59
delete p;
60
}
61
62
template
<
typename T
>
void
LoopList
<
T
>
::find(
const
T
&
one)
63
{
64
Node
<
T
>*
p
=
new
Node
<
T
>
;
65
p
=
head;
66
if
( head
->
data
==
one ) cout
<<
"
It's the First element!\n
"
;
67
else
68
{
69
while
(p
->
data
!=
one
&&
p
->
next
!=
head )p
=
p
->
next;
70
if
( p
->
next
==
head
&&
p
->
data
!=
one )
71
cout
<<
"
No such an element in this List!
"
<<
endl;
72
else
if
(p
->
next
==
head
&&
p
->
data
==
one ) cout
<<
"
It's the last element!
"
<<
endl;
73
else
cout
<<
"
It's in the position of
"
<<
size
-
1
<<
endl;
74
}
75
delete p;
76
}
77
78
template
<
typename T
>
void
LoopList
<
T
>
::erase(
const
T
&
one)
79
{
80
Node
<
T
>*
p
=
new
Node
<
T
>
;
81
Node
<
T
>*
temp
=
new
Node
<
T
>
;
82
Node
<
T
>*
temp_head
=
new
Node
<
T
>
;
83
temp_head
=
head;
84
p
=
head;
85
temp
=
head;
86
while
(p
->
data
!=
one
&&
p
->
next
!=
head)
{ temp
=
p; p
=
p
->
next;}
87
if
( p
->
next
==
head
&&
p
->
data
!=
one) cout
<<
"
No such an element!
"
<<
endl;
88
else
if
(p
==
head)
89
{
90
temp
=
p
->
next;
91
p
->
next
=
NULL;
92
head
=
temp;
93
p
=
head;
94
while
(p
->
next
!=
temp_head) p
=
p
->
next;
95
p
->
next
=
head;
96
size
--
;
97
}
98
else
{ p
=
temp; p
->
next
=
p
->
next
->
next; size
--
; }
99
delete p;
100
delete temp;
101
delete temp_head;
102
}
103
template
<
typename T
>
void
LoopList
<
T
>
::print()
const
104
{
105
Node
<
T
>*
p
=
new
Node
<
T
>
;
106
p
=
head;
107
while
( p
->
next
!=
head )
108
{
109
cout
<<
p
->
data
<<
"
"
;
110
p
=
p
->
next;
111
}
112
cout
<<
p
->
data
<<
endl;
113
delete p;
114
}
115
116
int
main()
117
{
118
LoopList
<
int
>
list;
119
int
n;
120
cout
<<
"
Please input the number which you want to create!
"
<<
endl;
121
cin
>>
n;
122
cout
<<
"
Input the number you want to add in the array.\n
"
;
123
cout
<<
"
----------------------------------------------------------------\n\n
"
;
124
while
(n
--
)
125
{
126
int
a;
127
cin
>>
a;
128
list.push_back(a);
129
}
130
cout
<<
"
You input the array as follow
"
<<
endl;
131
list.print();
132
cout
<<
"
Do you want to delete an element ?
"
<<
endl;
133
cout
<<
"
Yes/No?
"
<<
endl;
134
string
s;
135
cin
>>
s;
136
if
( s
==
"
yes
"
||
s
==
"
YES
"
)
137
{
138
cout
<<
"
which number?
"
<<
endl;
139
int
tem;
140
cin
>>
tem;
141
list.erase(tem);
142
cout
<<
"
Now the array as follow
"
<<
endl;
143
list.print();
144
}
145
cout
<<
"
Do you want to find an element?
"
<<
endl;
146
cout
<<
"
Yes/No?
"
<<
endl;
147
string
s2;
148
cin
>>
s2;
149
if
( s2
==
"
yes
"
||
s2
==
"
YES
"
)
150
{
151
cout
<<
"
which number?
"
<<
endl;
152
int
tem;
153
cin
>>
tem;
154
list.find(tem);
155
}
156
157
cout
<<
"
Now the size of the array is
"
<<
list.Size()
<<
endl;
158
cout
<<
"
The first number is :
"
<<
list.front()
<<
endl;
159
cout
<<
endl;
160
cout
<<
"
----------------------------------------------------------------\n\n
"
;
161
system(
"
pause
"
);
162
return
0
;
163
164
}
165
posted on 2009-02-24 23:16
亦夏
阅读(225)
评论(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
知识库
博问
管理
<
2009年2月
>
日
一
二
三
四
五
六
25
26
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
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)