本文是对高淇版Java三百集,LinkedList内容学习的记录。java中LinkedList本质是双向链表,LinkedList中包含三个属性,Node first
,Node previous
,int size
分别记录双链表的头,尾和数据的大小。
一、LinkedList常用方法
1.1 add()方法
public void add(int index, E element)
1.2 get()方法
public E get(int index)
1.3 set()方法
public E set(int index, E element)
1.4 remove()方法
public E remove(int index)
二、自定义实现LinkedList功能
2.1 自定义MyNode类和MyLinkedList类
(1)MyNode类
package cn.edu.ucas.gqlearn;
/**
* @Author: hyk
* @Date: 2020/7/20
* @version: 1.0
*/
public class MyNode<E> {
MyNode<E> previous;
E element;
MyNode<E> next;
public MyNode() {
}
public MyNode(MyNode previous, E element, MyNode next) {
this.previous = previous;
this.element = element;
this.next = next;
}
}
(2) MyLinkedList类,这里实现了add()方法并且重写了toString()方法
package cn.edu.ucas.gqlearn;
import java.util.LinkedList;
/**
* @Author: hyk
* @Date: 2020/7/20
* @version: 1.0
*/
public class MyLinkedList<E> {
private MyNode<E> first;
private MyNode<E> last;
private int size;
public MyLinkedList() {
}
public void add(E element){
MyNode<E> newNode = new MyNode<E>(last, element, null);
if(last == null){
first = newNode;
}else{
last.next = newNode;
}
last = newNode;
size++;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
MyNode<E> tempNode = first;
while (tempNode!=null){
sb.append(tempNode.element + ",");
tempNode = tempNode.next;
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
MyLinkedList<String> myLinkedList = new MyLinkedList<>();
for (int i = 0; i < 26; i++) {
myLinkedList.add((char)('A' + i) + "");
}
System.out.println(myLinkedList.toString());
}
}
2.2 添加get()方法
package cn.edu.ucas.gqlearn;
import java.util.LinkedList;
/**
* @Author: hyk
* @Date: 2020/7/20
* @version: 1.0
*/
public class MyLinkedList<E> {
private MyNode<E> first;
private MyNode<E> last;
private int size;
public MyLinkedList() {
}
public void add(E element){
MyNode<E> newNode = new MyNode<E>(last, element, null);
if(last == null){
first = newNode;
}else{
last.next = newNode;
}
last = newNode;
size++;
}
public E get(int index){
if (index < 0 || index >= size){
throw new RuntimeException("索引值有误:" + index);
}
MyNode<E> tempNode = null;
if (index <= (size >> 1)){
tempNode = first;
for (int i = 0; i < index; i++) {
tempNode = tempNode.next;
}
}else{
tempNode = last;
for (int i = size-1; i > index; i--) {
tempNode = tempNode.previous;
}
}
return tempNode.element;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
MyNode<E> tempNode = first;
while (tempNode!=null){
sb.append(tempNode.element + ",");
tempNode = tempNode.next;
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
MyLinkedList<String> myLinkedList = new MyLinkedList<>();
for (int i = 0; i < 26; i++) {
myLinkedList.add((char)('A' + i) + "");
}
System.out.println(myLinkedList.toString());
System.out.println(myLinkedList.get(22));
}
}
2.3 添加remove()方法
package cn.edu.ucas.gqlearn;
import java.util.LinkedList;
/**
* @Author: hyk
* @Date: 2020/7/20
* @version: 1.0
*/
public class MyLinkedList<E> {
private MyNode<E> first;
private MyNode<E> last;
private int size;
public MyLinkedList() {
}
public void add(E element){
MyNode<E> newNode = new MyNode<E>(last, element, null);
if(last == null){
first = newNode;
}else{
last.next = newNode;
}
last = newNode;
size++;
}
public E get(int index){
checkRange(index);
MyNode<E> tempNode = getMyNode(index);
return tempNode.element;
}
private void checkRange(int index) {
if (index < 0 || index >= size){
throw new RuntimeException("索引值有误:" + index);
}
}
private MyNode<E> getMyNode(int index) {
MyNode<E> tempNode = null;
if (index <= (size >> 1)){
tempNode = first;
for (int i = 0; i < index; i++) {
tempNode = tempNode.next;
}
}else{
tempNode = last;
for (int i = size-1; i > index; i--) {
tempNode = tempNode.previous;
}
}
return tempNode;
}
public void remove(int index){
checkRange(index);
MyNode<E> tempNode = getMyNode(index);
if (index == 0){
first = first.next;
first.previous = null;
}else if (index == (size - 1)){
last = last.previous;
last.next = null;
}else{
tempNode.previous.next = tempNode.next;
tempNode.next.previous = tempNode.previous;
}
size --;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
MyNode<E> tempNode = first;
while (tempNode!=null){
sb.append(tempNode.element + ",");
tempNode = tempNode.next;
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
MyLinkedList<String> myLinkedList = new MyLinkedList<>();
for (int i = 0; i < 26; i++) {
myLinkedList.add((char)('A' + i) + "");
}
System.out.println(myLinkedList.toString());
System.out.println(myLinkedList.get(22));
myLinkedList.remove(2);
System.out.println(myLinkedList.toString());
myLinkedList.remove(0);
System.out.println(myLinkedList.toString());
myLinkedList.remove(23);
System.out.println(myLinkedList.toString());
}
}
2.4添加insert()方法
package cn.edu.ucas.gqlearn;
import java.util.LinkedList;
/**
* @Author: hyk
* @Date: 2020/7/20
* @version: 1.0
*/
public class MyLinkedList<E> {
private MyNode<E> first;
private MyNode<E> last;
private int size;
public MyLinkedList() {
}
public void add(E element){
MyNode<E> newNode = new MyNode<E>(last, element, null);
if(last == null){
first = newNode;
}else{
last.next = newNode;
}
last = newNode;
size++;
}
public E get(int index){
checkRange(index);
MyNode<E> tempNode = getMyNode(index);
return tempNode.element;
}
private void checkRange(int index) {
if (index < 0 || index >= size){
throw new RuntimeException("索引值有误:" + index);
}
}
private MyNode<E> getMyNode(int index) {
MyNode<E> tempNode = null;
if (index <= (size >> 1)){
tempNode = first;
for (int i = 0; i < index; i++) {
tempNode = tempNode.next;
}
}else{
tempNode = last;
for (int i = size-1; i > index; i--) {
tempNode = tempNode.previous;
}
}
return tempNode;
}
public void remove(int index){
checkRange(index);
MyNode<E> tempNode = getMyNode(index);
if (tempNode == null){
System.out.println("下标异常!!!");
return;
}
if (index == 0){
first = first.next;
first.previous = null;
}else if (index == (size - 1)){
last = last.previous;
last.next = null;
}else{
tempNode.previous.next = tempNode.next;
tempNode.next.previous = tempNode.previous;
}
size --;
}
public void insert(int index, E element){
checkRange(index);
MyNode<E> newNode = null;
if (index == 0){
newNode = new MyNode<>(null,element,first);
first.previous = newNode;
first = newNode;
}else if (index == size -1 ){
newNode = new MyNode<>(last,element,null);
last.next = newNode;
last = newNode;
}else{
MyNode<E> tempNode = getMyNode(index);
newNode = new MyNode<>(tempNode.previous,element,tempNode);
tempNode.previous.next = newNode;
tempNode.previous = newNode;
}
size++;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
MyNode<E> tempNode = first;
while (tempNode!=null){
sb.append(tempNode.element + ",");
tempNode = tempNode.next;
}
sb.setCharAt(sb.length()-1,']');
return sb.toString();
}
public static void main(String[] args) {
MyLinkedList<String> myLinkedList = new MyLinkedList<>();
for (int i = 0; i < 26; i++) {
myLinkedList.add((char)('A' + i) + "");
}
System.out.println(myLinkedList.toString());
System.out.println(myLinkedList.get(22));
myLinkedList.remove(2);
System.out.println(myLinkedList.toString());
myLinkedList.remove(0);
System.out.println(myLinkedList.toString());
myLinkedList.remove(23);
System.out.println(myLinkedList.toString());
myLinkedList.insert(0,"begin!");
System.out.println(myLinkedList.toString());
myLinkedList.insert(myLinkedList.size - 1, "end!");
System.out.println(myLinkedList.toString());
myLinkedList.insert((myLinkedList.size - 1) / 2, "medium");
System.out.println(myLinkedList.toString());
}
}