这是我在练习的时候写的双向链表,简单实用,功能齐全,附源码,需要jar包的可直接下载使用。
Node类:
//节点类
class Node<T> {
Node pre =null;//前指针
Node next = null;//后指针
T t = null;//数据域
public Node(){
this.next = null;
this.pre =null;
this.t =null;
}
}
链表类属性:
private Node<T> head;//头指针
private Node<T> tail;//尾指针
public DLList(){
this.head = null;
this.tail = null;
}
常用增删改查功能:
//增
//在指定位置插入元素
public void insert(int index,T t) throws DLListIndexOutOfBoundsException{
if(index > this.length() || index < 0){
throw new DLListIndexOutOfBoundsException("长度出界!");
}else{
Node s = new Node();//需要插入数据的节点
Node p = this.head;//指针
s.t = t;
if(this.isEmptty()){
this.head = new Node();
this.head.t =t;
this.tail =this.head;
}else{
if(index == 0){
s.next = this.head;
this.head.pre =s;
this.head = s;
}
else if(index ==1){
if(this.head.next != null){
s.pre = this.head;
s.next = this.head.next;
this.head.next.pre = s;
this.head.next = s;
}else{
s.pre = this.head;
this.head.next = s;
}
}else{
if(index == this.length()){
while(p.next !=null){
p = p.next;
}
s.pre = p;
p.next = s;
}else{
for(int i = 0;i < index;i++ ){
p=p.next;
}
s.pre = p.pre;
s.next= p;
p.pre.next= s;
p.pre =s;
}
}
}
}
}
//头插入
public void insert_head(T t){
try {
this.insert(0, t);
} catch (DLListIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//尾插入
public void insert_tail(T t){
Node p =this.head;//指针
Node s = new Node();
s.t = t;
if(this.isEmptty()){
this.head = new Node();
this.head.t =t;
}else{
while(p.next != null){
p=p.next;
}
s.pre = p;
p.next = s;
this.tail =s;
}
}
//*****************************************
//删
//删除指定位置的节点
public void delate(int index) throws DLListIndexOutOfBoundsException{
if(index > this.length() || index < 0){
throw new DLListIndexOutOfBoundsException("长度出界!");
}else{
Node p = this.head;//指针
if(index == 0){
if(this.length() == 1){
this.head = null;
}else{
this.head = this.head.next;
this.head.pre = null;
}
}
else if(index< this.length()-1){
for(int i = 0;i < index;i++){
p = p.next;
}
p.pre.next = p.next;
p.next.pre = p.pre;
}
else if(index == this.length()-1){
for(int i = 0;i < index;i++){
p = p.next;
}
p.pre.next = null;
}
}
}
public void delate(boolean f ){
if(f){
try {
this.delate(this.length()-1);
} catch (DLListIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
try {
this.delate(0);
} catch (DLListIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//删除尾节点 ,默认方法
public void delate(){
try {
this.delate(this.length()-1);
} catch (DLListIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//改
//根据位置索引,支持反向索引修改
public void changed(int index,T t) throws DLListIndexOutOfBoundsException{
if(index > this.length() || index < this.rlength()){
throw new DLListIndexOutOfBoundsException("长度出界!");
}else{
//需要修改的新数据
Node s = new Node();
s.t = t;
if(index >= 0){
Node p = this.head;//指针
for(int i = 0;i< index;i++){
p = p.next;
}
if(index == 0){
s.next = p.next;
p.next.pre = s;
this.head = s;
}
else if(index == this.length()-1){
s.pre = p.pre;
p.pre.next = s;
}else{
s.pre = p.pre;
s.next = p.next;
p.pre.next = s;
p.next.pre= s;
}
}else{
Node p = this.tail;//指针
for(int i = -1;i > index;i--){
p= p.pre;
}
if(index == this.rlength()){
s.next = p.next;
p.next.pre = s;
this.head = s;
}
else if(index == -1){
s.pre = this.tail.pre;
this.tail.pre.next = s;
this.tail = s;
}else{
s.pre = p.pre;
s.next = p.next;
p.pre.next = s;
p.next.pre= s;
}
}
}
}
//根据内容查找并修改,如果有相同元素只修改一个,如果被修改内容不存在将抛出异常
//m为原数据,t为修改后的新数据
public void changed(T m,T t) throws NotFindException, DLListIndexOutOfBoundsException{
int a = this.get(m);
this.changed(a, t);
}
//查
//查找方法,支持反向查询
public Object find(int index) throws DLListIndexOutOfBoundsException{
Object t =null;
if(index < this.rlength() || index >= this.length()){
throw new DLListIndexOutOfBoundsException("长度出界!");
}else{
if(index>=0){
t = this.get(index);
}else{
Node p = this.tail;
for(int i =-1;i> index;i--){
p=p.pre;
}
t = p.t;
}
}
return t;
}
//按位置索引查,返回数据
public Object get(int index) throws DLListIndexOutOfBoundsException{
Object t =null;
if(index >= this.length() || index < 0){
throw new DLListIndexOutOfBoundsException("长度出界!");
}else{
Node p = this.head;//指针
for(int i = 0;i < index;i++){
p = p.next;
}
t = (Object)p.t;
}
return t;
}
//按内容搜索,返回位置
public int get(T t) throws NotFindException{
boolean b = true;
int a = 0;
Node p = this.head;
while(p != null){
if(p.t .equals(t)){
b = false;
break;
}
a++;
p = p.next;
}
if(b){
throw new NotFindException("未找到对应目标!");
}
return a;
}
//返回第Index个数据的上一个数据
public Object return_pre(int index) throws DLListIndexOutOfBoundsException{
Node s= null;
if(index == 0){
throw new DLListIndexOutOfBoundsException("不可应用!");
}else{
try {
s = this.getNode(index);
} catch (DLListIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Object o = s.pre.t;
return o;
}
//返回第index个节点的下一个节点数据
public Object return_next(int index) throws DLListIndexOutOfBoundsException{
Node s= null;
if(index == this.length()-1){
throw new DLListIndexOutOfBoundsException("不可应用!");
}else{
s = this.getNode(index);
}
Object o = s.next.t;
return o;
}
附带功能:
//将链表中的String类型内容拼合成String型
public String turnToString() throws Exception{
String str ="";
for(int i =0;i<this.length();i++){
str += this.get(i);
}
return str;
}
//返回链表头文件
public Object returnHead(){
return this.head.t;
}
//返回链表尾部文件
public Object returnTail(){
return this.tail.t;
}
//比较两个链表是否相同
public boolean equals(DLList l){
boolean t = true;
if(this.length() != l.length()){
t = false;
}
Node p = this.head;//指针
for(int i = 0;i< this.length();i++){
try {
if(!(this.get(i).equals(l.get(i)))){
t = false;
break;
}
} catch (DLListIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return t;
}
//返回两个链表的长度差,f为第一个链表,s为第二个链表
public static int compares(DLList f,DLList s){
int a = f.length();
int b = s.length();
int c = b-a;
return c;
}
//打印输出链表
public static void printLink(DLList l){
for(int i = 0;i< l.length();i++){
try {
System.out.print(l.get(i)+"\t");
} catch (DLListIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//返回链表长度
public int length(){
int a = 0;
Node p =this.head;
while(p != null){
a++;
p = p.next;
}
return a;
}
//返回尾循环长度
public int rlength(){
int a = 0;
Node p = this.tail;//指针
while(p != null){
a++;
p = p.pre;
}
return -a;
}
//链表判空
public boolean isEmptty(){
if(this.head == null){
return true;
}else{
return false;
}
}
//链表清空
public void clear(){
this.head = null;
this.tail = null;
}
另外该包中包含自定义异常类:
class DLListIndexOutOfBoundsException extends Exception{
/**
*
*/
private static final long serialVersionUID = 1L;
private String message;
public DLListIndexOutOfBoundsException(String message){
super(message);
this.message = message;
}
}
class NotFindException extends Exception{
/**
*
*/
private static final long serialVersionUID = 2L;
private String message;
public NotFindException(String message){
super(message);
this.message = message;
}
}