LinkedList方法

时间:2016-03-31 00:07:19

标签: java object linked-list

我在打印信息时遇到问题。该程序以包含以下选项的菜单开始:

  1. 添加员工
  2. 删除员工
  3. 打印员工信息
  4. 退出
  5. 如果您想添加一名员工,它会询问您想添加多少,但我还没有这样做。 现在我只想打印出员工的名字和姓氏,然后打印出每个员工的循环。

    主要

    public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner keyboard = new Scanner(System.in);
            System.out.println("1. Add Employee");
            System.out.println("2. Remove Employee");
            System.out.println("3. Print Employee Information");
            System.out.println("4. Exit");
    
    
            int option = keyboard.nextInt();
            keyboard.nextLine();//for the string buffer
            System.out.println("Enter employee first name: ");
    
                String firstName = keyboard.nextLine();
            System.out.println("Enter employee last name: ");
                String lastName = keyboard.nextLine();
            System.out.println("Enter employee address: ");
                String address = keyboard.nextLine();
            System.out.println("Enter employee city: ");
                String city = keyboard.nextLine();
            System.out.println("Enter employee state: ");
                String state = keyboard.nextLine();
            System.out.println("Enter employee zip: ");
                String zip = keyboard.nextLine();
            System.out.println("Enter employee age: ");
                int age = keyboard.nextInt();
    
                keyboard.nextLine();
                //for the string buffer
    
            Employee empList = new Employee(firstName, lastName, 
                    address, city, state, zip, age);
    
    
            //populate employee class
            LinkedList ll = new LinkedList();
    
            ll.toString();
            System.out.println(ll);
    
        }
    

    链表

    public class LinkedList {
        private Node first = null;
        Node last;
    
        private class Node{
            Node next;
            Employee e;
            //Employee e;
    
            Node (Employee val, Node n){
                 Employee emp = new Employee(val.getFName(), val.getLName(), val.getAddress(),
                         val.getCity(), val.getState(), val.getZip(), val.getAge());
                 e = val;
            }
    
            Node(Employee val){
                this(val,null);
            }
            //SET with e, and GET from val
        }
        public LinkedList(){
            first = null;
            last = null;
        }
    
    public void print(){
            Node ref = first;
            while (ref != null){
                System.out.print(ref.e + " ");
                ref = ref.next;
            }
        }
        public void add(Employee e){
            if(isEmpty()){
                first = new Node(e);
                last = first;
            }
            else{
                last.next = new Node(e);
                last = last.next;
            }
        }
    
    
        public String toString(){
            StringBuilder strBuilder = new StringBuilder();
            Node p = first;
            while(p != null){
                strBuilder.append(p.e + "\n");
                p = p.next;
            }
            return strBuilder.toString();
        }
    
    
    }
    

0 个答案:

没有答案