【算法数据结构Java实现】Java实现单链表

简介: 1.背景          单链表是最基本的数据结构,仔细看了很久终于搞明白了,差不每个部分,每个链都是node的一个对象。需要两个参数定位:一个是index,表示对象的方位。另一个是node的对象。2.代码node类public class Node { protected Node next; protected int data; public Node(in

1.背景

          单链表是最基本的数据结构,仔细看了很久终于搞明白了,差不每个部分,每个链都是node的一个对象。需要两个参数定位:一个是index,表示对象的方位。另一个是node的对象。



2.代码


node类
public class Node {
	 protected Node next;
	 protected int data;
	 public Node(int data){
		 this.data=data;
	 }
	 public void display(){
     System.out.print(data+"");
   }
}

arraylist类
public class myArrayList {
      public Node first;//定义头结点
      private int pos=0;//节点位置
      public myArrayList(){
    	  // this.first=null;
      }
      //插入一个头结点
      public void addFirstNode(int data){
    	    Node node=new Node(data);
    	    node.next=first;
    	    first=node;
      }
      //删除头结点
      public Node deleteFirstNode(){
    	    Node tempNode=first;
    	    first=tempNode.next;
    	    return tempNode;
      }
      // 在任意位置插入节点 在index的后面插入  
      public void add(int index, int data) {  
           Node node = new Node(data);  
           Node current = first;  
           Node previous = first;  
            while ( pos != index) {  
               previous = current;  
               current = current. next;  
                pos++;  
           }  
           node. next = current;  
           previous. next = node;  
            pos = 0;  
      }  
      // 删除任意位置的节点  
      public Node deleteByPos( int index) {  
           Node current = first;  
           Node previous = first;  
            while ( pos != index) {  
                pos++;  
               previous = current;  
               current = current. next;  
           }  
            if(current == first) {  
                first = first. next;  
           } else {  
                pos = 0;  
               previous. next = current. next;  
           }  
            return current;  
      }     
      public void displayAllNodes() {  
          Node current = first;  
           while (current != null) {  
              current.display();
              System.out.println();
              current = current. next;  
          }            
     }  
      
}


实现的main函数:
public class Main {
   public static void main(String args[]){
	   myArrayList ls=new myArrayList();   	  
	   ls.addFirstNode(15);	   
	   ls.addFirstNode(16);
	   ls.add(1, 144);
	   ls.add(2, 44);
	   ls.deleteByPos(1);
	  ls.displayAllNodes();	   
   }
}



实现结果:

16

44

15


package LinkedList;   
  
/**  
 * <p><strong>我的Java单链表练习</strong></p>  
 * <p>单链表提供了在列表头的高效插入和删除操作,不过在单链表的末尾的插入操作效率很低.</p>  
 * <p>单链表指针域保存着下一节点的引用,尾结点的指针域等于null</p>  
 * @author baby69yy2000  
 */  
public class SingleLinkedList<T> {   
       
    /**  
     * 结点类  
     */  
    private static class Node<T> {   
        T nodeValue; // 数据域   
        Node<T> next; // 指针域保存着下一节点的引用   
           
        Node(T nodeValue, Node<T> next) {   
            this.nodeValue = nodeValue;   
            this.next = next;   
        }   
           
        Node(T nodeValue) {   
            this(nodeValue, null);   
        }   
    }   
  
    // 下面是SingleLinkedList类的数据成员和方法   
    private Node<T> head, tail;   
       
    public SingleLinkedList() {   
        head = tail = null;   
    }   
       
    /**  
     * 判断链表是否为空  
     */  
    public boolean isEmpty() {   
        return head == null;   
    }   
       
    /**  
     * 创建头指针,该方法只用一次!  
     */  
    public void addToHead(T item) {   
        head = new Node<T>(item);   
        if(tail == null) tail = head;   
    }   
       
    /**  
     * 添加尾指针,该方法使用多次  
     */  
    public void addToTail(T item) {   
        if (!isEmpty()) { // 若链表非空那么将尾指针的next初使化为一个新的元素   
            tail.next = new Node<T>(item); // 然后将尾指针指向现在它自己的下一个元素   
            tail = tail.next;   
        } else { // 如果为空则创建一个新的!并将头尾同时指向它   
            head = tail = new Node<T>(item);         
        }   
    }   
       
    /**  
     * 打印列表  
     */  
    public void printList() {   
        if (isEmpty()) {   
            System.out.println("null");   
        } else {   
            for(Node<T> p = head; p != null; p = p.next)   
                System.out.println(p.nodeValue);   
        }   
    }   
       
    /**  
     * 在表头插入结点,效率非常高  
     */  
    public void addFirst(T item) {   
        Node<T> newNode = new Node<T>(item);   
        newNode.next = head;   
        head = newNode;   
    }   
       
    /**  
     * 在表尾插入结点,效率很低  
     */  
    public void addLast(T item) {   
        Node<T> newNode = new Node<T>(item);   
        Node<T> p = head;   
        while (p.next != null) p = p.next;   
        p.next = newNode;   
        newNode.next = null;   
    }   
       
    /**  
     * 在表头删除结点,效率非常高  
     */  
    public void removeFirst() {   
        if (!isEmpty()) head = head.next;   
        else System.out.println("The list have been emptied!");   
    }   
       
    /**  
     * 在表尾删除结点,效率很低  
     */  
    public void removeLast() {   
        Node<T> prev = null, curr = head;   
        while(curr.next != null) {   
            prev = curr;   
            curr = curr.next;   
            if(curr.next == null) prev.next = null;   
        }   
    }   
       
    /**  
     * <p>插入一个新结点</p>  
     * <ul>插入操作可能有四种情况:  
     * <li>①表为空, 返回false</li>  
     * <li>②表非空,指定的数据不存在</li>  
     * <li>③指定的数据是表的第一个元素</li>  
     * <li>④指定的数据在表的中间</li></ul>  
     * @param appointedItem 指定的nodeValue  
     * @param item 要插入的结点  
     * @return 成功插入返回true;  
     */  
    public boolean insert(T appointedItem, T item) {   
        Node<T>  prev = head, curr = head.next, newNode;   
        newNode = new Node<T>(item);   
        if(!isEmpty()) {   
            while((curr != null) && (!appointedItem.equals(curr.nodeValue))) { //两个判断条件不能换   
                prev = curr;   
                curr = curr.next;   
            }   
            newNode.next = curr; //②③④   
            prev.next = newNode;   
            return true;    
        }   
        return false; //①   
    }   
       
    /**  
     * <p>移除此列表中首次出现的指定元素</p>  
     * <ul>删除操作可能出现的情况:  
     * <li>①prev为空,这意味着curr为head. head = curr.next; --> removeFirst();</li>  
     * <li>②匹配出现在列表中的某个中间位置,此时执行的操作是 --> prev.next = curr.next;,</li></ul>  
     * <p>在列表中定位某个结点需要两个引用:一个对前一结点(prev左)的引用以及一个对当前结点(curr右)的引用.</p>  
     * prev = curr;  
     * curr = curr.next;  
     */  
    public void remove(T item) {   
        Node<T> curr = head, prev = null;   
        boolean found = false;   
        while (curr != null && !found) {   
            if (item.equals(curr.nodeValue)) {   
                if(prev == null) removeFirst();   
                else prev.next = curr.next;   
                found = true;   
            } else {   
                prev = curr;   
                curr = curr.next;   
            }   
        }   
    }   
       
    /**  
     * 返回此列表中首次出现的指定元素的索引,如果列表中不包含此元素,则返回 -1.  
     */  
    public int indexOf(T item) {   
        int index = 0;   
        Node<T> p;   
        for(p = head; p != null; p = p.next) {   
            if(item.equals(p.nodeValue))   
                return index;   
            index++;   
                   
        }   
        return -1;   
    }   
       
    /**  
     * 如果此列表包含指定元素,则返回 true。  
     */  
     public boolean contains(T item) {   
         return indexOf(item) != -1;   
     }   
       
    public static void main(String[] args) {   
        SingleLinkedList<String> t = new SingleLinkedList<String>();   
        t.addToHead("A");   
        //t.addFirst("addFirst");   
        t.addToTail("B");   
        t.addToTail("C");   
        System.out.println(t.indexOf("C")); // 2   
        System.out.println(t.contains("A")); // true   
        //t.addLast("addLast");   
        //t.removeLast();   
        //t.insert("B", "insert");   
        //t.removeFirst();   
        //t.remove("B"); // A C   
        t.printList(); // A B C   
           
    }   
  
}  



/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/


目录
相关文章
|
17天前
|
存储 算法 关系型数据库
深入理解InnoDB索引数据结构和算法
1. **索引定义**:索引是提升查询速度的有序数据结构,帮助数据库系统快速找到数据。 2. **索引类型**:包括普通索引、唯一索引、主键索引、空间索引和全文索引,每种有特定应用场景。 3. **数据结构**:InnoDB使用B+树作为索引结构,确保所有节点按顺序排列,降低查询时的磁盘I/O。 4. **B+树特性**:所有数据都在叶子节点,非叶子节点仅存储索引,提供高效范围查询。 5. **索引优势**:通过减少查找数据所需的磁盘I/O次数,显著提高查询性能。 **总结:**InnoDB索引通过B+树结构,优化了数据访问,使得查询速度快,尤其适合大数据量的场景。
26 0
深入理解InnoDB索引数据结构和算法
|
9天前
|
存储 算法 索引
【算法与数据结构】队列的实现详解
【算法与数据结构】队列的实现详解
|
12天前
|
算法
【算法与数据结构】二叉树(前中后)序遍历2
【算法与数据结构】二叉树(前中后)序遍历
|
1天前
|
存储 机器学习/深度学习 算法
上机实验三 图的最小生成树算法设计 西安石油大学数据结构
上机实验三 图的最小生成树算法设计 西安石油大学数据结构
8 1
|
3天前
|
Java API
编码的奇迹:Java 21引入有序集合,数据结构再进化
编码的奇迹:Java 21引入有序集合,数据结构再进化
13 0
|
9天前
|
算法 索引
【算法与数据结构】深入二叉树实现超详解(全源码优化)
【算法与数据结构】深入二叉树实现超详解(全源码优化)
|
9天前
|
存储 算法
【算法与数据结构】深入解析二叉树(二)之堆结构实现
【算法与数据结构】深入解析二叉树(二)之堆结构实现
|
12天前
|
算法 C语言
【算法与数据结构】 C语言实现单链表队列详解2
【算法与数据结构】 C语言实现单链表队列详解
|
12天前
|
存储 算法 C语言
【算法与数据结构】 C语言实现单链表队列详解1
【算法与数据结构】 C语言实现单链表队列详解
|
14天前
|
搜索推荐 Java
Java排序算法
Java排序算法
17 0