算法(Algorithms)第4版 练习 链表类 1.3.19~1.3.29
package com.qiusongde.linkedlist; import java.util.Iterator;
import java.util.NoSuchElementException; public class LinkedList<Item> implements Iterable<Item> { private Node<Item> first; //Node should be public static in this class
//When it comes to LinkedList, Node should be accessed outside
public static class Node<E> {
public E item;
public Node<E> next;
}
/**
* initialize LinkedList
*/
public LinkedList() {
first = null;
} public LinkedList(Node<Item> first) {
this.first = first;
} /**
* insert item at the beginning of the list
* @param item the item to be inserted
*/
public void insertAtBeginning(Item item) { Node<Item> oldfirst = first; first = new Node<Item>();
first.item = item;
first.next = oldfirst; } /**
* remove the item at the beginning of the list
* @return return the item at the beginning of the list
* @throws NoSuchElementException if this Linked List is empty
*/
public Item removeFromBeginning() { if(isEmpty())
throw new NoSuchElementException("LinkedList is empty"); Item item = first.item;
first = first.next; return item;
} //1.3.19
/**
* remove the last node in the linked list whose first node is first
*
* @return return the item of the last node
* @throws NoSuchElementException if this Linked List is empty
*/
public Item removeTheLast() { Node<Item> precurrent;
Item item = null; precurrent = findPreLastNode(); //has not found
if(precurrent.next == null) {
throw new NoSuchElementException("LinkedList is empty");
} item = precurrent.next.item;
//some implementation will add one empty node as head, and head.next = first
//if so, it's not necessary to have if condition here
if(precurrent.next == first)
first = first.next;
else
precurrent.next = precurrent.next.next; return item;
} /**
* return the previous last node
*
* @return return the previous last node.
* If the last node is the first node, the previous last node is a virtual one
*/
private Node<Item> findPreLastNode() { Node<Item> precurrent = new Node<Item>();
precurrent.next = first; //find the previous last node
//precurrent.next is the same as current
while(precurrent.next != null && precurrent.next.next != null) {
precurrent = precurrent.next;
} return precurrent;
} //1.3.20
/**
* delete the kth element in a linked list, if it exists.
*
* @param k the kth element, it should larger than 1
* @throws IllegalArgumentException if k < 1
* @throws NoSuchElementException if the size of the list is less than k
*/
public Item delete(int k) { if(k < 1)
throw new IllegalArgumentException("k must larger than 1"); Node<Item> precurrent = new Node<Item>();
precurrent.next = first;
Item item; while(precurrent.next != null && k > 1) {
precurrent = precurrent.next;
k--;
} if(precurrent.next == null)
throw new NoSuchElementException("LinkedList hasn't so many elements"); item = precurrent.next.item;
if(precurrent.next == first)
first = precurrent.next.next;
else
precurrent.next = precurrent.next.next; return item;
} //1.3.21
/**
* find if some node in the list has key as its item field
*
* @param list the linked list of T
* @param key the T key
*
* @return {@code true} some node exists.
* {@code false} no node exist
*/
public static <T> boolean find(LinkedList<T> list, T key) { for(T s : list) {
if(s.equals(key))
return true;
} return false;
} //1.3.24
/**
* remove the node following the node x
* (and does nothing if the argument or the next field in the argument node is null)
*
* @param x the given node
*/
public static <T> void removeAfter(Node<T> x) { if(x == null || x.next == null)
return; Node<T> current = x.next;
x.next = null; while(current != null) {
Node<T> temp = current.next;
current.next = null;
current = temp;
} } //1.3.25
/**
* insert the second node after the first on its list.
* and does nothing if either argument is null.
*
* @param first the first node
* @param second the second node to be inserted after the first
*/
public static <T> void insertAfter(Node<T> first, Node<T> second) { if(first == null || second == null)
return; second.next = first.next;
first.next = second; } //1.3.26
/**
* remove all of the nodes in the list that have key as its item field
*
* @param list the linked list of T
* @param key the T key
*
* @return void
*
*/
public static <T> void remove(LinkedList<T> list, T key) {
Node<T> precurrent;
precurrent = findPreNode(list, key); //remove all of the nodes
while(precurrent.next != null) { if(precurrent.next == list.first)
list.first = list.first.next;
else
precurrent.next = precurrent.next.next; precurrent = findPreNode(list, key);
} } //1.3.26
/**
* find the node in the list whose item equals key
*
* @param key the T key
*
* @return return the previous node whose item equals key
*/
private static <T> Node<T> findPreNode(LinkedList<T> list, T key) {
Node<T> precurrent = new Node<T>();
precurrent.next = list.first; while(precurrent.next != null && !precurrent.next.item.equals(key)) {
precurrent = precurrent.next;
} return precurrent;
} //1.3.27
/**
* return the value of the maximum key in the list
*
* @param list the linked list of Integer
*
* @return return the maximum key in the list
*/
public static int max(LinkedList<Integer> list) { if(list.first == null)
return 0; int max = 0;
for(int val : list) {
if(val > max)
max = val;
} return max;
} //1.3.28
/**
* return the value of the maximum key in the list by recursion
*
* @param list the linked list of Integer
*
* @return return the maximum key in the list
*/
public static int maxByRecursion(LinkedList<Integer> list) { if(list.first == null)
return 0; int first = list.first.item;//first item
list.first = list.first.next;//remove first item in the list
int max = maxByRecursion(list);//calculate the maximum value of the new list if(first > max)
return first;
else
return max;
} /**
*
* see if the list is empty
*
* @return true if the list is empty.
* false if the list isn't empty
*/
public boolean isEmpty() {
return first == null;
} @Override
public String toString() {
String s = ""; Node<Item> temp = first; while(temp != null) {
Item item = temp.item;
s += item + " ";
temp = temp.next;
} return s;
} @Override
public Iterator<Item> iterator() {
return new ListIterator();
} private class ListIterator implements Iterator<Item> { private Node<Item> current = first; @Override
public boolean hasNext() {
return current != null;
} @Override
public Item next() {
if(!hasNext())
throw new NoSuchElementException(); Item item = current.item;
current = current.next; return item;
} @Override
public void remove() {
throw new UnsupportedOperationException();
} } }
算法(Algorithms)第4版 练习 链表类 1.3.19~1.3.29的更多相关文章
- 1.2 Data Abstraction(算法 Algorithms 第4版)
1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...
- 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)
1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...
- LeetCode算法题-链表类
1.将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. (可以参照第2的merge2List实现) 示例: 输入:1->2->4, 1->3 ...
- 数据结构与算法之PHP实现链表类(单链表/双链表/循环链表)
链表是由一组节点组成的集合.每个节点都使用一个对象的引用指向它的后继.指向另一个节点的引用叫做链表. 链表分为单链表.双链表.循环链表. 一.单链表 插入:链表中插入一个节点的效率很高.向链表中插 ...
- ubuntu命令行下java工程编辑与算法(第四版)环境配置
ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...
- 在Eclipse下配置算法(第四版)运行环境
第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...
- 配置算法(第4版)的Java编译环境
1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...
- 算法(第四版)C#题解——2.1
算法(第四版)C#题解——2.1 写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...
- 常见排序算法题(java版)
常见排序算法题(java版) //插入排序: package org.rut.util.algorithm.support; import org.rut.util.algorithm.Sor ...
随机推荐
- 【2048小游戏】——原生js爬坑之封装行的移动算法&事件
引言:2048小游戏的核心玩法是移动行,包括横行和纵行,玩家可以选择4个方向,然后所有行内的数字就会随着行的移动而向特定的方向移动.这个行的移动是一个需要重复调用的算法,所以这里就要将一行的移动算法封 ...
- 粗略。。Java项目设计模式之笔记----studying
设计模式 设计模式:解决这个问题的一种行之有效的思想. 设计模式:用于解决特定环境下.反复出现的特定问题的解决方式. 设计模式学习概述 ★ 为什么要学习设计模式 1.设计模式都是一些相对优秀的解决方式 ...
- Archlinux 下的 VMWare Workstation 维护笔记
印象中 Archlinux 下的 VMWare Workstation 总是出问题, 因此写这个帖子, 记录出问题时间/原因/解决方案. PS: 每次更新内核后可能需要重新编译 vmware 的内核模 ...
- apache hadoop 2.4.0 64bit 在windows8.1下直接安装指南(无需虚拟机和cygwin)
工作须要.要開始搞hadoop了,又是大数据,自己感觉大数据.云.仅仅是ERP.SOAP风潮之后与智能地球一起诞生的概念炒作. 只是Apache是个奇妙的组织.Java假设没有它也不会如今如火中天.言 ...
- gulp入门-压缩js/css文件(windows)
类似于grunt,都是基于Node.js的前端构建工具.不过gulp压缩效率更高. 工具/原料 nodejs/npm 方法/步骤 首先要确保pc上装有node,然后在global环境和项目文件中都in ...
- C# 杀掉后台进程
var p = Process.GetProcessesByName("WINWORD"); if (p.Any()) { for (int i = 0; i < p.Len ...
- Erlang服务器内存吃紧的优化解决方法
问题提出:服务器100万人在线,16G内存快被吃光.玩家进程占用内存偏高 解决方法: 第一步:erlang:system_info(process_count). 查看进程数目是否正常,是否超过了er ...
- Python学习总结之五 -- 入门函数式编程
函数式编程 最近对Python的学习有些怠慢,最近的学习态度和学习效率确实很不好,目前这种病况正在好转. 今天,我把之前学过的Python中函数式编程简单总结一下,分享给大家,也欢迎并感谢大家提出意见 ...
- python 基础 2.1 if 流程控制(一)
一.if else 1.if 语句 if expression: //注意if后有冒号,必须有 statement(s) //相对于if缩进4个空格 注:pytho ...
- <转载> pycharm快捷键及一些常用设置
1.编辑(Editing ) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctrl ...