• 【Java】 大话数据结构(2) 线性表之单链表


     

    本文根据《大话数据结构》一书,实现了Java版的单链表

    每个结点中只包含一个指针域的链表,称为单链表

    单链表的结构如图所示:

    单链表与顺序存储结构的对比:

    实现程序:

    package LinkList;
    /**
     * 说明:
     * 1.《大话数据结构》中没有线性表的长度,但提到可以存储于头节点的数据域中。
     *   本程序的线性表长度存放于count变量中,线性表长度可以使程序比较方便。
     * 2.程序中,第i个位置代表第i个结点,头结点属于第0个结点
     * 3.因为链表为泛型,整表创建采用整型(随机整数做元素),所以有出现一些类型转换
     * 4.Java程序的方法一般以小写开头,但为和书上一致,程序中方法采用了大写开头。
     * 
     * 注意点:
     * 1.count在增删元素时要加一或减一千万别忘了
     * 2.清空线性表要每个元素都null
     * 
     * @author Yongh
     *
     */
    public class LinkList<E> {
    	private Node<E> head;  //头结点
    	private int count;  //线性表长度(《大话》存储于head.data中)	
    	
    	/**
    	 * 结点
    	 */
    	private class Node<E>{
    		E data;
    		Node<E> next;
    		public Node(E data,Node<E> next){
    			this.data=data;
    			this.next=next;
    		}
    	}
    	
    	/**
    	 * 线性表的初始化
    	 */
    	public LinkList(){
    		head=new Node<E>(null, null);  //不是head=null;
    		count=0;
    	}
    	
    	/**
    	 * 判断线性表是否为空
    	 */
    	public boolean IsEmpty() {
    		if(count==0) {
    			System.out.println("表为空!");
    			return true;
    		}else {
    			System.out.println("表不为空!");
    			return false;
    		}
    		//return count==0;
    	}
    	
    	/**
    	 * 清空线性表(自己编写的)
    	 */
    	public void ClearList() {
    		Node<E> node;
    		while(count!=0) {
    			node=head.next;
    			head.next=node.next;
    			node=null;
    			count--;
    		}
    		System.out.println("线性表已清空!");
    	}
    	
    	/**
    	 * 清空线性表(书中改写)
    	 */
    	public void ClearList2() {
    		Node<E> q,p;
    		q=head.next;
    		while(q!=null) {
    			p=q.next;
    			q=null;
    			q=p;
    			count--;
    		}
    		head.next=null;
    		System.out.println("线性表已清空!");
    	}
    	
    	
    	/**
    	 * 获取第i个结点(包括第0个结点,头结点)
    	 * 获取结点值只需要GetNode(i).data即可,不再写方法了
    	 */
    	public Node<E> GetNode(int i) {
    		if(i<0||i>count) {		
    			throw new RuntimeException("元素位置错误!");
    		}else if (i==0) {
    			return head;   
    		}else {
    			Node<E> node=head.next;
    			for(int k=1;k<i;k++) {
    				node=node.next;
    			}
    			return node;	
    		}
    	}
    	
    	/**
    	 * 获取第i个结点的数据(包括头结点)
    	 */
    	public E GetData(int i) {
    		return GetNode(i).data;
    	}
    	
    	/**
    	 * 查找元素,0代表查找失败
    	 */
    	public int LocateElem(E e) {
    		Node<E> node;
    		node=head.next;
    		if(node.data==e)
    			return 1;
    		for(int k=1;k<count;k++) {
    			node=node.next;
    			if(node.data==e)
    				return k+1;							
    		}
    		System.out.println("查找失败!");
    		return 0;
    	}
    	
    	/**
    	 * 第i个位置插入新的元素
    	 */
    	public void ListInsert(int i,E e) {
    		if(i<1||i>count+1) {
    			throw new RuntimeException("插入位置错误!");
    		}else {
    			Node<E> newNode=new Node<E>(e,null);
    			newNode.next=GetNode(i-1).next;  //因为GetNode()方法中包含了获取头结点,所以不需单独判断了
    			GetNode(i-1).next=newNode;
    			count++;
    			System.out.println("插入成功!");
    		}
    	}
    	
    	/**
    	 * 删除第i个位置元素,并返回其值
    	 */
    	public E ListDelete(int i) {
    		if(i<1||i>count)
    			throw new RuntimeException("删除位置错误!");		
    		Node<E> node=GetNode(i);
    		E e=node.data;
    		GetNode(i-1).next=node.next;
    		node=null;
    		count--;
    		System.out.println("删除成功!");
    		return e;		
    	}
    	
    	/**
    	 * 获取线性表长度
    	 */
    	public int ListLength() {
    		return count;
    	}
    	
    	/**
    	 * 整表创建,头插法
    	 */
    	public LinkList<Integer> CreateListHead(int n){
    		LinkList<Integer> list1=new LinkList<Integer>();
    		Node<Integer> node,lastNode;
    		for(int i=0;i<n;i++) {
        		int data=(int)(Math.random()*100);  //生成100以内的随机数
        		node=new Node<Integer>(data, null);
        		node.next=(LinkList<E>.Node<Integer>) list1.head.next;
        		list1.head.next=(LinkList<Integer>.Node<Integer>) node;
        		list1.count++;
        	}
    		return list1;
    	}
    	
    	
    	/**
    	 * 整表创建,尾插法
    	 */
    	public LinkList<Integer> CreateListTail(int n){
    		LinkList<Integer> list2=new LinkList<Integer>();
    		Node<Integer> node,lastNode;
    		lastNode=(LinkList<E>.Node<Integer>) list2.head;		
    		for(int i=0;i<n;i++) {
    			int data=(int)(Math.random()*100);  //生成100以内的随机数
    			node=new Node<Integer>(data, null);
    			lastNode.next=node;
    			lastNode=node;
    			list2.count++;  		   		
    		}
    		return list2;
        }
    }
    

      

    测试代码:

      基本数据类型和引用类型各写了一个测试代码。

    package LinkList;
    
    /**
     * 基本数据类型测试
     */
    public class LinkListTest1 {
    	public static void main(String[] args) {
    		LinkList<Integer> nums = new LinkList<Integer>();
    		nums.IsEmpty();
    		System.out.println("——————————插入1到5,并读取内容——————————");
    		for (int i = 1; i <= 5; i++)
    			nums.ListInsert(i, 2*i);
    		nums.IsEmpty();
    		int num;
    		for (int i = 1; i <= 5; i++) {
    			num = nums.GetData(i);
    			System.out.println("第" + i + "个位置的值为:" + num);
    		}
    		System.out.println("——————————查找0、2、10是否在表中——————————");
    		System.out.print("0的位置:");
    		System.out.println(nums.LocateElem(0));
    		System.out.print("2的位置:");
    		System.out.println(nums.LocateElem(2));
    		System.out.print("10的位置:");
    		System.out.println(nums.LocateElem(10));
    		System.out.println("——————————删除2、10——————————");
    		num = nums.ListDelete(1);
    		System.out.println("已删除:" + num);
    		num = nums.ListDelete(4);
    		System.out.println("已删除:" + num);
    		System.out.println("当前表长:" + nums.ListLength());
    		for (int i = 1; i <= nums.ListLength(); i++) {
    			num = nums.GetData(i);
    			System.out.println("第" + i + "个位置的值为:" + num);
    		}
    		nums.ClearList();
    		nums.IsEmpty();
    	}
    }
    

      

    表为空!
    ——————————插入1到5,并读取内容——————————
    插入成功!
    插入成功!
    插入成功!
    插入成功!
    插入成功!
    表不为空!
    第1个位置的值为:2
    第2个位置的值为:4
    第3个位置的值为:6
    第4个位置的值为:8
    第5个位置的值为:10
    ——————————查找0、2、10是否在表中——————————
    0的位置:查找失败!
    0
    2的位置:1
    10的位置:5
    ——————————删除2、10——————————
    删除成功!
    已删除:2
    删除成功!
    已删除:10
    当前表长:3
    第1个位置的值为:4
    第2个位置的值为:6
    第3个位置的值为:8
    线性表已清空!
    表为空!
    LinkListTest1
    package LinkList;
    
    public class LinkListTest2 {
        public static void main(String[] args) {
            LinkList<Student> students =new LinkList<Student>();
            students .IsEmpty();
            System.out.println("——————————插入1到5,并读取内容——————————");
            Student[] stus= {new Student("小A",11),new Student("小B",12),new Student("小C",13),
                    new Student("小D",14),new Student("小E",151)};
            for(int i=1;i<=5;i++)
                students.ListInsert(i, stus[i-1]);
            students .IsEmpty();
            Student stu;
            for(int i=1;i<=5;i++) {
                stu=students .GetData(i);
                System.out.println("第"+i+"个位置为:"+stu.name);
            }
            System.out.println("——————————查找小A、小E、小龙是否在表中——————————");
            System.out.print("小A的位置:");
            stu=stus[0];
            System.out.println(students .LocateElem(stu));     
            System.out.print("小E的位置:");
            stu=stus[4];
            System.out.println(students .LocateElem(stu)); 
            System.out.print("小龙的位置:");
            stu=new Student("小龙",11);
            System.out.println(students .LocateElem(stu)); 
            System.out.println("——————————删除小E、小B——————————");
            stu=students .ListDelete(2);
            System.out.println("已删除:"+stu.name);
            stu=students .ListDelete(4);
            System.out.println("已删除:"+stu.name);
            System.out.println("当前表长:"+students .ListLength());
            for(int i=1;i<=students .ListLength();i++) {
                stu=students .GetData(i);
                System.out.println("第"+i+"个位置为:"+stu.name);
            }
            students .ClearList();
            students .IsEmpty();       
        }
    }
     
    class Student{
        public Student(String name, int age) {
            this.name=name;
            this.age=age;
        }
        String name;
        int age;
    }
    

      

    表为空!
    ——————————插入1到5,并读取内容——————————
    插入成功!
    插入成功!
    插入成功!
    插入成功!
    插入成功!
    表不为空!
    第1个位置为:小A
    第2个位置为:小B
    第3个位置为:小C
    第4个位置为:小D
    第5个位置为:小E
    ——————————查找小A、小E、小龙是否在表中——————————
    小A的位置:1
    小E的位置:5
    小龙的位置:查找失败!
    0
    ——————————删除小E、小B——————————
    删除成功!
    已删除:小B
    删除成功!
    已删除:小E
    当前表长:3
    第1个位置为:小A
    第2个位置为:小C
    第3个位置为:小D
    线性表已清空!
    表为空!
    LinkListTest2

     【Java】 剑指offer(17) 在O(1)时间删除链表结点

  • 相关阅读:
    C#2008与.NET 3.5 高级程序设计读书笔记(6)继承和多态
    C#2008与.NET 3.5 高级程序设计读书笔记(5)定义封装的类类型
    C#2008与.NET 3.5 高级程序设计读书笔记(10) 集合与泛型
    C#2008与.NET 3.5 高级程序设计读书笔记(8)对象的生命周期
    Jquery1.4.1 学习
    JavaScript高级程序设计(第2版) 之 JavaScript的传值方式
    解密宝典——十招教你学会软件破解(转载收藏)
    使用sql server中的全文索引( 转 )
    JavaScript高级程序设计(第2版) 之 JavaScript变量作用域
    .NET读取Excel数据为null的解决办法
  • 原文地址:https://www.cnblogs.com/yongh/p/9125113.html
Copyright © 2020-2023  润新知