Data Structure Note 4 Stack

Source: Internet
Author: User
Stack definition and concept Stack is a linear table that only inserts and deletes operations at one end of the table.
( 1 ) Usually referred to as the end of the insert or delete stack is the top, and the other end is called the bottom of the stack ).
( 2 When there are no elements in the table, it is called an empty stack.
( 3 Stack is a linear table of last in first out (LIFO.
Stack modification is based on the principle of "back-to-first-out. Each deletion (rollback) is always in the current stack " Latest " The last element to be inserted into the stack. The first element to be inserted is placed at the bottom of the stack. Stack in C #CodeImplementation

Using System;

Namespace Everydaystudy. Data Structure
{
Public   Class Dapstack
{
Private   Object [] _ Array;
Private   Const   Int _ Defacapcapacity =   10 ;
Private   Int _ Size;

PublicDapstack ()
{
_ Array= New Object[_ Defacapcapacity];
_ Size= 0;
}

Public Dapstack ( Int Initialcapacity)
{
If (Initialcapacity < 0 )
{
Throw   New Argumentoutofrangeexception ( " Stack space cannot be less than zero " );
}
If (Initialcapacity < _ Defacapcapacity)
{
Initialcapacity = _ Defacapcapacity;
}

_ Array= New Object[Initialcapacity];
_ Size= 0;
}

Public   Virtual Object Pop ()
{
If (_ Size = 0 )
{
Throw New Invalidoperationexception ( " There is no data in the stack. " );
}
Object Obj2 = _ Array [ -- This . _ SIZE];
_ Array [_ SIZE] =   Null ;
Return Obj2;
}

Public   Virtual   Void Push ( Object OBJ)
{
If (_ Size = _ Array. length)
{
Object [] Destinationarray =   New   Object [ 2 * _ Array. Length];
_ Array = Destinationarray;
}
_ Array [_ size ++ ] = OBJ;
}

PublicVirtual IntCount
{
Get
{
Return_ Size;
}
}

}
}

Others

There seems to be nothing to pay attention to, probably because the stack structure is exposed too much when we go to school, so we can understand this stack.

I will not talk about other things. I will use this book to write about the wireless applications of other people's families so late. Well, if you have any questions, try again. Wait until you get to bed and tomorrow will go to work.

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.