双向链表的Java实现

public class LinkListNode {//定义节点

		public int data;//数据域
		public LinkListNode nextNode;//下个节点
		public LinkListNode preNode;//前面节点

		public LinkListNode(char c)//参数是字符型,数据是整形,是为了用字符数组添加节点
		{
			data=c-'0';
	
			nextNode=null;
			preNode=null;
		}
		public int getValues()//获取数据
		{
			return data;
		}
		public void setValues(int dat)//修改数据
		{
			data=dat;
		}
}

双向链表要分别建两个类,一个是LinkListNode节点类,封装节点所有用
public class LinkList {//双向链表

	private LinkListNode headPointer;//头指针
	private LinkListNode tailPointer;//尾指针
	private int length;//链表长度
	public LinkList()//无参构造函数,构造空链表
	{
		headPointer=null;
		tailPointer=null;
		length=0;
	}
	public LinkList(char toCharArray[])//有参构造函数,字符数组传入,构建多个节点
	{
		int lengths=toCharArray.length;
		this.length=lengths;
		LinkListNode nowNode=new LinkListNode(' ');//构建当前链表将要插入节点的位置的节点,同时new LinkListNode(' ')防止java中的空指针异常。
		for(int i=0;i<length;i++)
		{
			if(toCharArray[i]>='0'&&toCharArray[i]<='9')
			{
				LinkListNode node=new LinkListNode(toCharArray[i]);//新建节点
				node.nextNode=null;//让该节点的下一个节点的指向为空
				if(i==0)//构建头结点
				{
					headPointer=node;//头节点的指向是将要插入的节点
					headPointer.nextNode=node;//头结点下一个节点为将要插入的节点
					headPointer.preNode=null;//头节点的前面一个节点指向为null
					nowNode=node;//插入节点后,把node设置为链表中将要插入下一个节点的节点。
				}
				else
				{
				nowNode.nextNode=node;//同上
				node.preNode=nowNode;
				nowNode=node; 
				tailPointer=node;
				}
			}			
		}
	}
	public void travel()//从头节点遍历
	{
		LinkListNode p=headPointer;
		while(p!=null)
		{
			System.out.print(p.getValues());//输出节点值
			p=p.nextNode;
		}
	}

	public int search(int target)//查找目标在节点中的位置,如果查找不到,返回-1,否则,返回节点位置
	{
		LinkListNode p=headPointer;
		int position=0;
		while(p!=null)
		{
		
			if(p.getValues()==target)
				return position;
			position++;
			p=p.nextNode;
		}
		return -1;
	}
	public void insert(int index,char target)//插入节点
	{
		
		LinkListNode p1=headPointer;//获取头节点
		LinkListNode p2=headPointer;//获取头节点,让p2的位置在p1的后面
		LinkListNode node=new LinkListNode(target);
		int counts=0;
		if(index==0)//如果在头节点插入
		{
			node.nextNode=headPointer;
			headPointer.preNode=node;
			headPointer=node;		
			headPointer.preNode=null;
		}
		else 
		{
			while(p2!=null)//在中间插入的情况
			{
				
				if(counts==index)//到达插入的位置
				{
					p1.nextNode=node;
					node.preNode=p1;
					node.nextNode=p2;
					p2.preNode=node;
					
					break;
				}
				else//未到达索引位置,往前进
				{
				p1=p2;
				p2=p2.nextNode;
				}
				counts++;
			}
				if(p2==null)//在末尾插入的情况
				{
					p1.nextNode=node;
					node.preNode=p1;
					node.nextNode=null;
					tailPointer=node;
				}
		}
		this.length++;
	}
	public LinkListNode getHead()//获取头节点
{
	return headPointer;
}
	public LinkListNode getTailNode()//获取尾节点
{
	return tailPointer;
}
	public void delete(int index)//删除索引为index的元素
	{
		if(headPointer!=null)
		{
		LinkListNode p1=headPointer;
		LinkListNode p2=headPointer;
		int counts=0;
		if(index==0)//删除头节点
		{
			headPointer=headPointer.nextNode;
			headPointer.preNode=null;
		}
		else
		{
			p2=p2.nextNode;
			while(p2.nextNode!=null)//删除中间和尾节点的节点
			{
				counts++;
				if(counts==index)
				{
					p1.nextNode=p2.nextNode;
					p2.nextNode.preNode=p1;
					break;
				}
				else
				{
				p1=p2;
				p2=p2.nextNode;
				}
			}
			if(p2.nextNode==null)//如果删除的时候尾节点,将重置尾节点
				{
				p1.nextNode=p2.nextNode;
				tailPointer=p1;
				}
				
			
		}
		this.length--;//删除节点,长度减一
		}
		else
		{
			System.out.println("空链表,无法删除!");
		}
	}
	public int getLength()
	{
		return this.length;
	}	
	public static void main(String[] args) {		
		char []a={'1','2','3'};
		LinkList linkList=new LinkList(a);	
		linkList.insert(3, '0');
                linkList.travel();//从头节点遍历
		System.out.print("\n");
		linkList.delete(1);
		linkList.travel();
	}

的成员,另一个是LinkList的链表类,用来实现链表的各项功能
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
双向链表是一种数据结构,每个节点包含了指向前一个节点和后一个节点的指针。在 Java 中,可以使用自定义类来实现双向链表。以下是一个简单的双向链表实现示例: ```java public class DoublyLinkedList { private Node head; private Node tail; private class Node { int data; Node prev; Node next; public Node(int data) { this.data = data; this.prev = null; this.next = null; } } public void addToFront(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; } else { newNode.next = head; head.prev = newNode; head = newNode; } } public void addToBack(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; } else { tail.next = newNode; newNode.prev = tail; tail = newNode; } } public void remove(int data) { Node current = head; while (current != null) { if (current.data == data) { if (current == head) { head = current.next; if (head != null) { head.prev = null; } } else if (current == tail) { tail = current.prev; if (tail != null) { tail.next = null; } } else { current.prev.next = current.next; current.next.prev = current.prev; } break; } current = current.next; } } public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } } ``` 以上代码中的 `DoublyLinkedList` 类实现双向链表的常见操作,如在头部和尾部添加节点、删除指定节点以及打印链表。你可以创建 `DoublyLinkedList` 对象并调用相应的方法来操作双向链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值