Question

In: Computer Science

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

FindOccurence.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.");
}
}
}

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;
}
}
}

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);
}
}

Solutions

Expert Solution

Screenshot

Program

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);
}
}

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) {
   PersonNode curNode;

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

// 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) {
   PersonNode curNode;

   curNode =tailNode;
   while (curNode != null) {
       if(curNode.getAge()==ageValue) {
           return curNode;
       }
       curNode = curNode.prevNode;
   }
   return null;
}


public void printPersonList() {
PersonNode curNode;

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

FindOcurrence.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.");
}
}
}

-----------------------------------------------------------

Output

Alex 23
Tom 41
Michelle 34
Vicky 24
-1
List before replacing:
Alex, 23
Tom, 41
Michelle, 34
Vicky, 24
23
Connor

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

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


Related Solutions

9.7 LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class,...
9.7 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 import java.util.Scanner; public class SortedList { public static void main (String[] args)...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
You are asked to delete the last occurrence of an item from a linked list. So,...
You are asked to delete the last occurrence of an item from a linked list. So, for instance: Input: 2 -> 3 -> 2 -> 4 Delete last occurrence of 2, result: 2 -> 3 -> 4 Implement the following method to do the deletion. You may NOT use or implement helper methods - all your code must be implemented inside the given method. You may NOT use recursion. public class Node { public int data; public Node next; }...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Can you fix please? this is adding the given location of doubly linked list in java...
Can you fix please? this is adding the given location of doubly linked list in java but if I make reverse is not adding the new node that I added but is printing forward correctly. just fix to me that part public void addAtLocation(E newNode, int location) {        Node node = new Node(newNode);               node.data = newNode;               Node previous = head;                      int counter = 1;...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list....
1- Use LinkList. Write removeLast(n). Delete the last occurrence of an item from a linked list. So if the item is 7 and the list is [1,3,7,4,7,3,7,2], the result is [1,3,7,4,7,3,2] 2- Use LinkList. Write removeAll(int n). Deletes all occurrences of an item n from a linked list. So if the item is 7 and the list1 is [1,3,7,4,7,3,2] , then list1.removeAll(7) then list1 becomes [1,3,4,3,2]. Demonstrate by displaying the list contents before and after calling the above methods. Eg:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT