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

    单链表优点:1.不需要预先给出元素个数。

                             2.单链表插入删除时不需要移动数据元素。

    单链表缺点:1.每个节点有指针,空间利用率低。

                             2.单链表不支持随机读取数据。

    Node.java

    package com.sheepmu;
    
    public class Node {
    	  Object value;//结点的数据值
    	  Node next;//下一个结点对象的引用
    	public Node(Object value, Node next) {//一般结点的构造函数
    		super();
    		this.value = value;
    		this.next = next;
    	}
    	public Node(Object value){ //!!!!
    		this(value,null);
    	}
    	public Object getValue() {
    		return value;
    	}
    	public void setValue(Object value) {
    		this.value = value;
    	}
    	public Node getNext() {
    		return next;
    	}
    	public void setNext(Node next) {
    		this.next = next;
    	}
    	
    	public String toString(){
    		return value.toString();
    	}
    }
    

    SingleLinkedList.java

    package com.sheepmu;
    
     public class SingleLinkedList{
    	 private Node head;
    	 private Node current;
    	 private int size;
    	 public SingleLinkedList(){
    		 head=current=new Node(null);//初始时令head和current都是头指针,即下一结点为头结点,即数据域为空。
    		 size=0;
    	 }
    	  
    	public boolean isEmpty(){
    		return size==0;
    	}
    	 
    	public int getSize(){
    		return size;
    	}
    //	public void addToFirst(Object obj){//在表头添加
    //		Node newNode=new Node(obj);
    //   	  	newNode.next=head;
    //   	  	head=newNode;
    //		size++;
    //	}
    	 
    	public void addToLast(Object obj){//在表尾插入结点
    		Node newNode=new Node(obj);
    		current=head.next;
    		while(current.next!=null) 
    			current=current.next;//得到当前尾结点
    		
    		current.next=newNode;
    		 newNode.next=null;
    		size++;	 
    	}
    	
    	public void insert(int i,Object obj){// 单链表  指定位置前插入
    		Node newNode=new Node(obj);
    		Node prve=head;
    		 current=head.next;
    		 int j=0;
    		 while(current!=null&&j<i){
    			 prve=current;
    			 current=current.next;
    			 j++;
    		 }
    	 newNode.next=current;
    	 prve.next=newNode;
    	 size++;
    	}
    	
    	public void delete(int i){//单链表删除指定位置
    		Node prve=head;
    		current=head.next;
    		int j=0;
    		while(current!=null&&j<i){
    			 prve=current;
    			 current=current.next;
    			j++;
    		}
    		prve.next=current.next;
    		size--;
    	}
    	
    	 
    	
    	public Object getValue(int i){//得到结点值
    		current=head.next;
    		int j=0;
    		while(current!=null&&j<i){
    			current=current.next;
    			j++;
    		}
    		return current.value;
    	}
    	
    	public void print(){//遍历打印单链表
    		if(isEmpty())
    			System.out.println("链表为空");
    		else{
    			for(Node current=head.next;current!=null;current=current.next){
    				System.out.print(current.value+" ");
    			}
    		}
    	}
     }

    SingleLinkedListTest.java

    package com.sheepmu;
    /**
     * 建立一个线性表,依次输入元素0,1,2...9;然后在第4个位置插入9 ,然后删除数据元素7。最后依次显示当前线性表元素。
     * 采用单链表实现。
     * @author SheepMu
     *
     */
    public class SingleLinkedListText {
    	public static void main(String[] args) throws Exception{
    		SingleLinkedList singleLinkedList=new SingleLinkedList();
    		System.out.println("初始线性表:");
    		for(int i=0;i<10;i++){
    			singleLinkedList.insert(i, new Integer(i));
    		}
    		singleLinkedList.print();
    		
    		System.out.println("在位置4插入元素9后的线性表:");
    		singleLinkedList.insert(4, new Integer(9));
     
    		singleLinkedList.print();
    		
    //		System.out.println("表头插入元素0后的线性表:");
    //		singleLinkedList.addToFirst(new Integer(0));
    //		singleLinkedList.print();
    		
    		System.out.println("表尾插入元素0后的线性表:");
    		singleLinkedList.addToLast(new Integer(0));
    		singleLinkedList.print();
    		
    		System.out.println("删除第5个元素后的线性表:");
    		singleLinkedList.delete(5);
    		singleLinkedList.print();
    		
    		 
    	}	
    	
    }
    


  • 相关阅读:
    第七周作业
    第六周作业
    第四周作业
    第三周作业
    第二周作业
    第一周作业
    第0次作业
    第四次作业
    第三次作业
    第二次作业
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3766911.html
Copyright © 2011-2022 走看看