单链表:链表是一系列存储数据元素的单元通过指针串连接形成的。单链表的存储单元有两个域,一个是数据域,一个是指针域,这个包含数据域和指针域的存储单元叫做节点。
数据域[data]:结点的数据域 data 可以使用一个 Object 类型的对象来实现,用于存储任 何类型的数据元素,并通过对象的引用指向该元素;
(在代码中为显示方便,数据域用int数据类型)
指针域[next]:存储该节点的直接后继节点,指针域 next 可以通过节点对象的引 用来实现。
1.定义节点的接口Node
package inter;
public interface Node {
//获取节点的数据域
public int getObject();
//设置节点的数据域
public void setObject(int obj);
}
2.定义单链表的节点类
package list;
import inter.Node;
public class SLNode implements Node {
//数据中的data
private int element;
//指针域中指向下一个直接后继元素
private SLNode next;
public SLNode() {
this(0, null);
}
public SLNode(int element, SLNode next) {
this.element = element;
this.next = next;
}
public void setNext(SLNode next) {
this.next = next;
}
public SLNode getNext() {
return next;
}
public int getObject() {
return element;
}
public void setObject(int obj) {
this.element = obj;
}
public void show() {
System.out.println(element + "/r/n");// "/r/n"表示换行
}
}
``
3.定义单链表及相关操作的分析
单链表用数列表示(a0,a1,a2,...,a[i-1],a[i],a[i+1],an),a0为首节点(head),an为尾节点
(1)在单链表首部插入:每次插入节点,都在首部插入,即待插节点总是在a0后面
public void addinhead(int e) {
SLNode sl = new SLNode();
sl.setObject(e);
SLNode currentNode = head;
SLNode previousNode = head;
currentNode = currentNode.getNext();
sl.setNext(currentNode);
previousNode.setNext(sl);
System.out.println(sl.getObject() + "||" + previousNode.getNext());
}
(2)在单链表尾部插入:每次插入节点,都在首部插入,即待插节点总是在an后面,变为尾节点。单链表尾部节点的标志是an的下一个节点为null
// 在单链表的尾部添加数据
public void addintail(int e) {
SLNode sl = new SLNode();
sl.setObject(e);
SLNode currentNode = head;
while (currentNode.getNext() != null) {
currentNode = currentNode.getNext();
}
currentNode.setNext(sl);
sl.setNext(null);
System.out.println(sl.getObject() + "||");
}
(3)在单链表任意位置i插入data e:即将数据域为e的新节点p插到a[i]位置。遍历单链表,直到单链表中的节点 位置到index,将a[i]的直接后继元素直接变为p的直接后继,a[i]的直接后继为p
// 在单链表index处添加e
public void add(int index, int e) {
SLNode sl = new SLNode();
SLNode currentNode = head;
SLNode previousNode = head;
while (pos != index) {
previousNode = currentNode;
currentNode = currentNode.getNext();
pos++;
}
sl.setNext(currentNode);
sl.setObject(e);
previousNode.setNext(sl);
System.out.println(sl.getObject() + "||" + previousNode.getNext());
pos = 0;
}
4.其他删除,查找,清除单链表
package list;
public class SingleLink {
private SLNode head;// 头节点
private int pos = 0; // 节点的位置
public SingleLink() {
this.head = new SLNode();
}
// 查找某节点index中的数据域,若存在则返回数据域的值,不存在则返回null
public SLNode getObjectByIndex(int index) {
// 首先定位到头节点
int j = 0;
SLNode currentSN = head;
while (j < index) {
currentSN = currentSN.getNext();
j++;
}
if (j > index || currentSN == null) {
return null;
}
System.out.println(currentSN.getObject());
return currentSN.getNext();
}
// 查找单链表中是否存在某节点的数据域为e,若存在则返回true,不存在则返回false
public boolean contains(int o) {
boolean is_exits = false;
// 首先定位到头节点
SLNode currentSN = head;
while (currentSN.getNext() != null) {
System.out.println(currentSN.getObject());
if (currentSN.getObject() != o) {
currentSN = currentSN.getNext();
} else {
is_exits = true;
break;
}
}
return is_exits;
}
// 在单链表index处添加e
public void add(int index, int e) {
SLNode sl = new SLNode();
SLNode currentNode = head;
SLNode previousNode = head;
while (pos != index) {
previousNode = currentNode;
currentNode = currentNode.getNext();
pos++;
}
sl.setNext(currentNode);
sl.setObject(e);
previousNode.setNext(sl);
System.out.println(sl.getObject() + "||" + previousNode.getNext());
pos = 0;
}
// 在单链表的头部添加数据
public void addinhead(int e) {
SLNode sl = new SLNode();
sl.setObject(e);
SLNode currentNode = head;
SLNode previousNode = head;
currentNode = currentNode.getNext();
sl.setNext(currentNode);
previousNode.setNext(sl);
System.out.println(sl.getObject() + "||" + previousNode.getNext());
}
// 在单链表的尾部添加数据
public void addintail(int e) {
SLNode sl = new SLNode();
sl.setObject(e);
SLNode currentNode = head;
while (currentNode.getNext() != null) {
currentNode = currentNode.getNext();
}
currentNode.setNext(sl);
sl.setNext(null);
System.out.println(sl.getObject() + "||");
}
// 显示单链表中的所有节点的数据域,不包括首节点
public void showAllData() {
SLNode slnode = head;
while (slnode.getNext() != null) {
slnode = slnode.getNext();
System.out.println(slnode.getObject());
}
}
// 显示单链表中所有节点的个数,不包括首节点
public int size() {
int count = 0;
SLNode currentNode = head;
while (currentNode.getNext() != null) {
count++;
currentNode = currentNode.getNext();
}
return count;
}
/**
* function:删除单链表位置为index处的节点
*
* @param index为节点位置
*
* */
public void deleteNode(int index) {
int j = 0;
if (index == 0) {
head = head.getNext();
} else {
SLNode currentNode = head;
SLNode previousNode = head;
while (j != index) {
previousNode = currentNode;
currentNode = currentNode.getNext();
++j;
}
previousNode.setNext(currentNode.getNext());
System.out.println(currentNode.getObject() + "已被删除");
}
}
//清除单链表中的所有数据
public void clearLink(){
SLNode currentNode = head;
while (currentNode.getNext()!=null) {
currentNode = currentNode.getNext();
head.setNext(currentNode.getNext());
}
}
}
5.测试类
package list;
public class Test {
public static void main(String[] args) {
SingleLink mLink = new SingleLink();
mLink.add(1, 112);
mLink.add(2, 212);
mLink.add(3, 3233);
mLink.add(4, 432);
mLink.add(5, 532);
mLink.addinhead(123);
mLink.addinhead(223);
mLink.addinhead(333);
mLink.addinhead(443);
mLink.addintail(121);
mLink.addintail(12321);
mLink.addintail(1234321);
mLink.addintail(123454321);
System.out.println(1+"//"+mLink.getObjectByIndex(1));
System.out.println(2+"//"+mLink.getObjectByIndex(2));
System.out.println(3+"//"+mLink.getObjectByIndex(4));
System.out.println("********"+mLink.contains(532));
// System.out.println(mLink.getObjectByIndex(4));
mLink.showAllData();
System.out.println(mLink.size());
mLink.deleteNode(1);
mLink.deleteNode(5);
mLink.showAllData();
mLink.clearLink();
System.out.println("-----------------------------");
mLink.showAllData();
System.out.println(mLink.size());
}
}