</>Learn to Write Code
Learn
Programming FundamentalsCode InfrastructurePractical Programming
PseudocodeReal CodeLanguagesVideosCode TalkAbout
Learn → Code Infrastructure → Stacks & Queues

Code Infrastructure

01Stacks & Queues
Lesson 1 of 1·Code Infrastructure

Stacks & Queues

Learn two ways to track order when removing items from a list: last-in-first-out with a stack, and first-in-first-out with a queue.

Related:stackqueueLIFOFIFOpushpopenqueuedequeue

Order matters: last-in-first-out vs. first-in-first-out

A stack and a queue both hold a list of items, but they disagree about which one comes back out first.

A stack is last-in-first-out: like a stack of dishes, the last plate you put down is the first one you pick back up.

A queue is first-in-first-out: like a checkout line, the first person to join the line is the first one served.

  • A stack: add to the top, remove from the top (LIFO).
  • A queue: add to the back, remove from the front (FIFO).
  • Same kind of list, different end you remove from.
Picture a stack of dinner plates next to a line of people waiting to check out: the plates come off the top you just added to, while the line empties from the front you joined last.
Try it: If you push "A", then "B", then "C" onto a stack, what comes off first when you pop? If you instead enqueue "A", "B", "C" onto a queue, what comes off first when you dequeue?

Pop two orders off a stack, then dequeue two jobs off a queue

We push three orders onto a stack and pop them back off, then enqueue three print jobs and dequeue them in the order they arrived.

Pseudocode
CREATE orderStack AS LIST WITH "Burger", "Fries", "Shake"

CREATE lastOrder AS REMOVE LAST FROM orderStack
SHOW lastOrder
CREATE nextOrder AS REMOVE LAST FROM orderStack
SHOW nextOrder

CREATE printQueue AS LIST WITH "Report", "Invoice", "Label"

CREATE firstJob AS REMOVE FIRST FROM printQueue
SHOW firstJob
CREATE secondJob AS REMOVE FIRST FROM printQueue
SHOW secondJob

The same idea in Python

A plain list works as a stack with .pop(), and as a queue with .pop(0) to remove from the front instead of the end.

Python
orderStack = ["Burger", "Fries", "Shake"]

lastOrder = orderStack.pop()
print(lastOrder)
nextOrder = orderStack.pop()
print(nextOrder)

printQueue = ["Report", "Invoice", "Label"]

firstJob = printQueue.pop(0)
print(firstJob)
secondJob = printQueue.pop(0)
print(secondJob)

The same idea in JavaScript

An array works as a stack with .pop(), and as a queue with .shift() to remove from the front instead of the end.

JavaScript
const orderStack = ["Burger", "Fries", "Shake"];

const lastOrder = orderStack.pop();
console.log(lastOrder);
const nextOrder = orderStack.pop();
console.log(nextOrder);

const printQueue = ["Report", "Invoice", "Label"];

const firstJob = printQueue.shift();
console.log(firstJob);
const secondJob = printQueue.shift();
console.log(secondJob);

The same idea in C#

C# has dedicated Stack<T> and Queue<T> types: Push()/Pop() on the stack, Enqueue()/Dequeue() on the queue.

C#
Stack<string> orderStack = new Stack<string>();
orderStack.Push("Burger");
orderStack.Push("Fries");
orderStack.Push("Shake");

string lastOrder = orderStack.Pop();
Console.WriteLine(lastOrder);
string nextOrder = orderStack.Pop();
Console.WriteLine(nextOrder);

Queue<string> printQueue = new Queue<string>();
printQueue.Enqueue("Report");
printQueue.Enqueue("Invoice");
printQueue.Enqueue("Label");

string firstJob = printQueue.Dequeue();
Console.WriteLine(firstJob);
string secondJob = printQueue.Dequeue();
Console.WriteLine(secondJob);

The same idea in Java

Java uses an ArrayDeque as a stack with push()/pop(), and a Queue as a FIFO queue with add()/poll().

Java
Deque<String> orderStack = new ArrayDeque<>();
orderStack.push("Burger");
orderStack.push("Fries");
orderStack.push("Shake");

String lastOrder = orderStack.pop();
System.out.println(lastOrder);
String nextOrder = orderStack.pop();
System.out.println(nextOrder);

Queue<String> printQueue = new LinkedList<>();
printQueue.add("Report");
printQueue.add("Invoice");
printQueue.add("Label");

String firstJob = printQueue.poll();
System.out.println(firstJob);
String secondJob = printQueue.poll();
System.out.println(secondJob);

The same idea in C++

C++ has dedicated std::stack and std::queue adapters. Each reads its next value with top()/front(), then a separate pop() removes it.

C++
std::stack<std::string> orderStack;
orderStack.push("Burger");
orderStack.push("Fries");
orderStack.push("Shake");

std::string lastOrder = orderStack.top();
orderStack.pop();
std::cout << lastOrder << '\n';

std::string nextOrder = orderStack.top();
orderStack.pop();
std::cout << nextOrder << '\n';

std::queue<std::string> printQueue;
printQueue.push("Report");
printQueue.push("Invoice");
printQueue.push("Label");

std::string firstJob = printQueue.front();
printQueue.pop();
std::cout << firstJob << '\n';

std::string secondJob = printQueue.front();
printQueue.pop();
std::cout << secondJob << '\n';
Lesson 1 of 1 · Concept
Start of courseEnd of Code Infrastructure track

Learn to Write Code

Learn the idea first, write it in pseudocode, then see how real programming languages express the same logic.

Learn

CurriculumPseudocode StandardLanguagesReal Code

Elsewhere

Code TalkAbout BrianSpread the WordCoders Rock — PracticecodeBetter on YouTubeSupporting files on GitHubAdvertising & Promotions
© 2026 Brian Kittrell, an exteran studios site. Educational content and examples are provided for learning purposes.