Stack

A stack is a fundamental data structure in programming that follows the Last In, First Out (LIFO) principle, meaning that the last element added to the stack is the first one to be removed. It supports two primary operations: push, which adds an element to the top of the stack, and pop, which removes the top element from the stack. Stacks are commonly used in programming for tasks such as function call management, expression evaluation, and backtracking algorithms. For example, in a web browser, a stack could be used to manage the history of visited web pages, where each new page visited is pushed onto the stack, and when the user navigates back, pages are popped off the stack in reverse order.

Select Languages

Examples

C

// no built in implementation

C#

using System.Collections.Generic;

Stack<string> cars = new Stack<string>();

cars.Push("Mazda");
cars.Push("Toyota");
cars.Push("Honda");
Console.WriteLine(
  cars.Contains("Honda") + "\n"
);

cars.Pop();
Console.WriteLine(
  cars.Contains("Honda")
);

C++

#include <iostream>
#include <stack>
using namespace std;

stack<string> cars;
cars.push("Mazda");
cars.push("Toyota");
cars.push("Honda");
    
cout << cars.top() << endl;;
    
cars.pop();
cout << cars.top() << endl;

Go

import "fmt"

cars := []string{}
cars = append(cars, "Mazda")
cars = append(cars, "Toyota")
cars = append(cars, "Honda")

fmt.Println(cars[len(cars) - 1])
cars = cars[:len(cars) - 1]
fmt.Println(cars[len(cars) - 1])

Java

import java.util.*;

ArrayList<String> cars = new ArrayList<>();
cars.add("Mazda");
cars.add("Toyota");
cars.add("Honda");

System.out.println(cars.get(cars.size() - 1));

cars.remove(cars.size() - 1);
System.out.println(cars.get(cars.size() - 1));

JavaScript

const cars = [];

cars.push('Mazda');
cars.push('Toyota');
cars.push('Honda');

console.log(cars[cars.length - 1]);

cars.pop();
console.log(cars[cars.length - 1]);

Kotlin

import java.util.ArrayDeque;

val cars = ArrayDeque<String>();
cars.push("Mazda");
cars.push("Toyota");
cars.push("Honda");
println(cars.peek());

cars.pop();
println(cars.peek());

MatLab

cars = [];

cars = [cars, "Mazda"];
cars = [cars, "Toyota"];
cars = [cars, "Honda"];

fprintf(cars(end));

cars(end)=[];
fprintf(cars(end));

PHP

$cars = array();
array_push($cars, "Mazda");
array_push($cars, "Toyota");
array_push($cars, "Honda");

echo $cars[count($cars) - 1] . "\n";
array_pop($cars);
echo $cars[count($cars) - 1];

Python

cars = []

cars.append('Mazda')
cars.append('Toyota')
cars.append('Honda')

print(cars[len(cars) - 1])

cars.pop(len(cars) - 1);
print(cars[len(cars) - 1])

R

cars <- list()
cars <- append(cars, "Mazda")
cars <- append(cars, "Honda")
cars <- append(cars, "Toyota")

print(cars[length(cars)])

cars <- cars[-length(cars)]
print(cars[length(cars)])

Ruby

cars = []
cars << "Mazda"
cars << "Toyota"
cars << "Honda"

puts cars[cars.length - 1]

cars.pop()
puts cars[cars.length - 1]

Rust

let mut cars: Vec<string> = vec![];
cars.push("Mazda".to_string());
cars.push("Toyota".to_string());
cars.push("Honda".to_string());

println!("{}", cars[cars.len() - 1]);

cars.pop();
println!("{}", cars[cars.len() - 1]);

Scala

import scala.collection.mutable.Stack;

var cars = Stack[String]();

cars.push("Mazda");
cars.push("Toyota");
cars.push("Honda");

println(cars.top);

cars.pop;
println(cars.top);

Swift

var cars:Array<String> = [];
cars.append("Mazda");
cars.append("Toyota");
cars.append("Honda");

print(cars[cars.count - 1]);

cars.removeLast();
print(cars[cars.count - 1]);

TypeScript

const cars:string[] = [];

cars.push('Mazda');
cars.push('Toyota');
cars.push('Honda');

console.log(cars[cars.length - 1]);

cars.pop();
console.log(cars[cars.length - 1]);

Copyright 2025. All Rights Reserved. IronCodeMan.