zoukankan      html  css  js  c++  java
  • Java关于链表的增加、删除、获取长度、打印数值的实现

    package com.shb.java;
    
    public class Demo8 {
        public Node headNode = null;
        /**
         * @param args
         * @date 2016-9-28
         * @author shaobn
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Demo8 linkedList = new Demo8();
            linkedList.addNode(1);
            linkedList.addNode(2);
            linkedList.addNode(34);
            linkedList.addNode(14);
    //        System.out.println(linkedList.getLength());
            linkedList.deleteNode(1);
            linkedList.printNode();
        }
        /**
         * 插入到链表中
         * @param data
         * @date 2016-9-28
         * @author shaobn
         */
        public void addNode(int data){
            Node node = new Node(data);
            if(headNode==null){
                headNode = node;
                return;
            }
            Node tmpNode = headNode;
            if(tmpNode.next==null){
                tmpNode.next = node;
                return;
            }
            while(tmpNode.next!=null){
                tmpNode = tmpNode.next;
            }
            tmpNode.next = node;
        }
        /**
         * 打印节点的值
         * 
         * @date 2016-9-28
         * @author shaobn
         */
        public void printNode(){
            Node tmpNode = headNode;
            while(tmpNode!=null){
                System.out.println(tmpNode.data);
                tmpNode = tmpNode.next;
            }
        }
        /**
         * 删除节点
         * 
         * @date 2016-9-28
         * @author shaobn
         */
        public void deleteNode(int index){
            if(index<=0||index>this.getLength()){
                throw new RuntimeException("索引选取不合适");
            }
            int count = 1;
            Node tmpNode = headNode;
            while(tmpNode.next!=null){
                count++;
                if(index==1){
                    headNode = tmpNode.next;
                    break;
                }
                if(count==index){
                    tmpNode.next = tmpNode.next.next;                
                }
                tmpNode = tmpNode.next;
                continue;
            }
            
            
            
        }
        /**
         * 获得本链表的长度
         * @return
         * @date 2016-9-28
         * @author shaobn
         */
        public int getLength(){
            int length = 0;
            Node tmpNode = this.headNode;
            while(tmpNode!=null){
                length++;
                tmpNode = tmpNode.next;
            }
            return length;
            
        }
    }
    /**
     * 节点的类
     * @author shaobn
     * @date 2016-9-28
     * @package_name com.shb.java
     */
    class Node{
        public int data;
        public Node next;
        public Node(int data){
            this.data = data;
        }
        
        
        
    }
  • 相关阅读:
    PHP中include和require的区别详解
    CCNP
    PHP内存缓存功能memcached
    NumPy数据的归一化
    Python计算美国总统的身高并实现数据可视化
    NumPy实现数据的聚合,计算最大值,最小值
    Python通用函数实现数组计算
    Python当中的array数组对象
    人工智能之神经科学——探索脑:高尔基染色
    Android开发:getSupportFragmentManager()不可用
  • 原文地址:https://www.cnblogs.com/assassin666/p/5916085.html
Copyright © 2011-2022 走看看