Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing a Linked List Stack and Queue, and I don't know if the code I have set up is efficient enough
    text
    copied!<p>Been working on an implementation for Linked list Queue and Stack classes for an assignment. I've tested both of these and they appear to be working, but I'm worried that some parts of the implementation can be done better than I currently have set up, and I don't wanna get points taken off for inefficient code.</p> <p>Here's the classes I have set up:</p> <p>Node</p> <pre><code>public class Node { Node next; Car car; /** * A node object, used for the creation of LinkedLists. * @param car */ public Node(Car car) { next = null; this.car = car; } } </code></pre> <p>Stack</p> <pre><code>public class LStack { Node head = null; /** * Adds a car object to the list * @param car = the car object to be added */ public void push(Car car) { Node oldHead = head; head = new Node(car); head.next = oldHead; } /** * Removes the top car from the list * @return the car at the top of the list */ public Car pop() { Car headCar = head.car; head = head.next; return headCar; } /** * Checks if the list is empty * @return whether or not the list is empty */ public boolean isEmpty() { return (head == null); } /** * * @return the size of the list */ public int size() { Node nextNode = head; int count = 0; while (nextNode != null) { count++; nextNode = nextNode.next; } return count; } /** * Displays the list of car license plate information */ public void display() { Node nextNode = head; while (nextNode != null) { System.out.print(nextNode.car.getLicense() + "|"); nextNode = nextNode.next; } System.out.println(); } /** * */ public void reverseStack() { // not implemented yet } } </code></pre> <p>Queue</p> <pre><code>public class LQueue { Node head = null; /** * Adds a car object to the list * * @param car * = the car object to be added */ public void insert(Car car) { Node current = head; if (current != null) { while (current.next != null) { current = current.next; } current.next = new Node(car); } else { head = new Node(car); } } /** * Removes the top car from the list * * @return the car at the top of the list */ public Car remove() { Car headCar = head.car; head = head.next; return headCar; } /** * Checks if the list is empty * * @return whether or not the list is empty */ public boolean isEmpty() { return (head == null); } /** * * @return the size of the list */ public int size() { Node nextNode = head; int count = 0; while (nextNode != null) { count++; nextNode = nextNode.next; } return count; } /** * Displays the list of car license plate information */ public void display() { Node nextNode = head; while (nextNode != null) { System.out.print(nextNode.car.getLicense() + "|"); nextNode = nextNode.next; } System.out.println(); } /** * */ public void reverseQueue() { } } </code></pre> <p>and the Car class isn't really important, it just stores license plate information in a string.</p> <p>Mostly I'm worried about the ones I have set up with the While loops, I'm not sure if there's a more memory efficient way of implementing those. Is there a more standardized way of setting these up that I may have missed?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload