レベル 4: 単一リンクリストの実現のクエリ関数
注: 3 番目のパスは、コードを変更せずに 4 番目のパスをコピーしました (行 93 に戻り値を追加します: return Index;)。
500
- ミッション要件
- 参考回答
- コメント 62
ミッションの詳細
前のレベルでは、要素の追加、要素の削除など、単一リンク リストの基本機能を理解しました。連鎖テーブルはランダム アクセスを実現できないため、指定された位置の要素を取得するには、ヘッド ノードからのみトラバースできます。このレベルの課題: 前のレベルをベースに、単結合リストの機能を引き続き改良し、指定された位置の要素を取得する機能を実現します。
関連情報
前のレベルを参照してください
プログラミング要件
このレベルのプログラミング タスクは、右側のコード スニペットの中央Begin
から中央までのEnd
コードを完成させることです。具体的な要件は次のとおりです。
index
指定された位置にある要素を取得して返す、補完get(int index)
メソッドです。
package step4;
/**
* Created by zengpeng on 2017/12/25.
*/
public class MyLinkedList {
private Node first;//头结点,不存数据
private Node last;//指向链表的最后一个节点
private int size;
public MyLinkedList() {
size = 0;
first = new Node(0, null);
last = null;
}
/**
* 添加到链表尾部
*
* @param item
*/
public void add(int item) {
final Node l = last;
final Node node = new Node(item, null);
last = node;
if (first.next == null) {//首次添加
first.next = node;
} else {
l.next = node;
}
++size;
}
/**
* 添加数据item到指定位置index
* index从0开始
* @param index
* @param item
*/
public void add(int index, int item) {
checkPosIndex(index);
int n = index;
Node l = first;
while ((n--) > 0) {
l = l.next;
}
final Node node = new Node(item, null);
if (null == first.next) {//首次添加
last = node;
}
node.next = l.next;
l.next = node;
++size;
}
/**
* 删除指定位置index处的元素并返回, index从0开始
* @param index
* @return
*/
public int remove(int index) {
checkPosIndex(index);
Node f = first;
while ((index--) > 0) {
f = f.next;
}
Node del = f.next;
if (del == last) {//删除最后一个元素
last = f;
}
f.next = del.next;
del.next = null;
int oldVal = del.item;
del = null;
--size;
return oldVal;
}
/**
* 获取链表中第index个元素
* @param index
* @return
*/
public int get(int index) {
checkPosIndex(index);
/********** Begin *********/
Node f = first.next;
while ((index--) > 0) {
f = f.next;
}
int out = f.item;
return out;
/********** End *********/
}
public int size() {
return size;
}
private void checkPosIndex(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
//结点内部类
private static class Node {
int item;
Node next;
Node(int item, Node next) {
this.item = item;
this.next = next;
}
}
}
------------------------------------------------------------------YU星河