C # Data structures and algorithms-sequential stacks

Source: Internet
Author: User

Theoretical basis:

A stack is a linear table of operations that is scoped to the end of the table. The end of the table is inserted, deleted, and so on, so it has a special meaning, the footer is called the top of the stack, the other end is fixed, called the bottom of the stack (Bottom). Empty stack is called when there are no data elements in the stack.

650) this.width=650; "class=" AlignCenter "src=" Http://img0.tuicool.com/faIr6fF.png "alt=" Fair6ff.png "/>


Stacks can be divided into sequential stacks and chain stacks.

A contiguous storage space is used to store the data elements in the stack, which are called sequential stacks (Sequence stacks). Similar to the sequential table, a one-dimensional array is used to hold the data elements in the sequential stack. The top indicator top is set at the end of the array labeled 0, top with the insert and delete changes, when the stack is empty, top=-1.

The chain stack is usually represented by a single-linked list, and its implementation is a simplification of the single-linked list. Therefore, the structure of the link stack node is the same as the structure of the single-linked table node. Since the operation of the chain stack is only performed at one end, the stack is set at the head of the list for ease of operation and does not require a head node.


Stacks in C #:

c#2.0 The following versions only provide a non-generic stack class that inherits the ICollection, IEnumerable, and ICloneable interfaces.

C#2.0 provides a generic stack<t> class that inherits the Ienumerable<t>, ICollection, and IEnumerable interfaces, with more specific information about the generic stack<t> class, Readers can refer to the. NET Framework for books.


C # itself has written stacks and queues, we can directly use, here to achieve the following, is for a deeper understanding.


The stack is usually written as: s= (A1,a2,..., an), S is the 1th letter of the English word stack. The elements in the stack are stacked in the order of A1,a2,a3,...,an, and the stack is the top element of the stack. That is, the order of the stack and the stack in reverse, an first out of the stack, A1 the last out of the stack. Therefore, the operation of the stack is based on the principle of last-in, first-out, or LIFO, or advanced-out, or filo, so the stack is also known as the LIFO table or the Filo table.

For stacks, the main operations are:

1. Construct an empty stack

2. Empty stack: Clearstack ()

3, the stack length (get the number of elements in the stack): Stacklength ()

4, return to the top of the stack element: GetTop ()

5. Press Stack operation: Push (Object E)

6. Stack operation (Stack operation): Pop ()

7, judge whether the stack is empty: IsEmpty ()

8, judge whether the stack is full: isfull ()

Instance:

using system; class stack{    int maxsize;         //capacity of sequential stacks     object[] data;       //array for storing data in stacks     int top;             //Indicator Stack Top      public object this[int index]     {        get{return data[index];}         set{ data[index] = value;}     }    //Stack capacity attribute     public int maxsize     {        get         {            return maxsize ;         }        set        {             maxsize = value;         }    }    //gets the property of the top of the stack     public int  Top    {        get         {            return  top;        }    }    // Initialize stack     public stack (int size) with constructors     {         data = new object[size];         maxsize = size;        top = -1;     }    //the length of the stack (number of elements in the stack)     public int stacklength ()      {        return top+1;    }     //emptying order stack     public void clearstack ()     {         top = -1;    }     Determine if the sequence stack is empty     public bool isempty ()     {         if  (top == -1)         {             return true;         }        else         {            return  false;  &nbsp     }    }    //judgment sequence stack is full      public bool isfull ()     {         if  (top == maxsize-1)         {             return true;         }        else        {             return false;         }    }    //in-stack operation      Public void push (object e)     {         if (Isfull ())         {           &nbsP; console.writeline ("The Stack is full! ");            return;         }        data[++top] = e;     }    //the stack operation and returns the element     public object pop of the stack ( )     {        object temp = null;         if  (IsEmpty ())          {            console.writeline ("Stack is empty!") ");            return temp;         }        temp = data[top];         top --;         Return temp;    }    //get stack top data elements     public  Object gettop ()     {        if  (IsEmpty ( ))         {             console.writeline ("Stack is empty!") ");            return null;         }        return data[top];     }} //definition Test Class Class test{    static void main ()      {        stack s = new stack (10) ;         random r = new random ();      //randomly generated data         int mid;         for (int i = 0;i<10;i++)          {            mid =  ( int) R.next (10,100)     //get integer data between 10~100              s.push (mid);          //Data Stack              console.writeline ("The data {0} is stacked, now the number of elements in the stack is: {1}",                 mid,s.stacklength ()) ;        }         Console.WriteLine ("\ n prepare to stack data element 888:");         s.push (888);          console.writeline ("Number of elements in stack: {0}", S.stacklength ());         conSole. WriteLine ();         for (int j = 0;j<10;j++)//loop out stack         {             console.writeline ("The data {0} stack, the number of elements in the stack is now: {1}",                 s.pop (), S.stacklength ());         }    }}

Example 2: The following example demonstrates the use of the stack (stacks):

using system;using system.collections;namespace collectionsapplication{     Class program{        static void main (string[]  args) {            stack st = new  stack ();             st. Push (' A ');             st. Push (' M ');             st. Push (' G ');             st. Push (' W ');             console.writeline ("Current  stack:  ");            foreach  ( CHAR&NBSP;C&NBSP;IN&NBSP;ST) {               &nbSp; console.write (c +  " ");}             console.writeline ();             st. Push (' V ');             st. Push (' H ');             console.writeline ("The  next poppable value in stack: {0} ", St. Peek ());             console.writeline ("Current  stack:  ");            foreach  ( CHAR&NBSP;C&NBSP;IN&NBSP;ST) {                 console.write (c +  " ");}             console.writeline ();          &nbsP;  console.writeline ("removing values "); St. Pop (); St. Pop (); St. Pop ();             console.writeline ("Current  stack:  ");            foreach  (char &NBSP;C&NBSP;IN&NBSP;ST) {                 console.write (c +  " ");}             console.readline ();}}} When the above code is compiled and executed, it produces the following results://current stack://w g m a//the next poppable  value in stack: h//current stack://h v w g m a//removing  Values//current stack://g m a


Reference:
Http://www.cnblogs.com/Richet/archive/2008/10/16/1313045.html
Http://www.w3cschool.cc/csharp/csharp-stack.html
http://blog.163.com/fujl_2008/blog/static/10378107200810972618942/

This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1659997

C # Data structures and algorithms-sequential stacks

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.