PHP implements two-way linked list and stack, and c implements two-way linked list

Source: Internet
Author: User

PHP implements two-way linked list and stack, and c implements two-way linked list

I wrote an article in PHP to implement one-way linked list and sort one-way linked list. Portal: http://www.cnblogs.com/yydcdut/p/3777760.html. The two-way linked list has been written and I will mention it again: http://www.cnblogs.com/yydcdut/p/4262661.html.

Next, let's share the implementation of two-way linked list and stack. Although the Code was previously written, it is easy to understand the code written in PHP!

Two-way linked list

<? Php // bidirectional linked list class Hero {public $ pre = null; // The front pointer public $ no; // rank public $ name; // name public $ next = null; // post pointer/*** constructor, declaring the chain table header */public function _ construct ($ no = '', $ name = '') {$ this-> no = $ no; $ this-> name = $ name;}/*** insert */static public function addHero ($ head, $ hero) {$ cur = $ head; $ isExist = false; // checks whether the linked list is empty. if ($ cur-> next = null) {$ cur-> next = $ hero; $ hero-> pre = $ cur;} else {// if it is not an empty node, add a ranking. // locate the added Location while ($ cur-> next! = Null) {if ($ cur-> next-> no> $ hero-> no) {// jump out of break if the ranking is higher than the ranking ;} else if ($ cur-> next-> no ==$ hero-> no) {// if it is equal to the rank, $ isExist = true indicates that this element exists; echo "<br> the same number cannot be added";} $ cur = $ cur-> next;} if (! $ IsExist) {// if the element does not exist, execute the insert operation if ($ cur-> next! = Null) {$ hero-> next = $ cur-> next;} $ hero-> pre = $ cur; if ($ cur-> next! = Null) {$ hero-> next-> pre = $ hero;} $ cur-> next = $ hero; }}// traverse static public function showHero ($ head) {$ cur = $ head; while ($ cur-> next! = Null) {echo "<br> No :". $ cur-> next-> no. "Name :". $ 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;} // continue to find $ cur = $ cur-> next ;} if ($ isFind) {if ($ cur-> next! = Null) {$ cur-> next_pre = $ cur-> pre;} $ cur-> pre-> next = $ cur-> next ;} else {echo "<br> no target found" ;}}$ head = new Hero (); $ hero1 = new Hero (1, '20140901 '); $ hero3 = new Hero (3, '000000'); $ hero2 = new Hero (2, '000000'); Hero: addHero ($ head, $ hero1); Hero:: addHero ($ head, $ hero3); Hero: addHero ($ head, $ hero2); Hero: showHero ($ head); Hero: delHero ($ head, 2); Hero: showHero ($ head);?>

Insert a two-way linked list:

if($cur->next!=null)    $hero->next=$cur->next;$hero->pre=$cur; if($cur->next!=null)    $hero->next->pre=$hero;$cur->next=$hero;

 

If ($ cur-> next! = Null) $ cur-> next-> pre = $ cur-> pre; $ cur-> pre-> next = $ cur-> next;

 

Stack

<? Php class myStack {private $ top =-1; private $ maxSize = 5; private $ stack = array (); public function push ($ val) {if ($ this-> top ==$ this-> maxSize) {echo "<br> full" ;}$ this-> top ++; $ this-> stack [$ this-> top] = $ val;} public function showStack () {if ($ this-> top =-1) {echo "<br> stack is empty! "; Return;} for ($ I = $ this-> top; $ I>-1; $ I --) {echo" <br> stack [". $ I. "] = ". $ this-> stack [$ I] ;}} public function pop () {if ($ this-> top =-1) {echo "<br> stack is empty! "; Return ;}$ val = $ this-> stack [$ this-> top]; $ this-> top --; echo" <br> pop-up ". $ val ;}$ mystack = new myStack; $ mystack-> push ('20140901'); $ mystack-> push ('20160901 '); $ mystack-> showStack (); $ mystack-> pop ();?>

I am the dividing line of tiantiao

"PHP linked list" Portal: http://www.cnblogs.com/yydcdut/p/3777760.html

 

 

 

Reprinted please indicate the source: http://www.cnblogs.com/yydcdut


Java uses a two-way linked list to simulate a Stack. only partial Stack functions are implemented.

Public class Stack {

Private Node top;

Public Stack (){
This. top = null;
}

Public void push (Node node ){
If (node = null)
Return;
If (this. top = null ){
This. top = node;
Node. setNext (null );
Node. setPre (null );
}
Else {
This. top. setNext (node );
Node. setPre (this. top );
Node. setNext (null );
This. top = node;
}
}

Public Node pop (){
If (this. top = null)
Return null;
Node curr = this. top;
Node pre = curr. getPre ();
Pre. setNext (null );
This. top = pre;
Return curr;
}

Public Node top (){
Return this. top;
}

Public boolean isEmpty (){
Return this. top = null? True: false;
}

Public void empty (){
This. top = null;
}

Public static void main (String [] args ){
Stack stack = new Stack ();
Node n1 = new Node (1 );
Node n2 = new Node (2 );
Node n3 = new Node (3 );

System. out. println (stack. isEmpty ());
Stack. push (n1 );
System. out. println (stack. top (). getValue ());
Stack. push (n2 );
Stack. push (n3 );
System. out. println (stack. pop (). getValue ());
Stack. empty ();
}
}

Class Node {
Private int value;
Private Node next;
Private Node pre;

Public Node (int value, Node next, Node pre ){
This. value = value;
This. next = next;
This. pre = pre;
}

Public Node (int value ){
This. value = value;
This. next = null;
This. pre = null;
}

Public int getValue (){
Return value;
}

Public void setValue (int value ){
This. value = value;
}

Public Node getNext (){
Return next;
}

Public void setNext (Node next ){
This. next = next;
}

Public Node getPre (){
Return pre;
}

Public void setPre (Node pre ){
This. pre = pre;
}
}
Using a linked list to implement stacks or what stacks or queue data are stored in a container
Than using built-in arrays to implement queues
Int a [100];
Operation example: Insert new elements when all existing elements are moved
(When the implementation efficiency is relatively low)

The following figure shows how to select a linked list for containers to join the queue:
The constructor node (C struct) needs to enter the element into the queue and then use the linked list insert operation to insert a new node into the linked list.

The linked list is used to implement stacks or data containers in a queue. The linked list is used to insert nodes and delete nodes to implement stack and queue operations.

Than using a linked list to implement several functions written in queue C
// Enter the team
Void insert (pointing to the queue (linked list) pointer to the elements to be added to the queue );
// Team out
Void delete (pointer to queue (linked list );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.