1

• Ask the user to enter a set of 5 numbers.

• For each number entered, add it into the front of the linked list.

• Now, ask the user to enter a search number.

• Using a for loop or while loop, search whether the number exist in one of the Nodes in the linked list.

• If there is a matching node, create a new node with data 88 and insert it right before the matching node. Otherwise, display the message “No such number”.

Hi everyone, I would like you to help me with the java code for the last part.

public static void main(String[] args) {
    LinkedList list = new LinkedList();
    Scanner sc = new Scanner(System.in);   
    System.out.println("Enter a number: ");
    int num = sc.nextInt(); sc.nextLine();
    for(int i = 0; i < 4; i++){
        list.addFront(num);
    }
    System.out.print("Enter a number: ");
    int search = sc.nextInt(); sc.nextLine();
    for(Node j = list.getHead(); j!= null; j=j.getNext()){
        if((Integer)j.getData()==search){
            list.addNode();
        }else{
            System.out.println("No such number");
        }

    }
    public static Node addNode(T n);//???
}
0

2 Answers 2

1

I think your code will not even work for the first point. What you do is read a number once and then put the same number 4 times into the linked list.

For adding the node to the list before another node you need the index of the node where you want to put it in front of and then use the add(int index, E element) mehtod of the LinkedList. The index can be found by indexOf(Object o). https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

Sign up to request clarification or add additional context in comments.

Comments

0

Re factored your code.Here is a working solution.You don't need to create an additional function for addNode(). There is a predefined function known as add(index,element) when you are using java.util.LinkedList.But i would advice to first create your own linklist instead of using predefined LinkedList class. This will clear all your doubts.

Here i am assumming you are using java.util.LinkedList.

public static void main(String args[]){
     LinkedList<Integer> list = new LinkedList<Integer>();
     Scanner sc = new Scanner(System.in);    
     for(int i = 0; i < 4; i++)
     {
         System.out.println("Enter a number: ");
         int num = sc.nextInt(); sc.nextLine();
         list.addFirst(num);
     }

    System.out.print("Initial list:"+list);
    System.out.print("Enter a number: ");
    int search = sc.nextInt(); sc.nextLine();
    Iterator<Integer > itr=list.iterator();
    int i=0;
    boolean flag=false;

    while(itr.hasNext())
    {
        int data=itr.next(); 
        if(data==search){
        list.add(i,88);
        flag=true;
        break;
    }
    i++;    //index of data  
    }
    if(!flag)
    {
        System.out.println("No such number");
    }
   else
   {
       System.out.println("Number inserted at "+i);
   }
   System.out.print("final list:"+list);
   sc.close();

}

Hope it helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.