Merge two Queues in Java -
i have been working on code merge , print these 2 queues, no avail. if point me in right direction or let me know doing wrong, appreciated.
node class
public class node<e> { private e element; private node next; public node(e element, node<e> next) { // here this.element = element; this.next = next; } public e getelement() { return element; } public void setnextnode(node<e> next) { // here this.next = next; } public node<e> getnextnode() { // replace return null useful return next; } }
linkedlist class
public class linkedlistqueue<e> implements queue<e> { private node<e> head; private node<e> tail; private int size; public linkedlistqueue() { } public void enqueue(e element) { node newnode = new node(element, null); if (size == 0) { head = newnode; } else { tail.setnextnode(newnode); } tail = newnode; size++; } public e dequeue() { if (head != null) { e element = head.getelement(); head = head.getnextnode(); size--; if (size == 0) { tail = null; } return element; } return null; } public e first() { if (head != null) { return head.getelement(); } return null; } public int getsize() { return size; } public void printlist() { if (head != null) { node currentnode = head; { system.out.println(currentnode.tostring()); currentnode = currentnode.getnextnode(); } while (currentnode != null); } system.out.println(); } }
queue class
public interface queue<e> { public void enqueue(e e); public e dequeue(); public e first(); public int getsize(); public void printlist(); }
main class
public static void main(string[] args) { linkedlistqueue q1 = new linkedlistqueue(); q1.enqueue(1); q1.enqueue(2); q1.enqueue(3); q1.enqueue(4); q1.enqueue(5); q1.printlist(); linkedlistqueue q2 = new linkedlistqueue(); q2.enqueue(6); q2.enqueue(7); q2.enqueue(8); q2.enqueue(9); q2.printlist(); } public static linkedlistqueue merge(linkedlistqueue q1, linkedlistqueue q2){ linkedlistqueue q3 = new linkedlistqueue(); linkedlistqueue merged = linkedlistqueue.merge(q1,q2); } }
here have put in linkedlist class
public static linkedlistqueue merge(linkedlistqueue q1, linkedlistqueue q2) { if (q1 == null) { return (q2); } else if (q2 == null) { return (q1); } linkedlistqueue merge = new linkedlistqueue(); merge.enqueue(q1); merge.enqueue(q2); return merge; }
and here have put in main. prints 2 queues prints out node location , not prints out when call other queues.
linkedlistqueue q3 = new linkedlistqueue(); q3 = linkedlistqueue.merge(q1,q2); q3.printlist();
your problem in snippet of code:
public static linkedlistqueue merge(linkedlistqueue q1, linkedlistqueue q2){ linkedlistqueue q3 = new linkedlistqueue(); linkedlistqueue merged = linkedlistqueue.merge(q1,q2); //problematic line }
specifically part linkedlistqueue.merge(q1,q2);
. syntax saying call static method merge
defined in class linkedlistqueue
. however, looking in code linkedlistqueue
, don't see static
merge
method defined.
the way code written, expecting like:
public class linkedlistqueue<e> implements queue<e>{ public static linkedlistqueue<e> merge(linkedlistqueue<e> q1, linkedlistqueue<e> q2){ //definition here } //more class code }
see here more static methods.
Comments
Post a Comment