Stack中使用的Java链表

时间:2017-05-20 11:40:22

标签: java

为了改善我自己的数据结构,我试图设计自己的Stack。为此,我决定使用链表让我使用未定义的长度堆栈(因为我来自C编程)。我的代码是:

public class Main {

    public static void main(String[] args) {
        Stack stk = new Stack();
        obj buf_el;
        Scanner s=new Scanner(System.in);
        boolean stop=false;
    do{
        String buffer= s.next();
        System.out.println("Created first buff");
        if(buffer.equals("exit")) stop=true;
        if(buffer.equals("pop")){
            buf_el = stk.pop();
            System.out.println("Element Popped from stack:\t\t[#]\t-->\t"+buf_el.field());
        }
        else if(buffer.equals("push")){
            obj pushed = new obj(s.next());
            stk.push(pushed);
            System.out.println("Element pushed in the stack:\t\t[#]\t<--\t"+pushed.field());
        }
        else if(buffer.equals("stats")) stk.stats();
        else if(buffer.equals("all")) stk.traverse();
    }
    while(stop==false);
    }
}

class obj{
    private String field = new String();
    private   obj next = null;
    private   obj back = null;
    obj (String field){
        this.field=field;
    }

    public obj() {

    }

    void concat(obj next){
        this.next=next;
        this.next.back=this;
    }
    void del(){
        this.next=null;
        this.back.concat(null);
    }
    obj next(){
        return this.next;
    }
    obj back(){
        return this.back;
    }
    String field(){
        return this.field;
    }
}

class Stack{
    private obj head = null;
    private obj tail = null;
    private int pops=0;
    private int pushes=0;
    void push(obj el){
        pushes++;
        if(this.head==null | this.tail==null){
            this.head = el;
            this.tail = el;
        }
        else{
            head.concat(el);
        }
    }
    obj pop(){
        if(head==null & tail==null) System.out.println("The stack is empty, Operation failed.");
        else{
            pops++;
            if(this.head==this.tail){
                System.out.println("This is the last element; the stack is empty now");
                obj ret=new obj(this.head.field());
                this.head=null;
                this.tail=null;
                return ret;
            }
            else{
                obj ret=new obj(this.head.field());
                this.head=this.head.back();
                return ret;
            }
        }
        return null;
    }
    void stats(){
        System.out.println("Number of pops\t||\t"+pops);
        System.out.println("Number of pushes\t||\t"+pushes);
        System.out.println("Length of the stack\t||\t"+(pushes-pops));
    }
    void traverse(){
        if(head==null & tail==null) System.out.println("The stack is empty, Operation failed");
        obj cursor=null;
        System.out.println("    TAIL");
        System.out.println("\t|");
        System.out.println("\t|");
        System.out.println("\t|");
        System.out.println("\tV");
        do {
            cursor=tail;
            System.out.println(cursor.field());
            System.out.println("\t|");
            System.out.println("\t|");
            System.out.println("\t|");
            System.out.println("\tV");
            cursor=cursor.next();
        }
        while(cursor!=null);
        System.out.println("    HEAD");
    }
}

GitHub

但它有一个很大的问题:当我推送一个新元素时,它会被覆盖在前一个元素上。错误在哪里?可能我误解了与#34;相关的东西。 Java中的实践。而且,你会用什么方法制作这样的程序?

P.S。我知道有一个堆栈库,但我相信编写这些东西可以提高我对语言的了解。

3 个答案:

答案 0 :(得分:0)

您应该尝试在Obj类中覆盖equalsTo函数; 看起来应该是这样的:

@Override
public boolean equals(obj o){
    if (o.field.equalsTo(this.field) {
        return true;
    }
}
像那样,这应该有用

 if(this.head==null | this.tail==null){
            this.head = el;
            this.tail = el;
        }
        else{
            head.concat(el);
        }

答案 1 :(得分:0)

我已经看到在你的代码中发出。首先,如果你想添加到end然后使用tail将concat移动到当前node.if你推t1头和尾都指向同一节点后t2添加和头将指向t1,tail将指向t2。代码中缺少该部分。

tail.concat(el);
 tail=tail.next();

第二个遍历过程循环初始化一次侧循环

cursor=head;

答案 2 :(得分:0)

我做了两处修改让ik工作:

首先,你需要制作新的元素头

void push(obj el) {
    pushes++;
    if (this.head == null | this.tail == null) {
        this.head = el;
        this.tail = el;
    } else {
        el.concat(head);
        head = el;
    }
}

第二个弹出你必须使下一个元素头

obj pop() {
    if (head == null & tail == null)
        System.out.println("The stack is empty, Operation failed.");
    else {
        pops++;
        if (this.head == this.tail) {
            System.out.println("This is the last element; the stack is empty now");
            obj ret = new obj(this.head.field());
            this.head = null;
            this.tail = null;
            return ret;
        } else {
            obj ret = new obj(this.head.field());
            this.head = this.head.next();
            return ret;
        }
    }
    return null;
}

修改 没有检查遍历,不得不改变它:

void traverse() {
    if (head == null & tail == null)
        System.out.println("The stack is empty, Operation failed");
    obj cursor = null;
    System.out.println("    TAIL");
    System.out.println("\t|");
    System.out.println("\t|");
    System.out.println("\t|");
    System.out.println("\tV");
    cursor = tail;
    while (cursor != null) {
        System.out.println(cursor.field());
        System.out.println("\t|");
        System.out.println("\t|");
        System.out.println("\t|");
        System.out.println("\tV");
        cursor = cursor.back();
    }
    System.out.println("    HEAD");
}