在自定义LinkedStack方法中没有添加toString()方法

时间:2017-09-25 16:57:23

标签: java linked-list stack

我遇到了将值添加到链接堆栈的问题,至少在打印出值时会出现问题。我甚至不确定我是否正确编码,甚至将值添加到堆栈中。我为此创建了3个类,一个Node类,一个LinkedStack类和一个运行该程序的驱动程序。

这是节点类:

public class Node
{
    int data;
    Node link;

    //contructor
    public Node(int d)
    {
        data =  d;
        link = null;
    }

    public int getData()
    {
        return data;
    }

    public Node getLink()
    {
        return link;
    }

    public void setData(int d)
    {
        data = d;
    }

    public void setLink(Node n)
    {
        link = n;
    }
}

这是LinkedStack类:

import java.util.NoSuchElementException;

public class LinkedStack
{

    //declare variables
    private Node top;
    private int size;

    //constructor to initialize variables
    public LinkedStack()
    {
        size = 0;
        top = null;
    }

    //push method to add numbers to the stack
    public void push(int value)
    {
        if(!isEmpty())
        {
            top.setData(value);
            size++;
        }
    }

    //method to remove the top number in the stack
    public int pop()
    {
        Node temp = top;
        if(!isEmpty())
        {
            temp = top;
            size--;
        }

        return temp.getData();
    }

    //boolean method to check if the stack is empty
    public boolean isEmpty()
    {
        if(top == null)
            return true;
        else
            return false;
    }

    //method to return the size of the stack
    public int size()
    {
        return size;
    }

    //method to print out the top number in the stack
    public int peek()
    {
        if (isEmpty() == true)
        {
            throw new NoSuchElementException("Stack is empty.");
        }
        else
        {
            return top.getData();
        }
    }

    //method to turn the stack into a String
    public String toString()
    {
        String returnStr = "Stack: [";
        for(int i = 0 ; i < size; i++)
        {
            returnStr += top.getData() + ", ";
        }

        returnStr += "]";

        return returnStr;
    }
}

这是驱动程序类:

import java.util.Scanner;

public class Driver
{
    public static void main(String[] args)
    {
        //declare variables and initialize scanner
        Scanner key = new Scanner(System.in);
        int size, choice, value, end;

        end = 0;

        //declare and initialize the stack
        LinkedStack stack1 = new LinkedStack();

        //loop to continue operations
        while(end == 0)
        {
            //print out menu for commands
            System.out.println("\t1) Push \n\t2) Pop \n\t3) Peek \n\t4) Size \n\t5) isEmpty \n\t6) End");
            System.out.print("Please choose an option: ");
            choice = key.nextInt();

            //switch the choice and execute commands
            switch (choice)
            {
                case 1: System.out.println("Please enter a value: ");
                    value = key.nextInt();
                    stack1.push(value);
                    System.out.println(stack1.toString());
                    break;
                case 2: stack1.pop();
                    System.out.println(stack1.toString());
                    break;
                case 3: stack1.peek();
                    System.out.println(stack1.toString());
                    break;
                case 4: System.out.println("Size: " + stack1.size());
                    System.out.println(stack1.toString());
                    break;
                case 5: if(stack1.isEmpty())
                {
                    System.out.println("Stack is empty.");
                }
                else
                    System.out.println("Stack is NOT empty.");
                    System.out.println(stack1.toString());
                    break;
                case 6: end = 1;
                    System.out.println("Goodbye!");
                    break;
            }
        }
    }
}

我得到的问题是堆栈中没有打印。 在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您忘记初始化Node对象

df1<-aggregate(resp_correlativo ~ usua_rut, FUN = paste, collapse = 
               "+", data = filter(data.plot,preg_codigo==1))
df2<-aggregate(resp_correlativo ~ usua_rut, FUN = paste, collapse = 
               "+", data = filter(data.plot,preg_codigo==2))
df<-cbind(df1,df2[,2])
colnames(df)<-c("UserID","Question1","Question2")

答案 1 :(得分:0)

我更新了你的一些方法,我认为休息方法很好。

public int pop()
{
    Node temp = top;
    if(!isEmpty())
    {
        top = temp.link;
        size--;
    } else {
           throw new IllegalAccessError();
    }
    return temp.getData();
}

public void push(int value)
{
    Node node = new Node(value);
    if(!isEmpty())
    {
        node.link = top;
        top = node;      
    } else {
        top = node;
    }
    size++;
}
   public String toString()
    {
        String returnStr = "Stack: [";
        Node tmp = top;
        while(tmp != null) {
            returnStr += tmp.getData() + ", ";
            tmp = tmp.link;
        }
        returnStr += "]";

        return returnStr;
    }
相关问题