【问题标题】:Learning Linked List Queue Based Implementation学习基于链表队列的实现
【发布时间】:2021-01-24 21:36:57
【问题描述】:

我目前在一个数据结构类中,并且在我的基于链表队列的实现项目中遇到了一些麻烦 - 我已经构建了所有内容,但是我的新行列表实例并没有告诉我向我创建的“llq”实例。下面是我的 LinkedList 类和我的 Driver 类。

请善待!这个特定的主题/主题有点丢失-我愿意接受任何和所有的反馈-谢谢!

  import java.util.LinkedList;

  public class LinkedListQueue {

//Declaring Linked List, Node Head & Tail
LinkedList<String> list; 
Node head;
Node tail;

class Node{
    int data;
    Node prev;
    Node next;

/**
* Node Constructor
* @param data
*/
Node(int d){
    data = d;
}
}

/**
 * Linked List Queue Parameterized Constructor
 * @param list
 */
  public LinkedListQueue(){
    list = new LinkedList<String>();
}

/**
 * Enqueue Method (adds element to tail)
 * @param list
 * @return add
 */
public void enqueue(String string){
    list.add(string);
}

/**
 * Dequeue Method (removes first element)
 * @param list
 * @return removeFirst
 */   
public void dequeue(){
    if(list.isEmpty()){   /*check if linked list is empty*/
        System.out.println("The Queue is Empty.");
    }
    else {
        list.removeFirst();    /*remove first element of linked list*/
    }
}

/**
 * Size Method (Size of queue)
 * @param list
 * @return size
 */ 
public int size(){
    return list.size();    
}

/**
 * Display Method (prints element in queue)
 * @param list
 * @return prints element at the indicated index
 */ 
public void display(){
    for(int i=0;i< list.size();i++){     
        System.out.print(list.get(i)+"  ");   
    }
    System.out.println("\n");
}

/**
 * getHead Method (gets head of the queue)
 * @param list
 * @return first element
 */ 
public String getHead(){

    if(list.isEmpty()){
        return "The Queue is Empty.";
    }
    else {
        return list.getFirst();
    }
}

/**
 * getTail Method (gets tail of the queue)
 * @param list
 * @return last element
 */ 
public String getTail(){

    if(list.isEmpty()){
        return "The Queue is Empty.";
    }
    else {
        return list.getLast();
     }
}

/**
 * IsEmpty Method (checks if queue is empty)
 * @param list
 * @return true/false
 */
public boolean isEmpty(){
    if(list.isEmpty()){     /*checks if linked list is empty*/
        return true;
    }
    else{
        return false;
    }
}

/**
 * Delete Queue Method (clears queue)
 * @param list
 * @return clear
 */ 
public void deleteQueue() {
    list.clear();
}
}



import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;


 public class Driver{

   public static void main(String[] args){

       //New instance of linked list queue
       LinkedList llq =new LinkedList();

       //Checking if stack is empty
       System.out.println("Is the Queue empty? " + llq.isEmpty());
       
       llq.addFirst("Queue Element 1");  
       
       llq.enqueue("Queue Element 2");
       llq.enqueue("Queue Element 3");
       llq.enqueue("Queue Element 4");
       llq.enqueue("Queue Element 5");

       System.out.println("The element at the head is: " + llq.getHead());
       
       System.out.println("The element at the tail is: " + llq.getTail());
        
       //Checking size of stack
       System.out.println("Size of the Queue: " + llq.size());
        
       //Printing Stack
       System.out.println("*****Display the Queue*****" );
       llq.display();
            
       llq.addLast("Queue Element 6");  

       llq.dequeue("Queue Element 2");
       llq.dequeue("Queue Element 3");

       //Printing Stack
       System.out.println("*****Display the Queue*****" );
       llq.display();
           
       llq.dequeue("Queue Element 6");  
       llq.dequeue("Queue Element 7");  


           
       //Checking if stack is empty
       System.out.println("Is the stack empty? " + llq.isEmpty());
           
       //Clearing Stack
       llq.deleteQueue();
           
       //Checking if stack is empty
       System.out.println("Is the stack empty? " + llq.isEmpty());
   }
 }

【问题讨论】:

  • 在luk2302给出的答案之上,你的程序还有很多问题。示例 - 错误使用 @param(您错误地使用了不带任何参数的方法),然后使用了 Node(您从未将其用于任何功能), isEmpty 方法中的冗余逻辑。有很多问题!!!

标签: java data-structures linked-list queue implementation


【解决方案1】:

应该是

LinkedListQueue llq = new LinkedListQueue();

相反,因为您不想要普通的LinkedList,而是您自己的包装器。

【讨论】:

    猜你喜欢
    • 2012-04-30
    • 2011-03-10
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    相关资源
    最近更新 更多