Logout

Stack & Queue Implemented by Inheritance of List

(Refer to the List class just before this in the notes.)

/Users/adelaide/Public/Netbeans - All JSR Projects/Linked List 2/src/linked/list/pkg2/Stack.java
11 public class Stack extends List{
12     
13     public Stack(){
14         super();                             //Call the super constructor; i.e. List's constructor.
15     }
16     
17     public void push(Object data){
18         addToList(data);                     //And all that is done is re-naming the methods to be used in a more appropriate way.
19     }
20     
21     public Object pop(){
22         return removeLastIn();               //For a stack, we will want to remove the last one added.
23     }  
24     
25 }
/Users/adelaide/Public/Netbeans - All JSR Projects/Linked List 2/src/linked/list/pkg2/Queue.java
11 public class Queue extends List {
12     
13     
14     public Queue(){
15         super();
16     }
17     
18     public void enQueue(Object data){         //For adding to a list or a queue, we just do it the same way, with addToList.
19         addToList(data);
20     }
21     
22     public Object deQueue(){                  //For a queue, we want to remove the first one added in.
23         return removeFirstAddedIn();
24     }
25 }