Hi Eoghan, yep. Here's an example: Let's suppose you wanted to use the Stack functionality in a LinkedList, then you can do something like the following. NB: I'm assuming that you may want to store other data types and not just integers. In that case, I'll throw in some generics. public interface Stack { void push (); T pop() throws NoSuchElementException; // (in our case, also don't forget to import java.util.NoSuchElementException) T peek() throws NoSuchElementException; // (in our case) boolean isFull(); boolean isEmpty(); void resize(); } You can then " implement " our Stack interface in a LinkedList class you're building. public class LinkedList implements Stack { private T head; public LinkedList(T head){ this.head = head } @Override public void push(T value){ ..... } @Override public T pop throws NoSuchElementException{ .... } ..... And so on } You can also build a Queue interface and have the same LinkedList class extend it as well, achieving both Stack and Queue functionalities: E.g) public Class LinkedList implements Stack, Queue { ... } Once you've done that, you can use the LinkedList like this: class Driver { LinkedList someLL = new LinkedList(null); } Hope this helps :)
Hi Alex Yip, private variables introduce 'Encapsulation' to the class. It's simply a precautionary measure so that classes outside of it can't access and manipulate those variables. You can remove the 'private' modifier if you wish to. As for ++top, you are absolutely correct. ++top is so that the value of top is increased before assigning 'data' to it. If we used top++, we would end up replacing the value at the top of the stack and then adding one to top. Hope this helps :)
Hey Everyone! Thanks for checking out my video! Don't forget to Like, Subscribe, and MOST IMPORTANTLY click that Notification Bell for updates! :)
That’s the best explanation video I’ve seen so far, thank you so much!
Thanks a lot!! Please don't forget to like and share as it will help the channel grow.
Kevin, do you know how to make interface files for LinkedList, Stack and Queue with all-around functionalities?
Hi Eoghan, yep.
Here's an example:
Let's suppose you wanted to use the Stack functionality in a LinkedList, then you can do
something like the following.
NB: I'm assuming that you may want to store other data types and not just integers. In that case, I'll throw in some
generics.
public interface Stack {
void push ();
T pop() throws NoSuchElementException; // (in our case, also don't forget to import java.util.NoSuchElementException)
T peek() throws NoSuchElementException; // (in our case)
boolean isFull();
boolean isEmpty();
void resize();
}
You can then " implement " our Stack interface in a LinkedList class you're building.
public class LinkedList implements Stack {
private T head;
public LinkedList(T head){
this.head = head
}
@Override
public void push(T value){
.....
}
@Override
public T pop throws NoSuchElementException{
....
}
..... And so on
}
You can also build a Queue interface and have the same LinkedList class extend it as well, achieving both Stack and Queue functionalities:
E.g) public Class LinkedList implements Stack, Queue { ... }
Once you've done that, you can use the LinkedList like this:
class Driver {
LinkedList someLL = new LinkedList(null);
}
Hope this helps :)
May I ask why three instance variables are using private and the Push method using ++top(because you want top immediately increase by 1?)
Hi Alex Yip, private variables introduce 'Encapsulation' to the class. It's simply a precautionary measure so that classes outside of it can't access and manipulate those variables. You can remove the 'private' modifier if you wish to.
As for ++top, you are absolutely correct. ++top is so that the value of top is increased before assigning 'data' to it. If we used top++, we would end up replacing the value at the top of the stack and then adding one to top.
Hope this helps :)
Isn't top variable obsolete? The top will be always size-1
Thanks a lot 😊
You're welcome, sunny!
Can u explain abstract classes-polymorphism from hackerrank
I can probably do a video on OOP later on.
Please cover data structures please