Question

// Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

// Java Queue LinkedList Assignment


// A queue is implemented using a singly linked list.
// the variable: back "points" at the first node in the linked list
// new elements ( enqueued) are added at the back
// the variable: front "points" at the last node in the linked list.
// elements are removed (dequeued) from the front
//
// Several queue instance methods are provided for you; do not change these
// Other instance methods are left for you as "ToDo" tasks
//
// You may not use any other Java classes or algorithms in completing the ToDo items
// You may not add any instance variables to the class
// You may add your own private instance methods for modularity sake
//
public class Practice {

   private Node front, back;

   static class Node {
       public Node (char item, Node next) { this.item = item; this.next = next; }
       public char item;
       public Node next;
   }

//This is the problem to solve.

   // toString
   // the function toString returns a single String consisting of all the characters in the queue.
   // the leftmost character in the returned string should be the character at the FRONT of the queue
   // Example; if the linked list representation is: back--> 'a' 'b' 'c' 'd' <-- front
   // the returned string would be "dcba"
   // a normal traversal of the linked structure from back to front would give us the string we want REVERSED
   //
   // You may not use any other Java classes ( e.g. ArrayList, ) to solve this.
   // You may use basic java arrays and methods of the String class
   //
   // Hint1: you can build a String by starting with the empty string and the concatenating characters as shown below
   // String x ="";
   // x = x + 'q'; // adds 'q' onto the right end of String x
   // Hint2: recursion is one way to handle the reversal issue

   public String toString() {
       return ""; // TODO 1 fix this

   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

public String toString() {
// set str to "" we will add other values later
String str = "";
// create a temp node
Node temp = front;
// traverse till temp becomes null
while (temp != null) {
// add item to str
str = temp.item + str;
// move temp forward
temp = temp.next;
}
  
// retuirn str
return str;
}

screenshot:

public String toString({ // set str to we will add other values Later String str = ; // create a temp node Node temp = f

Add a comment
Know the answer?
Add Answer to:
// Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java please, These are instance methods which modify the linked list ,* accessed through the...

    In Java please, These are instance methods which modify the linked list ,* accessed through the instance variable: first * Note that this list keeps track of the number of elements in the instance varible N * It is important that N accurately reflect the length of the list. * You may not add any fields to the node or list classes. * You may not add any methods to the node class. static class Node ; } public Node...

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

    I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...

  • To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. t...

    To solve real world problem, the programer uses ADT like list, stack, queue, set, map, ... etc. to build our data model. In AS8, we pick and choose STL queue and stack to build a postfix calculator. In this assignment you are to Design your own linked-list, a series of integer nodes. The private attributes of this IntLinked Queue including a integer node struct and private control variables and methods: private: struct Node { int data; Node *next; }; Node...

  • Using Java coding, complete the following: This program should implement a LinkedList data structure to handle...

    Using Java coding, complete the following: This program should implement a LinkedList data structure to handle the management of student records. It will need to be able to store any number of students, using a Linked List. LinearNode.java can be used to aid creating this program Student should be a class defined with the following:    • String name    • int year    • A linked list of college classes (college classes are represented using strings)    • An...

  • Study the "Queue as linked list simple example" posted in this module. Rewrite the code to...

    Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

  • JAVA Program: reverse a linked list and find the middle node in the linked list. inFile...

    JAVA Program: reverse a linked list and find the middle node in the linked list. inFile (use argv[1]): A text file contains a list of English words (strings), giving below outFile1 (use argv[2])a text file includes       i) The completed sorted linked list, in ascending order. //With caption indicating you are printing the original sorted list       ii) The reversed linked list. //With caption indicating you are printing the reversed sorted list       outFile2( use argv[3]): All debugging outputs.                        ...

  • Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

    Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT