如何使用链接列表创建堆栈

时间:2014-05-07 23:38:35

标签: java

我必须使用Linked List创建Stack。这是我的LinkedList类:

package stack;

import java.util.EmptyStackException;

public class List {

    Element head;
    int size = 0;

    public void Add(Object value) {
        if (size == 0) {
            head = new Element(value);
            size++;
            return;
        }

        Insert(size, value);
    }

    public void Insert(int index, Object value) {
        if (index > size) {
            throw new IndexOutOfBoundsException("Bad index");
        }

        Element tmp = head;
        for (int i = 0; i < index -1 ; i++) {
            tmp = tmp.next;
        }

        Element newElement = new Element(value);
        newElement.next = tmp.next;
        tmp.next = newElement;
        size++;
    }

    public int Size() {
        return size;
    }

    public Object Get(int index) {
        if (index > size - 1) {
            throw new IndexOutOfBoundsException("Index doesn't exist");
        }
        Element tmp = head;
        for (int i = 0; i < index; i++) {
            tmp = tmp.next;

        }
        System.out.println(tmp.value.toString());
        return tmp;
    }

    public Object Set(int index, Object value) {
        if (index > size - 1) {
            throw new IndexOutOfBoundsException("Index doesn't exist");
        }

        Element tmp = head;
        for (int i = 0; i < index; i++) {
            tmp = tmp.next;
        }

        Element returnValue = new Element(value);
        returnValue = tmp;
        tmp.value = value;

        return returnValue;
    }

    public Object Delete(int index) {

        Element tmp;
        tmp = head;
        for (int i = 0; i < index - 1; i++) {
            tmp = tmp.next;
        }

        Element returnValue;
        returnValue = tmp;
        head = tmp.next;
        size--;
        return returnValue;

    }

    public boolean Contains(Object value) {
        Element tmp = head;
        for (int i = 0; i < size - 1; i++) {
            tmp = tmp.next;
            if (tmp.value == value) {
                System.out.println("true");
                return true;
            }
        }
        System.out.println("False");
        return false;
    }

    public boolean IsEmpty() {
        return Size() > 0;
    }

    public void Clear() {

        size = 0;
    }

    public void Display() throws EmptyStackException {
        if (size <= 0) {
            throw new EmptyStackException();

        }
        Element tmp = head;
        for (int i = 0; i < size; i++) {
            System.out.println(tmp.value.toString());
            tmp = tmp.next;
        }
    }
}

我得到了Stack接口

public interface IStack {

    public interface Stack {

        public void push(Object value); // odłóż na stos

        public Object pop() throws EmptyStackException; //pobierz ze stosu

        public Object peek() throws EmptyStackException; //odczytaj ze stosu,
        // and some others

Any1知道如何创建pop方法吗?

1 个答案:

答案 0 :(得分:0)

LinkedList已经是一个堆栈,它有pop,push和peek方法。只需创建一个适配器类并使用这些方法