Home > Back-end >  Grocery shopping list (linked list: inserting at the end of a list) Java (SOLVED)
Grocery shopping list (linked list: inserting at the end of a list) Java (SOLVED)

Time:07-23

I am working on a Lab for a java class. Here are the directions and the codes given: Given main() in the ShoppingList class, define an insertAtEnd() method in the ItemNode class that adds an element to the end of a linked list. DO NOT print the dummy head node.

Ex. if the input is:

4 Kale Lettuce Carrots Peanuts where 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are the names of the items to be added at the end of the list.

The output is:

Kale Lettuce Carrots Peanuts

 public class ItemNode {
   private String item;
   private ItemNode nextNodeRef; // Reference to the next node                                        

   public ItemNode() {
      item = "";
      nextNodeRef = null;
   }

   // Constructor                                                                                     
   public ItemNode(String itemInit) {
      this.item = itemInit;
      this.nextNodeRef = null;
   }

   // Constructor                                                                                     
   public ItemNode(String itemInit, ItemNode nextLoc) {
      this.item = itemInit;
      this.nextNodeRef = nextLoc;
   }

   // Insert node after this node.                                                                   
   public void insertAfter(ItemNode nodeLoc) {
      ItemNode tmpNext;

      tmpNext = this.nextNodeRef;
      this.nextNodeRef = nodeLoc;
      nodeLoc.nextNodeRef = tmpNext;
   }

   // TODO: Define insertAtEnd() method that inserts a node
   //       to the end of the linked list  


   // Get location pointed by nextNodeRef                                                             
   public ItemNode getNext() {
      return this.nextNodeRef;
   }
   
   public void printNodeData() {
      System.out.println(this.item);
   }
}

This is also part of it, but this code can not be edited:

import java.util.Scanner;

public class ShoppingList {
   public static void main (String[] args) {
      Scanner scnr = new Scanner(System.in);

      ItemNode headNode;  // Create intNode objects                                                   
      ItemNode currNode;
      ItemNode lastNode;

      String item;
      int i;

      // Front of nodes list                                                                         
      headNode = new ItemNode();
      lastNode = headNode;

      int input = scnr.nextInt();

      for(i = 0; i < input; i   ){
         item = scnr.next();
         currNode = new ItemNode(item);
         lastNode.insertAtEnd(headNode, currNode);
         lastNode = currNode;
      }

      // Print linked list                                                                           
      currNode = headNode.getNext();
      while (currNode != null) {
         currNode.printNodeData();
         currNode = currNode.getNext();
      }
   }
}

This is what I have, but the code doesn't give the output in the correct order. Can anyone help me understand what I need to change, please?

public class ItemNode {
   private String item;
   private ItemNode nextNodeRef; // Reference to the next node                                        

   public ItemNode() {
      item = "";
      nextNodeRef = null;
   }

   // Constructor                                                                                     
   public ItemNode(String itemInit) {
      this.item = itemInit;
      this.nextNodeRef = null;
   }

   // Constructor                                                                                     
   public ItemNode(String itemInit, ItemNode nextLoc) {
      this.item = itemInit;
      this.nextNodeRef = nextLoc;
   }

   // Insert node after this node.                                                                   
   public void insertAfter(ItemNode nodeLoc) {
      ItemNode tmpNext;

      tmpNext = this.nextNodeRef;
      this.nextNodeRef = nodeLoc;
      nodeLoc.nextNodeRef = tmpNext;
   }

   // TODO: Define insertAtEnd() method that inserts a node
   //       to the end of the linked list  
   public void insertAtEnd(ItemNode headNode, ItemNode currNode){
       currNode.nextNodeRef = headNode.nextNodeRef;
         headNode.nextNodeRef = currNode;


   // Get location pointed by nextNodeRef                                                             
   public ItemNode getNext() {
      return this.nextNodeRef;
   }
   
   public void printNodeData() {
      System.out.println(this.item);
   }
}

CodePudding user response:

The issue comes from the fact that you need to navigate to the final node of the list before appending the new node.

You can do this as such:

public void insertAtEnd(ItemNode headNode, ItemNode currNode) {
  while(headNode.nextNodeRef != null) headNode = headNode.nextNodeRef;
  headNode.nextNodeRef = currNode;
}

Hope this helps. If you need any clarifications feel free to ask.

CodePudding user response:

You should pass your lastNode as an argument instead of headNode so you can get an easy reference to the current last element

public void insertAtEnd(ItemNode lastNode, ItemNode currNode){
   lastNode.nextNodeRef = currNode;
  •  Tags:  
  • java
  • Related