Stack sequential storage structure _ C # implementation 2 [ArrayList implementation]

Source: Internet
Author: User

Reference from: [Michael McMillan. Data Structures and Algorithms Using C #]

The implementation here is to use ArrayList. When a new data item is added to the stack, you do not need to worry about adjusting the table size. I think this implementation is clear, where p_index points directly to the top element of the stack.

Using System;
Using System. Collections;
Namespace Stack
{
Class CStack
{
Private int p_index; // stack top
Private ArrayList list;
// Attributes
Public int Count
{
Get {return list. Count ;}
}
// Constructor
Public CStack ()
{
List = new ArrayList ();
P_index =-1;
}

Public void Push (object item) // stack entry
{
List. Add (item );
P_index ++;
}

Public object Pop () // output Stack
{
Object obj = list [p_index];
List. RemoveAt (p_index );
P_index --;
Return obj;
}

Public void Clear () // Clear the stack
{
List. Clear ();
P_index =-1;
}

Public object Peek () // returns the top element of the stack.
{
Return list [p_index];
}

}
}

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.