Java 实现链表堆栈和队列,而我不';我不知道我设置的代码是否足够有效
一直在为分配的链表队列和堆栈类进行实现。我已经测试了这两个,它们似乎都能工作,但我担心实现的某些部分会比我目前设置的做得更好,我不想因为效率低下的代码而被扣分 以下是我开设的课程: 节点 堆叠 排队 汽车类别并不重要,它只是以字符串形式存储车牌信息Java 实现链表堆栈和队列,而我不';我不知道我设置的代码是否足够有效,java,stack,queue,linked-list,Java,Stack,Queue,Linked List,一直在为分配的链表队列和堆栈类进行实现。我已经测试了这两个,它们似乎都能工作,但我担心实现的某些部分会比我目前设置的做得更好,我不想因为效率低下的代码而被扣分 以下是我开设的课程: 节点 堆叠 排队 汽车类别并不重要,它只是以字符串形式存储车牌信息 我最担心的是我用While循环设置的那些,我不确定是否有一种更有效的内存实现方法。有没有一种更标准化的设置方法我可能会错过?我要改变的一件事是,我会让LQueue同时引用队列的头和尾。这样,您就可以在固定时间内insert(),而不必迭代所有现有元素
我最担心的是我用While循环设置的那些,我不确定是否有一种更有效的内存实现方法。有没有一种更标准化的设置方法我可能会错过?我要改变的一件事是,我会让
LQueue
同时引用队列的头和尾。这样,您就可以在固定时间内insert()
,而不必迭代所有现有元素
此外,如果您愿意,还可以使两个
size()
方法以恒定时间运行:跟踪成员变量中的当前大小,在insert()
上递增,在remove()上递减
我要改变的一件事是,我要使LQueue
同时引用队列的头和尾。这样,您就可以在固定时间内insert()
,而不必迭代所有现有元素
另外,如果您愿意,两种
size()
方法都可以在固定时间内运行:跟踪成员变量中的当前大小,在insert()
上递增,在remove()
上递减。我能想到的只有两件事:
我能想到的只有两件事:
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;
}
}
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
}
}
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() {
}
}