PHP實現雙向鏈表、棧_PHP教程

來源:互聯網
上載者:User

PHP實現雙向鏈表、棧


雙向鏈表 複製代碼no=$no; $this->name=$name; } /** *插入 */ static public function addHero($head,$hero) { $cur = $head; $isExist=false; //判斷目前這個鏈表是否為空白 if($cur->next==null) { $cur->next=$hero; $hero->pre=$cur; } else { //如果不是空節點,則安排名來添加 //找到添加的位置 while($cur->next!=null) { if($cur->next->no > $hero->no) {//如果大於了排名,跳出 break; } else if($cur->next->no == $hero->no) {//如果等於排名,則代表有這個元素了 $isExist=true; echo "
不能添加相同的編號"; } $cur=$cur->next; } if(!$isExist) {//如果元素不存在,執行插入操作 if($cur->next!=null) {$hero->next=$cur->next;} $hero->pre=$cur; if($cur->next!=null) {$hero->next->pre=$hero;} $cur->next=$hero; } } } //遍曆 static public function showHero($head) { $cur=$head; while($cur->next!=null) { echo "
編號:".$cur->next->no."名字:".$cur->next->name; $cur=$cur->next; } } static public function delHero($head,$herono) { $cur=$head; $isFind=false; while($cur!=null) { if($cur->no==$herono) { $isFind=true; break; } //繼續找 $cur=$cur->next; } if($isFind) { if($cur->next!=null) {$cur->next_pre=$cur->pre;} $cur->pre->next=$cur->next; } else {echo "
沒有找到目標";} } } $head = new Hero(); $hero1 = new Hero(1,'1111'); $hero3 = new Hero(3,'3333'); $hero2 = new Hero(2,'2222'); Hero::addHero($head,$hero1); Hero::addHero($head,$hero3); Hero::addHero($head,$hero2); Hero::showHero($head); Hero::delHero($head,2); Hero::showHero($head);?>複製代碼雙向鏈表的插入操作: if($cur->next!=null) $hero->next=$cur->next;$hero->pre=$cur; if($cur->next!=null) $hero->next->pre=$hero;$cur->next=$hero; QQ20140706152241 刪除操作: if($cur->next!=null) $cur->next->pre=$cur->pre;$cur->pre->next=$cur->next; QQ20140706152857 棧 複製代碼top == $this->maxSize) { echo "
已經滿了"; } $this->top++; $this->stack[$this->top]=$val; } public function showStack() { if($this->top==-1) { echo "
棧為空白!"; return ; } for($i=$this->top;$i>-1;$i--) { echo "
stack[".$i."]=".$this->stack[$i]; } } public function pop() { if($this->top==-1) { echo "
棧為空白!"; return ; } $val=$this->stack[$this->top]; $this->top--; echo "
彈出".$val; } } $mystack = new myStack; $mystack->push('111'); $mystack->push('222'); $mystack->showStack(); $mystack->pop(); $mystack->pop();?>複製代碼 棧(Stack):是限制在表的一端進行插入和刪除操作的線性表。又稱為後進先出LIFO (Last In First Out)或先進後出FILO (First In Last Out)線性表。 棧在電腦的實現有多種方式: 硬堆棧:利用CPU中的某些寄存器組或類似的硬體或使用記憶體的特殊地區來實現。這類堆棧容量有限,但速度很快;軟堆棧:這類堆棧主要在記憶體中實現。堆棧容量可以達到很大。在實現方式上,又有動態方式和靜態方式兩種棧頂(Top):允許進行插入、刪除操作的一端,又稱為表尾。用棧頂指標(top)來指示棧頂元素。 棧底(Bottom):是固定端,又稱為表頭。 空棧:當表中沒有元素時稱為空白棧。 棧的鏈式儲存結構稱為鏈棧,是運算受限的單鏈表。其插入和刪除操作只能在表頭位置上進行。因此,鏈棧沒有必要像單鏈表那樣附加頭結點,棧頂指標top就是鏈表的頭指標。 當然,php中的數組API裡面帶的有push和pop函數。

http://www.bkjia.com/PHPjc/845432.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/845432.htmlTechArticlePHP實現雙向鏈表、棧 雙向鏈表 複製代碼 ?php //雙向鏈表 class Hero { public $pre=null;//前指標 public $no;//排名 public $name;//名字 public $next=null;//後指...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.