zoukankan      html  css  js  c++  java
  • 用java实现单链表

    首先创建接口声明:声明单链表的public方法,接口声明如下:

    public interface IMyList<T>{
        public boolean add(T entry);

        public boolean add(int aPosition,T entry);
        public T remove(int aPosition);
        public boolean contains(T entry);
        public boolean replace(int aPosition,T entry);
        public int getLength();
        public boolean isFull();
        public boolean isEmpty();
    }

    然后定义链表使用的节点类型,Node类,代码如下:

    class Node<T>{
        private T data;
        private Node<T> next;
        public Node(){}
        public Node(T anEntry){
            data=anEntry;
        }
        public T getData(){
            return data;
        }
        public Node<T> getNext(){
            return next;
        }
        public void setData(T entry){
            data=entry;
        }
        public void setNext(Node<T> node){
            next=node;
        }
    }

    最后实现单链表类,并进行插入测试,代码如下

    public class MyList<T> implements IMyList<T>{
        private int length;
        private Node<T> firstNode;
        public MyList(){
            clear();       
        }
        private final void clear(){
            length=0;
        }
        public boolean add(T entry){
            Node<T> newNode=new Node<T>(entry);
            if(!isEmpty()){
                Node<T> lastNode=getNodeAt(length-1);
                lastNode.setNext(newNode);
                length++;
                return true;
            }else{
                firstNode=newNode;
                length++;
                return true;
            }
        }
        public boolean add(int aPosition,T entry){
            Node<T> newNode=new Node<T>(entry);
            if(aPosition==0){
                newNode.setNext(firstNode);
                //firstNode.setNext(newNode);
                firstNode=newNode;
                length++;
                return true;
            }
            if(!isEmpty()&&aPosition<length){
                Node<T> NodeBefor=getNodeAt(aPosition-1);
                Node<T> NodeAfter=NodeBefor.getNext();
                NodeBefor.setNext(newNode);
                newNode.setNext(NodeAfter);
                length++;
                return true;
            }
            return false;
        }
        public T remove(int aPosition){
            Node<T> currentNode=firstNode;
            if(aPosition==0){
                currentNode=firstNode;
                firstNode=firstNode.getNext();
                //return firstNode.getData();
            }else{
                int index=1;
                Node<T> beforNode=firstNode;
                while(index<aPosition-1&&beforNode!=null){
                    beforNode=beforNode.getNext();
                    index++;
                }
                currentNode=beforNode.getNext();
                beforNode.setNext(currentNode.getNext());
            }
            length--;
            return currentNode.getData();
        }
        public boolean contains(T entry){
            Node<T> currentNode=firstNode;
            int index=0;
            boolean find=false;
            while(index<length&&!find&&currentNode!=null){
                if(currentNode.getData().equals(entry)){
                    find=true;
                }
            }
            return find;
        }
        public boolean replace(int aPosition,T entry){
            boolean done=false;
            int index=0;
            Node<T> currentNode=firstNode;
            while(!done&&index<length&&currentNode!=null){
                if(currentNode.getData().equals(entry))
                    done=true;
                currentNode=currentNode.getNext();
                index++;
            }
            return done;
        }
        public int getLength(){
            return this.length;
        }
        private Node<T> getNodeAt(int aPosition){
            Node<T> currentNode=firstNode;
            for(int index=0;index<aPosition;++index){
                currentNode=currentNode.getNext();
            }
            return currentNode;
        }
        public boolean isEmpty(){
            return (length==0);
        }
        public boolean isFull(){
            return false;
        }
        public void display(){
            Node<T> currentNode=firstNode;
            int index=0;
            while(currentNode!=null&&index<length){
                System.out.println(currentNode.getData());
                currentNode=currentNode.getNext();
                index++;
            }
        }
        public static void main(String[] args){
            MyList<String> lst=new MyList<String>();
            lst.add("sunzhenxing");
            lst.add(0, "tongji");
            lst.add("sunhailong");
            lst.display();
            System.out.println("=============");
            lst.remove(0);
            lst.display();
            System.out.println("=============");
            lst.remove(0);
            lst.display();
        }
    }

  • 相关阅读:
    jQuery拖动调整表格列宽度-resizableColumns
    图像切割之(一)概述
    内存泄漏以及常见的解决方法
    中国大概能用的NTPserver地址
    SWFUpload简单使用样例 Java版(JSP)
    XML是什么,它能够做什么?——写给XML入门者
    MfC 进度条控件
    IP地址的分类——a,b,c 类是怎样划分的
    Python的包管理工具Pip
    Chapter 1 Securing Your Server and Network(6):为SQL Server訪问配置防火墙
  • 原文地址:https://www.cnblogs.com/sunzhenxing19860608/p/1584112.html
Copyright © 2011-2022 走看看