我是编程和尝试实现一种算法的新手,该算法使用链表和堆栈来反转队列中的K个元素,但由于某种原因,我无法执行该算法,我们将不胜感激。
尽管我向队列中添加了值,但我仍然发现队列将为空,但是我在控制台中看到了添加的值,但是当我检查queue.isEmpty()时,它为true。
这是我的代码
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();
}
}
LinkedList类
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
答案 0 :(得分:0)
我并不完全肯定,但是看起来您的队列就像一个堆栈。您的enQueue是推到队列的前端,而不是队列的尾端。