本系列博客習題來自《算法(第四版)》设哗,算是本人的讀書筆記柳弄,如果有人在讀這本書的舶胀,歡迎大家多多交流概说。為了方便討論,本人新建了一個微信群(算法交流)嚣伐,想要加入的糖赔,請?zhí)砑游业奈⑿盘枺簔hujinhui207407 謝謝。另外轩端,本人的個人博客 http://www.kyson.cn 也在不停的更新中放典,歡迎一起討論
知識點
- 鏈表節(jié)點增刪查改
題目
1.3.24 編寫一個方法removeAfter(),接受一個鏈表結點作為參數(shù)并刪除該結點的后續(xù)結點(如果參數(shù)結點或參數(shù)結點的后續(xù)結點為空則什么也不做)基茵。
1.3.24 Write a method removeAfter() that takes a linked-list Node as argument and removes the node following the given one (and does nothing if the argument or the next field in the argument node is null).
答案
public class LinkedListExecise4<Item> {
private static class Node<Item> {
Node next;
Item item;
@Override
public String toString() {
// TODO Auto-generated method stub
return "item:"+item;
}
}
public Node<Item> removeAfter(Node node, Node first) {
if (first == null) {
return null;
}
Node current = first;
while (current != null) {
if (current.item.equals(node.item))
{
if (current.next != null)
{
current.next = current.next.next;
return first;
} else {
return first;
}
}
current = current.next;
}
return null;
}
/**
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* 創(chuàng)建鏈表
* */
Node<String> first = new Node<String>();
Node<String> second = new Node<String>();
Node<String> third = new Node<String>();
Node<String> forth = new Node<String>();
Node<String> fifth = new Node<String>();
first.item = "我的";
first.next = second;
second.item = "名字";
second.next = third;
third.item = "叫";
third.next = forth;
forth.item = "頂級程序員不穿女裝";
forth.next = fifth;
fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
fifth.next = null;
LinkedListExecise4<String> linkedListExercise4 = new LinkedListExecise4<String>();
Node targetNode = first;
System.out.println("即將移除節(jié)點:"+targetNode+"之后的節(jié)點");
Node resultNode = linkedListExercise4.removeAfter(targetNode, first);
System.out.println("新鏈表:\n-------");
Node current2 = resultNode;
while (current2.next != null) {
System.out.println(current2.item);
current2 = current2.next;
}
System.out.println(current2.item);
System.out.println("-------");
}
}
代碼索引
題目
1.3.25 編寫一個方法insertAfter()奋构,接受兩個鏈表結點作為參數(shù),將第二結點插入鏈表并使之成為第一個結點的后續(xù)結點(如果兩個參數(shù)為空則什么也不做)拱层。
1.3.25 1.3.25 Write a method insertAfter() that takes two linked-list Node arguments and inserts the second after the first on its list (and does nothing if either argument is null).
答案
public class LinkedListExecise5<Item> {
private static class Node<Item> {
Node next;
Item item;
@Override
public String toString() {
// TODO Auto-generated method stub
return "item:" + item;
}
}
public Node<Item> insertAfter(Node<Item> targetNode, Node<Item> node,
Node<Item> first) {
if (targetNode == null || node == null) {
return first;
}
Node<Item> current = first;
while (current != null) {
if (current.equals(targetNode)) {
Node<Item> t = current.next;
current.next = node;
node.next = t;
return first;
}
current = current.next;
}
return null;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* 創(chuàng)建鏈表
* */
Node<String> first = new Node<String>();
Node<String> second = new Node<String>();
Node<String> third = new Node<String>();
Node<String> forth = new Node<String>();
Node<String> fifth = new Node<String>();
first.item = "我的";
first.next = second;
second.item = "名字";
second.next = third;
third.item = "叫";
third.next = forth;
forth.item = "頂級程序員不穿女裝";
forth.next = fifth;
fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
fifth.next = null;
LinkedListExecise5<String> linkedListExercise5 = new LinkedListExecise5<String>();
Node targetNode = second;
System.out.println("即將移除節(jié)點:" + targetNode + "之后的節(jié)點");
Node<String> insertedNode = new Node<String>();
insertedNode.item = "天天開心笑哈哈";
Node resultNode = linkedListExercise5.insertAfter(targetNode,
insertedNode, first);
System.out.println("新鏈表:\n-------");
Node current2 = resultNode;
while (current2.next != null) {
System.out.println(current2.item);
current2 = current2.next;
}
System.out.println(current2.item);
System.out.println("-------");
}
}
代碼索引
題目
1.3.26 編寫一個方法remove()弥臼,接受一個鏈表和一個字符串key作為參數(shù),刪除鏈表中所有item域為key的結點根灯。
1.3.26 Write a method remove() that takes a linkedlist and a string key as arguments and removes all of the nodes in the list that have key as its item field.
答案
public class LinkedListExecise6 {
private static class Node {
Node next;
String item;
@Override
public String toString() {
// TODO Auto-generated method stub
return "item:" + item;
}
}
public Node remove(Node first, String key) {
Node newFirst = new Node();
newFirst.next = first;
Node current = newFirst;
while (current.next != null) {
if (current.next.item.equals(key)) {
current.next = current.next.next;
}else {
current = current.next;
}
}
return newFirst.next;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* 創(chuàng)建鏈表
* */
Node first = new Node();
Node second = new Node();
Node third = new Node();
Node forth = new Node();
Node fifth = new Node();
first.item = "我的";
first.next = second;
second.item = "名字";
second.next = third;
third.item = "叫";
third.next = forth;
forth.item = "頂級程序員不穿女裝";
forth.next = fifth;
fifth.item = "微博:https://m.weibo.cn/p/1005056186766482";
fifth.next = null;
LinkedListExecise6 linkedListExercise6 = new LinkedListExecise6();
Node resultNode = linkedListExercise6.remove(first, "我的");
System.out.println("新鏈表:\n-------");
Node current2 = resultNode;
while (current2.next != null) {
System.out.println(current2.item);
current2 = current2.next;
}
System.out.println(current2.item);
System.out.println("-------");
}
}
代碼索引
題目
1.3.27 編寫一個方法max()径缅,接受一個鏈表的首結點作為參數(shù),返回鏈表中鍵最大的節(jié)點的值烙肺。假設所有鍵均為正整數(shù)纳猪,如果鏈表為空則返回0。
1.3.27 Write a method max() that takes a reference to the first node in a linkedlist as argument and returns the value of the maximum key in the list. Assume that all keys are positive integers, and return 0 if the list is empty.
答案
public class LinkedListExecise7 {
private static class Node {
Node next;
Integer item;
@Override
public String toString() {
// TODO Auto-generated method stub
return "item:" + item;
}
}
public Integer max(Node first) {
if (null == first) {
return 0;
}
if (first.next == null) {
return first.item;
}
Node current = first;
Integer max = current.item;
while (current != null) {
if (current.item > max) {
max = current.item;
}
current = current.next;
}
return max;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* 創(chuàng)建鏈表
* */
Node first = new Node();
Node second = new Node();
Node third = new Node();
Node forth = new Node();
Node fifth = new Node();
first.item = 1;
second.item = 3;
third.item = 999;
forth.item = 33;
fifth.item = 21;
first.next = second;
second.next = third;
third.next = forth;
forth.next = fifth;
fifth.next = null;
System.out.println("原鏈表:\n-------");
Node current1 = first;
while (current1.next != null) {
System.out.println(current1.item);
current1 = current1.next;
}
System.out.println(current1.item);
System.out.println("-------");
System.out.println("正在求最大值...");
LinkedListExecise7 linkedListExercise7 = new LinkedListExecise7();
Integer result = linkedListExercise7.max(first);
System.out.println("result:" + result);
}
}
代碼索引
廣告
我的首款個人開發(fā)的APP壁紙寶貝上線了桃笙,歡迎大家下載氏堤。