单链表的基本操作实现案例

package parking;

import java.util.Stack;

/**
* 链表
*
* @author ASUS
*
*/

class Node {
Object data;// 链表的值
Node next;// 指向链表的下一个结点

Node(Object data) {
this.data = data;
}
}

public class LinkNode {

private Node head;// 表示链表头结点
private int size;// 表示链表结点数量

public LinkNode() {
this.head = null;
this.size = 0;
}

// 获取链表数量
private int getSize() {
return size;
}

// 判断链表是否为空
private boolean isEmpty(Node head) {
return head == null ? true : false;
}

// 头插入法,给链表增加结点,入栈
private void addHnode(Object obj) {
Node node = new Node(obj);
if (head == null) {
head = node;
} else {
node.next = head;
head = node;
}
size++;
}

// 尾插入法,给链表增加结点,入栈
private void addWnode(Object obj) {
Node node = new Node(obj);
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = node;
node.next = null;
}
size++;
}

// 正向输出链表
private void sysNode(Node head) {
if (head == null) {
System.out.println("链表为空");
} else {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "-->");
temp = temp.next;
}
}

System.out.println();
}

// 反向输出链表用栈
private void sysNodeStack(Node head) {
Stack stack = new Stack();
if (head == null) {
System.out.println("链表为空");
} else {
Node temp = head;
while (temp != null) {
stack.push(temp.data);
temp = temp.next;
}
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + "-->");
}
System.out.println();
}

// 反向输出链表,递归
private void sysNodeDG(Node head) {
if (head == null) {
System.out.println("链表为空");
}else {
if(head.next!=null) {
sysNodeDG(head.next);
}
System.out.print(head.data + "-->");
}


}

// 删除头结点(出栈)
private Object deleteHnode() {
if (head == null) {
System.out.println("链表为空");
return null;
}
Object obj = head.data;
if (head.next == null) {
head = null;
} else {
head = head.next;
}
size--;
return obj;
}

// 判断链表是否包含该结点
private boolean isHasNode(Node head, Node node) {
boolean flag = false;
if (isEmpty(head)) {
System.out.println("链表为空,没有该结点");
return false;
}
Node temp = head;
while (temp != null) {
if (temp.data == node.data) {
return true;
} else {
temp = temp.next;
}
}

return false;
}

// 删除指定结点
private void deleteNode(Node node) {
if (isEmpty(head)) {
System.out.println("链表为空");
return;
}

Node temp = head;//当前结点
Node pre = head;// 前一个结点
while (temp.data != node.data) {
if (temp.next == null) {
System.out.println("没有找到该结点");
return;
}
pre = temp;
temp = temp.next;
}
//删除结点是头结点时
if (head==temp) {
//删除头结点
deleteHnode();
} else {
pre.next = temp.next;
}
size--;
}



public static void main(String[] args) {
// TODO Auto-generated method stub
LinkNode link = new LinkNode();
int i;
//头插入法
for (i = 1; i < 5; i++) {
link.addHnode(i);
}
//尾插入法
for (i = 5; i < 10; i++) {
link.addWnode(i);
}
System.out.println("结点大小:" + link.getSize());
System.out.println("正向输出链表:");
link.sysNode(link.head);
System.out.println("反向输出链表:用栈实现:");
link.sysNodeStack(link.head);
System.out.println("反向输出链表:用递归实现:");
link.sysNodeDG(link.head);
//设定指定结点
Node node = new Node(3);
System.out.println();
System.out.println("删除头结点:" + link.deleteHnode());
link.sysNode(link.head);
System.out.println("包含结点-->" + link.isHasNode(link.head, node));
System.out.println("删除指定结点:");
link.deleteNode(node);
link.sysNode(link.head);

}

}

效果:

结点大小:9
正向输出链表:
4-->3-->2-->1-->5-->6-->7-->8-->9-->
反向输出链表:用栈实现:
9-->8-->7-->6-->5-->1-->2-->3-->4-->
反向输出链表:用递归实现:
9-->8-->7-->6-->5-->1-->2-->3-->4-->
删除头结点:4
3-->2-->1-->5-->6-->7-->8-->9-->
包含结点-->true
删除指定结点:
2-->1-->5-->6-->7-->8-->9-->