学习链表队列的实现方式

huangapple go评论172阅读模式
英文:

Learning Linked List Queue Based Implementation

问题

  1. import java.util.LinkedList;
  2. public class LinkedListQueue {
  3. // Declaring Linked List, Node Head & Tail
  4. LinkedList<String> list;
  5. Node head;
  6. Node tail;
  7. class Node {
  8. int data;
  9. Node prev;
  10. Node next;
  11. /**
  12. * Node Constructor
  13. *
  14. * @param data
  15. */
  16. Node(int d) {
  17. data = d;
  18. }
  19. }
  20. /**
  21. * Linked List Queue Parameterized Constructor
  22. */
  23. public LinkedListQueue() {
  24. list = new LinkedList<String>();
  25. }
  26. /**
  27. * Enqueue Method (adds element to tail)
  28. *
  29. * @param string
  30. */
  31. public void enqueue(String string) {
  32. list.add(string);
  33. }
  34. /**
  35. * Dequeue Method (removes first element)
  36. */
  37. public void dequeue() {
  38. if (list.isEmpty()) { /* check if linked list is empty */
  39. System.out.println("The Queue is Empty.");
  40. } else {
  41. list.removeFirst(); /* remove first element of linked list */
  42. }
  43. }
  44. /**
  45. * Size Method (Size of queue)
  46. *
  47. * @return size
  48. */
  49. public int size() {
  50. return list.size();
  51. }
  52. /**
  53. * Display Method (prints element in queue)
  54. */
  55. public void display() {
  56. for (int i = 0; i < list.size(); i++) {
  57. System.out.print(list.get(i) + " ");
  58. }
  59. System.out.println("\n");
  60. }
  61. /**
  62. * getHead Method (gets head of the queue)
  63. *
  64. * @return first element
  65. */
  66. public String getHead() {
  67. if (list.isEmpty()) {
  68. return "The Queue is Empty.";
  69. } else {
  70. return list.getFirst();
  71. }
  72. }
  73. /**
  74. * getTail Method (gets tail of the queue)
  75. *
  76. * @return last element
  77. */
  78. public String getTail() {
  79. if (list.isEmpty()) {
  80. return "The Queue is Empty.";
  81. } else {
  82. return list.getLast();
  83. }
  84. }
  85. /**
  86. * IsEmpty Method (checks if queue is empty)
  87. *
  88. * @return true/false
  89. */
  90. public boolean isEmpty() {
  91. if (list.isEmpty()) { /* checks if linked list is empty */
  92. return true;
  93. } else {
  94. return false;
  95. }
  96. }
  97. /**
  98. * Delete Queue Method (clears queue)
  99. */
  100. public void deleteQueue() {
  101. list.clear();
  102. }
  103. }
  1. import java.util.LinkedList;
  2. public class Driver {
  3. public static void main(String[] args) {
  4. // New instance of linked list queue
  5. LinkedListQueue llq = new LinkedListQueue();
  6. // Checking if stack is empty
  7. System.out.println("Is the Queue empty? " + llq.isEmpty());
  8. llq.enqueue("Queue Element 1");
  9. llq.enqueue("Queue Element 2");
  10. llq.enqueue("Queue Element 3");
  11. llq.enqueue("Queue Element 4");
  12. llq.enqueue("Queue Element 5");
  13. System.out.println("The element at the head is: " + llq.getHead());
  14. System.out.println("The element at the tail is: " + llq.getTail());
  15. // Checking size of stack
  16. System.out.println("Size of the Queue: " + llq.size());
  17. // Printing Stack
  18. System.out.println("*****Display the Queue*****");
  19. llq.display();
  20. llq.enqueue("Queue Element 6");
  21. llq.dequeue();
  22. llq.dequeue();
  23. // Printing Stack
  24. System.out.println("*****Display the Queue*****");
  25. llq.display();
  26. llq.dequeue();
  27. llq.dequeue();
  28. // Checking if stack is empty
  29. System.out.println("Is the stack empty? " + llq.isEmpty());
  30. // Clearing Stack
  31. llq.deleteQueue();
  32. // Checking if stack is empty
  33. System.out.println("Is the stack empty? " + llq.isEmpty());
  34. }
  35. }
英文:

I'm currently in a Data Structures Class and am running into a bit of trouble with my Linked List Queue based Implementation Project- I have everything built however my new instance of lined list is not telling me to add a cast to the "llq" instance I created. Below is my LinkedList class and my Driver Class.

Please be kind! This particular subject/topic has be a bit lost- I'm open to any and all feedback- thank you!

  1. import java.util.LinkedList;
  2. public class LinkedListQueue {
  3. //Declaring Linked List, Node Head &amp; Tail
  4. LinkedList&lt;String&gt; list;
  5. Node head;
  6. Node tail;
  7. class Node{
  8. int data;
  9. Node prev;
  10. Node next;
  11. /**
  12. * Node Constructor
  13. * @param data
  14. */
  15. Node(int d){
  16. data = d;
  17. }
  18. }
  19. /**
  20. * Linked List Queue Parameterized Constructor
  21. * @param list
  22. */
  23. public LinkedListQueue(){
  24. list = new LinkedList&lt;String&gt;();
  25. }
  26. /**
  27. * Enqueue Method (adds element to tail)
  28. * @param list
  29. * @return add
  30. */
  31. public void enqueue(String string){
  32. list.add(string);
  33. }
  34. /**
  35. * Dequeue Method (removes first element)
  36. * @param list
  37. * @return removeFirst
  38. */
  39. public void dequeue(){
  40. if(list.isEmpty()){ /*check if linked list is empty*/
  41. System.out.println(&quot;The Queue is Empty.&quot;);
  42. }
  43. else {
  44. list.removeFirst(); /*remove first element of linked list*/
  45. }
  46. }
  47. /**
  48. * Size Method (Size of queue)
  49. * @param list
  50. * @return size
  51. */
  52. public int size(){
  53. return list.size();
  54. }
  55. /**
  56. * Display Method (prints element in queue)
  57. * @param list
  58. * @return prints element at the indicated index
  59. */
  60. public void display(){
  61. for(int i=0;i&lt; list.size();i++){
  62. System.out.print(list.get(i)+&quot; &quot;);
  63. }
  64. System.out.println(&quot;\n&quot;);
  65. }
  66. /**
  67. * getHead Method (gets head of the queue)
  68. * @param list
  69. * @return first element
  70. */
  71. public String getHead(){
  72. if(list.isEmpty()){
  73. return &quot;The Queue is Empty.&quot;;
  74. }
  75. else {
  76. return list.getFirst();
  77. }
  78. }
  79. /**
  80. * getTail Method (gets tail of the queue)
  81. * @param list
  82. * @return last element
  83. */
  84. public String getTail(){
  85. if(list.isEmpty()){
  86. return &quot;The Queue is Empty.&quot;;
  87. }
  88. else {
  89. return list.getLast();
  90. }
  91. }
  92. /**
  93. * IsEmpty Method (checks if queue is empty)
  94. * @param list
  95. * @return true/false
  96. */
  97. public boolean isEmpty(){
  98. if(list.isEmpty()){ /*checks if linked list is empty*/
  99. return true;
  100. }
  101. else{
  102. return false;
  103. }
  104. }
  105. /**
  106. * Delete Queue Method (clears queue)
  107. * @param list
  108. * @return clear
  109. */
  110. public void deleteQueue() {
  111. list.clear();
  112. }
  113. }
  114. import java.util.LinkedList;
  115. import java.util.PriorityQueue;
  116. import java.util.Queue;
  117. public class Driver{
  118. public static void main(String[] args){
  119. //New instance of linked list queue
  120. LinkedList llq =new LinkedList();
  121. //Checking if stack is empty
  122. System.out.println(&quot;Is the Queue empty? &quot; + llq.isEmpty());
  123. llq.addFirst(&quot;Queue Element 1&quot;);
  124. llq.enqueue(&quot;Queue Element 2&quot;);
  125. llq.enqueue(&quot;Queue Element 3&quot;);
  126. llq.enqueue(&quot;Queue Element 4&quot;);
  127. llq.enqueue(&quot;Queue Element 5&quot;);
  128. System.out.println(&quot;The element at the head is: &quot; + llq.getHead());
  129. System.out.println(&quot;The element at the tail is: &quot; + llq.getTail());
  130. //Checking size of stack
  131. System.out.println(&quot;Size of the Queue: &quot; + llq.size());
  132. //Printing Stack
  133. System.out.println(&quot;*****Display the Queue*****&quot; );
  134. llq.display();
  135. llq.addLast(&quot;Queue Element 6&quot;);
  136. llq.dequeue(&quot;Queue Element 2&quot;);
  137. llq.dequeue(&quot;Queue Element 3&quot;);
  138. //Printing Stack
  139. System.out.println(&quot;*****Display the Queue*****&quot; );
  140. llq.display();
  141. llq.dequeue(&quot;Queue Element 6&quot;);
  142. llq.dequeue(&quot;Queue Element 7&quot;);
  143. //Checking if stack is empty
  144. System.out.println(&quot;Is the stack empty? &quot; + llq.isEmpty());
  145. //Clearing Stack
  146. llq.deleteQueue();
  147. //Checking if stack is empty
  148. System.out.println(&quot;Is the stack empty? &quot; + llq.isEmpty());
  149. }
  150. }

答案1

得分: 0

应该是

  1. LinkedListQueue llq = new LinkedListQueue();

因为你不想要一个普通的 LinkedList,而是想要自己封装过的包装类。

英文:

Should be

  1. LinkedListQueue llq = new LinkedListQueue();

instead since you do not want an ordinary LinkedList but your own wrapper around it.

huangapple
  • 本文由 发表于 2020年10月10日 03:32:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64286237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定