java - How to use generic in Stack data structure? -


i have implemented simple stack in java , works. uses node class holding stack int values. now, i'm planning class nodewithmin should contain node in concern , minimum values node bottom. means when top node of stack have node , minimum value of stack.

i want use generic stack class switch whichever class ( node / nodewithmin) plug it. so, -

public class mystack extends stack< node or nodewithmin >{  } 

where stack needs

class stack<t>{  } 

let me know if need further clarification of question. understand inside of methods in stack class needs changed. advice on how can ? thank you.

import java.util.*;  class node {      public node above;     public node below;     public int data;      public node(int data) {          this.data = data;     }  }  class nodewithmin {      public nodewithmin above;     public nodewithmin below;      public int data;     public int min;      public nodewithmin(int data , int min){          this.data = data;         this.min = min;     }  }   class stack {      private int capacity;     public node top;     public node bottom;     public int size;      hashset<integer> hashset = new hashset<integer>();      public stack ( int cap ){          this.top = null;         this.bottom = null;         this.capacity = cap;         this.size = 0;     }      public static int randomint(int n) {          return (int) (math.random() * n);     }      public static int randomintinrange(int min, int max) {          return randomint(max + 1 - min) + min;     }      public boolean isfull() {          return capacity == size;      }      public void join (node newnode, node stacktop) {          // not empty stack          if ( stacktop != null ){              stacktop.above = newnode;         }          // if new node not null         // previous top below of inserted node current top          if ( newnode != null){              newnode.below = stacktop;         }     }      public boolean push(int v) {          if (size >= capacity)              return false;          size++;         node n = new node(v);         if (size == 1) bottom = n;         join(n, top);          // define new top          top = n;          // pushing sucessful          return true;     }      public int min(){          if ( top == null) return -1;          node curr = this.top;         int min =-1 ;           system.out.println("\n\n");         while( curr != null){              hashset.add(curr.data);             curr = curr.below;         }          system.out.println();         min = collections.min(hashset);          return min;      }      public int pop() {          node t = top;         top = top.below;         size--;         return t.data;     }      public int peek (){          stack mystack = this;          return mystack.top.data ;      }      public boolean isempty() {          return size == 0;      }      public int removebottom() {          node b = bottom;         bottom = bottom.above;         if (bottom != null) bottom.below = null;         size--;         return b.data;     }      public void display(){          if ( top == null) return;          node curr = this.top;         int min =-1 ;           system.out.println("\n\n");         while( curr != null){              system.out.println( curr.data);              curr = curr.below;              if ( curr != null){                  system.out.println("↑");             }         }          system.out.println();     }      public static void main ( string[] args ){          // system.out.println("\nmy stack here \n");         stack s = new stack(5);          (int j = 0; j < 5; j ++){             s.push( randomintinrange(0, 100) );         }          s.display();         system.out.println("the min of stack =  "+ s.min());      }  } 

it looks me if nodewithmin same basic thing node, plus has min stuff. i'd make nodewithmin extend node. declare mystack as:

public class mystack extends stack< ? extends node > { ... } 

mystack work either node or nodewithmin.

as stack, notable problem is written explicitly depend on instances of node. that's issue because want stack generic, able take t. you'll have keep of t instances in correct order without expecting them remember comes before or after, , that's inconsistent you're trying nodewithmin.

so think need rethink how generic want stuff be. if stack should generic, i.e. should able keep kind of object in lifo order, mystack have override large portions of methods in stack in order make use of knows node. otoh, if want keep bunch of node (or subtypes thereof) objects in lifo order, ditch stack , go straight mystack.

oh btw, use of hashset troublesome. since instantiate once, run risk of keeping stuff around no longer exists in stack, i.e. pop current min, min() method keep returning it. better make local variable in min() method since that's place it's used.

further, in order use collections.min() method, of objects in stack must implement comparable, , argues against using generic t. want move min() method mystack (assuming change node implement comparable).


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -