学习链表队列的实现方式

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

Learning Linked List Queue Based Implementation

问题

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
     */
    public LinkedListQueue() {
        list = new LinkedList<String>();
    }

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

    /**
     * Dequeue Method (removes first element)
     */
    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)
     *
     * @return size
     */
    public int size() {
        return list.size();
    }

    /**
     * Display Method (prints element in queue)
     */
    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)
     *
     * @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)
     *
     * @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)
     *
     * @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)
     */
    public void deleteQueue() {
        list.clear();
    }
}
import java.util.LinkedList;

public class Driver {

    public static void main(String[] args) {

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

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

        llq.enqueue("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.enqueue("Queue Element 6");

        llq.dequeue();
        llq.dequeue();

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

        llq.dequeue();
        llq.dequeue();

        // 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());
    }
}
英文:

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!

  import java.util.LinkedList;
public class LinkedListQueue {
//Declaring Linked List, Node Head &amp; Tail
LinkedList&lt;String&gt; 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&lt;String&gt;();
}
/**
* 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(&quot;The Queue is Empty.&quot;);
}
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&lt; list.size();i++){     
System.out.print(list.get(i)+&quot;  &quot;);   
}
System.out.println(&quot;\n&quot;);
}
/**
* getHead Method (gets head of the queue)
* @param list
* @return first element
*/ 
public String getHead(){
if(list.isEmpty()){
return &quot;The Queue is Empty.&quot;;
}
else {
return list.getFirst();
}
}
/**
* getTail Method (gets tail of the queue)
* @param list
* @return last element
*/ 
public String getTail(){
if(list.isEmpty()){
return &quot;The Queue is Empty.&quot;;
}
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(&quot;Is the Queue empty? &quot; + llq.isEmpty());
llq.addFirst(&quot;Queue Element 1&quot;);  
llq.enqueue(&quot;Queue Element 2&quot;);
llq.enqueue(&quot;Queue Element 3&quot;);
llq.enqueue(&quot;Queue Element 4&quot;);
llq.enqueue(&quot;Queue Element 5&quot;);
System.out.println(&quot;The element at the head is: &quot; + llq.getHead());
System.out.println(&quot;The element at the tail is: &quot; + llq.getTail());
//Checking size of stack
System.out.println(&quot;Size of the Queue: &quot; + llq.size());
//Printing Stack
System.out.println(&quot;*****Display the Queue*****&quot; );
llq.display();
llq.addLast(&quot;Queue Element 6&quot;);  
llq.dequeue(&quot;Queue Element 2&quot;);
llq.dequeue(&quot;Queue Element 3&quot;);
//Printing Stack
System.out.println(&quot;*****Display the Queue*****&quot; );
llq.display();
llq.dequeue(&quot;Queue Element 6&quot;);  
llq.dequeue(&quot;Queue Element 7&quot;);  
//Checking if stack is empty
System.out.println(&quot;Is the stack empty? &quot; + llq.isEmpty());
//Clearing Stack
llq.deleteQueue();
//Checking if stack is empty
System.out.println(&quot;Is the stack empty? &quot; + llq.isEmpty());
}
}

答案1

得分: 0

应该是

LinkedListQueue llq = new LinkedListQueue();

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

英文:

Should be

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:

确定