public void enqueue(Object element)
// Adds element to the rear of this queue.
{
LLObjectNode newNode = new LLObjectNode(element);
if (rear == null)
front = newNode;
else
rear.setLink(newNode);
rear = newNode;
}
public Object dequeue()
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
else
{
Object element;
element = front.getInfo();
front = front.getLink();
if (front == null)
rear = null;
return element;
}
}
public boolean isEmpty()
// Returns true if this queue is empty; otherwise, returns false.
{
if (front == null)
return true;
else
return false;
}
1
A
答えて
0
さてあなたは、少なくともequeue
で次の手順を実行する必要があります。
newNode.setLink(front);
実は、私はfront
はいつもrear.getLink()
througアクセスすることができますので、あなたがfront
とrear
の両方が必要とは思いません。ここで
は提案です:
public class CircularLinkedList {
LLObjectNode rear;
// Adds element to the rear of this queue.
public void enqueue(Object element) {
LLObjectNode newNode = new LLObjectNode(element);
if (!isEmpty())
rear.setLink(newNode);
LLObjectNode front = front();
rear = newNode;
// Set new nodes successor to front
newNode.setLink(front);
}
private LLObjectNode front() {
return rear.getLink();
}
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
public Object dequeue() {
if (isEmpty())
throw new QueueUnderflowException(
"Dequeue attempted on empty queue.");
Object element = front().getInfo();
// Exclude front from list
if (onlyOneLeft())
rear = null;
else
rear.setLink(front().getLink());
return element;
}
private boolean onlyOneLeft() {
return front() == rear;
}
public boolean isEmpty() {
// Returns true if this queue is empty; otherwise, returns false.
return rear == null;
}
}
2
public class CircLinkedUnbndQueue<T> implements UnboundedQueueInterface<T>
{
protected LLNode<T> rear; // reference to the rear of this queue
public CircLinkedUnbndQueue()
{
rear = null;
}
public void enqueue(T element)
// Adds element to the rear of this queue.
{
LLNode<T> newNode = new LLNode<T>(element);
if (rear == null)
{
rear = newNode;
}
else
{
//links the newNode to the rear node's pointer and then 're'points the
//rear node to the newNode.
if(rear.getLink() == null)
{
rear.setLink(newNode);
newNode.setLink(rear);
}
else
{
newNode.setLink(rear.getLink());
rear.setLink(newNode);
}
}
//'repositions' the reat node at the end of the queue.
rear = newNode;
}
public T dequeue()
// Throws QueueUnderflowException if this queue is empty;
// otherwise, removes front element from this queue and returns it.
{
if (isEmpty())
throw new QueueUnderflowException("Dequeue attempted on empty queue.");
else
{
T element;
rear = rear.getLink();
element = rear.getInfo();
if (rear.getLink() == null)
rear = null;
return element;
}
}
public boolean isEmpty()
// Returns true if this queue is empty; otherwise, returns false.
{
if (rear == null)
return true;
else
return false;
}
}
私はこれは古い記事です知っているが、それだけで使用しているので、私は最近、この問題でのトラブルがあったし、これが尋ねた質問に沿って、より多くのことを信じていますリアノードを使用します。
関連する問題
- 1. VBのリストボックスのみを選択して循環させるにはどうすればいいですか?
- 2. C++で循環キューを使用してキューを実装するにはどうすればよいですか?
- 3. 循環参照を使用してオブジェクトを保存するにはどうすればよいですか?
- 4. この循環インクルードを解決するにはどうすればよいですか?
- 5. MySQLで循環外部キーを使用してテーブルを削除する方法
- 6. sqliteを使用して、Cで外部キーをアクティブにするにはどうすればよいですか?
- 7. この循環依存関係を取り除くにはどうすればよいですか?
- 8. エンティティの循環グラフを保存するInvalidOperationExceptionを防止するにはどうすればよいですか?
- 9. 外部ライブラリを使用せずにファイルを圧縮するにはどうすればよいですか?
- 10. netbeans 6.7を使用して外部キーを定義するにはどうすればよいですか?
- 11. PHPを使用して外部フォームを投稿するにはどうすればよいですか?
- 12. [ORACLE]インラインビューを使用して外部キー値を取得するにはどうすればよいですか?
- 13. Backbone.jsの循環参照でスタックオーバーフローを回避するにはどうすればよいですか?
- 14. スウィフトで循環サークルのif文を作成するにはどうすればよいですか?
- 15. WebpackとAngular2を使用して外部CSSファイルを含めるにはどうすればよいですか?
- 16. QMLリストビューを循環させることは可能ですか?
- 17. GWTを外部コンポーネントエンジンとして使用するにはどうすればよいですか?
- 18. VS Lightswitchの循環依存性を解決するにはどうすればよいですか?
- 19. 外部のサードパーティライブラリを使用するiPhoneアプリをリリースするにはどうすればよいですか?
- 20. SQLTABLEの結果を外部ページにリンクさせるにはどうすればよいですか?
- 21. フォームを使用してポイントをpictureBox1の枠線の外に移動させないようにするにはどうすればよいですか?
- 22. Javaガベージコレクタは、アクセスパスが壊れていると循環参照をどのように扱うのですか?
- 23. コアアニメーションを使用してNSTextFieldの背景色をアニメーション化するにはどうすればよいですか?
- 24. jqueryを使用してCSSの背景イメージを変更するにはどうすればよいですか?
- 25. ASP.NETと外部データソースでjqueryのUIオートコンプリートを使用するにはどうすればよいですか?
- 26. URLの書き換えで外部情報を使用するにはどうすればよいですか?
- 27. Railsアプリケーションの外部でRailsセッションストアを使用するにはどうすればよいですか?
- 28. didUpdateLocation関数の外部で座標を使用するにはどうすればよいですか?
- 29. 複数のプロジェクトに外部log4j.propertiesを使用するにはどうすればよいですか?
- 30. VerticalFieldManagerのボタンを使用して親画面を変更するにはどうすればよいですか?
あなたの質問がありますか? –
数日前に同じ質問がありました。宿題? – sje397
質問はあなたがこのページに行くためにクリックしたものです。 – Tony