链表结构

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
< ?php
    class Links
    {
        public $name = null;
        public $next = null;
        public function __construct($name,Links $next=null)
        {
            $this->name = $name;
            if(isset($next))
            {
                $this->next = $next;
            }
        }
    }
 
    function create(Links $link,Links $child)
    {
        if(!$link->next)
        {
            $link->next = $child;
        }
        else
        {
            $tmp = $link->next;
            $link->next = $child;
            $child->next = $tmp;
        }
    }
 
    function find(Links $link,$node)
    {
        $tmp = $link;
        $ret = null;
        while ($tmp->next)
        {
            if($tmp->name == $node)
            {
                $ret = $tmp;
                break;
            }
            $tmp = $tmp->next;
        }
        return $ret;
    }
 
    function getParent(Links $link,$node)
    {
        $tmp = $link;
        $ret = null;
        do
        {
            if(!$tmp->next)break;
            if($tmp->next->name == $node)
            {
                $ret = $tmp;
            }
        }
        while ($tmp = $tmp->next);
        return $ret;
    }
 
    function delete(Links $link,$node)
    {
        $tmp = getParent($link,$node);
        if($tmp->next->next)
        {
            $tmp->next = $tmp->next->next;
        }
        else
        {
            $tmp->next = null;
        }
    }
    $link = new Links(" ");
    create($link,new Links("aa"));
    create($link,new Links("bb"));
    print_r(find($link,"bb"));
    print_r(getParent($link,'aa'));
    print_r($link);
?>
 

Comments: 1

Leave a reply »

 
  • 建议使用SPL linked list

     
     
     
  • Leave a Reply
     
    Your gravatar
    Your Name
     
     
     
     
     
 
About This Website

Lamp development & SEO & Plan of Website & Project Managment

Learn more »
Follow Us (SNS)
Help & Support

more about Bruce.xu»

Get in touch

QQ: +252339382
Email: shjuto @ gmail.com

Online contact form »