DuLNode.class
package Shuang;
public class DuLNode {
public Object data; //存放节点值的数据域
public DuLNode prior; //存放指向前驱结点的指针域
public DuLNode next; //存放指向后结继点的指针域
public int value;
public DuLNode() { //无参时的构造函数
this(null);
}
public DuLNode(Object data) //构建数据域值为data的新结点
{
this.data=data;
this.prior=null;
this.next=null;
}
}
Ilist
package lianbiao;
public interface Ilist {
public int length();
public Object get(int i) throws Exception;
public void insert(int i,Object x) throws Exception ;
public void remove(int i) throws Exception ;
}
DuLinkList.class
package Shuang;
import java.util.Scanner;
import lianbiao.Node;
class DuLinkList implements Ilist
{
public static DuLNode head;
public DuLinkList() {
head =new DuLNode();
head.prior =head;
head.next =head;
}
//尾插法,其中n为双链表的结点个数
public void create1(int n)throws Exception{
Scanner sc = new Scanner(System.in);
for(int j=0;j<n;j++) //逆序输入n个结点的数据域值
insert(j,sc.next()); //生成新结点,插入到表尾
}
//逆向创建双向链表
public DuLinkList(int n) throws Exception{
this();
Scanner sc =new Scanner(System.in);
for(int j=0;j<n;j++)
{
insert(0,sc.next());
}
}
void showInfo() //输出列表
{
for(DuLNode p=head.next; p!=null; p=p.next)
System.out.print(p.data+" ");
}
//带头结点双向链表插入
public void insert(int i, Object x) throws Exception{
DuLNode p = head.next; //初始化
int j=0;
while(!p.equals(head)&&j<i) //寻找插入位置i
{
p=p.next; //指向后继结点
++j;
}
if(j!=i&&!p.equals(head)) //i不合法
{
throw new Exception("插入位置不合法!");
}
DuLNode s =new DuLNode(x);
p.prior.next=s;
s.prior=p.prior;
s.next=p;
p.prior=s;
}
//删除操作
public void remove(int i) throws Exception{
DuLNode p = head.next;
int j =0;
while(!p.equals(head)&&j<i) {
p=p.next;
++j;
}
if(j!=i) {
throw new Exception("删除位置不合法");
}
p.prior.next=p.next;
p.next.prior=p.prior;
}
//在链表头部添加结点
public void addHead(int data) {
DuLNode newNode = new DuLNode(data);
if(head == null) {
head = newNode;//如果链表为空,增加新结点
}
else {
newNode.setNext(head);
head.setPrevious(newNode);
head = newNode;
}
int length = 0;
length++;
}
public void display() {
DuLNode node=head.next; //取出带头结点的双向循环链表的首结点
while(!node.equals(head)) {
System.out.print(node.data+" "); //输出结点的值
node=node.next; //取下一个结点
}
System.out.println();
}
public Object get(int i) throws Exception //按序查找
{
DuLNode p=head.next; //p指向首节点
int j= 0; //计数器j
while(p!=null && j<i) //从首节点开始找,直到p指向i结点 或p为空
{
p=p.next ; //指向下一结点
++j;
}
if(j>i || p==null) //i<0或大于表长-1时
{
throw new Exception("第"+i+"个元素不存在");
}
return p.data; //返回p的数据域值
}
public int indexOf(Object x) //按值查找
{
DuLNode p =head.next;
int j=0;
while(p!= null && !p.data.equals(x)) //从单链表首节点开始找,直到p.data为x或达到单链表的表尾
{
p=p.next;
++j;
}
if(p!=null) //返回值为x的结点在单链表中的位置
return j;
else
return -1; //值为x的结点不在单链表中,则返回-1
}
public int length() {
// TODO Auto-generated method stub
DuLNode p=head.next; //初始化,p指向首结点,length为计数器
int length=0;
while(p!=head) { //从首结点开始向后查找,直到p为空
p=p.next; //指向后继结点
++length; //长度增1
}
return length;
}
}
测试:
package Shuang;
import java.util.Scanner;
public class text01 {
public static void main(String[] args) throws Exception{
//DuLinkList L1 = new DuLinkList(100);
//L1.insert(0, 10);
//L1.insert(1,12);
//L1.display();
System.out.println("有");
int n = new Scanner(System.in).nextInt();
System.out.println("该双链表有"+n+"个结点值");
//创建新双链表
System.out.println("请输入该双链表的各个结点值");
DuLinkList L=new DuLinkList();
L.create1(n);
System.out.println("用头插法创建的双链表为:");
//调用方法输出
L.display();
System.out.println("查找双链表第二位为:"+L.get(1));
L.remove(1);
System.out.println("");
System.out.println("删除第二位的双链表为:");
L.display();
//System.out.println("请输入该双链表的各个结点值");
//DuLinkList L2=new DuLinkList(n,false);
//System.out.println("用头插法创建的双链表为:");
//L2.display();
}
}