0% found this document useful (0 votes)
4 views

Linear Data Structures_ Stacks Cheatsheet _ Codecademy

The document provides an overview of the Java Stack class, detailing its key methods including push(), pop(), and peek(), along with constructors and helper methods. It explains how the stack operates on a last in, first out (LIFO) principle and discusses potential issues like stack overflow. Additionally, it describes the internal structure of the stack using a LinkedList for storage and tracking size and maximum capacity.

Uploaded by

usernxt123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Linear Data Structures_ Stacks Cheatsheet _ Codecademy

The document provides an overview of the Java Stack class, detailing its key methods including push(), pop(), and peek(), along with constructors and helper methods. It explains how the stack operates on a last in, first out (LIFO) principle and discusses potential issues like stack overflow. Additionally, it describes the internal structure of the stack using a LinkedList for storage and tracking size and maximum capacity.

Uploaded by

usernxt123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Cheatsheets / Linear Data Structures

Stacks

Java Stack: pop()

The .pop() method of the Java Stack class public String pop() {
removes the node at the top of the stack. It does so
if (!this.isEmpty()) {
using the LinkedList method
.removeHead() . It then decreases the stack String data =
size variable, returns the data of the removed node, this.stack.removeHead();
and throws an error if the stack is empty. The helper this.size--;
method .isEmpty() is used to verify this.
return data;
} else {
throw new Error("Stack is empty!");
}
}

Java Stack: Constructors

The main Java Stack class constructor instantiates public Stack() {


these variables:
this(DEFAULT_MAX_SIZE);
A stack in the form of a LinkedList
A maxSize integer that determines the }
maximum number of nodes in the stack
A size integer that keeps track of the public Stack(int maxSize) {
current size of the stack
this.stack = new LinkedList();
Another Stack constructor can be written that does
not take any arguments. This constructor is used if no this.size = 0;
maxSize value is specified as a parameter. It sets this.maxSize = maxSize;
the maxSize variable to
}
Integer.MAX_VALUE , which is the maximum
integer in Java. This is stored in a variable
DEFAULT_MAX_VALUE . This effectively creates an
unbounded stack.
Java Stack: Helper Methods

A Java Stack class can implement two helper public boolean hasSpace() {
methods to determine actions that should be taken
return this.size < this.maxSize;
with the stack:
.hasSpace() returns a boolean }
representing if there is space left in a bounded
stack. It is used within the Stack public boolean isEmpty() {
.push() method.
.isEmpty() returns a boolean return this.size == 0;
representing whether the stack is empty or not. }
It is used within the Stack .pop() and
.peek() methods.

Java Stack: peek()

The .peek() method of the Java Stack class public String peek() {
examines, but does not remove, the top node of the
if (this.isEmpty()) {
stack. It returns the top node value if there is one, and
returns null if the stack is empty. The top node is return null;
accessed using the head attribute of the linked list } else {
stack . The top node’s data is accessed using the return this.stack.head.data;
data attribute of the head node.
}
}

Java Stack: push()

A key method of the Java Stack class is public void push(String data) {
.push() . This method takes a single String
if (this.hasSpace()) {
argument, data , and adds this value to the top of the
stack using the LinkedList method this.stack.addToHead(data);
.addToHead() . It then increases the size this.size++;
variable and throws an error if the stack is full to ensure } else {
that the stack does not overflow with nodes. It verifies
throw new Error("Stack is
this using the helper method .hasSpace() .
full!");
}
}
Java Stack Behavior

A stack can be implemented in Java by creating a public class Stack {


Stack class with these methods. Each adjusts the
stack in a different way.
One constructor initializes an internal public LinkedList stack;
LinkedList for storage and a size and public int size;
maxSize for tracking stack size.
static final int DEFAULT_MAX_SIZE =
The other constructor initializes a stack with a
maxSize property value of Integer.MAX_VALUE;
Integer.MAX_VALUE by default. public int maxSize;
.hasSpace() determines if the stack has
space for more data.
public Stack() {
.isEmpty() determines if the stack has
any data. this(DEFAULT_MAX_SIZE);
.push() adds new data to the top of the }
stack.
.pop() removes the top element of the
stack and return its value. public Stack(int maxSize)
.peek() looks at but does not remove the
element at the top of the stack. public boolean hasSpace()

public boolean isEmpty()

public void push(String data)

public String pop()

public String peek()


}
Stack overflow

Every stack has a size that determines how many nodes


it can accommodate. Attempting to push a node in a
full stack will result in a stack overflow. The program
may crash due to a stack overflow.
A stack is illustrated in the given image.
stackA.push(xg) will result in a stack overflow
since the stack is already full.

The stack data structure

A stack is a data structure that follows a last in, first out


(LIFO) protocol. The latest node added to a stack is the
node which is eligible to be removed first. If three
nodes ( a , b and, c ) are added to a stack in this
exact same order, the node c must be removed first.
The only way to remove or return the value of the node
a is by removing the nodes c and b .

Main methods of a stack data structure

The stack data structure has three main methods:


push() , pop() and peek() . The push()
method adds a node to the top of the stack. The
pop() method removes a node from the top of the
stack. The peek() method returns the value of the
top node without removing it from the stack.
Print Share

You might also like