java数据结构双链表的自定义实现

package com.wck.test83;
/**
 * @author kun
 * @date 2021年8月3日
 */
public class DoublyLinkedList {
	  class Node{
		  Object item;
		  Node next;
		  Node prior;
	  }
      Node head=new Node();
	  int size=0;
	  int flag=1;
	  Node temp;
		public boolean isEmpty() {
			return size == 0;
		}
		
		public int getSize() {
			return size;
		}
	  //插入方法
	  public void Insert(Object obj){
		  if(size==0){
			  head.item="";
			  head.prior=null;
			  head.next=null;
			  size=1;    
		  } if(size==1){
			  Node newnode=new Node();
			  newnode.item=obj;
			  head.next=newnode;
			  newnode.prior=head;
			  newnode.next=null;
		  }if(size>1){
			  Node newnode=new Node();
			  newnode.item=obj;
			  head.next.prior=newnode;
			  newnode.next=head.next;
			  head.next=newnode;
			  newnode.prior=head;
  
		  }
		  size++;
		
	  }
	  
	  //删除
	  public void delete(Object obj){
		  Node nodehead = head.next;
		  for(int i=0;i<size-1;i++){
			 if(nodehead.item.equals(obj)){
				 temp=nodehead;
			 }
			  nodehead = nodehead.next;
		  }	  
		if(temp!=null){
			  temp.prior.next=temp.next;
			  temp.next.prior=temp.prior;	
			  size--;
		  }if(temp==null){
			  System.out.println("没有元素"+obj);
		  }
		
		   
	  }
	  
	  //修改
	  
	  public void update(Object obj,Object obj1) {
		  Node nodehead = head.next;
		  for(int i=0;i<size-1;i++){
			 if(nodehead.item.equals(obj)){
				 temp=nodehead;
			 }
			  nodehead = nodehead.next;
		  }	  
		if(temp!=null){
			  temp.item=obj1;
		  }if(temp==null){
			  System.out.println("没有元素"+obj+"修改失败");
		  }
	 }
	  
	  //查找根据内容查找位置
	  public void lookforpostion(Object obj) {
		  Node nodehead = head.next;
		  int count =0;
		  for(int i=0;i<size-1;i++){
			 if(nodehead.item.equals(obj)){
				 temp=nodehead;
				 System.out.println("元素"+"\""+obj+"\""+"在第"+(count+1)+"个位置");
			 }
			  nodehead = nodehead.next;
			  count++;
		  }	  
		if(count==5){
			System.out.println("该元素未找到!");
		}
	 }
	  
	  //根据位置查元素值
	  public void lookforcontent(int option){
		    Node nodehead = head.next;
		    Object content=null;
		    while(flag<=option){
		    	content=nodehead.item;
		    	nodehead = nodehead.next;
		    	flag++;
		    }
		    System.out.println(content);
	  }
	  
	  //遍历方法
	  public void prit(int size){
		 
		  
		  Node nodehead = head.next;
		  for(int i=0;i<size-1;i++){
			  System.out.print(nodehead.item+"\t");
			  nodehead = nodehead.next;
		  }
		
		System.out.println();
	  }
}

实现类:

package com.wck.test83;
public class DoublyLinkedListTest {
	public static void main(String[] args) {
	DoublyLinkedList dLL=new DoublyLinkedList();
	//添加
	System.out.println("添加实现....");
	dLL.Insert("aaa");
	dLL.Insert("bbb");
	dLL.Insert("ccc");
	dLL.Insert("ddd");
	dLL.Insert("eee");
    int size=dLL.getSize();
    dLL.prit(size);
    System.out.println("---------------------");
    //删除
    System.out.println("删除元素eee实现.......");
    dLL.delete("eee");
    int size1=dLL.getSize();
    dLL.prit(size1);
    System.out.println("---------------------");
    //修改
    System.out.println("修改ddd为ddd1实现.........");
    dLL.update("ddd", "ddd1");
    int size2=dLL.getSize();
    dLL.prit(size2);
    System.out.println("---------------------");
    
    //查找
    System.out.println("查找bbb元素在哪个位置.......");
    dLL.lookforpostion("bbb");
    System.out.println("------------------");
    //根据位置查元素值
    dLL.lookforcontent(1);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以自定义双向链表实现一些特定的需求。双向链表是一种数据结构,它的每个节点都包含一个指向前一个节点和后一个节点的指针。这使得在双向链表中进行插入和删除操作非常高效,因为只需要修改前后节点的指针就可以完成。另外,访问节点可能会比较慢,因为需要从第一个节点开始遍历链表。但是,Java的LinkedList类提供了丰富的方法,可以模拟链式队列、链式堆栈等数据结构,为用户提供了极大的方便。 下面是一个简单的自定义双向链表Java代码示例: ```java // 定义节点类 class HeroNode2 { public int no; public String name; public String nickname; public HeroNode2 next; // 指向下一个节点,默认为null public HeroNode2 pre; // 指向上一个节点,默认为null // 构造器 public HeroNode2(int no, String name, String nickname) { this.no = no; this.name = name; this.nickname = nickname; } @Override public String toString() { return "HeroNode2{" + "no=" + no + ", name='" + name + '\'' + ", nickname='" + nickname + '\'' + '}'; } } // 自定义双向链表类 class DoubleLinkedList { private HeroNode2 head; // 头节点 // 构造器 public DoubleLinkedList() { head = new HeroNode2(0, "", ""); } // 在链表尾部添加节点 public void add(HeroNode2 node) { HeroNode2 temp = head; while (temp.next != null) { temp = temp.next; } temp.next = node; node.pre = temp; } // 遍历链表 public void display() { HeroNode2 temp = head.next; while (temp != null) { System.out.println(temp); temp = temp.next; } } // 在某个节点后面插入新节点 public void insertAfter(HeroNode2 newNode, HeroNode2 afterNode) { HeroNode2 temp = head; while (temp != null) { if (temp == afterNode) { newNode.next = temp.next; if (temp.next != null) { temp.next.pre = newNode; } newNode.pre = temp; temp.next = newNode; break; } temp = temp.next; } } // 删除某个节点 public void delete(HeroNode2 node) { HeroNode2 temp = head; while (temp != null) { if (temp == node) { temp.pre.next = temp.next; if (temp.next != null) { temp.next.pre = temp.pre; } break; } temp = temp.next; } } } ``` 以上是一个简单的自定义双向链表实现。你可以根据需要添加其他方法或功能来满足具体需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java双向链表实现](https://download.csdn.net/download/weixin_38669628/11056304)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Java 自定义双向链表](https://blog.csdn.net/ana35287/article/details/102111857)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Java实现双向链表](https://blog.csdn.net/m0_63732435/article/details/127195219)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值