java - Copy Elements From Stack to Queue -


i'm supposed copy elements stack queue.

i haven't been able think of way keep stack way , still copy elements queue.

i ended method removes elements stack , adds them queue:

public void copyfromstack(){ e t; int c = w.size(); while(c != 0){ t = w.pop(); enqueue(t); c--;  }} 

trying push elements not option because it'll backwards.

edit: class contains method queue class:

public class queue<e> { protected int size; protected node<e> head; protected node<e> tail; nodestack<e> w = new nodestack<e>(); nodestack<e> w2 = new nodestack<e>();  public queue(){     size = 0;     head = tail = null;}  public boolean isempty(){     return size==0;}  public void enqueue(e elem) {  node<e> node = new node<e>();  node.setelement(elem);  node.setnext(null);  if (size == 0) head = node;  else tail.setnext(node); tail = node;  size++;  }  public e dequeue() { if (size == 0) system.out.print("queue empty.");  e tmp = head.getelement();  head = head.getnext();  size--; if (size == 0) tail = null;  return tmp; }   public string tostring(){     string s = "";     e t;     int c = size;     while(c != 0){         t = dequeue();         s += t + " ";         enqueue(t);         c--; }     return s;}  public int finditem(e elem){     int index=0;     int c = size;     e t; while(c != 0){ t = dequeue(); if (t == elem) return index; else index++; c--;} system.out.print("not found!"); return -1;}  public void copytostack(){ system.out.print("elements copied stack are: "); e t; int c = size; while(c != 0){ t = dequeue();         w.push(t); enqueue(t); c--; system.out.print(w.pop()+" "); }}  public void copyfromstack(){ e t; int c = w.size(); while(c != 0){ t = w.pop(); enqueue(t); c--;  }} 

q: haven't been able think of way keep stack way a:

a: that's because reading "classic" stack destructive. "reading" element == removing element stack.

two solutions:

1) modify stack implementation can "peek" each element

... or ...

2) create new stack containing elements first one.

q: ended method ... trying push elements not option because it'll backwards.

"a: variation on "option 2): above.

solution: create new stack object, , push each element @ same time enqueue element.

ps:

the standard jre implementation of stack includes peek() , search() methods. don't think here. if wanted "option 1)", you'd have implement own, custom stack.

================== update ==================

note, too:

  1. you should indent methods, , indent "if" , "loop" blocks within methods.

  2. you should use "camel-case" (lower-case first letter) method names.

here "official" java coding conventions. useful in 1995; they're useful today:

http://www.oracle.com/technetwork/java/index-135089.html

there's third option: java's "stack" happens implement "iterator". here's example:

example code:

package com.testcopy;  import java.util.arraydeque; import java.util.iterator; import java.util.queue; import java.util.stack;  public class testcopy {      public static void main (string[] args) {          testcopy app = new testcopy ();         app.run ();     }      public void run () {         // create , populate stack         stack<string> mystack = new stack<string> ();         mkdata(mystack);          // copy queue         queue<string> myqueue = new arraydeque<string> ();         copyfromstack (mystack, myqueue);          // print          int i=0;         (string s : myqueue) {             system.out.println ("myqueue[" + i++ + "]: " + s);         }     }      @suppresswarnings("unchecked")     public void mkdata (stack stack) {         stack.push("a");         stack.push("b");         stack.push("c");         // stack should contain c, b,     }      public void copyfromstack (stack stack, queue queue) {         @suppresswarnings("rawtypes")         iterator = stack.iterator ();         while (it.hasnext()) {             queue.add(it.next());         }     } } 

example output:

myqueue[0]: myqueue[1]: b myqueue[2]: c 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -