Stack<T>

Summary

System.Stack<T> provides a LIFO (last in, first out) data structure.

Description

System.Stack<T> provides a LIFO (last in, first out) data structure.

Examples

Instantiation
1
2
3
import System;
 
auto stack = new System.Stack<int>();
Initialization with data
1
2
3
import System;
 
auto stack = new System.Stack<int>([ 1, 2, 3 ]);
Comparison with System.Queue<T>
1
2
3
4
5
6
import System;
 
auto stack = new System.Stack<int>([ 1, 2, 3 ]);
auto queue = new System.Queue<int>([ 1, 2, 3 ]);
Console.log(stack.pop()); // 3
Console.log(queue.pop()); // 1

Methods

  • clear

    Clears the stack by removing all elements from the stack.

  • isEmpty

    Checks if the stack is empty.

  • length

    Returns the number of elements in the stack.

  • peek

    Returns the most recently added element without removing it.

  • pop

    Returns and removes the most recently added element.

  • push

    Adds an element to the collection.

  • Stack (Constructor)

    Constructs a System.Stack<T> object.

  • toString

    Returns a string representation of the stack.

Share

HTML | BBCode | Direct Link