Home Data Structures and Algorithms
Post
Cancel

Data Structures and Algorithms

Today I decided to do some practice with data structures and algorithms in Java. I decided to write a post going over some of the basics.

Stacks

Stacks are a Last in First Out data structure. This is like a stack of items in real life. The last or “top” item is the first one to come off when you start taking items off the stack.

To create a stack in java we can use the syntax:

1
Stack<String> stack = new Stack<String>();

This would create a new instance of the Stack object called “stack”.

Methods for working with stacks include push(add), pop(remove) and peek(look at the top but dont remove). Here are some examples:

1
stack.push("Minecraft");
1
stack.pop();
1
stack.peek();

Check out some code i wrote up working with stacks in Java on my github here

This post is licensed under CC BY 4.0 by the author.
Trending Tags