Home > Net >  how to add bunch of data into linked list
how to add bunch of data into linked list

Time:11-10

Basically , i just tried to learn linked list but I cant seems to understand how to insert bunch of data from different variables into it. Does it work like an array/ arraylist? Before we end the loop we supposed to store the data right , but how??

Let say I have variables ( name, age, phonenum) .

'''

    char stop='Y';

    while(stop!='N'){

    System.out.println("\nEnter your name : ");
    int name= input.nextLine();
    System.out.println("\nEnter your age: ");
    int age= input.nextInt();
    System.out.println("\nEnter your phone number: ");
    int phonenum= input.nextLine();

    System.out.println("Enter 'Y' to continue, 'N' to Stop: ");
    stop = sc.nextLine().charAt(0);

}

'''

CodePudding user response:

First, change your code to use appropriate types. Name and phone should be of type String, not int.

Define a class to hold your fields. Records are an easy way to do that.

record Person ( String name , int age , String phone ) {}

Declare your list to hold objects of that class.

List< Person > list = new LinkedList<>() ;

Instantiate some Person objects, and add to list.

list.add( New Person( "Alice" , 29 , "477.555.1234" ) ) ;

In the line above, I hard-coded some example data. In your own code, you will be passing to the constructor the variables you populated by interacting with the user.

list.add( New Person( name , age , phonenum ) ) ;

CodePudding user response:

You can create an object which has name, age and phenomenon then create an insert method which you call in your while loop.

In psuedo code it would look something like this:

public class Data { 
    String name; 
    int age; 
    int phenomenon; 
    //constructor
    //getters & setters
}

This class above will hold contain the user input. You can gather all the user input and store it in an array and perform the insert with array of data instead of inserting one object at a time

public void InsertData(LinkedList<Data> list, Arraylist<Data> input) {
  for(Data d: input){
   list.add(d);
  } 
}

You can read up on linkedlists a bit more here to understand how exactly linkedlists work and implement your own from scratch: https://www.geeksforgeeks.org/implementing-a-linked-list-in-java-using-class/

CodePudding user response:

Try this

Possibility : 1

import java.util.*;

public class Naddy {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        char stop = 'Y';

        LinkedList<Object> list = new LinkedList<Object>();

        while (stop != 'N') {

            System.out.println("\nEnter your name : ");
            String name = input.nextLine();
            System.out.println("\nEnter your age: ");
            int age = input.nextInt();
            System.out.println("\nEnter your phone number: ");
            long phonenum = input.nextLong();

            list.add(name);
            list.add(age);
            list.add(phonenum);
            System.out.println("Enter 'Y' to continue, 'N' to Stop: ");
            input.nextLine();
            stop = input.nextLine().charAt(0);
        }

        System.out.println(list);
    }
}

possibility : 2

import java.util.*;

public class Naddy {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        char stop = 'Y';

        LinkedList<User> list = new LinkedList<User>();

        while (stop != 'N') {

            System.out.println("\nEnter your name : ");
            String name = input.nextLine();
            System.out.println("\nEnter your age: ");
            int age = input.nextInt();
            System.out.println("\nEnter your phone number: ");
            long phonenum = input.nextLong();

            list.add(new User(name, age, phonenum));
            System.out.println("Enter 'Y' to continue, 'N' to Stop: ");
            input.nextLine();
            stop = input.nextLine().charAt(0);
        }

        System.out.println(list);
    }
}

class User {
    private String name;
    private int age;
    private long phonenum;

    public User() {
    }

    public User(String name, int age, long phonenum) {
        this.name = name;
        this.age = age;
        this.phonenum = phonenum;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public long getPhonenum() {
        return phonenum;
    }

    public void setPhonenum(long phonenum) {
        this.phonenum = phonenum;
    }

    @Override
    public String toString() {
        return "User [age="   age   ", name="   name   ", phonenum="   phonenum   "]";
    }

}
  • Related