Join Raghavendra Dixit for an in-depth discussion in this video Implementing stacks using arrays, part of Introduction to Data Structures & Algorithms in Java.
- [Instructor] So let's see how we can implement stacks…using arrays.…We know that arrays have a fixed length,…so first we declare the maximum size of the array, say nine,…and let's take an array of int type of size nine.…Now we have an array of eight type and each element…is initialized to zero.…Now set the difference to keep track of the top of the stack…and we initialize it to minus one because right now…there are no elements in the stack.…
So let's say we want to insert a push a new element five.…Think about how we can do that, well all we need to do…is implement the value of top by one, so it points to…the zero the next now, and we update the data…at index zero to five, that's it.…Now let's push 12, so we implement the top by one again…to make it point to the first index and update the data…at index one to twelve.…
Similarly we can keep pushing other elements like this.…Now let's see the peek operation.…All peek does here is return the element pointed by…the top index, which in this case is two,…and the value of top is not changed…
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.