JAVA - 使用链接列表实现堆栈

时间:2014-04-03 08:46:08

标签: java linked-list stack

我正在尝试使用我的链接列表来实现堆栈,即DSAStack.java,即DSALinkedList.java

我该怎么办?我想我应该push()执行insertFirst()pop()执行peekFirst()removeFirst()来获取LIFO行为?那isEmpty()怎么样 和其他方法?

我不确定,请帮帮我。使用代码的清晰解释将非常明显。提前谢谢!

以下是DSAStack.java

public class DSAStack implements Iterable {
    public static int DEFAULT_CAPACITY = 100;

    private DSALinkedList list;
    private int           count;
    private Object[]      stack;

    public DSAStack() {
        count = 0;
        stack = new Object[DEFAULT_CAPACITY];
}

public DSAStack(int maxCapacity) {
    count = 0;
    stack = new Object[maxCapacity];
    }

    public int getCount() {
        return count;
    }

    public boolean isEmpty() {
        boolean empty = (count == 0);   
        return empty;   
    }

    public boolean isFull() {
        boolean full = (count == stack.length);
        return full;            
    }

    public void push(Object value) {
        if (isFull())
            throw new IllegalArgumentException("Stack is full");
        else
            stack[count] = value;

        count++;
    }

    public Object pop() {
        Object topVal = top();
        count--;

        return topVal;
    }

    public Object top() {
        Object topVal;

        if (isEmpty()) 
            throw new IllegalArgumentException("Stack is empty");
        else
            topVal = stack[count-1];

        return topVal; 
    }

    public Iterator iterator() {
        return list.iterator();
    }
}

这是DSALinkedList.java

import java.util.*;

public class DSALinkedList {
    public DSAListNode head;
    public DSAListNode tail;
    Object[] newValue;

    public DSALinkedList() {
        head = null;
        tail = null;
    }

    public void insertFirst(Object newValue){
        DSAListNode newNd;
        newNd = new DSAListNode(newValue);

        if (head == null) {
            head = newNd;
            tail = newNd;   
        } else {
            newNd.setNext(head);
            head = newNd;
        }
    }

    public void insertLast(Object newValue){
        DSAListNode newNd;
        newNd = new DSAListNode(newValue);

        if(head == null){
            head = newNd;
        } else {
            tail.next = newNd;
            tail      = newNd;  
        }
    }

    public boolean isEmpty() {
        return (head == null);
    }

    public Object peekFirst(){
        Object nodeValue;

        if (head == null)
            throw new IllegalArgumentException("head is empty");
        else 
            nodeValue = head.getValue();

        return nodeValue;
    }

    public Object peekLast(){
        Object nodeValue;

        if (head == null)
            throw new IllegalArgumentException("head is empty");
        else
            nodeValue = tail.getValue();

        return nodeValue;
    }

    public Object removeFirst(){
        Object nodeValue;

        if (head == null){
            throw new IllegalArgumentException("head is empty");
        } else {
            nodeValue = head.getValue();
            head      = head.getNext();
        }

        return nodeValue;
    }
}

1 个答案:

答案 0 :(得分:1)

您的DSAStack类是用户和链接列表之间的接口。因此,该类提供LIFO接口并强制它在用户上。从这里开始,它应该隐藏链表中的实现,这样用户就不必担心插入最后或插入第一个,他们只想插入。

所以回答你的问题。 DSAStack需要执行以下操作:

- size() -> returns int size
- push(Object e) -> returns bool (able to be inserted)
- pop() -> returns Object and removes it from linkedlist
- peek() -> returns Object
- isEmpty() -> returns bool if empty

您的DSAStack并不意味着保存任何数据。所以你不需要计数或堆栈变量。相反,我们需要将它们存储在DSALinkedList类中。因此,DSAStack应该实例化一个DSALinkedList对象,传递maxCapacity,然后启动该对象。

当用户说他们想在DSAStack上使用pop()时,该类需要告诉DSALinkedList,嘿!我想弹出你的一个物体!现在DSALinkedList需要在这里实现细节。

重写代码就像这样:

    public DSAStack(int maxCapacity) {
        list = new DSALinkedList[maxCapacity];
    }

    public int getCount() {
        return list.size();
    }

    public boolean isEmpty() {
        return list.isEmpty(); 
    }

    public boolean isFull() {
        return list.isFull();          
    }

    public void push(Object value) {
        list.insertLast(value);
    }

    public Object pop() {
        return list.removeLast();
    }

    public Object top() {
        return list.peekLast();
    }

    public Iterator iterator() {
        return list.iterator();
    }
}
相关问题