数据结构复习:单链表
package com.test.ds.linkedlist;
public class LinkedList {
long count;
Node head;
public LinkedList() {
count = 0;
}
public void add(Object value) {
Node newNode = new Node(value);
if (head == null) {
// 如果链表为空,则直接将头指针指向newNode
head = newNode;
} else {
// 如果链表不为空,则在末尾节点后增加节点
getNodeByIndex(count - 1).next = newNode;
}
count++;
}
public Node getNodeByIndex(long index) {
if (index < 0 || index >= count) {
throw new IndexOutOfBoundsException("超出链表最大索引了");
}
Node searchNode = head;
// 注意i的范围,搜索的时候
// i不能大于或等于index,因为当index=count-1时候,searchNodex.next肯定是为null
// ,会造成NullPointerException
for (long i = 0; i < index; i++) {
searchNode = searchNode.next;
}
return searchNode;
}
public void removeAt(long index) {
if (index < 0 || index >= count) {
throw new IndexOutOfBoundsException("超出链表最大索引了");
}
// 删除头结点
if (0 == index) {
head = head.next;
} else {
// //先找到index 的前驱节点
Node preNode = getNodeByIndex(index - 1);
preNode.next = preNode.next.next;
}
count--;
}
/**
* 在指定索引处加入节点
*
* @param index
* @param value
*/
public void insert(int index, Object value) {
Node tempNode;
if (0 == index) {
if (null == head) {
head = new Node(value);
} else {
tempNode = new Node(value);
tempNode.next = head;
//插入的节点成为了头节点
head = tempNode;
}
}else{
//找到节点index的前驱节点
Node preNode = getNodeByIndex(index-1);
tempNode = new Node(value);
tempNode.next = preNode.next;
preNode.next = tempNode;
}
count++;
}
private class Node {
Object data;
Node next;
public Node(Object value) {
this.data = value;
}
}
public long size() {
return count;
}
@Override
public String toString() {
Node tempNode = head;
StringBuffer sb = new StringBuffer();
while (tempNode != null) {
sb.append("\t" + tempNode.data.toString());
tempNode = tempNode.next;
}
return sb.toString();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println("空链表中加入1,2,3,4后:"+list.toString());
list.removeAt(3);
list.removeAt(2);
list.removeAt(1);
System.out.println("移除位置3,2,1上的数据后:"+list.toString());
list.insert(1, 2);
list.insert(2, 3);
list.insert(3, 4);
System.out.println("在位置1,2,3上插入数据后:"+list.toString());
}
}
public class LinkedList {
long count;
Node head;
public LinkedList() {
count = 0;
}
public void add(Object value) {
Node newNode = new Node(value);
if (head == null) {
// 如果链表为空,则直接将头指针指向newNode
head = newNode;
} else {
// 如果链表不为空,则在末尾节点后增加节点
getNodeByIndex(count - 1).next = newNode;
}
count++;
}
public Node getNodeByIndex(long index) {
if (index < 0 || index >= count) {
throw new IndexOutOfBoundsException("超出链表最大索引了");
}
Node searchNode = head;
// 注意i的范围,搜索的时候
// i不能大于或等于index,因为当index=count-1时候,searchNodex.next肯定是为null
// ,会造成NullPointerException
for (long i = 0; i < index; i++) {
searchNode = searchNode.next;
}
return searchNode;
}
public void removeAt(long index) {
if (index < 0 || index >= count) {
throw new IndexOutOfBoundsException("超出链表最大索引了");
}
// 删除头结点
if (0 == index) {
head = head.next;
} else {
// //先找到index 的前驱节点
Node preNode = getNodeByIndex(index - 1);
preNode.next = preNode.next.next;
}
count--;
}
/**
* 在指定索引处加入节点
*
* @param index
* @param value
*/
public void insert(int index, Object value) {
Node tempNode;
if (0 == index) {
if (null == head) {
head = new Node(value);
} else {
tempNode = new Node(value);
tempNode.next = head;
//插入的节点成为了头节点
head = tempNode;
}
}else{
//找到节点index的前驱节点
Node preNode = getNodeByIndex(index-1);
tempNode = new Node(value);
tempNode.next = preNode.next;
preNode.next = tempNode;
}
count++;
}
private class Node {
Object data;
Node next;
public Node(Object value) {
this.data = value;
}
}
public long size() {
return count;
}
@Override
public String toString() {
Node tempNode = head;
StringBuffer sb = new StringBuffer();
while (tempNode != null) {
sb.append("\t" + tempNode.data.toString());
tempNode = tempNode.next;
}
return sb.toString();
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
System.out.println("空链表中加入1,2,3,4后:"+list.toString());
list.removeAt(3);
list.removeAt(2);
list.removeAt(1);
System.out.println("移除位置3,2,1上的数据后:"+list.toString());
list.insert(1, 2);
list.insert(2, 3);
list.insert(3, 4);
System.out.println("在位置1,2,3上插入数据后:"+list.toString());
}
}
作者:
Chris Wang
出处:
http://chriswang.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。