简体   繁体   中英

how to implement a linked list properly using java

I am working in a project with linked list (JAVA) but I am not being able to figure out how to insert the person. How could I get the switch to work? Or should I use something else? I also thought about creating a class outside the main method and just call it but it did not work as well. Any help will be much appreciate

public void listOfPeople() { // Beginning of the method listOfPeople where shows the employees

        //  *** instance the person object ad loading your variables
        Personqueue p1 = new Personqueue(); //saving the employee in a variable???
        p1.setFname("John");
        p1.setLname("Smith");
        p1.setDOA(15);
        p1.setPassportN(306589);
        p1.setNumber(1);
        vectorObj.add(p1); // add the employee in a vector

        Personqueue p2 = new Personqueue(); //saving the employee in a variable???
        p1.setFname("Paul");
        p1.setLname("Clooney");
        p1.setDOA(5);
        p1.setPassportN(30614584);
        p1.setNumber(2);
        vectorObj.add(p2); // add the employee in a vector



    }

    public void ascendingOrder(Vector<Personqueue> vector) { // bubble sort method to order the vector

        int j;
        boolean flag = true; // set flag to true to begin first pass
        Personqueue temp; // holding the variable temporarily

        while (flag) {

            flag = false; // set flag to false awaiting a possible swap
            for (j = 0; j < vector.size() - 1; j++) {

                if (vector.get(j).getNumber() > vector.get(j + 1).getNumber()) {

                    temp = vector.get(j); // swap elements
                    vector.set(j, vector.get(j + 1));
                    vector.set(j + 1, temp);
                    flag = true; // shows a swap occurred

                }

            }

        }

    }

    public void newPerson(int positionPerson) { // beginning of newPerson method

        String Option = null; // declaration of local variables that are used only in this method and don't use too much space
        Personqueue p = new Personqueue(); //instead of setting it to null, here we are calling 
                                                  // a constructor which was declared in Personqueue class.

//      switch (positionPerson) {
//      case 1: // insert a person at the start of the queue
//      //  p = new QueueStart();
//          break; // executes in order to end the switch in case one of the options is valid
//      case 2: // insert a person at a chosen point in the queue
//      //  p = new ChoosePosition();
//      case 3: // insert a person at the end of the queue
//          //  p = new EndQueue();
//          break;
//      default:
//          System.out.println("Invalid Option!!!"); // in case the option is not one of the cases above, print this...
//          return; // return to the do/while loop in Principal method
//      }

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Name:"); // user input
        try {
            Option = br.readLine();
            String name = Option; 

            p.setFname(name);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Surname:"); // user input
        try {
            Option = br.readLine();
            String lname = Option; 

            p.setLname(lname);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Date Of Arrival: "); // user input
        try {
            Option = br.readLine();
            int doa = Integer.parseInt(Option); // use parseInt in order to convert Integer to String to be read by BufferedReader.

            p.setDOA(doa);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Passport Number:"); // user input
        try {
            Option = br.readLine();
            int pn = Integer.parseInt(Option);

            p.setPassportN(pn);

        } catch (IOException e) {
            System.out.println(e);

        }

        System.out.println("Number:"); // user input
        try {
            Option = br.readLine();
            int no = Integer.parseInt(Option);

            p.setNumber(no);

        } catch (IOException e) {
            System.out.println(e);

        }

        vectorObj.addElement(p); // save all the data in the vector

        System.out.print("Saving Person :" + p.getFname()); // print to the user the name's been saved

        try {
            for (int i = 0; i < 6; i++) { 
                System.out.print(".");
                Thread.sleep(300); // suspend the "." execution for a specified period. 

            }

        } catch (InterruptedException e) { //  exception p o method thread //  catch for thread above

            e.printStackTrace(); 
        }

        System.out.println();
        System.out.println("Person: " + p.getFname() + " is saved!!!");

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {

            e.printStackTrace();
        }

    } // end of the Method newPerson

Check your variables in listOfPeople(...), p2 was not used correctly.

Take a look at Example of LinkedList in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM