链表最大优势是插入删除比较方便
其实链表也是一种灵活的数据结构,没有想象的那么复杂
public class LinkList<T> {
private Node first;
private LinkList(){
first = null;
}
class Node<T>{
T data;
Node next;
public Node(){}
public Node(T t){
this.data = t;
}
public void display(){
System.out.print(" " + data);
}
}
boolean isEmpty(){
return null == first;
}
//头插法
public void insertFirst(T t){
Node node = new Node(t);
node.next = first;
first = node;
}
public Node deleteFirst(){
Node node = first;
first = first.next;
return node;
}
//查找
public Node find(T t){
Node current = first;
while(current.data != t){
if(null == current)
return null;
current = current.next;
}
return current;
}
public void display(){
if(isEmpty()){
System.out.println("linkList is null.");
return;
}
Node current = null;
current = first;
while(null != current){
current.display();
current = current.next;
}
}
/**
* 逆序输出 面试题
* 从尾到头打印链表 使用递归(优雅)
* 递归的原理,其实就是一个栈(stack) “逐层递进”与“逐层回归”的过程!
* @param head
*/
public void reversePrint(Node head){
if(null == head){
return;
}
reversePrint(head.next);
System.out.print(head.data + " ");
}
//插入一个节点,插到指定节点的后边
public boolean insert(T t, Node newNode){
boolean flag = false;
LinkList.Node node = this.find(t);
if(null != node){
if(null != node.next){
newNode.next = node.next;
}
node.next = newNode;
flag = true;
}
return flag;
}
//删除指定节点
public boolean delete(T t){
boolean flag = false;
LinkList.Node current = first;
LinkList.Node prev = first;
while(null != current && current.data != t){
prev = current;
current = current.next;
}
if(null == current){
System.out.println("Not find target node.");
return false;
}
if(first == current){
first = current.next;
}else if(null != current.next){
//如果目标后继节点不为空
prev.next = current.next;
}else{
prev.next = null;
}
flag = true;
return flag;
}
public static void main(String[] args){
LinkList list = new LinkList();
list.insertFirst(22);
list.insertFirst(33);
list.insertFirst(55);
list.insertFirst(66);
list.insertFirst(88);
// list.deleteFirst();
// list.delete(55);
list.display();
System.out.println("");
System.out.println("reverse List-->");
list.reversePrint(list.first);
}
}
【双向链表-2016】==============================================================================
package com.dataStruct.demo;
public class DLinkList {
private DLinkNode first;
private DLinkNode last;
public boolean isEmpty(){
if(null == first)
return true;
return false;
}
public void insertFirst(int id, String name){
DLinkNode dLinkNode = new DLinkNode(id, name);
if(isEmpty()){
last = dLinkNode;
}else{
first.prev = dLinkNode;
}
dLinkNode.next = first;
first = dLinkNode;
}
public void insertLast(int id, String name){
DLinkNode dLinkNode = new DLinkNode(id, name);
if(isEmpty()){
first = dLinkNode;
}else{
last.next = dLinkNode;
dLinkNode.prev = last;
}
last = dLinkNode;
}
public void delFirst(){
if(isEmpty())
return;
if(first == last){
first = null;
last = null;
}else{
first = first.next;
first.next.prev = null;
}
}
public void delLast(){
if(isEmpty())
return;
if(first == last){
first = null;
last = null;
}else{
last = last.prev;
last.next = null;
}
}
public boolean delete(int id){
if(isEmpty()){
System.out.println("DLinkNode is null!");
return false;
}
DLinkNode current = first;
while(null != current){
if(current.id == id){
if(first == last){
first = null;
last = null;
}
if(current == first){
first = first.next;
}else if(current == last){
last = current.prev;
current.prev.next = null;
}else{
current.prev.next = current.next;
current.next.prev = current.prev;
}
return true;
}
current = current.next;
}
return false;
}
public void display(){
DLinkNode current = first;
if(isEmpty()){
System.out.println("DLinkList is empty!!");
return;
}
if(first == last){
first.display();
return;
}
while(null != current){
current.display();
current = current.next;
}
}
public static void main(String[] args){
DLinkList dl = new DLinkList();
dl.insertFirst(1, "ZhangSan");
dl.insertLast(2, "LiSi");
dl.insertLast(3, "WangWu");
// dl.delete(2);
// dl.delFirst();
dl.delLast();
dl.display();
}
}
【小结】
逆序 (2017/12/22)
通过递归把一个链表变成了栈结构,可以说肥肠牛逼
数据结构老手的惯用伎俩,实战中也有很广泛的用途
public void reverse(Node node){
if(null == node)
return;
/*差之毫厘,谬以千里*/
// node = node.next;
reverse(node.next);
System.out.println(node.data + " ");
}