Question

9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...

9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list)

Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is not found, null should be returned. The program will replace the name value of each found node with a new name.

Ex. If the input is

Alex 23
Tom 41
Michelle 34
Vicky 23
-1
23
Connor
34
Michela

the output is

List before replacing:
Alex, 23
Tom, 41
Michelle, 34
Vicky, 23

List after replacing the first occurrence of 23:
Connor, 23
Tom, 41
Michelle, 34
Vicky, 23

List after replacing the last occurrence of 34:
Connor, 23
Tom, 41
Michela, 34
Vicky, 23

File is marked as read only

Current file:

FindOccurrence.java

import java.util.Scanner;

public class FindOccurrence {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
PersonList personList = new PersonList();
PersonNode curNode;
PersonNode foundFirst;
PersonNode foundLast;
String name;
String replaceName;
int age;
int findAge;

name = scnr.next();

while (!name.equals("-1")) {
// Insert into linked list   
age = scnr.nextInt();
curNode = new PersonNode(name, age);
personList.append(curNode);
name = scnr.next();
}

System.out.println("List before replacing:");
personList.printPersonList();

foundFirst = new PersonNode();
findAge = scnr.nextInt();
foundFirst = personList.findFirst(findAge);
if (foundFirst != null) {
replaceName = scnr.next();
foundFirst.setName(replaceName);
System.out.println();
System.out.println("List after replacing the first occurrence of " + findAge + ":");
personList.printPersonList();
}
else {
System.out.println("Age value not found in the list.");
}
  
foundLast = new PersonNode();
findAge = scnr.nextInt();
foundLast = personList.findLast(findAge);
if (foundLast != null) {
replaceName = scnr.next();
foundLast.setName(replaceName);
System.out.println();
System.out.println("List after replacing the last occurrence of " + findAge + ":");
personList.printPersonList();
}
else {
System.out.println("Age value not found in the list.");
}
}
}

Current file:

PersonList.java

public class PersonList {
// Linked list nodes
public PersonNode headNode;
public PersonNode tailNode;

public PersonList() {
// Front of nodes list   
headNode = null;
tailNode = null;
}

// append
public void append(PersonNode newNode) {
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else {
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
}

// prepend   
public void prepend(PersonNode newNode) {
if (headNode == null) { // list empty
headNode = newNode;
tailNode = newNode;
}
else {
newNode.nextNode = headNode;
headNode.prevNode = newNode;
headNode = newNode;
}
}
// insertAfter   
public void insertAfter(PersonNode curNode, PersonNode newNode) {
PersonNode sucNode;
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else if (curNode == tailNode) { // Insert after tail   
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
else {
sucNode = curNode.nextNode;
newNode.nextNode = sucNode;
newNode.prevNode = curNode;
curNode.nextNode = newNode;
sucNode.prevNode = newNode;
}
}

// TODO: Write findFirst() method
// Find the node with the first occurrence of the age
// Start with the headNode and traverse forward
public PersonNode findFirst(int ageValue) {

}

// TODO: Write findLast() method   
// Find the node with the last occurrence of the age   
// Start with the tailNode and traverse backward
public PersonNode findLast(int ageValue) {
  
}


public void printPersonList() {
PersonNode curNode;

curNode = headNode;
while (curNode != null) {
curNode.printNodeData();
curNode = curNode.nextNode;
}
}
}

File is marked as read only

Current file:

PersonNode.java

public class PersonNode {
private String name;
private int age;
public PersonNode prevNode; // Reference to the previous node
public PersonNode nextNode; // Reference to the next node

public PersonNode() {
name = "";
age = 0;
prevNode = null;
nextNode = null;
}

// Constructor   
public PersonNode(String nameInit, int ageInit) {
this.name = nameInit;
this.age = ageInit;
this.prevNode = null;
this.nextNode = null;
}

// Constructor   
public PersonNode(String nameInit, int ageInit, PersonNode prevNode, PersonNode newNextNode) {
this.name = nameInit;
this.age = ageInit;
this.prevNode = prevNode;
this.nextNode = newNextNode;
}

public String getName() {
return this.name;
}

public long getAge() {
return this.age;
}

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

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

public void printNodeData() {
System.out.println(this.name + ", " + this.age);
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class FindOccurrence {
public static void main (String[] args) {
Scanner scnr = new Scanner(System.in);
PersonList personList = new PersonList();
PersonNode curNode;
PersonNode foundFirst;
PersonNode foundLast;
String name;
String replaceName;
int age;
int findAge;

name = scnr.next();

while (!name.equals("-1")) {
// Insert into linked list   
age = scnr.nextInt();
curNode = new PersonNode(name, age);
personList.append(curNode);
name = scnr.next();
}

System.out.println("List before replacing:");
personList.printPersonList();

foundFirst = new PersonNode();
findAge = scnr.nextInt();
foundFirst = personList.findFirst(findAge);
if (foundFirst != null) {
replaceName = scnr.next();
foundFirst.setName(replaceName);
System.out.println();
System.out.println("List after replacing the first occurrence of " + findAge + ":");
personList.printPersonList();
}
else {
System.out.println("Age value not found in the list.");
}
  
foundLast = new PersonNode();
findAge = scnr.nextInt();
foundLast = personList.findLast(findAge);
if (foundLast != null) {
replaceName = scnr.next();
foundLast.setName(replaceName);
System.out.println();
System.out.println("List after replacing the last occurrence of " + findAge + ":");
personList.printPersonList();
}
else {
System.out.println("Age value not found in the list.");
}
}
}

public class PersonList {
// Linked list nodes
public PersonNode headNode;
public PersonNode tailNode;

public PersonList() {
// Front of nodes list   
headNode = null;
tailNode = null;
}

// append
public void append(PersonNode newNode) {
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else {
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
}

// prepend   
public void prepend(PersonNode newNode) {
if (headNode == null) { // list empty
headNode = newNode;
tailNode = newNode;
}
else {
newNode.nextNode = headNode;
headNode.prevNode = newNode;
headNode = newNode;
}
}
// insertAfter   
public void insertAfter(PersonNode curNode, PersonNode newNode) {
PersonNode sucNode;
if (headNode == null) { // List empty
headNode = newNode;
tailNode = newNode;
}
else if (curNode == tailNode) { // Insert after tail   
tailNode.nextNode = newNode;
newNode.prevNode = tailNode;
tailNode = newNode;
}
else {
sucNode = curNode.nextNode;
newNode.nextNode = sucNode;
newNode.prevNode = curNode;
curNode.nextNode = newNode;
sucNode.prevNode = newNode;
}
}

// TODO: Write findFirst() method
// Find the node with the first occurrence of the age
// Start with the headNode and traverse forward
public PersonNode findFirst(int ageValue) {

   if(headNode == null)
       return null;
      
   PersonNode t = headNode;
   while(t.getAge()!=ageValue && t!=null)
   {
       t = t.nextNode;
   }
  
   return t;
}

// TODO: Write findLast() method   
// Find the node with the last occurrence of the age   
// Start with the tailNode and traverse backward
public PersonNode findLast(int ageValue) {
  
if(tailNode == null)
       return null;
      
   PersonNode t = tailNode;
   while(t.getAge()!=ageValue && t!=null)
   {
       t = t.prevNode;
   }
  
   return t;
}


public void printPersonList() {
PersonNode curNode;

curNode = headNode;
while (curNode != null) {
curNode.printNodeData();
curNode = curNode.nextNode;
}
}
}


public class PersonNode {
private String name;
private int age;
public PersonNode prevNode; // Reference to the previous node
public PersonNode nextNode; // Reference to the next node

public PersonNode() {
name = "";
age = 0;
prevNode = null;
nextNode = null;
}

// Constructor   
public PersonNode(String nameInit, int ageInit) {
this.name = nameInit;
this.age = ageInit;
this.prevNode = null;
this.nextNode = null;
}

// Constructor   
public PersonNode(String nameInit, int ageInit, PersonNode prevNode, PersonNode newNextNode) {
this.name = nameInit;
this.age = ageInit;
this.prevNode = prevNode;
this.nextNode = newNextNode;
}

public String getName() {
return this.name;
}

public long getAge() {
return this.age;
}

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

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

public void printNodeData() {
System.out.println(this.name + ", " + this.age);
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • Inventory (linked lists: insert at the front of a list)

    import java.util.Scanner;public class Inventory {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);             InventoryNode headNode;                                                    InventoryNode currNode;      InventoryNode lastNode;      String item;      int numberOfItems;      int i;      // Front of nodes list           ...

  • Grocery shopping list (linked list: inserting at the end of a list)

    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           ...

  • Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the...

    Implement the following in java. 1. An insertAtBeginning(Node newNode) function, that inserts a node at the beginning(root) of the linked list. 2. A removeFromBeginning() function, that removes the node from the beginning of the linked list and assigns the next element as the new beginning(root). 3. A traverse function, that iterates the list and prints the elements in the linked list. For the insertAtBeginning(Node newNode) function: 1. Check if the root is null. If it is, just assign the new...

  • THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...

    THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers...

  • 10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)

    10.16 LAB: Grocery shopping list (linked list: inserting at the end of a list)Hello, I searched for similar problems, but their InsertAtEnd() have two parameters, while my question asks for one.Given main(), define an InsertAtEnd() member function 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  Peanutswhere 4 is the number of items to be inserted; Kale, Lettuce, Carrots, Peanuts are...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-ยป2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT