Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//class to represent one node in a list class ListNode { //package access members; List can access these directly T data; ListNode nextNode; //constructor creates

//class to represent one node in a list class ListNode { //package access members; List can access these directly T data;

ListNode nextNode;

//constructor creates a ListNode that refers to object

ListNode( T object ) { this( object, null ); } // end ListNode one-argument constructor

//constructor creates ListNode that refers to //Object and to next ListNode

ListNode( T object, ListNode node ) { data = object; nextNode = node; } // end ListNode two-argument constructor

//return reference to data in node T getObject() { return data; // return Object in this node } // end method getObject //return reference to next node in list ListNode getNext() { return nextNode; // get next node } // end method getNext } // end class ListNode

//class List definition

public class List { private static final String Integer = null;

private ListNode firstNode; private ListNode lastNode; private String name; // string like "list" used in printing

//constructor creates empty List with "list" as the name public List() { this( "list" ); } // end List no-argument constructor

//constructor creates an empty List with a name

public List( String listName ) { name = listName; firstNode = lastNode = null; } // end List one-argument constructor

//insert Object at front of List public void insertAtFront( T insertItem ) { if ( isEmpty() ) // firstNode and lastNode refer to same object

firstNode = lastNode = new ListNode( insertItem ); else // firstNode refers to new node firstNode = new ListNode( insertItem, firstNode ); } // end method insertAtFront

//insert Object at end of List public void insertAtBack( T insertItem ) { if ( isEmpty() ) // firstNode and lastNode refer to same Object

firstNode = lastNode = new ListNode( insertItem ); else // lastNode's nextNode refers to new node

lastNode = lastNode.nextNode = new ListNode( insertItem ); } // end method insertAtBack

//remove first node from List

public T removeFromFront() throws RuntimeException {

if ( isEmpty() ) // throw exception if List is empty throw new RuntimeException( name+" is Empty !!" );

T removedItem = firstNode.data;

// retrieve data being removed

//update references firstNode and lastNode if ( firstNode == lastNode ) firstNode = lastNode = null; else firstNode = firstNode.nextNode; return removedItem; // return removed node data } // end method removeFromFront

//remove last node from List

public T removeFromBack() throws RuntimeException { if ( isEmpty() ) // throw exception if List is empty throw new RuntimeException( name+" is Empty !!" ); T removedItem = lastNode.data; // retrieve data being removed

// update references firstNode and lastNode if ( firstNode == lastNode ) firstNode = lastNode = null; else // locate new last node { ListNode current = firstNode; //loop while current node does not refer to lastNode

while ( current.nextNode != lastNode ) current = current.nextNode; lastNode = current; // current is new lastNode

current.nextNode = null; } // end else

return removedItem;

// return removed node data } // end method removeFromBack

//determine whether list is empty public boolean isEmpty() { return firstNode == null; // return true if List is empty } // end method isEmpty

//output List contents public void print() {

if ( isEmpty() ) { System.out.printf( "Empty %s ", name ); return; }

System.out.printf( "The %s is: ", name );

ListNode current = firstNode; //while not at end of list, output current node's data

while ( current != null ) { System.out.printf( "%s ", current.data ); current = current.nextNode; } // end while System.out.println( " " ); } // end method print } ===========

//class to represent Queue public class Queue { List queueList; // constructor Queue() { this("My Queue"); } // constructor Queue(String name) { queueList=new List(name); } public void enqueue( T item ) { queueList.insertAtBack( item ); } public T dequeue() throws RuntimeException { return queueList.removeFromFront(); } public boolean isEmpty() { return queueList.isEmpty(); }

public void print() { queueList.print(); } }

=========== //class to represent Stack

public class Stack { List stackList; // constructor Stack() { this("My Stack"); } // constructor Stack(String name) { stackList=new List(name); }

public void push( T item ) { stackList.insertAtFront( item ); }

public T pop() throws RuntimeException { return stackList.removeFromFront(); }

public boolean isEmpty() { return stackList.isEmpty(); }

public void print() { stackList.print(); } }

=============

the question says :

image text in transcribed

(in java program language) pls I need it fast pls help

Using only the given Queue (both in linked list representation by reusing ListTs class through composition), implement the following: a. Read four String from user and store them in either Queue or Stack. After that, appends the same content of your data structure at the end but in reverse order. Read five Integers from the user and store those in a Queue. After that, reverse the content of your Queue. b. Read five Integers from user and store them in either Queue or Stack. After that, check the contents of your data structure and move the appearances of the smallest element to the bottom/end of that data structure. c. Note: You can't use any API classes or data structures other than Stacks and Queue. You are not allowed to add/change any code in class Queue

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

More Books

Students also viewed these Databases questions