In computer science, a stack is a linear table that is only inserted or deleted at the end of a table. It stores data according to the principle of "back-in-first-out". The data that comes first is pushed to the bottom of the stack, and the final data is placed at the top of the stack, when data needs to be read, the data will pop up from the top of the stack (the last data is read by the first, and it can only insert and delete special linear tables at one end. Pile up items in a bucket, first pile in under pressure, then one piece to pile up. Only one of the above items can be taken when you take the data. The heap and fetch operations are carried out on the top, and the bottom is usually not moving.
Stack is a data structure similar to bucket accumulation items. One End for deletion and insertion is called the stack top, and the other is called the stack bottom. Insert is generally called push and delete is called pop ). Stack is also known as a post-import, first-out table (LIFO table ).
1. Push)Algorithm
1. If top is greater than or equal to N, overflow information is provided and an error is handled. (check whether the stack is full before entering the stack. If the stack is full, overflow occurs. If not, perform ② );
2. Set Top = Top + 1 (Stack pointer plus 1, pointing to the stack address );
3. S (top) = x, end (X is the element of the new stack );
2. Stack rollback (POP) Algorithm
1. If top is less than or equal to 0, overflow information is provided and an error is handled. (check whether the stack is empty before stack rollback, and if it is empty, overflow occurs. If it is not empty, perform ② );
2. x = S (SOP) (the elements after stack rollback are assigned to X );
3. Top = Top-1, end (Stack pointer minus 1, pointing to the top of stack ).
Let's take a look at the stack implementation interface: Public Interface Istack < T >
{
Int Length ();
Bool Isempty ();
Void Clear ();
Void Push (T items );
T POP ();
T getpopvalue ();
}
The following Code Is the stack implementation class: Code
Public Class Jstack < T > : Istack < T >
{
Private Int Size;
Private T [] item;
Private Int Top;
Public T This [ int Index]
{< br> Get { return item [Index] ;}< br> set {item [Index] = value ;}< BR >}
Public int size
{< br> Get { return size ;}
set {size = value ;}
}
Public IntTop
{
Get{ReturnTop ;}
}
Public Jstack ( Int Length)
{
Top = - 1 ;
Size = Length;
Item = New T [length];
}
Public int length ()
{< br> return top + 1 ;
}
Public bool isempty ()
{< br> return (top = - 1 );
}
Public VoidClear ()
{
Top= -1;
}
Public Void Push (T items)
{
If (Top < Size - 1 )
{
Item [ ++ Top] = Items;
}
}
Public T POP ()
{
T TMP = Default (T );
If ( ! Isempty ())
{
TMP = Item [Top];
}
Return TMP;
}
Public T getpopvalue ()
{
If (Isempty ())
{
Throw New Argumentnullexception ( " Stack empty " , " Item null " );
}
Return Item [Top];
}
}
Perform the following tests:
Code
Try
{
Jstack < String > JS = New Jstack < String > ( 20 );
Console. writeline ( " ------ The stack has just been initialized ---- " );
Console. writeline (JS. Length ());
Console. writeline (JS. isempty (). tostring ());
JS. Push ( " A " );
JS. Push ( " B " );
JS. Push ( " C " );
JS. Push ( " D " );
Console. writeline ( " ------ Data has been inserted into the stack ---- " );
Console. writeline (JS. Length ());
Console. writeline (JS. isempty (). tostring ());
Console. writeline ("------ Fetch top stack data ----");
Console. writeline (JS. getpopvalue (). tostring ());
Console. writeline ( " ------ Clear stack ---- " );
JS. Clear ();
Console. writeline (JS. getpopvalue (). tostring ());
Console. Readline ();
}
Catch (Exception ex)
{
Console. writeline (ex. Message );
Console. Readline ();
}
When you need to reverse the object order, you can put the object in the stack in order, and then extract it from the stack, then the function is implemented.