Stack--powershell version

Source: Internet
Author: User
Tags shuffle

In the previous article on the queue, the queue is like a residential building in the garbage pipeline, from the entrance of the corridor of the rubbish into the garbage, the cleaners will be from the first floor of the exit of the garbage pipe to take away the rubbish. Each floor of the garbage channel entrance and the first floor of the garbage pipe exit between the formation of a queue, first thrown into the rubbish tunnel garbage will first reach the exit of the garbage channel, namely: first-out .

Stack is a more simple data structure, if the queue is a garbage channel, the stack is a garbage bin, first thrown into the garbage can be crushed to the bottom of the bucket, but it is still in the garbage will be poured out first. This last-in- first-out data structure is the stack.

The three elements of the queue are the team, the team head, the tail.

In PowerShell, you can use a dynamic array to represent the team, with Two variables representing the position of the team head and the end of the queue in the array.

The queue (garbage path) has two ends, the data from one end into the other . The head is called the tail of the team, out of the head called the team head .

stack (trash) only one end, from which the head from which the head out, into the top of the head (the garbage can be stood up).

In PowerShell, a dynamic array can be used to represent the stack, and the position of the stack at the top of the array is recorded with a variable .

$stack= new-Object System.Collections.ArrayList#Create a stack of numbers.$flag=$true while($flag){    $input= Read-host"Put a number into the stack"    $stack. ADD ([int]$input)    $choice= Read-host"Press ' C ' to continue, no other key to quit"    if($choice -ne' C ') {        $flag=$false    }}$top=$stack. count-1$stack _top=$stack[$top]#Show the top number of this stack.Write-host"The top of the stack is:"-Foregroundcolor Greenwrite-host$stack _top-foregroundcolor Green

The above code briefly describes the creation of Stacks and the method of viewing the top elements of the stack .
Now for a real-world example, you'll immediately understand what a stack is--playing poker.

First create a set of poker:

#Create a set of pokers.$pokers= new-Object System.Collections.ArrayList$pokerModel= @{"pokers"="2,3,4,5,6,7,8,9,10,j,q,k,a,smallking,bigking";"Colors"="Spade,heart,club,diamond"}$pokerEles=$pokerModel. Item ("pokers"). Split (",")$pokerColors=$pokerModel. Item ("Colors"). Split (",")foreach($pokerEle inch $pokerEles){    if(($pokerEle -eq "smallking")-or($pokerEle -eq "bigking"))    {        $pokers. ADD ($pokerEle)    }    Else    {         foreach($color inch $pokerColors)        {            $newPoker=$color+"_"+$pokerEle               $pokers. ADD ($newPoker)        }    }}

Then shuffle the card (put the first hand in the whole deck randomly, repeat 10,000 times):

 #  shuffle.  for  ( $i  = 0;  $i  -le  10000;  $i  ++ $ Randomplace  = get-random-min 0-max  $pokers  .count   $selectedPoker  =  $pokers  [0]   $pokers . RemoveAt (0)   $pokers . Insert (,  $selectedPoker  Span style= "color: #000000;" >)}

Then we put the wash cards into the "stack" (the card is the stack), like this. Then start licensing (every time the card is issued from the top pop out of the stack)--

#Push the cards into the stack.$myStack= new-Object System.Collections.Stackforeach($poker inch $pokers){    $myStack. Push ($poker)  }#Start the deal.Write-host"---Start dealing---"-Foregroundcolor Green$flag=$true while($flag){   $myStack. Pop ()$choice= Read-host"Press ' C ' to continue, no other key to quit"   if($choice -ne' C ') {       $flag=$false       }   Else{Write-host"---Continue dealing---"-Foregroundcolor Yellow}}

The results of the operation are as follows:

To this, a card-making process is complete, and this card is the stack , the process of card loading is the process of the stack , the process of licensing is out of the stack The process .

Stack--powershell version

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.