package zmx.javadata;
import org.omg.DynamicAny.DynSequenceOperations;
/**
* 链接点,相当于是车厢
* @author zmx
*
*/
public class Node {
//数据域
public long data;
//节点域 指针域
public Node next;
public Node(long value){
this.data=value;
}
/**
* 显示方法
*/
public void display1(){
System.out.println(data+" ");
}
}
package zmx.javadata;
/**
* 链表,相当于火车
* @author zmx
*
*/
public class LinkList {
//头结点
private Node first;
public LinkList(){
first=null;
}
/**
* 插入一个节点,在头结点后进行插入
*/
public void insertFirst(long value){
Node node=new Node(value);
node.next=first;
first=node;
}
/**
* 删除一个节点,在头结点后进行删除
*/
public Node deleteFirst(){
Node tmp=first;
first=tmp.next;
return tmp;
}
/**
* 显示方法
*/
public void display(){
Node current=first;
while(current!=null){
current.display1();
current=current.next;
}
}
/**
* 查找方法
*/
public Node find(long value){
Node current=first;
while(current.data!=value){
if(current.next==null){
return null;
}
current=current.next;
}
return current;
}
/**
* 删除方法 根据数据域进行删除
*/
public Node delete(long value){
Node previous=first;
Node current=first;
while(current.data!=value){
if(current.data!=value){
return null;
}
previous=current;
current=current.next;
}
if(current==first){
// Node tmp=first;
// first=tmp.next;
// return tmp;
first=first.next;
}
else{
previous.next=current.next;
}
return current;
}
}
package ch05;
import zmx.javadata.Node;
/**
* 双端链表
* @author zmx
*
*/
public class FirstLastLinkList {
//头结点
private Node first;
//尾节点
private Node last;
public FirstLastLinkList(){
first=null;
last=null;
}
/**
* 插入一个节点,在头结点后进行插入
*/
public void insertFirst(long value){
Node node=new Node(value);
if(isEmpty()){
last=node;
}
node.next=first;//first自带next,因为first是空的不能有值
first=node;
}
/**
* 插入节点,从尾节点插入
*/
public void insertLast(long value){
Node node=new Node(value);
if(isEmpty()){
first=node;
}
else{
/**
* 尾节点也一直是空的他一直指向的是新的节点。没添加一个结点他会不断的下移
*/
last.next=node;
}
last=node;
}
/**
* 删除一个节点,在头结点后进行删除
*/
public Node deleteFirst(){
Node tmp=first;
if(first.next==null){
last=null;
}
first=tmp.next;
return tmp;
}
/**
* 显示方法
*/
public void display(){
Node current=first;
while(current!=null){
current.display1();
current=current.next;
}
}
/**
* 查找方法
*/
public Node find(long value){
Node current=first;
while(current.data!=value){
if(current.next==null){
return null;
}
current=current.next;
}
return current;
}
/**
* 删除方法 根据数据域进行删除
*/
public Node delete(long value){
Node previous=first;
Node current=first;
while(current.data!=value){
if(current.data!=value){
return null;
}
previous=current;
current=current.next;
}
if(current==first){
// Node tmp=first;
// first=tmp.next;
// return tmp;
first=first.next;
}
else{
previous.next=current.next;
}
return current;
}
/**
* 判断是否为空
*/
public boolean isEmpty(){
return (first==null);
}
}
package ch05;
/**
* 双端链表
* @author zmx
*
*/
public class DoubleLinkList {
//头结点
private Node first;
//尾节点
private Node last;
public DoubleLinkList(){
first=null;
last=null;
}
/**
* 插入一个节点,在头结点后进行插入
*/
public void insertFirst(long value){
Node node=new Node(value);
if(isEmpty()){
last=node;
}
else{
first.previous=node;
}
node.next=first;//first自带next,因为first是空的不能有值
first=node;
}
/**
* 插入节点,从尾节点插入
*/
public void insertLast(long value){
Node node=new Node(value);
if(isEmpty()){
first=node;
}
else{
/**
* 尾节点也一直是空的他一直指向的是新的节点。没添加一个结点他会不断的下移
*/
last.next=node;
node.previous=last;
}
last=node;
}
/**
* 删除一个节点,在头结点后进行删除
*/
public Node deleteFirst(){
Node tmp=first;
if(first.next==null){
last=null;
}
else{
first.next.previous=null; //头结点后删除,前面已经没有节点了。
}
first=tmp.next;//(因为后面的tmp有.next所以前面默认有next)未解
return tmp;
}
/**
* 删除结点,从尾部进行删除
*/
public Node deleteLast(){
if(first.next==null){//这里的空有点不懂 最好还是用size来判断是否为空
first=null;
}else {
last.previous.next=null;
}
last=last.previous;
return last;
}
/**
* 显示方法
*/
public void display(){
Node current=first;
while(current!=null){
current.display1();
current=current.next;
}
}
/**
* 查找方法
*/
public Node find(long value){
Node current=first;
while(current.data!=value){
if(current.next==null){
return null;
}
current=current.next;
}
return current;
}
/**
* 删除方法 根据数据域进行删除
*/
public Node delete(long value){
Node previous=first;
Node current=first;
while(current.data!=value){
if(current.data!=value){
return null;
}
previous=current;
current=current.next;
}
if(current==first){
// Node tmp=first;
// first=tmp.next;
// return tmp;
first=first.next;
}
else{
current.previous.next=current.next;
}
return current;
}
/**
* 判断是否为空
*/
public boolean isEmpty(){
return (first==null);
}
}