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

时间:2022-10-25 00:24:45
首先定义结点:

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
  }*/
  
  
  
 }
}


                                                                                           


 

水木轩昊昊