用Java语言实现链表的增删改查

首先定义结点:

public class Node {

public Node nextNode=null;

public Integer value=null;

}

 

其次是定义链表实现功能:

package wxj;


public class LinkList {

 private Node header = null;
 private Integer length = 0;

 // 判空
 public boolean IsEmpty() {
  if (this.length == 0)
   return true;
  else
   return false;

 }

 // 得到链表长度
 public Integer Length() {
  return this.length;
 }

 // 插入头节点
 public void insertHead(Integer data) {

  Node p = new Node();
  p.value = data;
  p.nextNode = this.header;
  this.header = p;
  this.length++;
 }

 // 插入尾节点
 public void insertEnd(Integer data) {
  Node p = new Node();
  p.value = data;
  if (this.IsEmpty())
   this.header = p;
  else {
   Node q = this.header;
   while (q.nextNode != null) {
    q = q.nextNode;
   }
   q.nextNode = p;
  }
  this.length++;
 }

 // 在第i个位置插入
 public void insertData(Integer data, Integer i) {

  Node p = new Node();
  Node q = this.header;
  p.value = data;
  Integer j;
  for (j = 1; j < i; j++) {
   q = q.nextNode;
  }
  p.nextNode = q.nextNode;
  q.nextNode = p;
  this.length++;

 }

 // 删除头节点
 public void deleteHead() {
  if (this.IsEmpty())
   ;
  else {
   this.header = this.header.nextNode;
   this.length--;
  }
 }

 // 删除尾节点
 public void deleteEnd() {
  if (this.IsEmpty())
   ;
  else {
   Node p = this.header;
   if (p.nextNode == null)
    this.header = null;
   else {
    while (p.nextNode.nextNode != null) {
     p = p.nextNode;
    }
    p.nextNode = null;
   }
   this.length--;
  }
 }

 // 删除第i个位置上的数
 public void deleteData(Integer i) {
  if(this.length>=i){

  Node p = this.header;
  for (Integer j = 1; j < i - 1; j++) {

   p = p.nextNode;
  }
  p.nextNode = p.nextNode.nextNode;
  this.length--;
  }
  else
   System.out.println("长度不够");

 }

 // 获得头节点
 public Node getHead() {
  if (this.IsEmpty())
   return null;
  else {
   return this.header;
  }
 }

 // 获得尾节点
 public Node getEnd() {
  if (IsEmpty())
   return null;
  else {
   Node p = this.header;
   while (p.nextNode != null) {
    p = p.nextNode;
   }
   return p;
  }
 }

 // 找到第i个位置上的值
 public Integer getData(Integer i)  {
  if (this.length >= i && i > 0) {
   Node p = this.header;
   for (Integer j = 1; j < i; j++) {
    p = p.nextNode;
   }
   return p.value;
  } 
  else {
   System.out.println("不存在");

   //Exception e = new Exception();
   //throw e;
    return null;
  }
  
 }

 // 输出
 public void show() {
  System.out.println("该链表是:");
  Node p = this.header;
  while (p != null) {
   System.out.println(p.value);
   p = p.nextNode;
  }
 }

}


 

 

主类:

package wxj;

public class Main {

 public static void main(String[] args){
  LinkList l =new LinkList();
  l.insertHead(1);
  l.insertEnd(2);
  l.insertData(3, 2);
  l.insertData(4, 3);
  
  l.show();
  
  l.deleteData(5);
  l.show();
  //System.out.println(l.getData(3));
  /*try {
   System.out.println(l.getData(2));
  } catch (Exception e) {
   System.out.println("返回的值不存在");
   e.printStackTrace();
   // TODO: handle exception
  }*/
  
  
  
 }
}


                                                                                           


 

水木轩昊昊

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值