Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. public class Node { //Node class structure
  2.  
  3. int data; //data contained in Node; for assignment purposes, data is an int
  4. Node next; //pointer to Next Node
  5.  
  6. //Node Constructor
  7. public Node(int data) {
  8. this.data = data;
  9. next = null;
  10. }
  11.  
  12. //Set Methods
  13. public void setData(int data) { //set Node value
  14. this.data = data;
  15. }
  16. public void setNext(Node next) { //set next Node value
  17. this.next = next;
  18. }
  19.  
  20. //Get Methods
  21. public int getData() { //get Node value
  22. return this.data;
  23. }
  24. public Node getNext() { //get next Node value
  25. return this.next;
  26. }
  27.  
  28. //Display the Node Value
  29. public void displayNode() {
  30. System.out.println(data + "urgh"); //display value as a string
  31. }
  32. }
  33.  
  34. import Question1.Node;
  35.  
  36. //basic set-up of a FIFO singly linked list
  37. public class SLList{
  38.  
  39. protected Node head; //head of SLList
  40. protected Node tail; //tail of SLList
  41. int n; //number of elements in SLList
  42.  
  43. //SLList constructor
  44. public SLList() {
  45. head = null;
  46. n = 0;
  47. }
  48.  
  49. //check if list is empty
  50. public boolean isEmpty() {
  51. return head == null;
  52. }
  53.  
  54. //return the size of the list
  55. public int size() {
  56. return n;
  57. }
  58.  
  59. //add a new node to the end of the list
  60. public boolean insert(int x){
  61. Node y = new Node(x);
  62.  
  63. if (head == null){ //if head is null, thus an empty list
  64. head = y; //assign head as y
  65. }
  66. else{ //if there is already a tail node
  67. tail.next = y; //assign the tail's pointer to the new node
  68. }
  69. tail = y; //assign tail to y
  70. this.n++; //increment the queue's size
  71. return true; //show action has taken place
  72. }
  73.  
  74. //remove and return node from head of list
  75. public Node remove(){
  76. if (n == 0){ //if the list is of size 0, and thus empty
  77. return null; //do nothing
  78. }
  79. else{ //if there are node(s) in the list
  80. Node pointer = head; //assign pointer to the head
  81. head = head.next; //reassign head as next node,
  82. n--; //decrement list size
  83. return pointer; //return the pointer
  84. }
  85. }
  86.  
  87. //display SLList as string
  88. public void displayList() {
  89. Node pointer = head;
  90.  
  91. while (pointer != null) {
  92. pointer.displayNode();
  93. pointer = pointer.next;
  94. }
  95. System.out.println(" ");
  96. }
  97. }
  98.  
  99. import Question1.Node;
  100. import Question1.SLList;
  101.  
  102. public class PriorityQueue extends SLList {
  103.  
  104. private SLList list; //SLList variable
  105.  
  106. public PriorityQueue(){ //create the official SLList
  107. list = new SLList();
  108. }
  109.  
  110. //add a new node; new add method that ensures the first element is sorted to be the "priority"
  111. public boolean add(int x){
  112. Node y = new Node(x);
  113.  
  114. if (n == 0){ //if there are 0 elements, thus an empty list
  115. head = y; //assign head as y
  116. }
  117. else if (y.data < head.data){ //if new node y is the smallest element, thus highest priority
  118. y.next = head; //assign y's next to be current head of queue
  119. head = y; //reassign head to be actual new head of queue (y)
  120. }
  121. else{ //if there is already a tail node
  122. tail.next = y; //assign the tail's pointer to the new node
  123. }
  124. tail = y; //assign tail to y
  125. n++; //increment the queue's size
  126. return true; //show action has taken place
  127. }
  128.  
  129. //delete the minimim value (highest priority value) from the queue and return its value
  130. public Node deleteMin(){
  131. return list.remove(); //the list is sorted such that the element being removed in indeed the min
  132. }
  133.  
  134. //return the size of the queue
  135. public int size() {
  136. return n;
  137. }
  138.  
  139. //display Queue as string
  140. public void displayQueue() {
  141. System.out.println("->");
  142. list.displayList();
  143. }
  144. }
  145.  
  146. import Question1.PriorityQueue;
  147.  
  148. public class TestQ1 { //Test code
  149.  
  150. public static void main(String[] args){
  151. PriorityQueue PQueue1 = new PriorityQueue();
  152.  
  153. PQueue1.add(3);
  154. PQueue1.add(2);
  155. PQueue1.add(8);
  156. PQueue1.add(4);
  157.  
  158. System.out.println("Test add(x): ");
  159. PQueue1.displayQueue();
  160. System.out.println("Test size(): " + PQueue1.size());
  161.  
  162. PriorityQueue PQueue2 = new PriorityQueue();
  163. //Node node1 = PQueue1.deleteMin();
  164.  
  165. System.out.println("Test deleteMin():");
  166. PQueue2.displayQueue();
  167. System.out.println("Test size(): " + PQueue2.size());
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement