The book name is Java An introduction to problem sloving 7ed Plz give the code i
ID: 3854966 • Letter: T
Question
The book name is Java An introduction to problem sloving 7ed
Plz give the code in java and it match with the prof output below. Plz provide your output and match with my prof output.
The Assignment:
Chapter 12:
Programming Projects 9: This project is found starting on page 932.
Assignment Guidelines:
Write a parameterized class definition for a doubly linked list that has a parameter for the type of data stored in a node. Make the node class an inner class. Choosing which methods to define is part of this project. Also, write a program to thoroughly test your class definition.
Note:
This program is based on code from Listing 12.9. The solution given here includes support for iteration, both forward and reverse. Both forward and reverse iteration use the moreToIterate method, but backward iteration uses the resetIterationReverse rather than resetIteration. The ListNode class includes a previous instance variable which makes the previous variable in the outer class unnecessary. To make reverse iteration (especially resetIterationReverse) easier to write, there is a reference to the tail of the list as well as to the head of the list. An additional method, findInList, looks for an element in the list and sets current to point to that element if it is found. If it is not found, current is set to null. The method showListState is for testing and debugging purposes and prints the head of the list, the current element, the tail, and the number of elements in the list.
References:
Listing 12.9
Note:
The assignment must at least contain the following classes:
1. doublyLinkedList, in file doublyLinkedList.java and must contain the node class, as an inner class.
2. doublyLinkedListDemo, in file doublyLinkedListDemo.java
Listing 12.9
Sample Run:
----jGRASP: operation complete.
============================
============================
Find Elephant in list. Should NOT BE found.
Not found.
Head: Albatross Current: null Tail: Dolphin 4 items
Albatross
Baboon
Cheetah
Dolphin
============================
Find Cheetah in list. Should BE found.
Found.
Head: Albatross Current: Cheetah Tail: Dolphin 4 items
Albatross
Baboon
Cheetah
Dolphin
============================
Delete from the end of the list.
Head: Albatross Current: Elephant Tail: Elephant 5 items
Head: Albatross Current: null Tail: Dolphin 4 items
Albatross
Baboon
Cheetah
Dolphin
Iterate list in reverse.
Head: Albatross Current: Elephant Tail: Elephant 5 items
Beginning iteration.
Head: Albatross Current: Elephant Tail: Elephant 5 items
Head: Albatross Current: Dolphin Tail: Elephant 5 items
Head: Albatross Current: Cheetah Tail: Elephant 5 items
Head: Albatross Current: Baboon Tail: Elephant 5 items
Head: Albatross Current: Albatross Tail: Elephant 5 items
Finished iterating.
Head: Albatross Current: null Tail: Elephant 5 items
============================
Iterate to end of list.
Head: Albatross Current: Cheetah Tail: Elephant 5 items
Beginning iteration.
Head: Albatross Current: Cheetah Tail: Elephant 5 items
Head: Albatross Current: Dolphin Tail: Elephant 5 items
Head: Albatross Current: Elephant Tail: Elephant 5 items
Finished iterating.
Head: Albatross Current: null Tail: Elephant 5 items
============================
Add at beginning of list.
Head: Albatross Current: Cheetah Tail: Elephant 5 items
Albatross
Baboon
Cheetah
Dolphin
Elephant
============================
============================
Delete from the middle of the list.
Deleting current node: Caribou
Head: Baboon Current: Cheetah Tail: Elephant 4 items
Baboon
Cheetah
Dolphin
Elephant
============================
Go to previous node
Head: Baboon Current: Cheetah Tail: Elephant 5 items
Baboon
Caribou
Cheetah
Dolphin
Elephant
============================
Add more nodes to end of list.
Head: Baboon Current: Dolphin Tail: Elephant 5 items
Baboon
Caribou
Cheetah
Dolphin
Elephant
============================
Delete the first node.
Baboon
Caribou
Cheetah
============================
Add node to the middle of the list.
Head: Alligator Current: Baboon Tail: Cheetah 3 items
Head: Alligator Current: Baboon Tail: Cheetah 4 items
Alligator
Baboon
Caribou
Cheetah
============================
Add nodes to the end of the list.
Head: Alligator Current: Alligator Tail: Baboon 2 items
Head: Alligator Current: Baboon Tail: Cheetah 3 items
Alligator
Baboon
Cheetah
============================
Add a node to an empty list.
Head: Alligator Current: null Tail: Alligator 1 items
Head: null Current: null Tail: null 0 items
----jGRASP exec: java DoublyLinkedListDemo
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chegg.july;
/**
*
* @author Sam
*/
/**
Linked list with an iterator. One node is the "current node."
Initially, the current node is the first node. It can be changed
to the next node until the iteration has moved beyond the end
of the list.
*/
public class LinkedListWithIterator <T>
{
class ListNode {
T data;
ListNode prevLink;
ListNode nextLink;
public ListNode(T data, ListNode prevLink, ListNode nextLink) {
this.data = data;
this.prevLink = prevLink;
this.nextLink = nextLink;
}
private ListNode() {
data = null;
prevLink = null;
nextLink = null;
}
@Override
public String toString() {
return ""+ data;
}
}
private ListNode head;
private ListNode current;
private ListNode tail;
public LinkedListWithIterator ()
{
head = null;
current = null;
tail = null;
}
public void addANodeToStart (T addData)
{
head = new ListNode (addData, null, head);
if (head.nextLink!= null)
head.nextLink.prevLink = head;
if (tail == null)
tail = head;
if (current == head.nextLink)
current = head;
}
public void addANodeToEnd (T addData)
{
tail = new ListNode (addData, tail, null);
if (tail.prevLink!=null)
tail.prevLink.nextLink = tail;
if (head == null)
head = tail;
if (current == tail.prevLink)
current = tail;
}
/**
Sets iterator to beginning of list.
*/
public void resetIteration ()
{
current = head;
}
/**
Sets iterator to beginning of list.
*/
public void resetTailIteration ()
{
current = tail;
}
/**
Returns true if iteration is not finished.
*/
public boolean moreToIterate ()
{
return current != null;
}
/**
Advances iterator to next node.
*/
public void goToNext ()
{
if (current != null)
{
current = current.nextLink;
}
else if (head != null)
{
System.out.println (
"Iterated too many times or uninitialized iteration.");
System.exit (0);
}
else
{
System.out.println ("Iterating with an empty list.");
System.exit (0);
}
}
/**
Advances iterator to previous node.
*/
public void goToPrevious ()
{
if (current != null)
{
current = current.prevLink;
}
else if (head != null)
{
System.out.println (
"Iterated too many times or uninitialized iteration.");
System.exit (0);
}
else
{
System.out.println ("Iterating with an empty list.");
System.exit (0);
}
}
/**
Returns the data at the current node.
*/
public T getDataAtCurrent ()
{
T result = null;
if (current != null)
result = current.data;
else
{
System.out.println (
"Getting data when current is not at any node.");
System.exit (0);
}
return result;
}
/**
Replaces the data at the current node.
*/
public void setDataAtCurrent (T newData)
{
if (current != null)
{
current.data = newData;
}
else
{
System.out.println (
"Setting data when current is not at any node.");
System.exit (0);
}
}
/**
Inserts a new node containing newData after the current node.
The current node is the same after invocation as it is before.
Precondition: List is not empty; current node is not
beyond the entire list.
*/
public void insertNodeAfterCurrent (T newData)
{
ListNode newNode = new ListNode ();
newNode.data = newData;
if (current != null)
{
if (tail == current)
tail = newNode;
newNode.nextLink = current.nextLink;
newNode.prevLink = current;
if (current.nextLink != null)
current.nextLink.prevLink = newNode;
current.nextLink = newNode;
}
else if (head != null)
{
System.out.println (
"Inserting when iterator is past all " +
"nodes or is not initialized.");
System.exit (0);
}
else
{
System.out.println (
"Using insertNodeAfterCurrent with empty list.");
System.exit (0);
}
}
/**
Deletes the current node. After the invocation,
the current node is either the node after the
deleted node or null if there is no next node.
*/
public void deleteCurrentNode ()
{
if ((current != null) && (current.prevLink != null))
{
current.prevLink.nextLink = current.nextLink;
if (current.nextLink!=null)
current.nextLink.prevLink = current.prevLink;
if (current.nextLink == null)
tail = tail.prevLink;
current = current.nextLink;
}
else if ((current != null) && (current.prevLink == null))
{ //At head node
head = current.nextLink;
head.prevLink = null;
current = head;
if (current.nextLink == null)
tail = current;
}
else //current == null
{
System.out.println (
"Deleting with uninitialized current or an empty list.");
System.exit (0);
}
}
public int length() {
ListNode curr = head;
int count = 0;
while (curr!=null) {
count ++;
curr = curr.nextLink;
}
return count;
}
public void showList() {
System.out.println(getState());
ListNode curr = head;
while (curr!=null) {
System.out.println(curr);
curr = curr.nextLink;
}
}
public boolean find(T data) {
ListNode curr = head;
while (curr!=null) {
if (curr.data.equals(data)) {
current = curr;
return true;
}
curr = curr.nextLink;
}
return false;
}
public String getState() {
String result = "";
result += "head: " + head;
result += "; Current: " + current;
result += "; Tail: " + tail;
result += "; " + length() + " items";
return result;
}
// The methods length, onList, find, and showList, as well as the private inner class
}
class StringLLDriver {
public static void main(String[] args) {
LinkedListWithIterator<String> iterator = new LinkedListWithIterator<>();
System.out.println("============================");
System.out.println("============================");
System.out.println("Add a node to an empty list.");
System.out.println(iterator.getState());
iterator.addANodeToStart("Dolphin");
iterator.showList();
iterator.addANodeToStart("Cheetah");
iterator.addANodeToStart("Baboon");
iterator.addANodeToStart("Albatross");
System.out.println("Find Elephant in list. Should NOT BE found.");
if (iterator.find("Elephant"))
System.out.println("Fount");
else
System.out.println("Not Found");
iterator.showList();
System.out.println("============================");
System.out.println("Find Cheetah in list. Should NOT BE found.");
if (iterator.find("Cheetah"))
System.out.println("Fount");
else
System.out.println("Not Found");
iterator.showList();
System.out.println("============================");
System.out.println("Add node to the end of the list");
iterator.showList();
System.out.println("Delete from the end of the list.");
iterator.resetTailIteration();
System.out.println(iterator.getState());
iterator.deleteCurrentNode();
System.out.println(iterator.getState());
System.out.println("Iterate list in reverse.");
System.out.println(iterator.getState());
iterator.resetTailIteration();
System.out.println("Beginning iteration.");
while (iterator.moreToIterate()) {
System.out.println(iterator.getState());
iterator.goToPrevious();
}
System.out.println("Finished iterating.");
System.out.println(iterator.getState());
System.out.println("============================");
iterator.resetIteration();
System.out.println("Beginning iteration.");
System.out.println(iterator.getState());
while (iterator.moreToIterate()) {
System.out.println(iterator.getState());
iterator.goToNext();
}
System.out.println("Finished iterating.");
System.out.println(iterator.getState());
System.out.println("============================");
System.out.println("Add at beginning of list.");
iterator.showList();
System.out.println("============================");
System.out.println("============================");
iterator.showList();
iterator.resetIteration();
iterator.goToNext();
iterator.deleteCurrentNode();
iterator.showList();
System.out.println("============================");
System.out.println("============================");
System.out.println("============================");
System.out.println("Delete the first node.");
iterator.resetIteration();
iterator.deleteCurrentNode();
iterator.showList();
System.out.println("============================");
System.out.println("============================");
System.out.println("============================");
}
}
Demo class desription is missing and chegg protocol insist us to answer 1 question. So I helped you with the main class, and most of the demo class I hope you like it. Let me know you need help with the demo class.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.