Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

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

/** 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 StringLinkedListWithIterator { private ListNode head; private ListNode current; private ListNode previous; public StringLinkedListWithIterator () { head = null; current = null; previous = null; } public void addANodeToStart (String addData) { head = new ListNode (addData, head); if ((current == head.link) && (current != null)) //if current is at old start node previous = head; } /** Sets iterator to beginning of list. */ public void resetIteration () { current = head; previous = null; } /** Returns true if iteration is not finished. */ public boolean moreToIterate () { return current != null; } /** Advances iterator to next node. */ public void goToNext () { if (current != null) { previous = current; current = current.link; } 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 String getDataAtCurrent () { String 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 (String 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 (String newData) { ListNode newNode = new ListNode (); newNode.data = newData; if (current != null) { newNode.link = current.link; current.link = 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) && (previous != null)) { previous.link = current.link; current = current.link; } else if ((current != null) && (previous == null)) { //At head node head = current.link; current = head; } else //current == null { System.out.println ( "Deleting with uninitialized current or an empty list."); System.exit (0); } } // The methods length, onList, find, and showList, as well as the private inner class } 

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago