如何在java中处理未完成的函数调用?

时间:2017-01-18 00:58:45

标签: java oop coding-style

the output looks like this

import java.io.*;
    class LL_two {


        Node head;
        static class Node {
            int data;
            Node next;
            Node(int d){
                data = d;
                next = null;
            }
        }

        public void disp(int nth) throws IOException{
            Node n = head;
            int count = 1;
            while(count<nth && n.next!=null){
                n = n.next;
                count++;
            }
            //System.out.println(count);
            if(count<nth){
                System.out.println("No Search results; enter + number within 10 or 10");
                main(null);
            }


            while(n != null){
                System.out.println(n.data);
                n = n.next;
            }
        }

        public int sizeOf(){
            Node n = head;
            int count = 0;
            while(n != null){
                count++;
                n = n.next;
            }
            return count;
        }


        public static void main(String[] args) throws IOException{


            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            int nth = Integer.parseInt(br.readLine());

            Node[] n = new Node[10];
            LL_two ll = new LL_two();
            for(int i =0; i<n.length-4; i++) {
                if(i==0){
                    n[i] = new Node(i+100+1);

                } else {
                    n[i] = new Node(i+100+1);
                    n[i-1].next = n[i];
                }
            }
            ll.head = n[0];
            ll.disp(nth);
            int c =ll.sizeOf();
            System.out.println(c);
        }
    }

您好,

这是一个简单的单链表实现。程序显示从第n个(用户输入)节点到最后一个节点的数据。我使用for循环在链表中添加了6个节点。

我遇到的问题是当用户输入的数字大于链表大小时,还有其他未执行的功能作业(但是我想是添加到堆栈中)(这叫做什么) ?)。这些未完成的函数调用最后执行。处理这个问题的有效方法是什么?

此外,我正在尝试(学习)保持模块化。所以我在考虑是否可以在disp中使用sizeOf函数来检查用户是否输入了有效数字。我能想到的一种方法是调用sizeOf并传递参数n(这只是head)。但是这使得代码不那么模块化,不是吗?

任何其他改进此代码的输入,或者我的编码技巧一般都会非常感激。

谢谢!

0 个答案:

没有答案