JAVA实验二:设计一个带表头的双向链表(链表中数据的具体类型可以随意) 泛型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fighting123678/article/details/83338878

题目:设计一个带表头的双向链表(链表中数据的具体类型可以随意)

提供以下方法:
(1)insert:在某个位置插入对象;
(2)insert:在链表的最后插入对象;
(3)delete:在某个位置删除对象;
(4)delete:删除链表中与x相同的元素;
(5)size:返回当前链表中对象的个数;
(6)isEmpty:判断链表是否为空;
(7)traverse:遍历链表,打印出所有的元素;
(8)getData:取得某个位置的对象。构造main函数进行测试。

答案:

public class DoubleList<T>//泛型(类、接口、方法
{	
///////////////////////构造链表结构	
	protected Node head;//头指针
	protected Node rear;//尾指针
	protected int sum;//数目
//////////////////////初始化链表
	public DoubleList()
	{
		head=new Node();//创建头结点,不能是null类型的;
		rear=new Node();//创建尾结点,不能是null类型的;
		head.next=rear;
		rear.pre=head;
		//sum=0;//错误
	}
//////////////////在某个位置插入对象;
	public boolean insert(int pos,T no)
	{
		if(pos>sum||pos<=0) 
		{
			System.out.println("Can not do the change");
			return false;
		}
		
		Node p=head;
		for(int i=0;i<pos-1;i++)
		{
			p=p.next;
		}
		Node p1=p.next;
		//新建结点插入数据;
		Node newnode=new Node();
		newnode.value=no;
		newnode.pre=p;
		newnode.next=p1;
		p1.pre=newnode;
		p.next=newnode;
		sum+=1;
		return true;
	}
/////////////////////在链表的最后插入对象
	public boolean insertEnd(T no)
	{
		Node newnode=new Node();
		newnode.value=no;
		newnode.pre=rear.pre;
		newnode.next=rear;
		rear.pre.next=newnode;
		rear.pre=newnode;
		sum+=1;
		return true;
	}
/////////////////////在某个位置删除对象
	public boolean delete(int pos)
	{
		if(pos>sum||pos<=0) 
		{
			System.out.println("Can not do the change");
			return false;
		}
		
		Node p=head;
		for(int i=0;i<pos-1;i++)
		{
			if(p.next!=null)
			{
				p=p.next;
			}
			if(p.next==null)  return false;
		}
		Node r=p.next;
		r.next.pre=p;
		p.next=r.next;
		sum-=1;
		
/////////////////注意清除结点的操作;
		r.next=null;
		r.pre=null;
		return true;
	}
///////////////删除链表中与x相同的元素
	public boolean deleteEqual(T no)
	{
		if(sum==0) 
		{
			System.out.println("Can not do the change");
			return false;
		}
		
		int flag=0;
		Node p=head;
		for(int i=0;i<sum;i++)
		{
			if(p.next!=null&&p.next.value!=no)
			{
				p=p.next;
			}
			else if(p.next!=null&&p.next.value.equals(no))
			{
				flag=1;
				Node r=p.next;
				r.next.pre=p;
				p.next=r.next;
				sum-=1;
				r.next=null;
				r.pre=null;
			}
			else 
			{
				System.out.println("Can not do the change");
				return false;
			}
		}
		if(flag==0)  System.out.println("Can not do the change");
		return true;
	}
///////////////////////返回当前链表中对象的个数
	public int size()
	{
		return sum;
	}
////////////////////////判断链表是否为空
	public boolean empty()
	{
		if(sum==0) return true;
		else return false;
	}
//////////////////遍历链表,打印出所有的元素
	public void traverse() 
    {
        Node n = head.next;
        while (n.next != null) 
        {
            System.out.print(n.value + " ");
            n = n.next;
        }
        System.out.println();
    }
////////////////取得某个位置的对象
	public boolean getData(int pos)
    {
    	if(pos>sum)  
    	{
    		System.out.println("Can not do the change");
			return false;
    	}
    	else
    	{
    		Node pNode = head.next;
            for (int i = 1; i < pos; i++) 
            {
                pNode = pNode.next;
            }
            System.out.println("The value is "+pNode.value);
            return true;
    	}
    } 
	public static void main(String[] args) 
	{
		DoubleList<Integer> doubleList = new DoubleList<>();
//////////////////现在尾结点插入,然后在正确的位置和错误的位置分别示范
		for(int i=0;i<10;i++)
		{
			doubleList.insertEnd(i);
		}
		doubleList.traverse();
		doubleList.insert(1, 10);
		doubleList.traverse();
		doubleList.insert(3, 10);
		doubleList.traverse();
		doubleList.insert(13, 10);
		doubleList.traverse();
/////////////删除对象测试
		doubleList.delete(1);
		doubleList.traverse();
		doubleList.delete(3);
		doubleList.traverse();
		doubleList.delete(16);
		doubleList.traverse();
		doubleList.deleteEqual(2);
		doubleList.traverse();
		doubleList.deleteEqual(13);
		doubleList.traverse();
//////////////对于元素个数的测试
		System.out.println(doubleList.size());
/////////////对于链表是否为空的测试
		if(doubleList.empty()==true) System.out.println("It is Empty");
		else System.out.println("It is no Empty");
///////////////取得某个位置的对象
		doubleList.getData(1);
		doubleList.getData(9);
		doubleList.getData(10);
	}
}
////////////////////////构造结点的结构
class Node<T>
{
	protected T value;//某结点上的数据
	protected Node pre;//前指针
	protected Node next;//后指针
}

注意:

1、本题采用了泛型的方式,也就是说,不知道是什么样的类型,只能以泛型的方式进行设置
https://blog.csdn.net/s10461/article/details/53941091

(1)对于泛型类,定义方式是public class DoubleList<T>//泛型(类、接口、方法

(2)使用方式是DoubleList<Integer> doubleList = new DoubleList<>();

猜你喜欢

转载自blog.csdn.net/fighting123678/article/details/83338878