本文介紹了如何實現單鏈表,希望大家耐心學習。 ? ? ? ? ?
//節(jié)點class node{ //初始化變量,包括存儲的內容 和 下一個數據的指針 public $id = 0; public $data = ''; public $next = null; //構造函數,設置存儲內容的數據 public function __construct($id, $data) { $this->id = $id; $this->data = $data; } }//單鏈表 class singelLinkList{ private $header; //鏈表頭節(jié)點 //添加節(jié)點數據 public function addLink($id = null, $name = null) { $node = new node ($id, $name); $current = $this->header; if (!$current) { $this->header = $node; } else { # 鏈表頭插 $node->next = $current; $this->header = $node; # 鏈表尾插 /*# 循環(huán),獲取對象中最后一個元素 while ($current->next != null) { $current = $current->next; } # 最后一個元素的next指針指向$node $current->next = $node;*/ } } public function delLink($id = null, $name = null) { $current = $this->header; # 循環(huán) while ($current->next != null) { # 查找待刪除元素 $delCurrent 的上一個元素 if ($current->next->id == $id) { $delCurrent = $current->next; # 查找待刪除元素 $delCurrent 的下一個元素 $current->next = $delCurrent->next; # 刪除元素 $delCurrent $delCurrent = null; break; } $current = $current->next; } } }$lists = new singelLinkList(); $lists->addLink(1, 'aaaaaa'); $lists->addLink(2, 'bbbbbb'); $lists->addLink(3, 'cccccc'); $lists->addLink(4, 'dddddd'); $lists->addLink(5, 'eeeeee'); $lists->delLink(4);echo '<pre>'; print_r($lists);
以上就是在python的開發(fā)過程中如何進行單鏈表的實現(代碼)的詳細內容,更多請關注php中文網其它相關文章!
python怎么學習?python怎么入門?python在哪學?python怎么學才快?不用擔心,這里為大家提供了python速學教程(入門到精通),有需要的小伙伴保存下載就能學習啦!
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號