标签:
public interface Node {
//获取结点数据域
public Object gerData();
//设置结点数据域
public void setData(Object obj);
}
单链表结点定义
public class SLNode implements Node {
public Object element;
public SLNode next;
public SLNode() {
this(null, null);
}
public SLNode(Object ele, SLNode next) {
this.element = ele;
this.next = next;
}
public SLNode getNext() {
return next;
}
public void setNext(SLNode next) {
this.next = next;
}
/** ************** Methods of Node Interface ************* */
public Object gerData() {
return element;
}
public void setData(Object obj) {
element = obj;
}
} 单链表结构在单链表中进行查找操作,只能从链表的首结点开始,通过每个结点的 next 引用来一次访问链表中的每个结点以完成相应的查找操作。
在单链表中删除一个结点时,除首结点外都必须知道该结点的直接前驱结点的引用。并且在已知单链表中某个结点引用的基础上,完成其后续结点的删除操作需要的时间是Θ (1)。由于在单链表中数据元素的删除是通过节点的删除来完成的,因此
<strong>2.单链表的实现
class LinkList {
private Node head = null;
private Node tail = null;
int Length = 0;
public Node getHead() {
return head;
}
public void setHead(Node head) {
this.head = head;
}
public Node getTail() {
return tail;
}
public void setTail(Node tail) {
this.tail = tail;
}
// 判断链表是否为空
public boolean isEmpty() {
return (Length == 0);
}
// 查找指定位置的结点
public Node FindKth(int k) {
Node temp = head;
int i = 1;
while (temp != null && i < k) { // 移动元素,直到找到相应位置
temp = temp.next;
i++;
}
if (i == k)
return temp;
else
return null;
}
// 查找指定元素所在的位置
public int Find(String str) {
Node temp = head;
int current = 1;
while (temp != null && temp.key.equals(str)) {
temp = temp.next;
current++;
}
if (temp.key.equals(str))
return current;
else
return -1;
}
// 头插法
public void insertHead(String str) {
Node newLink = new Node(str);
newLink.next = head;
head = newLink;
Length++;
}
// 将结点插入指定位置
public void insert(int index, String str) {
Node newLink = new Node(str);
if (index > 1 && index < Length) {
newLink.next = FindKth(index - 1).next;
FindKth(index - 1).next = newLink;
// System.out.println(head.key); 此时head的值为4
Length++;
} else if (index == 1) {
newLink.next = head;
head = newLink;
Length++;
} else {
System.out.println("超出链表长度,插入无效");
}
}
// 删除指定位置的结点
public void delete(int index) {
if (index == 1) { // 删除位置为头结点
head = head.next;
Length--;
} else if (index > 1 && index < Length) { // 删除位置不为头结点或尾结点
Node temp = FindKth(index).next;
FindKth(index - 1).next = temp;
Length--;
} else {
System.out.println("超出链表长度,查找无效");
}
}
// 删除指定元素
public void deleteNode(String str) {
int current = Find(str);
if (current > 1 && current < Length) { // 删除的位置为头结点
Node temp = FindKth(current).next;
FindKth(current - 1).next = temp;
Length--;
} else if (current == 1) {
head = head.next;
Length--;
} else if (current == -1) {
System.out.println("要删除的元素不存在,请重新选择!");
}
}
// 初始化链表
public void initList(Node node) {
head = node;
head.next = tail;
}
// 打印单链表
public void display() {
Node current = head;
while (current != null) {
current.displayNode();
current = current.next;
}
}
}
public class NodeTest {
public static void main(String args[]) {
LinkList list = new LinkList();
list.insertHead("1");
list.insertHead("2");
list.insertHead("3");
list.insertHead("4");
System.out.println("头插法操作后的单链表:");
list.display();
System.out.println();
// 把元素插入指定位置
int index = 1;
System.out.println("插入元素后的单链表:");
list.insert(index, "a");
list.display();
// 删除元素
System.out.println();
System.out.println("删除元素后的单链表(删除指定元素):");
list.deleteNode("7");
list.display();
// 删除元素
System.out.println();
System.out.println("删除元素后的单链表(删除结点位置):");
list.delete(4);
list.display();
}
}
class Node {
public String key;
public Node next;// 指向下一个元素的指针
// 初始化头结点
public Node(String str) {
this.key = str;
this.next = null;
}
public void displayNode() {
System.out.print(key + " -> ");
}
}</strong>
标签:
原文地址:http://blog.csdn.net/qq_21394609/article/details/44871059