1. Java语言下的链表
1.1 内部类定义结点
class Node {
int data;
Node next;
Node(int elem) {
data = elem;
next = null;
}
}
1.2 初始链表,只有头结点
class LinkList {
...
Node head;
LinkList() {
head = new Node(0);
}
}
1.3 清空链表
void reset() {
head.next = null;
}
1.4 返回元素的位置(在第几个结点)
public int locate(int elem) {
int location = -1;
Node tempNode = head.next;
int TempLocation = 0;
while (tempNode != null) {
if (tempNode.data == elem) {
location = TempLocation;
break;
}
tempNode = tempNode.next;
TempLocation++;
}
return location;
}
1.5 在某个结点处插入元素
public boolean Insert_L(int location, int elem) {
Node tempNode = head;
Node NewNode;
for(int i=0; i<location; i++) {
if(tempNode.next==null) {
System.out.println("ERROR");
return false;
}
tempNode = tempNode.next;
}
NewNode = new Node(location);
NewNode.next = tempNode.next;
tempNode.next = NewNode;
return true;
}
1.5 删除某个结点的元素

Status Delete_L(LinkList &L, int i, ElemType &e) {
LinkList p; int j;
p = L; j = 0;
while(p->next&&j<i-1) {
p = p->next;
++j;
}
if(!(p->next)||j>i-1) return ERROR;
LinkList q;
q = p->next; p->next = q->next;
e = q->data;
free(q);
return OK;
}
public boolean Delete_L(int location) {
if(head.next==null) {
System.out.println("Empty");
return false;
}
Node p = head;
int j = 0;
for(int i=0; i<location-1; i++) {
if(p.next==null) {
System.out.print("ERROR");
return false;
}
p = p.next;
}
if(p.next==null||j>location-1) {
System.out.println("ERROR");
return false;
}
Node q;
q = p.next; p.next = q.next;
return true;
}
1.6 便历链表
public String toString() {
String resulting = null;
if(head.next==null) {
return "Empty";
}
Node NewNode = head.next;
while(NewNode!=null) {
resulting += NewNode.data+",";
NewNode = NewNode.next;
}
return resulting;
}
2. Java源码
2.1 LinkList类
package datastructure.linearlist;
public class LinkList {
class Node {
int data;
Node next;
Node(int elem) {
data = elem;
next = null;
}
}
Node head;
public LinkList() {
head = new Node(0);
}
void reset() {
head.next = null;
}
public int getLocation(int elem) {
int location = -1;
Node tempNode = head.next;
int tempLocation = 0;
int t = 0;
while (tempNode != null) {
if (tempNode.data == elem) {
location = tempLocation;
t++;
break;
}
tempNode = tempNode.next;
tempLocation++;
}
if (t == 0) {
return -1;
}
return location + 1;
}
public boolean insert(int location, int elem) {
Node tempNode = head;
Node NewNode;
for (int i = 1; i < location; i++) {
if (tempNode.next == null) {
System.out.println("ERROR");
return false;
}
tempNode = tempNode.next;
}
NewNode = new Node(location);
NewNode.next = tempNode.next;
tempNode.next = NewNode;
return true;
}
public boolean delete(int location) {
if (head.next == null) {
System.out.println("ERROR");
return false;
}
Node tempNode = head;
for (int i = 0; i < location - 1; i++) {
if (tempNode.next.next == null) {
System.out.print("ERROR");
return false;
}
tempNode = tempNode.next;
}
tempNode.next = tempNode.next.next;
return true;
}
public String toString() {
String resultString = " ";
if (head.next == null) {
return "Empty";
}
Node NewNode = head.next;
while (NewNode != null) {
resultString += NewNode.data + " ";
NewNode = NewNode.next;
}
return resultString;
}
public static void main(String[] args) {
LinkList linklist = new LinkList();
int[] elem = { 1, 2, 3 };
int[] location = { 1, 2, 3 };
linklist.insert(location[0], elem[0]);
linklist.insert(location[1], elem[1]);
linklist.insert(location[2], elem[2]);
System.out.println("打印链表:" + linklist.toString());
System.out.println(elem[2] + "->这个元素在第几个结点:" + linklist.getLocation(elem[2]));
if (linklist.delete(location[2]))
System.out.println("删除第" + location[2] + "个结点后,打印链表:" + linklist.toString());
else
System.out.println(",删除不成功");
linklist.reset();
System.out.println("清空链表后,打印链表:" + linklist.toString());
}
}
2.2 运行结果

2.3 注意
- 我的源码的结点个数从1开始,而没有按照数组的下标从0开始。
- 因为这样便于理解:头结点 -> 第1个结点 -> 第2个结点 -> … …