PHP implements two-way linked list and stack. PHP implements bidirectional linked list and stack bidirectional linked list Copy code? Php two-way linked list classHero {public $ prenull; first pointer public $ no; rank public $ name; name public $ nextnull; then PHP implements two-way linked list and stack
Two-Way linked list
// Two-Way linked list
Class Hero
{
Public $ pre = null; // Front pointer
Public $ no; // ranking
Public $ name; // name
Public $ next = null; // Post pointer
/**
* Constructor: declares the linked list header.
*/
Public function _ construct ($ no = '', $ name = '')
{
$ This-> no = $ no;
$ This-> name = $ name;
}
/**
* Insert
*/
Static public function addHero ($ head, $ hero)
{
$ Cur = $ head;
$ IsExist = false;
// Determine 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 the security ranking.
// Locate the added location
While ($ cur-> next! = Null)
{
If ($ cur-> next-> no> $ hero-> no)
{// Jump out if the ranking is greater
Break;
}
Else if ($ cur-> next-> no ==$ hero-> no)
{// If it is equal to the rank, it indicates that this element exists.
$ IsExist = true;
Echo"
The same number cannot be added ";
}
$ Cur = $ cur-> next;
}
If (! $ IsExist)
{// If the element does not exist, insert it.
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"
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 searching
$ Cur = $ cur-> next;
}
If ($ isFind)
{
If ($ cur-> next! = Null)
{$ Cur-> next_pre = $ cur-> pre ;}
$ Cur-> pre-> next = $ cur-> next;
}
Else
{Echo"
No target found ";}
}
}
$ Head = new Hero ();
$ Hero1 = new Hero (1, '20140901 ');
$ Hero3 = new Hero (3, '20140901 ');
$ Hero2 = new Hero (2, '20140901 ');
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;
QQ20140706152241
Delete operation:
If ($ cur-> next! = Null)
$ Cur-> next-> pre = $ cur-> pre;
$ Cur-> pre-> next = $ cur-> next;
QQ20140706152857
Stack
Class myStack
{
Private $ top =-1;
Private $ maxSize = 5;
Private $ stack = array ();
Public function push ($ val)
{
If ($ this-> top ==$ this-> maxSize)
{
Echo"
Full ";
}
$ This-> top ++;
$ This-> stack [$ this-> top] = $ val;
}
Public function showStack ()
{
If ($ this-> top =-1)
{
Echo"
Stack is empty! ";
Return;
}
For ($ I = $ this-> top; $ I>-1; $ I --)
{
Echo"
Stack [". $ I."] = ". $ this-> stack [$ I];
}
}
Public function pop ()
{
If ($ this-> top =-1)
{
Echo"
Stack is empty! ";
Return;
}
$ Val = $ this-> stack [$ this-> top];
$ This-> top --;
Echo"
". $ Val;
}
}
$ Mystack = new myStack;
$ Mystack-> push ('20140901 ');
$ Mystack-> push ('20140901 ');
$ Mystack-> showStack ();
$ Mystack-> pop ();
$ Mystack-> pop ();
?>
Stack: a linear table that is limited to insert and delete operations at one end of the table. It is also known as the Last In First Out (LIFO) or the First In First Out (First In Last Out) linear table.
Stack can be implemented on a computer in multiple ways:
Hard stack: use some Register groups in the CPU or similar hardware or special areas using memory. This type of stack has limited capacity, but is fast;
Soft stack: this type of stack is mainly implemented in memory. The stack capacity can be very large. In terms of implementation methods, there are two dynamic and static modes.
Top: The end of a table that allows insert or delete operations. Use the stack top pointer to indicate the top elements of the stack.
Bottom stack (Bottom): It is a fixed end, also known as the header.
Empty stack: when there are no elements in the table, it is called an empty stack.
The Stack's chain storage structure is called the chain stack, which is a single-chain table with limited operations. The insert and delete operations can only be performed at the header position. Therefore, the chain stack does not need to append header nodes as a single-chain table. The top pointer of the stack is the head pointer of the chain table.
Of course, the array API in php contains push and pop functions.
Al linked list? Php // bidirectional linked list class Hero {public $ pre = null; // The front pointer public $ no; // rank public $ name; // name public $ next = null; // The suffix refers...