-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path单向链表.php
155 lines (127 loc) · 2.7 KB
/
单向链表.php
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
<?php
/*单向链表学习 童晓斌 2016-08-10*/
//链表节点
class node{
public $id;
public $name;
public $next;
public function __construct($id,$name){
$this->id = $id;
$this->name = $name;
$this->next = null;
}
}
//单链表
class singelLinkList {
private $header;
public function __construct($id=null,$name=null){
$this->header = new node($id,$name);
}
//获取链表长度
public function getLinklength(){
$i = 0;
$current = $this->header;
while($current->next != null){
$i++;
$current = $current->next;
}
return $i;
}
//添加节点数据
public function addLink($node){
$current = $this->header;
while($current->next!=null){
if($current->next->id > $node->id){
break;
}
$current = $current->next;
}
$node->next = $current->next;
$current->next = $node;
}
//删除节点列表
public function delLink(){
$current = $this->header;
$flag=true;
while($current->next!=null){
if($current->next->id==$id){
$flag = false;
break;
}
$current = $current->next;
}
if($flag){
echo '未找到'.$id.'节点!';
}else{
$current->next = $current->next->next;
}
}
//获取链表
public function getLinkList(){
$current = $this->header;
if($current->next==null){
echo '链表为空';
return;
}
while($current->next!=null){
echo 'id='.$current->next->id.'name='.$current->next->name.'<br/>';
if($current->next->next==null){
break;
}
$current = $current->next;
}
}
//获取节点名字
public function getLinkname($id){
$current = $this->header;
if($current->next==null){
echo '无此id名称';
return;
}
$flag = true;
while($current->next!=null){
if($current->next->id==$id){
$flag = false;
break;
}
$current = $current->next;
}
if($flag){
echo '无此id名称';
}else{
echo '此节点id是'.$current->next->name;
}
}
//更新节点名字
public function reNodename($name,$newname){
$current = $this->header;
if($current->next==null){
echo '无此节点名字';
return;
}
$flag = true;
while($current->next!=null){
if($current->next->name==$name){
$current->next->name = $newname;
$flag = false;
break;
}
$current = $current->next;
}
if($flag){
echo '无此节点名字';
}else{
echo $name.'节点的名字已更新为'.$newname;
}
}
}
$singellinklist = new singelLinkList();
$singellinklist->addLink(new node(1,'tongxiaobin'));
$singellinklist->addLink(new node(2,'tongxiaobin1'));
$singellinklist->addLink(new node(3,'tongxiaobin3'));
$singellinklist->addLink(new node(4,'tongxiaobin4'));
$singellinklist->getLinkname(1);
$singellinklist->reNodename('tongxiaobin','hahaha');
$singellinklist->getLinkname(1);
echo $singellinklist->getLinklength();
?>