Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Stack.Push Method

Inserts an object at the top of the Stack.

[Visual Basic]
Public Overridable Sub Push( _
   ByVal obj As Object _
)
[C#]
public virtual void Push(
 object obj
);
[C++]
public: virtual void Push(
 Object* obj
);
[JScript]
public function Push(
   obj : Object
);

Parameters

obj
The Object to push onto the Stack. The value can be a null reference (Nothing in Visual Basic).

Remarks

Stack is implemented as a circular buffer.

If Count is less than the capacity of the stack, Push is an O(1) operation. If the capacity needs to be increased to accommodate the new element, Push becomes an O(n) operation, where n is Count.

A null reference (Nothing in Visual Basic) can be pushed onto the Stack as a placeholder, if needed. It occupies a slot in the stack and is treated like any object.

Example

[Visual Basic, C#, C++] The following example shows how to add elements to the Stack, remove elements from the Stack or just view the element at the top of the Stack.

[Visual Basic] 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("The")
        myStack.Push("quick")
        myStack.Push("brown")
        myStack.Push("fox")
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
        
        ' Removes an element from the Stack.
        Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _
           "{0}", myStack.Pop())
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
        
        ' Removes another element from the Stack.
        Console.WriteLine("(Pop)" & ControlChars.Tab & ControlChars.Tab & _
           "{0}", myStack.Pop())
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
        
        ' Views the first element in the Stack but does not remove it.
        Console.WriteLine("(Peek)" & ControlChars.Tab & ControlChars.Tab & _
           "{0}", myStack.Peek())
        
        ' Displays the Stack.
        Console.Write("Stack values:")
        PrintValues(myStack, ControlChars.Tab)
    End Sub
    
    Public Shared Sub PrintValues(myCollection As IEnumerable, _
       mySeparator As Char)
       
        Dim myEnumerator As System.Collections.IEnumerator = _
           myCollection.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.Write("{0}{1}", mySeparator, myEnumerator.Current)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' Stack values:    fox    brown    quick    The
' (Pop)        fox
' Stack values:    brown    quick    The
' (Pop)        brown
' Stack values:    quick    The
' (Peek)        quick
' Stack values:    quick    The
 

[C#] 
using System;
using System.Collections;
public class SamplesStack  {

   public static void Main()  {

      // Creates and initializes a new Stack.
      Stack myStack = new Stack();
      myStack.Push( "The" );
      myStack.Push( "quick" );
      myStack.Push( "brown" );
      myStack.Push( "fox" );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );

      // Removes an element from the Stack.
      Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );

      // Removes another element from the Stack.
      Console.WriteLine( "(Pop)\t\t{0}", myStack.Pop() );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );

      // Views the first element in the Stack but does not remove it.
      Console.WriteLine( "(Peek)\t\t{0}", myStack.Peek() );

      // Displays the Stack.
      Console.Write( "Stack values:" );
      PrintValues( myStack, '\t' );
   }


   public static void PrintValues( IEnumerable myCollection, char mySeparator )  {
      System.Collections.IEnumerator myEnumerator = myCollection.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.Write( "{0}{1}", mySeparator, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

Stack values:    fox    brown    quick    The
(Pop)        fox
Stack values:    brown    quick    The
(Pop)        brown
Stack values:    quick    The
(Peek)        quick
Stack values:    quick    The
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;


void PrintValues( IEnumerable* myCollection, Char mySeparator )  {
   System::Collections::IEnumerator* myEnumerator = myCollection->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      Console::Write( S"{0}{1}", __box(mySeparator), myEnumerator->Current );
   Console::WriteLine();
}

int main()  {

   // Creates and initializes a new Stack.
   Stack* myStack = new Stack();
   myStack->Push( S"The" );
   myStack->Push( S"quick" );
   myStack->Push( S"brown" );
   myStack->Push( S"fox" );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );

   // Removes an element from the Stack.
   Console::WriteLine( S"(Pop)\t\t{0}", myStack->Pop() );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );

   // Removes another element from the Stack.
   Console::WriteLine( S"(Pop)\t\t{0}", myStack->Pop() );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );

   // Views the first element in the Stack but does not remove it.
   Console::WriteLine( S"(Peek)\t\t{0}", myStack->Peek() );

   // Displays the Stack.
   Console::Write( S"Stack values:" );
   PrintValues( myStack, '\t' );
}

/*
This code produces the following output.

Stack values:   fox     brown   quick   The
(Pop)           fox
Stack values:   brown   quick   The
(Pop)           brown
Stack values:   quick   The
(Peek)          quick
Stack values:   quick   The
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

Stack Class | Stack Members | System.Collections Namespace | Peek | Pop

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.