recursive toString in Queue Linked List in Java -


i need implement tostring() recursive method linked list queue. know tostring method worked fine on linked list implementation did last week, wrong how i'm handling queue aspect of it.

tostring method queuelist:

public string tostring()  {       if (front.info == null)     {         system.out.println("error, queue empty");         return "";     }     if (front.link  == null) //base case: if last element in stack     {          return  (" \"" + front.info + "\" , ");     }      else //normal recursive function     {         return  (" \"" + front.info + "\" , " + front.link.tostring());      }     } 

and constructors , such queuelist:

public class queuenode  {     e info;     queuenode link; }  private queuenode front;//first element placed queue private queuenode rear;//last element placed queue private int noe;//counter number of elements in queue public queuelist()  {      front = null;     rear = null;     noe = 0; } 

i tried see going on in using test:

public boolean test() {      queuelist<string> q = new queuelist<string>();      q.enqueue("the godfather");     q.enqueue("casino");     q.enqueue("goodfellas");     string r = q.tostring();     q.prettyprint(); 

with output

in -> [ "the godfather" , queuelist$queuenode@a3901c6] -> out.  

i realize because i'm telling saying front.link.tostring() in recursive part of tostring method, if change front.link.info.tostring(), output

in -> [ "the godfather" , casino] -> out.  

it may possibly with enqueue , dequeue methods then, follows:

public void enqueue(e element)  {           queuenode newnode = new queuenode();//creates new node hold element         newnode.info = element;//set info of new node element         newnode.link = null;//make link null since it's @ of list         if (rear == null)//checks if queue empty         {             front = newnode;         }         else         {             rear.link = newnode;//sets second last node's link newnode         }         rear = newnode;//makes newnode new last link         noe++;//increase counter  } public e dequeue() throws invalidoperationexception  {     if (front == null)//sanitize code     {         throw new invalidoperationexception("there nothing in queue.");     }     e element = front.info;//creates element file takes info in front of queue     front = front.link;//makes second-to-front element new front     if (front == null)//if emptied queue, make sure rear empty     {         rear = null;     }     noe--;//reduce counter     return element; } 

please me out if can. thanks.

there absolutely no need make tostring recursive, , in fact incorrect so. data structure not recursive (i.e. tree) linear.

if list contained, say, 1 million items, run out of stack space (stackoverflow, literally).

use loop instead.

edit: if required recursively, issue recursive method must queuenode#tostringrecursive(), not queue#tostring(). method queue#tostring() allocates buffer , provides special tostringrecursive() method on queuenode recursion. queuenode#tostring() must responsible own node contents.

method queue#tostring()

public string tostring() {     stringbuilder buf = new stringbuilder();     if (front == null)         // queue empty     else         front.tostringrecursive(buf);     return buf.tostring(); } 

method queuenode#tostringrecursive()

public void tostringrecursive(stringbuilder buf) {     buf.append(this.tostring());     if (this.link != null)         this.tostringrecursive(buf); } 

where queuenode.tostring() responsible stringifying 1 node (itself).

note one way it. possible write recursive method on queue well, not called tostring(). queue#tostring() set initial conditions , invoke recursion.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -