Thursday, July 4, 2019

CS2040: Stack ADT

DA Stack

Stack only has a small number of operations
  • Peek
Peek allows the user to see the first element on top of the stack
  • Pop
Pop allows the user to remove and see the first element on top of the stack
  • Push
Push adds an element on top of the stack

Stack Interface


The user cannot access the bottom of the stack

Stack

We use normal arr to store the data..


Pop uses peek,
Each time we pop,
we remove the top counter

When we push an object, we will just add it like
a normal array, but we must regulate it to ensure it only
adds to the top

Enlargearr ensure that there is space

Stack implemented by LL

Using Compostion,
class A {B b = new B (…); // A is composed of instance of B
}  

We can use BasicLinkedlist from previous to create a stack.



We make use of the list structure we created earlier

Limit the pop by only removing the first

Or we could use inheritance,
class A extends B { // A is an extension of B
….
}
 

Here we are extending the list class we made earlier on.



<Prev Linked List ADT                                                Next Queue ADT>

No comments:

Post a Comment