如何使用静态方法将队列复制到其他队列.希尔特



在这个练习中需要: 创建一个静态方法,该方法将一种类型的队列的内容复制到另一种类型的队列中。真的,我不是不知道如何制作它。请帮我:(

我尝试像这样制作新类,实际上他什么也没显示,我理解 becouse ,但我做到了,看看返回方法 get(( 是什么,他们返回 --Queue 是空的,好吧,我怎么只是 thouth,但我怎么能用 char 返回数组? 或者别的什么,哦,我现在不:(

public class TestQueue { 
public static void CQueue(ICharQ a) {
a.get();
System.out.println("  " + a);
}
}

界面:

public interface ICharQ{        
void put (char ch);  //putting symb to queue
char get(); 
void reset ();
default char reset1(){return '-';}
}

队列代码:

class FixedQueue implements ICharQ {
private char q[]; // this array holds the queue
private int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public FixedQueue(int size) {
q = new char[size]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a characer into the queue.
public void put(char ch) {
if(putloc==q.length) {
System.out.println(" -- Queue is full.");
return;
}
q[putloc++] = ch;
}
// Get a character from the queue.
public char get() {
if(getloc == putloc) {
System.out.println(" -- Queue is empty.");
return (char) 0;
}
return q[getloc++];
}

}

循环队列。

class CircularQueue implements ICharQ {
private char q[]; // this array holds the queue
private int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public CircularQueue(int size) {
q = new char[size+1]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a characer into the queue.
public void put(char ch) {
/* Queue is full if either putloc is one less than
getloc, or if putloc is at the end of the array
and getloc is at the beginning. */
if(putloc+1==getloc |
((putloc==q.length-1) & (getloc==0))) {
System.out.println(" -- Queue is full.");
return;
}
q[putloc++] = ch;
if(putloc==q.length) putloc = 0; // loop back
}
// Get a character from the queue.
public char get() {
if(getloc == putloc) {
System.out.println(" -- Queue is empty.");
return (char) 0;
}
char ch = q[getloc++];
if(getloc==q.length) getloc = 0; // loop back
return ch;
}
public void reset(int res) {
putloc=getloc=0;
q = new char[res];
System.out.println(" ---Queue have not items= ZERO"+q);
}
}

动态队列。

class DynQueue implements ICharQ {
private char q[]; // this array holds the queue
private int putloc, getloc; // the put and get indices
// Construct an empty queue given its size.
public DynQueue(int size) {
q = new char[size]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a characer into the queue.
public void put(char ch) {
if(putloc==q.length) {
// increase queue size
char t[] = new char[q.length * 2];
// copy elements into new queue
for(int i=0; i < q.length; i++)
t[i] = q[i];
q = t;
}
q[putloc++] = ch;
}
// Get a character from the queue.
public char get() {
if(getloc == putloc) {
System.out.println(" -- Queue is empty.");
return (char) 0;
}
return q[getloc++];
}
public void reset(int res) {
putloc=getloc=0;
q = new char[res];
System.out.println(" ---Queue have not items= ZERO"+q[10]);
}

}

// Demonstrate the ICharQ interface.
class IQDemo {
public static void main(String args[]) {
FixedQueue q1 = new FixedQueue(10);
DynQueue q2 = new DynQueue(10);
CircularQueue q3 = new CircularQueue(10);
ICharQ iQ;
char ch;
int i;
iQ = q1;
// Put some characters into fixed queue.
for(i=0; i < 10; i++)
iQ.put((char) ('A' + i));
// Show the queue.
System.out.print("Contents of fixed queue: ");
for(i=0; i < 10; i++) {
ch = iQ.get();
System.out.print(ch);
}

System.out.println();
iQ = q2;
// Put some characters into dynamic queue.
for(i=0; i < 10; i++)
iQ.put((char) ('Z' - i));
// Show the queue.
System.out.print("Contents of dynamic queue: ");
for(i=0; i < 10; i++) {
ch = iQ.get();
System.out.print(ch);
}
System.out.println();
iQ = q3;
// Put some characters into circular queue.
for(i=0; i < 10; i++)
iQ.put((char) ('A' + i));
// Show the queue.
System.out.print("Contents of circular queue: ");
for(i=0; i < 10; i++) {
ch = iQ.get();
System.out.print(ch);
}
System.out.println();
// Put more characters into circular queue.
for(i=10; i < 20; i++)
iQ.put((char) ('A' + i));
// Show the queue.
System.out.print("Contents of circular queue: ");
for(i=0; i < 10; i++) {
ch = iQ.get();
System.out.print(ch);
}
System.out.println("nStore and consume from" +
" circular queue.");
// Use and consume from circular queue.
for(i=0; i < 20; i++) {
iQ.put((char) ('A' + i));
ch = iQ.get();
System.out.print(ch);

} 
}  
}

考虑复制队列时期望的输入和输出。创建副本需要什么?首先,您需要原始队列,然后需要返回原始队列的副本。

public static ICharQ /*copy*/ CQueue(ICharQ original)

现在可以使用您的接口方法

put (char ch);
char get();

您可以使用已知方法创建一个循环,该循环从原始队列get()中获取一个字符,直到该字符为空,并使用 put(char ch)将每个字符插入到副本中

现在使用上面提到的所有内容创建一个算法来循环,直到没有字符可以复制并返回创建的副本。

public static ICharQ CQueue(ICharQ original)
{
ICharQ copy = new FixedQueue(); //change this to use an if/else using instance of, won't work currently because it only returns a FixedQueue
Character copyChar;
while((copyChar = original.get()) != '0')
copy.put(copyChar);
return copy;
}

队列接口只提供几种方法,并且您知道的原则很简单(FIFO - 先进先出(和方法(添加,提供,轮询,窥视(。所以你只能放在最后,只能从头上得到。

使用 Queue 接口,如果不从源队列中删除元素,则无法将一个队列的元素复制到另一个队列。

如果你正在实现自己的队列,它还应该实现可迭代接口,以便它可以提供从元素末端到头部的迭代器。这样,您可以获取迭代器,并将每个元素添加到目标队列中。

似乎通过为 q、putloc 和 getloc 添加 get 和 set 方法并在每个类中分别实现它们,然后在 ICharQ 接口中实现 static copy(( 方法来解决这个问题。 这是我添加到ICharQ的一个片段:

void setQ(char[] q);
char[] getQ();
int getPutloc();
int getGetloc();
void setPutloc(int p);
void setGetloc(int g);
static ICharQ copy(ICharQ a, ICharQ b) {
b.setQ(a.getQ());
b.setPutloc(a.getPutloc());
b.setGetloc(a.getGetloc());
return b;
}

这是类中的实现:

public void setQ(char[] q) {
char[] a = new char[q.length];
System.arraycopy(q, 0, a, 0, q.length);
this.q = a;
}
public char[] getQ() {
return q;
}
public int getPutloc() {
return putloc;
}
public int getGetloc() {
return getloc;
}
public void setPutloc(int p) {
putloc = p;
}
public void setGetloc(int g) {
getloc = g;
}
从 main 调用时看起来如下:

ICharQ.copy(q1, q2);

但是,我希望转换为 CircularQueue 时可能会出现问题,因为它需要在数组大小中添加 +1 - 不幸的是,还没有解决这个小问题。 P.S.不要严格判断,差不多一个月前刚开始学习,但觉得这个任务很有意思,很想在这里多讨论一下。

相关内容

最新更新