C # stack of a set

Source: Internet
Author: User
ArticleDirectory
    • 1. Stack Definition
    • 2. Advantages
    • 3. Stack Constructor
    • 4. Stack attributes
    • 5. Stack Method
    • 6. Example of stack usage
    • 7. Remarks
1. Stack Definition

The system. Collections. Stack class indicates a simple post-first-out non-generic set of objects.

2. Advantages

1. A collection of later, first, and foremost.

2. Stack can accept null references as valid values and allow repeated Elements

3. Stack Constructor

Constructors

Note

Stack ()

Initialize a new instance of the stack class. The instance is empty and has the default initial capacity.

Stack (icollection)

Initializes a new instance of the stack class. The instance contains elements copied from the specified set and has the same initial capacity as the number of copied elements.

Stack (int32)

Initialize a new instance of the stack class. The instance is empty and has the specified initial capacity or default initial capacity (larger of the two capacities ).

4. Stack attributes

Attribute name

Note

Count

Obtains the number of elements contained in a stack.

5. Stack Method

Method Name

Note

Void clear ()

Remove all objects from the stack.

Bool contains (Object OBJ)

Determine whether an element is in the stack.

Object clone ()

Create a superficial copy of the stack.

Void copyto (array, int index)

Copy the stack from the specified array index to an existing one-dimensional array.

Object POP ()

Remove and return the object at the top of the stack.

Void push (Object OBJ)

Insert the object to the top of the stack.

Object PEEK ()

Returns the object at the top of the stack but does not remove it.

Object [] toarray ()

Copy the stack to the new array.

 

6. Example of stack usage

 

Code

  Class  Program
{
Public Static Void Main ()
{

// Create a stack
Stack mystack = New Stack ();
Mystack. Push ( " The " ); // Inbound Stack
Mystack. Push ( " Quick " );
Mystack. Push ( " Brown " );
Mystack. Push ( " Fox " );


// Print the value in the Set
Console. Write ( " Stack values: " );
Printvalues (mystack, ' \ T ' );

// Print the first element at the top of the stack and remove it.
Console. writeline ( " (POP) \ t {0} " , Mystack. Pop ());

// Print the value in the Set
Console. Write ( " Stack values: " );
Printvalues (mystack, ' \ T ' );

// Print the first element at the top of the stack and remove it.
Console. writeline ( " (POP) \ t {0} " , Mystack. Pop ());

// Print the value in the Set
Console. Write ( " Stack values: " );
Printvalues (mystack, ' \ T ' );

// Print the first element at the top of the stack
Console. writeline ( " (PEEK) \ t {0} " , Mystack. Peek ());

// Print the value in the Set
Console. Write ( " Stack values: " );
Printvalues (mystack, ' \ T ' );
Console. Read ();
}


Public Static Void Printvalues (ienumerable mycollection, Char Myseparator)
{
Foreach (Object OBJ In Mycollection)
Console. Write ( " {0} {1} " , Myseparator, OBJ );
Console. writeline ();
}

}

 

 

7. Remarks

1. The stack capacity is the number of elements that can be saved by the stack. The default initial capacity of the stack is 10. When adding elements to a stack, the capacity is automatically increased as needed by reallocation.

2. The capacity is the number of elements that the stack can store. Count is the number of elements actually stored in the stack. The capacity is always greater than or equal to count. If count exceeds the capacity when an element is added, the capacity is automatically increased by allocating an internal array before the old element is copied and the new element is added.

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.