Join Raghavendra Dixit for an in-depth discussion in this video Stacks, part of Introduction to Data Structures & Algorithms in Java.
- [Instructor] In this section, we will look at two new types of data structures. First, we will see stacks and its implementation using arrays. Then, we will move on to the second type of data structure called queues and its implementation using arrays. You will also see what are double ended queues and how to implement them using arrays. So let's just start with the stacks. Stacks are one of the very basic data structures.
A stack can be thought of as a container in which we can insert data from the top. So that means that every new data element you push into the stack sits on the top. This operation of inserting new data into the top of the stack is called push. So every new element that you push onto the stack sits on top of the stack . You can also remove or take out data elements from the stack, but note that we can remove only the topmost element of the stack.
We cannot take out a data element from the middle or bottom of the stack. So every time we take out an element, it will always be the top element of the stack. We call this as popping an item from the top of the stack, or it's called a pop operation. So what the pop method or operation does is it removes the top element of the stack and returns it. So if you want to remove the last element of the stack, you will have to take out all the other elements on top of it.
There is another operation that we can use on stacks, and that is to peek at the stack. What the peek method does is it reads the value of the topmost data element without removing it and returns that value. So if we call the peek method on this stack, it will read the value of the topmost element, which is two. That's it. Stacks are also called LIFO, which stands for Last In First Out, because of the way data is arranged in the stack.
That is, the last element to go into the stack is on the top, which also happens to be the first one to come out. So to sum up, there are three operations that can be carried out on stacks, push, pop, and peek.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Why study data structures and algorithms?
- How to calculate the time complexity
- Using Big O notation
- Using basic sorting and search algorithms
- Searching elements in unordered arrays and ordered arrays
- Implementing a linked list in Java
- Implementing stacks using arrays
- Queues using arrays
- Binary search trees
- Representing heaps using arrays
Skill Level Intermediate
1. Introduction to Algorithms
2. Analysis of Algorithms
3. Basic Sorting and Search Algorithms
4. Linked Lists
5. Stacks and Queues
7. Binary Search Trees
8. More Sorting Algorithms
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.