java实现单链表(一)

package com.test;

import javax.swing.text.AbstractDocument.LeafElement;

public class LinkList1<DT> {
    /**
     *
     * @author aolei
     *
     * @param <DT>
     *
     * 节点类
     */
     public static class Node<DT>{
         DT data;//数据
         Node<DT> pNext;//指针
         /*
          * 构造函数
          */
         Node(DT element,Node<DT> pNext){
             this.data = element;
             this.pNext = pNext;
         }
         Node(DT element){
             this(element, null);
         }
         Node(){}
     }
     private Node<DT> first;
     private static int length;//记录链表的长度
     public LinkList1() {
         // TODO Auto-generated constructor stub
         first = new Node();
         first.pNext = null;
     }
     /**
      * 判断链表是否为空
      * @return
      */
  public boolean isEmpty(){
      return first.pNext == null;
  }
  /**
   * 添加元素到链表的节点中
   * @param data
   */
  public void addElement(DT data){
      Node<DT> node = new Node<DT>(data);
      node.pNext = first.pNext;
      first.pNext = node;
      length++;
  }
  /**
   * 打印链表
   */
  public void printLinklist(){
      if ( isEmpty()){
          System.out.println("Linklist is NULL");
      }
      Node<DT> node = first.pNext;
      for (;node != null;node = node.pNext){
          System.out.println(node.data);
      }
  }
  /**
   * 插入节点
   * @param data 要插入的数据
   * @param local 要插入的位置
   * @return 是否插入成功
   */
  public boolean insertNode(DT data, int local){
      int currentLocal = 0;
      if (isEmpty()){
          System.out.println("插入失败");
          System.out.println("Linklist is NULL");
          return false;
      }else{
          Node<DT> pNode = first.pNext;
          Node<DT> newNode = new Node<DT>(data);
          while(pNode.pNext != null && currentLocal < local){
              pNode = pNode.pNext;
              currentLocal++;
          }
         newNode.pNext = pNode.pNext;
         pNode.pNext = newNode;
         return true;
      }
  }
  /**
   * 删除节点
   * @param local 要删除节点的位置
   * @return true 删除成功 false 删除失败
   */
  public boolean deleteNode(int local){
      int currentlocal = 0;
      if (isEmpty()){
          System.out.println("删除失败");
          System.out.println("Linklist is NULL");
          return false;
      }else{
          Node<DT> pNode = first;
          while(pNode.pNext != null && currentlocal < local){
              pNode = pNode.pNext;
              currentlocal++;
          }
          pNode.pNext = pNode.pNext.pNext;
          return true;
      }
  }
  public static void main(String[] args) {
      LinkList1<String> stringLinkList = new LinkList1<String>();
       /*stringLinkList.addElement("A");
        stringLinkList.addElement("B");
        stringLinkList.addElement("C");*/
        stringLinkList.printLinklist();
        /*if(stringLinkList.insertNode("D", 3)){
            System.out.println("插入成功");
            stringLinkList.printLinklist();
        }*/
        /*if (stringLinkList.deleteNode(0)){
            System.out.println("删除成功");
            stringLinkList.printLinklist();
        }*/
}
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值