C # Programming (50)----------Stack

Source: Internet
Author: User

Stack

Stacks and queues are a very similar container , their difference is that the queue is FIFO first , and the stack is LIFO .

Stack and stack<t> like a queue , the stack also provides generics and non-generic versions .

Stack method :

Method

Description

Pop ()

Read the stack from the top of the stack and delete the element

Push ()

Store data , there is a stack top

Peek ()

Read from the top of the stack, but do not delete

Case :

Using System;

Using System.Collections;

Using System.Collections.Generic;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

namespace Stack

{

Class Program

{

static void Main (string[] args)

{

stack<string> stack = new stack<string> ();

Stack. Push ("A");

Stack. Push ("B");

Stack. Push ("C");

foreach (var item in stack)

{

Console.WriteLine (item);

}

because the stack is LIFO, the result is CBA

The above we use the enumerator , is not to change the elements , can not be deleted

Now we use Pop to read the output

Console.WriteLine (" read elements using Pop method ");

while (stack. count!=0)

{

Console.WriteLine (Stack. Pop ());

}

Console.WriteLine (" stack size after Pop : {0}", stack.) Count);

Console.readkey ();

}

}

}

I. Creating a stack

the constructor for the Stack class provides three overloaded forms :

constructor

Description

Public Stack ()

Create

Public Stack (Icollec tion Col)

Use icollection collection of copied elements to create stack example Span style= "Font-family:times New Roman" >,

public Stack (int initialcapacity)

Create stack

Case :

Stack sack = new stack ();// Use default capacity

Stack sack1 = new stack (new string[5] {" stack element One ", " stack element two ", " stack element three ", " stack element four ", " stack element five "});// use the geometry element in the string array to initialize the Stack object

Stack sack2 = new stack,// Create a Stack object and specify the elements

Two. elements into the stack

In order to push elements into the stack , you can call the Stack class's Push method . The declaration of this method is as follows :

public virtual void Push (Object obj)

This method requires a parameter of type Object obj, which represents the object to be pushed into the stack . Case :

Stack sk = new stack ();

Sk. Push ("1");

Sk. Push ("2");

Sk. Push ("3");

Sk. Push ("4");

Sk. Push ("5");

Display the contents of the stack

foreach (var item in SK)

{

Console.WriteLine (item);

}

you can see that the order of the element's amount is the first in the stack , conforming to the LIFO specification

Console.readkey ();

Three. Element out of Stack

The element out stack refers to the:Removed fromStack top elements , and returns a reference to this element . You can implement element out-of-stack >pop method Font-family:times New Roman ">. stack Also available are peek method , used to get the top element object , This method does not remove the top element . The declarations of these two methods are as follows :

Public virtual Object Peek ();

Public virtual Object Pop ();

Case :

Stack sk = new stack ();

Sk. Push ("1");

Sk. Push ("2");

Sk. Push ("3");

Sk. Push ("4");

Sk. Push ("5");

Display the contents of the stack

foreach (var item in SK)

{

Console.WriteLine (item);

}

you can see that the order of the element's amount is the first in the stack , conforming to the LIFO specification

Console.WriteLine (" stack top element is : {0}", SK.) Peek ());;

Console.WriteLine (" Remove the top element : {0}", SK.) Pop ());

Display the contents of the stack

foreach (var item in SK)

{

Console.WriteLine (item);

}

Console.readkey ();

C # Programming (50)----------Stack

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.