dbkcpp
C++博客
首页
新随笔
联系
聚合
管理
posts - 1, comments - 1, trackbacks - 0
单链表操作(包括创建、添加、删除、倒置等操作)
人要忍得住诱惑。
本文主要是关于单链表的一些操作,包括创建、添加、删除、倒置,不多少了,上代码。
1
2
/**/
/*
************************************************************************
3
* Copyright (c) 2009
4
* All rights reserved.
5
*
6
* 文件名称:link.cpp
7
* 摘 要:链表操作
8
*
9
* 当前版本:1.0
10
* 作 者:fighter
11
* 完成日期:
12
*
13
**************************************************************************
*/
14
15
#include
<
stdlib.h
>
16
#include
<
stdio.h
>
17
18
typedef
struct
stdata
19
{
20
int
num;
21
stdata
*
next;
22
}
STDATA;
23
24
int
showlink(STDATA
*
head)
25
{
26
STDATA
*
next
=
head;
27
28
while
( NULL
!=
next)
29
{
30
printf(
"
%d \n
"
, next
->
num);
31
next
=
next
->
next;
32
}
33
34
printf(
"
show end!\n
"
);
35
return
0
;
36
}
37
38
int
createlink( STDATA
*
head )
39
{
40
int
i
=
1
;
41
STDATA
*
next
=
NULL;
42
STDATA
*
prev
=
head;
43
44
if
( NULL
==
head)
45
{
46
return
1
;
47
}
48
head
->
num
=
0
;
49
head
->
next
=
NULL;
50
51
for
( ; i
<
10
; i
++
)
52
{
53
next
=
( STDATA
*
)malloc(
sizeof
( STDATA));
54
if
( NULL
==
next)
55
{
56
return
1
;
57
}
58
next
->
num
=
i;
59
next
->
next
=
NULL;
60
61
prev
->
next
=
next;
62
prev
=
next;
63
}
64
65
return
0
;
66
}
67
68
int
linkadd( STDATA
*
head,
int
pos,
int
num)
69
{
70
int
ret
=
0
;
71
STDATA
*
prev
=
head;
72
STDATA
*
next
=
head;
73
STDATA
*
data
=
NULL;
74
75
while
( prev
->
num
!=
pos
&&
prev
->
next
!=
NULL)
76
{
77
prev
=
prev
->
next;
78
next
=
prev
->
next;
79
}
80
81
if
( prev
->
num
==
pos)
82
{
83
data
=
( STDATA
*
)malloc(
sizeof
( STDATA));
84
if
( NULL
==
data)
85
{
86
return
1
;
87
}
88
89
data
->
num
=
num;
90
91
prev
->
next
=
data;
92
data
->
next
=
next;
93
}
94
95
return
0
;
96
}
97
98
int
linkdel( STDATA
*
head,
int
pos)
99
{
100
int
ret
=
0
;
101
STDATA
*
prev
=
head;
102
STDATA
*
next
=
head;
103
STDATA
*
data
=
NULL;
104
105
while
( next
->
num
!=
pos
&&
next
->
next
!=
NULL)
106
{
107
prev
=
next;
108
next
=
next
->
next;
109
}
110
111
if
( next
->
num
==
pos)
112
{
113
data
=
next;
114
next
=
next
->
next;
115
prev
->
next
=
next;
116
free( data);
117
}
118
119
return
0
;
120
}
121
122
STDATA
*
linkreverse( STDATA
*
head)
123
{
124
int
ret
=
0
;
125
STDATA
*
prev
=
head;
126
STDATA
*
next
=
prev
->
next;
127
STDATA
*
ptmp
=
next
->
next;
128
129
prev
->
next
=
NULL;
130
131
while
( NULL
!=
ptmp)
132
{
133
next
->
next
=
prev;
134
prev
=
next;
135
next
=
ptmp;
136
ptmp
=
ptmp
->
next;
137
}
138
139
next
->
next
=
prev;
140
141
return
next;
142
}
143
144
int
main(
int
argc,
char
**
argv)
145
{
146
STDATA
*
head
=
( STDATA
*
)malloc(
sizeof
( STDATA));
147
createlink( head);
148
showlink( head);
149
150
linkadd( head,
4
,
100
);
151
showlink( head);
152
153
linkdel( head,
100
);
154
showlink( head);
155
156
head
=
linkreverse( head);
157
showlink( head);
158
159
return
0
;
160
}
161
posted on 2009-07-06 16:28
似水流年
阅读(287)
评论(0)
编辑
收藏
引用
所属分类:
练手的小例子
、
数据结构和算法
只有注册用户
登录
后才能发表评论。
相关文章:
单链表操作(包括创建、添加、删除、倒置等操作)
找出数组的所有子集,要求子集的所有元素和为一定值,如{1,2,3,4,5,8}有子集{1,2,3,4}、{2,8}等。
求两个数的最大公约数
求1-100内的所有素数
网站导航:
博客园
博客园最新博文
博问
管理
<
2026年6月
>
日
一
二
三
四
五
六
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
8
9
10
11
常用链接
我的随笔
我的评论
我参与的随笔
留言簿
给我留言
查看公开留言
查看私人留言
随笔分类
个人感悟
个人日记
随笔档案
2009年7月 (1)
文章分类
c/c++那些事
UML相关
读书感悟(1)
服务器设计(1)
客户端设计
练手的小例子(4)
设计模式
数据结构和算法(2)
数据库设计
文章档案
2010年4月 (1)
2009年7月 (6)
新闻分类
业界动态
相册
登山
篮球场
收藏夹
服务器设计
服务器设计相关
sodme
那谁的技术博客
搜索
最新评论
1. re: 找出数组的所有子集,要求子集的所有元素和为一定值,如{1,2,3,4,5,8}有子集{1,2,3,4}、{2,8}等。
用个 & 看着更正统些。
--any