【发布时间】:2015-07-07 21:29:20
【问题描述】:
我已经用 Java 实现了一个简单的堆栈,它可以工作。它使用 Node 类来保存堆栈 int 值。现在,我正在计划另一个类 NodeWithMin,它应该包含所关注的节点以及从节点到底部的最小值。这意味着当我将获得堆栈的顶部节点时,我将拥有节点和堆栈的最小值。
我想使用泛型与 Stack 类来切换我想插入的任何类(Node / NodeWithMin) .所以,最后它会是一些东西 -
public class myStack extends Stack< Node or NodeWithMin >{
}
堆栈需要在哪里
class Stack<T>{
}
如果您需要进一步澄清问题,请告诉我。我了解 Stack 类中的某些方法需要更改。关于我该怎么做的任何建议?谢谢。
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 the new node is not null
// previous top is now below of the inserted node which is the 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 the new top
top = n;
// pushing is 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 is here \n");
Stack s = new Stack(5);
for (int j = 0; j < 5; j ++){
s.push( randomIntInRange(0, 100) );
}
s.display();
System.out.println("the min of the stack is = "+ s.min());
}
}
【问题讨论】:
-
快速谷歌搜索给出:java2s.com/Tutorial/Java/0200__Generics/GenericclassStack.htm。希望这可以帮助您入门。
-
感谢您的链接。如您所见,我可以得到最低限度,但是,我的想法也略有不同。所以,当我 peek() 顶部时,它也应该给我 Stack 的最小值和顶部值。我可以在没有泛型的情况下实现它,但是,我想学习它。谢谢。
-
在这种情况下,您应该让您的 Node 和 NodeWithMin 实现一个通用接口,然后您的 Stack 就可以使用该接口。
标签: java generics data-structures