C # data structure and algorithm series (4) Stack

Source: Internet
Author: User

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]
{
Get {return item [index];}
Set {item [index] = value ;}
}

Public int Size
{
Get {return size ;}
Set {size = value ;}
}

Public int Top
{
Get {return top ;}
}

 

Public JStack (int length)
{
Top =-1;
Size = length;
Item = new T [length];
}

Public int Length ()
{
Return top + 1;
}

Public bool IsEmpty ()
{
Return (top =-1 );
}

Public void Clear ()
{
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 is 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 ("");
Js. Push ("B ");
Js. Push ("C ");
Js. Push ("D ");
Console. WriteLine ("------ the stack has inserted data ----");
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)
{

Related Article

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.