1.单端链表
单端链表是链表中最简单的一种格式,用户的操作(添加、删除、遍历)只能从链表头开始,具体代码实现如下
public class SingleLinkDemo {
private Node first;// 首节点
//在首节点插入
public void insertFirst(String data){
Node node = new Node(data);
node.next = first;
first = node;
}
//删除首节点元素
public Node deleteFirst(){
Node res = first;
if(null != first){
res = first;
first = first.next;
}
return res;
}
//查找指定data的node
public Node find(String data){
Node demoFirst = first;
while(null != demoFirst){
if(demoFirst.data.equals(data)){
return demoFirst;
}else{
demoFirst = demoFirst.next;
}
}
return null;
}
//删除指定data的node
public Node delete(String data){
Node current = first;
Node previous = first;
while(null != current){
if(current.data.equals(data)){
previous.next = current.next;
break;
}else{
previous = current;
current = current.next;
}
}
return current;
}
//展示所有节点内容
public void displayList(){
if(null != first){
StringBuffer sb = new StringBuffer();
Node demoFirst = first;
while(demoFirst != null){
sb.append(demoFirst.data).append(" ");
demoFirst = demoFirst.next;
}
System.out.println(sb.toString());
}
}
public boolean isEmpty(){
return first == null;
}
/**
* 节点类
*/
private class Node{
String data;
Node next;
public Node(String data) {
super();
this.data = data;
}
@Override
public String toString() {
return data;
}
}
}
2.双端链表
双端链表相对于单端链表多了一个特性:对最后一个链接点的引用,代码实现如下
public class DoubleLinkDemo {
private Node first;//首节点
private Node last;//尾节点
//在链表头插入数据
public void insertFirst(String data){
Node node = new Node(data);
if(isEmpty()){
last = node;//说明该节点是首个节点,则其既是首节点也是尾节点
}else{
node.next = first;
}
first = node;
}
//在链表尾插入数据
public void insertLast(String data){
Node node = new Node(data);
if(isEmpty()){
first = node;
}else{
last.next = node;
}
last = node;
}
//删除链表头数据
public Node deleteFirst(){
Node res = first;
if(!isEmpty()){
first = first.next;
}
return res;
}
//展示所有节点内容
public void displayList(){
if(null != first){
StringBuffer sb = new StringBuffer();
Node demoFirst = first;
while(demoFirst != null){
sb.append(demoFirst.data).append(" ");
demoFirst = demoFirst.next;
}
System.out.println(sb.toString());
}
}
public boolean isEmpty(){
return first == null;
}
/**
* 节点类
*/
private class Node{
String data;
Node next;
public Node(String data) {
super();
this.data = data;
}
@Override
public String toString() {
return data;
}
}
}
3.双向链表
双向链表是使用比较多的一种结构,相对于单端链表只能从链表头开始正向遍历,双向链表可以逆向遍历,每个节点需要保存前一个节点和后一个节点的引用,代码实现如下
public class DoubleDirectionLinkDemo {
private Node first;//首节点
private Node last;//尾节点
//在链表头插入数据
public void insertFirst(String data){
Node node = new Node(data);
if(isEmpty()){
last = node;
}else{
first.privious = node;
node.next = first;
}
first = node;
}
//在链表尾插入数据
public void insertLast(String data){
Node node = new Node(data);
if(isEmpty()){
first = node;
}else{
last.next = node;
node.privious = last;
}
last = node;
}
//删除链表头数据
public Node deleteFirst(){
Node res = first;
if(!isEmpty()){
if(res.next == null){//只有一个元素
last = null;
}else{
first.next.privious = null;
}
first = first.next;
}
return res;
}
//删除链表尾数据
public Node deleteLast(){
Node res = last;
if(!isEmpty()){
if(res.privious == null){//只有一个元素
first = null;
}else{
last.privious.next = null;
}
last = last.privious;
}
return res;
}
//删除指定节点
public Node delete(String data){
Node current = first;
if(!isEmpty()){
while(!current.data.equals(data)){
current = current.next;
if(null == current){
return current;
}
}
if(current == first){
first = current.next;
}else{
current.privious.next = current.next;
}
if(current == last){
last = current.privious;
}else{
current.next.privious = current.privious;
}
}
return current;
}
public boolean isEmpty(){
return first == null;
}
//展示所有节点内容
public void displayList(){
if(null != first){
StringBuffer sb = new StringBuffer();
Node demoFirst = first;
while(demoFirst != null){
sb.append(demoFirst.data).append(" ");
demoFirst = demoFirst.next;
}
System.out.println(sb.toString());
}
}
//节点类
private class Node{
String data;
Node next;//后节点
Node privious;//前节点
public Node(String data) {
super();
this.data = data;
}
@Override
public String toString() {
return data;
}
}
}