单链表的实现代码

首先定义一个节点类~记录每个节点使用:

[java]  view plain copy
  1. public class Node {  
  2.   
  3.     String data;//数据域  
  4.     Node next;//链域  
  5.   
  6.     public Node(String data) {  
  7.         this.data = data;  
  8.         next = null;  
  9.     }  
  10.   
  11. }  


下列代码就是构造了一个单链表,并且实现了各种操作:

[java]  view plain copy
  1. public class LinkedList {  
  2.   
  3.     int lenght;// 链表的长度  
  4.     Node head;// 定义头结点  
  5.   
  6.     public LinkedList() {  
  7.         head = null;// 链表中的头结点  
  8.         lenght = 0;  
  9.     }  
  10.   
  11.     /** 在链表末尾添加一个元素 */  
  12.     public void addElement(Node n) {  
  13.         if (lenght == 0) {// 如果链表为空,则直接添加。  
  14.             head = n;  
  15.         } else {  
  16.             Node t = head;  
  17.             while (t.next != null) {// 沿链表第一个元素向后寻找,直到找到末尾  
  18.                 t = t.next;  
  19.             }  
  20.             t.next = n;// 在末尾添加元素,也就是给最后一个next赋值~让链域不为空,这样链域为空的就成了链表中最后一个元素  
  21.         }  
  22.         // 在主函数中调用这个的方法为addElement(new Node("张三")),  
  23.         // 其中new一个Node 就确定了这个Node的数据域(data)为"张三",链域(next)为null,  
  24.         // 而这个函数的作用就是确定把这个新new出来Node放在谁的next中  
  25.         lenght++;// 修改长度  
  26.     }  
  27.   
  28.     /** 在链表中特定位置添加一个元素 */  
  29.     public boolean insert(Node n, int index) {  
  30.         if (index > lenght && index < 0) {// 如果插入位置超出链表长度  
  31.             System.out.println("超出范围!!");  
  32.             return false;  
  33.         } else {  
  34.             Node t = head;  
  35.             if (index == 0) {  
  36.                 n.next = head;// 让传入的节点指向头节点  
  37.                 head = n;// n作为新的头节点  
  38.             } else {  
  39.                 for (int i = 0; i < index - 1; i++) {// 找到要插入的位置的前一个位置  
  40.                     t = t.next;  
  41.                 }  
  42.                 n.next = t.next;// 新插入的节点指向当前位置节点  
  43.                 t.next = n;  
  44.                 // 这里需要用图解释的更为清晰  
  45.                 // 可见本文下方图—1;  
  46.             }  
  47.             lenght++;  
  48.             return true;  
  49.         }  
  50.     }  
  51.   
  52.     /** 删除链表最后一个节点 */  
  53.     public void removeLastElement() {  
  54.         if (lenght == 0) {  
  55.             System.out.println("链表中没有元素");  
  56.             return;  
  57.         }  
  58.         Node t = head;  
  59.         if (lenght == 1) {  
  60.             head = null;  
  61.         } else {  
  62.             for (int i = 0; i < lenght - 1; i++) {// 找到末尾节点的前一个节点  
  63.                 t = t.next;  
  64.             }  
  65.             t.next = null;  
  66.         }  
  67.         lenght--;  
  68.     }  
  69.   
  70.     /** 删除指定位置的节点 */  
  71.     public void removeElement(int index) {  
  72.         if (lenght == 0) {  
  73.             System.out.println("链表没有元素!");  
  74.             return;  
  75.         }  
  76.         if (index > lenght && index < 0) {  
  77.             System.out.println("超出范围!!");  
  78.             return;  
  79.         }  
  80.         Node t = head;  
  81.         if (index == 0) {  
  82.             head = head.next;  
  83.             t.next = null;  
  84.             lenght--;  
  85.             return;  
  86.         }  
  87.   
  88.         for (int i = 0; i < index - 1; i++) {// 找到要删除位置的前一位置  
  89.             t = t.next;  
  90.         }  
  91.         Node temp = t;  
  92.         t.next = t.next.next;  
  93.         temp.next = null;// 让该节点的next指向空  
  94.         lenght--;  
  95.   
  96.     }  
  97.   
  98.     /** 清空链表 */  
  99.     public void clearList() {  
  100.         head = null;  
  101.         lenght = 0;  
  102.     }  
  103.   
  104.     /** 打印链表中的所有元素 */  
  105.     public void printList() {  
  106.         if (lenght == 0) {  
  107.             System.out.println("链表为空!");  
  108.             return;  
  109.         }  
  110.         Node t = head;  
  111.         while (t!= null) {  
  112.             System.out.print(t.data + " ");  
  113.             t = t.next;  
  114.         }  
  115.     }  
  116. }  

                                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值