testing - Java Priority Queue in Linked List Test Cases -
so i'm trying implement priority queue linked list. think have basics together, reason test cases aren't working. when run it, size show fine, none of node values showing (only arrow "->" pops once). if me figure out why isn't working, or suggest better way set test cases in java (i've never done before) appreciated!
node class:
public class node { //node class structure int data; //data contained in node; assignment purposes, data int node next; //pointer next node //node constructor public node(int data) { this.data = data; next = null; } //set methods public void setdata(int data) { //set node value this.data = data; } public void setnext(node next) { //set next node value this.next = next; } //get methods public int getdata() { //get node value return this.data; } public node getnext() { //get next node value return this.next; } //display node value public void displaynode() { system.out.println(data + "urgh"); //display value string } } linked list class:
import question1.node; //basic set-up of fifo singly linked list public class sllist{ protected node head; //head of sllist protected node tail; //tail of sllist int n; //number of elements in sllist //sllist constructor public sllist() { head = null; n = 0; } //check if list empty public boolean isempty() { return head == null; } //return size of list public int size() { return n; } //add new node end of list public boolean insert(int x){ node y = new node(x); if (head == null){ //if head null, empty list head = y; //assign head y } else{ //if there tail node tail.next = y; //assign tail's pointer new node } tail = y; //assign tail y this.n++; //increment queue's size return true; //show action has taken place } //remove , return node head of list public node remove(){ if (n == 0){ //if list of size 0, , empty return null; //do nothing } else{ //if there node(s) in list node pointer = head; //assign pointer head head = head.next; //reassign head next node, n--; //decrement list size return pointer; //return pointer } } //display sllist string public void displaylist() { node pointer = head; while (pointer != null) { pointer.displaynode(); pointer = pointer.next; } system.out.println(" "); } } priority queue class:
import question1.node; import question1.sllist; public class priorityqueue extends sllist { private sllist list; //sllist variable public priorityqueue(){ //create official sllist list = new sllist(); } //add new node; new add method ensures first element sorted "priority" public boolean add(int x){ node y = new node(x); if (n == 0){ //if there 0 elements, empty list head = y; //assign head y } else if (y.data < head.data){ //if new node y smallest element, highest priority y.next = head; //assign y's next current head of queue head = y; //reassign head actual new head of queue (y) } else{ //if there tail node tail.next = y; //assign tail's pointer new node } tail = y; //assign tail y n++; //increment queue's size return true; //show action has taken place } //delete minimim value (highest priority value) queue , return value public node deletemin(){ return list.remove(); //the list sorted such element being removed in indeed min } //return size of queue public int size() { return n; } //display queue string public void displayqueue() { system.out.println("->"); list.displaylist(); } } test cases (so far, delete 1 wasn't working it's commented out):
import question1.priorityqueue; public class testq1 { //test code public static void main(string[] args){ priorityqueue pqueue1 = new priorityqueue(); pqueue1.add(3); pqueue1.add(2); pqueue1.add(8); pqueue1.add(4); system.out.println("test add(x): "); pqueue1.displayqueue(); system.out.println("test size(): " + pqueue1.size()); priorityqueue pqueue2 = new priorityqueue(); //node node1 = pqueue1.deletemin(); system.out.println("test deletemin():"); pqueue2.displayqueue(); system.out.println("test size(): " + pqueue2.size()); } }
change list.displaylist() displaylist(), , you'll see expected output.
why? because queue is list (that is, instance of sllist). when class a extends class b, instance of a instance of b. inheritance.
you've included instance variable private sllist list within priorityqueue implementation, example of composition. you'll 1 or other of these 2 options, depending on situation. in case seems you're trying use inheritance, there's no reason create separate list instance variable. you're adding data directly queue (using fact that, intrinsically, list in own right).
you should remove list instance variable, , usages of should refer parent class' methods or variables.
Comments
Post a Comment