队列反向实现 - 代码不起作用



我是编程新手,并尝试实现一种算法,该算法使用 linkedlist 和堆栈在队列中反转 K 元素,但由于某种原因,我无法执行该算法,非常感谢任何帮助。

我发现队列是空的,即使我向它添加值,我在控制台中看到添加的值,但是当我检查 queue.isEmpty(( 时,它是真的。

这是我的代码

public class QueueReverseK {
public void reverse(QueueADT queue, int k) {
if (queue.isEmpty()) {
System.out.println("in if empty");
return;
}
if (k > queue.size()) {
k = queue.size;
} else if (k < 0) {
k = 0;
}
StackADT tempStack = new StackADT(k);
QueueADT newQueue = new QueueADT();
for (int i = 0; i < k; i++) {
tempStack.push(queue.deQueue().getData());
}
while (!tempStack.isEmpty()) {
newQueue.enQueue(tempStack.pop());
}
while (!queue.isEmpty()) {
newQueue.enQueue(queue.deQueue().getData());
}
queue = newQueue;
}

队列类

public class Queue {    
LinkedList items;
int size;
Node head;
Node tail;
LinkedListADT list = new LinkedListADT();
public Queue() {
items = new LinkedList();
size = 0;
head = null;
tail = null;
}
public int size() {
return size;
}
public boolean isEmpty() {
if (head == null) return true;
else return false;
}
public void enQueue(int i) {
items.addHead(i);
}
public Node deQueue() {
return items.deleteHead();
}

public void printQueue() {
items.printList();
}

}

链接列表类

public class LinkedList {
Node head;
Node tail;
LinkedList() {
head = null;
tail = null;
}
public void addHead(int val) {
Node n = new Node(val);
if (head == null) {
head = n;
tail = n;
} else {
Node tempNode = head;
while (tempNode.next != null) {
tempNode = tempNode.next;
}
tempNode.next = n;
}
}
public void addTail(int val) {
Node n = new Node(val);
if (head == null) {
head = n;
tail = n;
} else {
tail.next = n;
tail = n;
}
}
public int deleteTail() {
Node n = tail;
if (head == null) {
return -1;
} else if (head == tail) {
head = null;
tail = null;
} else {
Node cur = head;
while (cur.getNext() != tail)
cur = cur.next;
tail = cur;
tail.next = null;
}
return n.getData();
}
public Node deleteHead() {
Node n = head;
head = head.next;
return n;
}
public int count() {
int size = 0;
Node n = head;
while (n != null) {
n = n.getNext();
size++;
}
return size;
}
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
public void printList() {
if (this.head == null) {
return;
}
// print all nodes
Node tempNode = this.head;
while (tempNode != null) {
System.out.print(tempNode.data + "->");
tempNode = tempNode.next;
}
}
void printMiddle(int n) {
Node slow_ptr = head;
Node fast_ptr = head;
if (head != null) {
while (fast_ptr != null && fast_ptr.next != null) {
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
System.out.print(slow_ptr.data + " ");
for (int i = 1; i <= n && slow_ptr != null; i++) {
slow_ptr = slow_ptr.next;
System.out.print(slow_ptr.data + " ");
}
}
}
}

主类

public static void main(String[] args) {
Queue Q = new Queue();
Q.enQueue(1);
Q.enQueue(2);
Q.enQueue(3);
Q.enQueue(4);
QueueReverseK k = new QueueReverseK();
k.reverse(Q, 2)
Q.printQueue();
}

堆栈类

public class Stack {
private int top;
private int items[];
private int max;
public StackADT(int n) {
this.max = n;
top = 0;
items = new int[n];
}
public boolean isEmpty() {
return top == 0;
}
public boolean isFull() {
return top == max;
}
public void push(int item) {
if (isFull()) throw new IllegalArgumentException();
else items[top++] = item;
}
public int pop() {
if (isEmpty()) throw new IllegalArgumentException();
else return items[--top];
}
public int size() {
return top;
}
}
}

输入队列:12 34 65 76 23 12 36 90 输出队列 : 12 34 65 76 90 36 12 23

我并不完全肯定,但看起来您的队列就像一个堆栈。你的enQueue正在推动队列的头部,而不是队列的尾部。

相关内容

  • 没有找到相关文章

最新更新