2013-04-03 62 views
0

刚刚完成春假,难以记住这些东西。堆栈的节点链表

现在我正在尝试创建一个Lstack类,它将创建一个实现为节点链表的堆栈ADT。

这是Lstack类

public class Lstack { 
    int numUsed = 0; 
    Lstack list = new Lstack(); 
    public Lstack(){ 
    } 
    public void push(Car x){ 

    } 
} 

我如何去推动汽车X(对象)进栈?

回答

5

堆栈是一个LIFO结构。
所以如果你实施它支持linked list那么你应该确保你pushlistpop
由于这是作业,我不会提供更多信息。这应该是足够的

+0

我明白,我的问题是我如何使用对象做到这一点。如果我只是输入字符串或整数,我会知道如何去做。 – user1801067 2013-04-03 19:49:52

+0

'Lstack'我假设你是自定义实现的列表?这将存储对“Car”的引用和“list”的下一个节点 – Cratylus 2013-04-03 19:51:02

0

堆栈是后进先出(LIFO)结构。所以,我会走这条路:

Car类必须具有相同类的next成员:

class Car { 
    String brand; 
    Car next; 
    public Car(String brand, Car car) { 
     this.brand = brand; 
     next = car; 
    } 
    // And the class code goes on 
} 

至于链表:

class Llist { 
    Car top; 
    public Llist() { 
     top = null; 
    } 
    public void push(String carBrand) { 
     top = new Car(carBrand, top); 
    } 
} 

只是一个例子。

0

我做对了吗?

public class Lstack { 
    int size; 
    int numUsed = 0; 
    Car[] list; 
    public Lstack(){ 
     list = new Car[size]; 
    } 
    public void push(Car x){ 
     list[numUsed] = x; 
     numUsed++; 
    } 
    public Car pop(){ 
     Car temp; 
     numUsed--; 
     temp = list[numUsed]; 
     return temp; 
    } 
    public boolean isEmpty(){ 
     if(numUsed==0){ 
      return true; 
     } 
     else 
      return false; 
    } 
    public int size(){ 
     return numUsed; 
    } 
    public void display(){ 
     System.out.println("--------------------------------------------"); 
     System.out.print("TOP | "); 
     for(int i = 0; i < numUsed; i++){ 
      System.out.print(list[i].plate +" | "); 
     } 
    } 

}