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 () |
Using the default initial HA container creation stack |
Public stack (icollection col) |
Use icollection Set copied elements to create stack, |
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 ", " Span style= "font-family: Arial" > stack element three ", " stack element four ", " stack element five "&NBSP;});// used by string geometric elements in array initialization 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