以下是我用java实现的数据结构中的链表package com.husiwang.LinkList;
/**
* Created by SiwangHu on 2015/2/1.
*/
public class Node {
private Object Data; //数据缓冲区
private Node Next; //下移结点
private Node Prior; //前一结点
//无参构造函数
public Node(){
Data=null;
Next=null;
Prior=null;
}
//有参构造函数
public Node(Object data){
Data=data;
Next=null;
Prior=null;
}
//有参构造函数
public Node(Object data,Node next,Node prior){
Data=data;
Next=next;
Prior=prior;
}
//指定下移结点
public void setNext(Node next) {
Next = next;
}
//获取下移结点
public Node getNext() {
return Next;
}
//设置数据
public void setData(Object data) {
Data = data;
}
//获取数据
public Object getData() {
return Data;
}
//获取前一结点
public Node getPrior() {
return Prior;
}
//设置前一结点
public void setPrior(Node prior) {
Prior = prior;
}
@Override
public String toString() {
return "Node{" +
"Data=" + Data +
", Next=" + Next +
", Prior=" + Prior +
'}';
}
}
package com.husiwang.LinkList;
/**
* Created by SiwangHu on 2015/2/1.
*/
public class LinkList {
private Node Head; //头结点
private int Length; //链表结点数目
//无参构造函数
public LinkList(){
Head=new Node();
Length=0;
}
//插入链表末尾
public void Insert(Object data){
Node Move=Head,node=new Node(data);
while(Move.getNext()!=null){
Move=Move.getNext();
}
Move.setNext(node);
node.setPrior(Move);
Length++;
}
//在指定位置插入数据
public void Insert(int Index,Object data){
if(Index
Node Move=Head,node=new Node(data);
for(int i=0;i
Move=Move.getNext();
}
node.setNext(Move.getNext());
Move.getNext().setPrior(node);
Move.setNext(node);
node.setPrior(Move);
Length++;
}
else{
throw new RuntimeException("下标过界");
}
}
//移除最后一个元素
public void Remove(){
if(!IsEmpty()) {
Node Move = Head;
while (Move.getNext() != null) {
Move = Move.getNext();
}
Move.getPrior().setNext(null);
Move.setPrior(null);
Length--;
}
}
//移除指定位置的元素
public void Remove(int Index){
if(!IsEmpty()) {
if (Index
if(Length==1){
Head.getNext().setPrior(null);
Head.setNext(null);
Length--;
return;
}
if(Length-1==Index){
Remove();
return;
}
Node Move = Head;
for (int i = 0; i
Move = Move.getNext();
}
Move.getNext().getNext().setPrior(Move);
Move.setNext(Move.getNext().getNext());
Length--;
} else {
throw new RuntimeException("下标过界");
}
}
}
//移除指定位置的元素
public void Remove(Object data){
int index=getIndex(data);
Remove(index);
}
//获取指定位置的元素
public Object Get(int Index){
if(Index
Node Move=Head;
for(int i=-1;i
Move=Move.getNext();
}
return Move.getData();
}
else{
throw new RuntimeException("下标过界");
}
}
//设定指定位置的元素
public void Set(int Index,Object data){
if(Index
Node Move=Head;
for(int i=-1;i
Move=Move.getNext();
}
Move.setData(data);
}
else{
throw new RuntimeException("下标过界");
}
}
//查找
public boolean Find(Object data){
Node Move=Head;
for(int i=-1;i
if(Move.getData()==data)
return true;
Move=Move.getNext();
}
return false;
}
//获取长度
public int getLength() {
return Length;
}
//获取下标
public int getIndex(Object data){
for(int i=0;i
if(Get(i)==data)
return i;
}
return Length;
}
//判断是否为空
public boolean IsEmpty(){
if(Length==0)
return true;
else
return false;
}
@Override
public String toString() {
String temp="";
Node Move=Head;
for(int i=-1;i
Move=Move.getNext();
if(Move!=null) {
temp += Move.getData() + " ";
}
}
return "LinkList{" +
"Data=" + temp +
", Length=" + Length +
'}';
}
}