The book name is Java An introduction to problem sloving 7ed Plz give the code i
ID: 3854617 • 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 too. My prof is get really mad when the code output is not macting with his output and he cut a lot of point for that so provide your code 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
Listing 12.8
Listing 12.7
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
//DoublyLinkedList.java
public class DoublyLinkedList<T> {
public class Node<T>
{
private T data;
private Node<T> next;
private Node<T> prev;
Node()
{
next=null;
prev=null;
data=null;
}
Node(T data)
{
this(data, null, null);
}
Node(T data, Node<T> next, Node<T> prev)
{
this.data = data;
this.next = next;
this.prev = prev;
}
T getData()
{
return data;
}
public void setNextNode(Node<T> next)
{
this.next = next;
}
public Node<T> getPrevNode()
{
return prev;
}
public void setPrevNode(Node<T> prev)
{
this.prev = prev;
}
public void setData(T data)
{
this.data = data;
}
public Node<T> getNextNode()
{
return next;
}
}
private Node<T> head;
private Node<T> tail;
public int getSize()
{
int count = 0;
if(head==null)
return count;
else
{
Node<T> temp=head;
do{
temp=temp.getNextNode();
count++;
}while(temp!=tail);
}
return count;
}
//Traverse from head
public void traverse(){
Node<T> temp=head;
while(temp!=null){
System.out.print(temp.getData()+" ");
temp=temp.getNextNode();
}
}
//Traverse from tail
public void traverseB(){
Node<T> temp=tail;
while(temp!=null){
System.out.print(temp.getData()+" ");
temp=temp.getPrevNode();
}
}
/* methods related to insertion in doubly linked list starts.*/
public void insertFront(T data){
Node<T> newnode=new Node<T>(data);
if(head==null){
head=newnode;
tail=newnode;
newnode.setNextNode(null);
newnode.setPrevNode(null);
}else{
newnode.setNextNode(head);
head.setPrevNode(newnode);
head=newnode;
}
}
public void insertAtEnd(T data){
Node<T> newnode=new Node<T>(data);
if(tail==null){
head=newnode;
tail=newnode;
newnode.setNextNode(null);
newnode.setPrevNode(null);
}else{
newnode.setPrevNode(tail);
tail.setNextNode(newnode);
tail=newnode;
}
}
public void insertAtLoc(T data,int position){
if(position<0||position==0){
insertFront(data);
}
else if(position>getSize()||position==getSize()){
insertAtEnd(data);
}else{
Node<T>temp=head;
Node<T> newnode=new Node<T>(data);
for(int i=0;i<position-1;i++){
temp=temp.getNextNode();
}
newnode.setNextNode(temp.getNextNode());
temp.getNextNode().setPrevNode(newnode);
temp.setNextNode(newnode);
newnode.setPrevNode(temp);
}
}
private void remove(Node<T> node){
if(node.getPrevNode()==null)
head=node.getNextNode();
else if(node.getNextNode()==null)
tail=node.getPrevNode();
else{
Node<T> temp=node.getPrevNode();
temp.setNextNode(node.getNextNode());
node.getNextNode().setPrevNode(temp);
}
node=null;
}
//Removal based on a given position
public T remove(int position){
T data=null;
if(position==1){
data=head.getData();
head=head.getNextNode();
}else if(position==getSize()){
data=tail.getData();
tail=tail.getPrevNode();
tail.setNextNode(null);
}else{
Node<T> temp=head;
for(int i=0;i<position;i++){
temp=temp.getNextNode();
}
Node<T> node=temp.getNextNode();
node.setPrevNode(temp.getPrevNode());
temp.getPrevNode().setNextNode(node);
temp=null;
}
return data;//Deleted node's data
}
}
// doublyLinkedListDemo
public class DoublyLinkedListDemo
{
public static void main(String args[]){
DoublyLinkedList< Integer> dll=new DoublyLinkedList<Integer>();
dll.insertFront(32);
dll.insertFront(35);
dll.insertFront(45);
dll.insertAtEnd(12);
dll.traverse();
System.out.println();
dll.traverseB();
System.out.println("Size is:"+dll.getSize());
dll.insertAtLoc(46,0);
System.out.println();
dll.traverse();
System.out.println("Removed at pos 3:"+dll.remove(3));
System.out.println();
dll.traverse();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.