第1關(guān):?jiǎn)窝h(huán)鏈表的實(shí)現(xiàn)—鏈表的添加、遍歷
200
- 任務(wù)要求
- 參考答案
- 評(píng)論42
- 任務(wù)描述
-
相關(guān)知識(shí)
-
單循環(huán)鏈表
- 添加操作
- 遍歷循環(huán)鏈表
-
單循環(huán)鏈表
- 編程要求
- 測(cè)試說(shuō)明
任務(wù)描述
在操作單鏈表時(shí),我們有時(shí)希望從單鏈表中的任一結(jié)點(diǎn)出發(fā)都能遍歷整個(gè)鏈表,但對(duì)于單鏈表來(lái)說(shuō),只有從頭結(jié)點(diǎn)開(kāi)始才能掃描表中的全部結(jié)點(diǎn)。因此我們需要改動(dòng)鏈表,使其首尾相接,這樣就能滿足我們的需求。
本關(guān)任務(wù):完成帶頭結(jié)點(diǎn)的單循環(huán)鏈表的添加功能,遍歷鏈表并輸出。
相關(guān)知識(shí)
單循環(huán)鏈表
循環(huán)鏈表是一種首尾相接的鏈表。其特點(diǎn)是無(wú)需增加存儲(chǔ)量,只需對(duì)表的鏈接方式稍作改變,即可使得表操作更加方便靈活。
在單鏈表中,將末尾結(jié)點(diǎn)的指針域null
改為指向表頭結(jié)點(diǎn)或開(kāi)始結(jié)點(diǎn),就得到單鏈形式的循環(huán)鏈表,稱為單循環(huán)鏈表。
為使空表和非空表的處理方式一致,循環(huán)鏈表中也可以設(shè)置一個(gè)頭結(jié)點(diǎn)。這樣空循環(huán)鏈表僅有一個(gè)自成循環(huán)的頭結(jié)點(diǎn)。 如下圖:
添加操作
單循環(huán)鏈表的添加操作與普通的單鏈表操作類似,只需添加時(shí)注意處理尾結(jié)點(diǎn),使其指向頭結(jié)點(diǎn)。下面是添加操作示意圖。
這里采用的是尾插法,即在鏈表的末尾添加結(jié)點(diǎn)。我們使tail
指向鏈表尾結(jié)點(diǎn),因此添加結(jié)點(diǎn)node
時(shí)只需改動(dòng)tail
和新結(jié)點(diǎn)node
之間的鏈接關(guān)系即可。由于有頭結(jié)點(diǎn),所以空表和非空表的處理方式是一樣的。
node.next=tail.next;
tail.next=node;
tail=node;
遍歷循環(huán)鏈表
在循環(huán)鏈表中,鏈表的尾結(jié)點(diǎn)不是指向null
。因此要輸出鏈表元素時(shí),其終止條件就不再是像非循環(huán)鏈表那樣判斷p==null
或p.next==null
,而是判別它們是否等于某一指定結(jié)點(diǎn),如頭結(jié)點(diǎn)head
。如下圖所示:
編程要求
本關(guān)的編程任務(wù)是補(bǔ)全右側(cè)代碼片段中Begin
至End
中間的代碼,具體要求如下:
- 完成單循環(huán)鏈表的添加功能;
- 遍歷單循環(huán)鏈表,并輸出元素的值。
具體請(qǐng)參見(jiàn)后續(xù)測(cè)試樣例
本關(guān)涉及的代碼文件MyCircleLinkedList.java
的代碼框架如下:
?
package step1;
/**
* Created by sykus on 2018/1/15.
*/
public class MyCircleLinkedList {
private Node head;//頭結(jié)點(diǎn), 不存數(shù)據(jù)
private Node tail;//尾結(jié)點(diǎn), 指向鏈表的最后一個(gè)節(jié)點(diǎn)
private int size;
public MyCircleLinkedList() {
head = new Node(Integer.MIN_VALUE, null);
head.next = head;
tail = head;
size = 0;
}
/**
* 添加到鏈表尾部
*
* @param item
*/
public void add(int item) {
/********** Begin *********/
Node node = new Node(item, tail.next);
tail.next = node;
tail = node;
size++;
/********** End *********/
}
/**
* 遍歷鏈表并輸出元素
*/
public void output() {
/********** Begin *********/
Node p = head;
while (p.next != head) {
p = p.next;
System.out.println(p.item);
}
/********** End *********/
}
public boolean isEmpty() {
return head.next == head;
}
public int size() {
return size;
}
//結(jié)點(diǎn)內(nèi)部類
private static class Node {
int item;
Node next;
Node(int item, Node next) {
this.item = item;
this.next = next;
}
}
}
//智科01 421045
第二關(guān)
package step2;
/**
* Created by sykus on 2018/1/15.
*/
public class MyCircleLinkedList {
private Node head;//頭結(jié)點(diǎn), 不存數(shù)據(jù)
private Node tail;//尾結(jié)點(diǎn), 指向鏈表的最后一個(gè)節(jié)點(diǎn)
private int size;
public MyCircleLinkedList() {
head = new Node(Integer.MIN_VALUE, null);
head.next = head;
tail = head;
size = 0;
}
/**
* 添加到鏈表尾部
*
* @param item
*/
public void add(int item) {
Node node = new Node(item, tail.next);
tail.next = node;
tail = node;
++size;
}
/**
* 遍歷鏈表并輸出元素
*/
public void output() {
Node p = head;
while (p.next != head) {
p = p.next;
System.out.println(p.item);
}
}
/**
* 刪除從頭結(jié)點(diǎn)開(kāi)始的第index個(gè)結(jié)點(diǎn)
* index從0開(kāi)始
*
* @param index
* @return
*/
public int remove(int index) {
checkPosIndex(index);
/********** Begin *********/
Node f = head;
while ((index--) > 0) {
f = f.next;
}
Node del = f.next;
if (del == tail) {
tail = f;
}
f.next = del.next;
del.next = null;
int oldVal = del.item;
del = null;
--size;
return oldVal;
/********** End *********/
}
public boolean isEmpty() {
return head.next == head;
}
public int size() {
return size;
}
private void checkPosIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
//結(jié)點(diǎn)內(nèi)部類
private static class Node {
int item;
Node next;
Node(int item, Node next) {
this.item = item;
this.next = next;
}
}
}
第3關(guān):雙向循環(huán)鏈表的實(shí)現(xiàn)—鏈表的插入文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-736401.html
package step3;
/**
* Created by sykus on 2018/1/15.
*/
public class MyDoubleLinkedList {
private Node head;//頭結(jié)點(diǎn)
private Node tail;//指向鏈表的尾結(jié)點(diǎn)
private int size;
public MyDoubleLinkedList() {
head = new Node(null, Integer.MIN_VALUE, null);
head.next = head.prev = head;
tail = head;
size = 0;
}
/**
* 添加元素到表尾
*
* @param item
*/
public void add(int item) {
/********** Begin *********/
Node newNode = new Node(null, item, null);
tail.next = newNode;
newNode.prev = tail;
newNode.next = head;
head.prev = newNode;
tail = newNode;
++size;
/********** End *********/
}
/**
* 打印雙向鏈表
*
* @param flag true從左向右順序打印, false從右向左順序打印
*/
public void printList(boolean flag) {
Node f = head;
if (flag) {//向右
while (f.next != head) {
f = f.next;
System.out.print(f.item + " ");
}
} else {//向左
while (f.prev != head) {
f = f.prev;
System.out.print(f.item + " ");
}
}
}
public int size() {
return size;
}
//結(jié)點(diǎn)內(nèi)部類
private static class Node {
int item;
Node next;//直接后繼引用
Node prev;//直接前驅(qū)引用
Node(Node prev, int item, Node next) {
this.prev = prev;
this.item = item;
this.next = next;
}
}
}
第4關(guān):雙向循環(huán)鏈表的實(shí)現(xiàn)—鏈表的刪除文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-736401.html
package step4;
/**
* Created by sykus on 2018/1/15.
*/
public class MyDoubleLinkedList {
private Node head;//頭結(jié)點(diǎn)
private Node tail;//指向鏈表的尾結(jié)點(diǎn)
private int size;
public MyDoubleLinkedList() {
head = new Node(null, Integer.MIN_VALUE, null);
head.next = head.prev = head;
tail = head;
size = 0;
}
/**
* 添加元素到表尾
*
* @param item
*/
public void add(int item) {
Node newNode = new Node(null, item, null);
tail.next = newNode;
newNode.prev = tail;
newNode.next = head;
head.prev = newNode;
tail = newNode;
++size;
}
/**
* 刪除指定位置index出的結(jié)點(diǎn),并返回其值
*
* @param index
* @return
*/
public int remove(int index) {
checkPosIndex(index);//
/********** Begin *********/
Node p = head.next;
while ((index--) > 0) {
p = p.next;
}
if (p == tail) {
tail = p.prev;
}
p.prev.next = p.next;
p.next.prev = p.prev;
int val = p.item;
p = null;
--size;
return val;
/********** End *********/
}
/**
* 打印雙向鏈表
*
* @param flag true從左向右順序打印, false從右向左順序打印
*/
public void printList(boolean flag) {
Node f = head;
if (flag) {//向右
while (f.next != head) {
f = f.next;
System.out.print(f.item + " ");
}
} else {//向左
while (f.prev != head) {
f = f.prev;
System.out.print(f.item + " ");
}
}
}
public int size() {
return size;
}
private void checkPosIndex(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
}
}
//結(jié)點(diǎn)內(nèi)部類
private static class Node {
int item;
Node next;//直接后繼引用
Node prev;//直接前驅(qū)引用
Node(Node prev, int item, Node next) {
this.prev = prev;
this.item = item;
this.next = next;
}
}
}
到了這里,關(guān)于第1關(guān):?jiǎn)窝h(huán)鏈表的實(shí)現(xiàn)—鏈表的添加、遍歷任務(wù)描述相關(guān)知識(shí)單循環(huán)鏈表添加操作遍歷循環(huán)鏈表編程要求測(cè)試說(shuō)明任務(wù)描述在操作單鏈表時(shí),的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!