欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现线性表----单向链表

程序员文章站 2022-09-14 09:40:24
链表的简单实现(只完成部分操作),这里同时为了复习java中的相关内容,简单粗暴直接上代码:class PosException extends Exception{ public PosException(){ } public PosException(String s){ super(s); }}interface AList{ public boolean isEmpty(); public int length...

链表的简单实现(只完成部分操作),这里同时为了复习java中的相关内容,简单粗暴直接上代码:

class PosException extends Exception{
    public PosException(){
    }
    public PosException(String s){
        super(s);
    }
}
interface AList<E>{
    public boolean isEmpty();
    public int length();
    public void Clear();
    public void InsertHead(E e);
    public void InsertTail(E e);
    public void Insert(E e, int index) throws PosException;
    public void Remove(int index) throws PosException;
    public int get(E e);
    public void Display();
}
class LinkList<E> implements AList<E>{
    private class Node{
        private E data;
        private Node next;
        public Node(E data, Node next){
            this.data = data;
            this.next = next;
        }
        public Node(E data){
            this(data, null);//调用本类中的有参构造器
        }
    }
    private Node head;
    private int len;
    public LinkList(){
        this.len = 0;
        this.head = null;
    }
    public boolean isEmpty(){
        return head == null;
    }
    public int length(){
        return len;
    }
    public void Clear(){
        this.len = 0;
        this.head.next = null;
        this.head = null;
    }
    public void InsertHead(E e){
        Node p = new Node(e);
        p.next = this.head;
        this.head = p;
        this.len++;
    }
    public void InsertTail(E e) {
        Node p = new Node(e);
        if(this.head == null){
            head = p;
            len++;
        }
        else {
            Node s = this.head;
            while (s.next != null)
                s = s.next;
            s.next = p;
            len++;
        }
    }
    public void Insert(E e, int index) throws PosException{
        if(index < 0 || index > len)
            throw new PosException("插入位置错误!");
        if(index == 0)
            InsertHead(e);
        Node p = this.head;
        for(int i = 0; i < index - 1; i++)
            p = p.next;
        Node s = new Node(e);
        s.next = p.next;
        p.next = s;
        len++;
    }
    public void Remove(int index) throws PosException{
        if(index < 0 || index > len)
            throw new PosException("删除位置错误!");
        Node p = this.head;
        for(int i = 0; i < index - 1; i++)
            p = p.next;
        p.next = p.next.next;
        len--;
    }
    public int get(E e){
        Node p = this.head;
        int index = 0;
        while(p != null){
            if(p.data == e)
                return index;
            p = p.next;
            index++;
        }
        return -1;
    }
    public void Display(){
        Node p = this.head;
        while(p!=null){
            System.out.print(p.data + " ");
            p = p.next;
        }
        System.out.println();
    }
}
public class LinkListAchieve {
    public static void main(String[] args) {
        LinkList<Integer> list = new LinkList();
        System.out.println("链表是否为空:" + list.isEmpty());
        System.out.println("链表长度:" + list.length());
        for(int i = 0; i < 5; i++)
            list.InsertHead(i + 1);
        System.out.println("头插法建立链表!");
        list.Display();
        System.out.println("链表是否为空:" + list.isEmpty());
        System.out.println("链表长度:" + list.length());
        list.Clear();
        System.out.println("链表清空!");
        System.out.println("链表是否为空:" + list.isEmpty());
        System.out.println("链表长度:" + list.length());
        for(int i = 0; i < 5; i++)
            list.InsertTail(i + 1);
        System.out.println("尾插法建立链表!");
        list.Display();
        System.out.println("链表是否为空:" + list.isEmpty());
        System.out.println("链表长度:" + list.length());
        System.out.println("在第一个元素和第二个元素之间插入元素10!");
        try {
            list.Insert(10, 1);
        }
        catch (PosException e){
            e.printStackTrace();
            System.exit(-1);
        }
        System.out.println("链表是否为空:" + list.isEmpty());
        System.out.println("链表长度:" + list.length());
        list.Display();
        System.out.println("第元素3的索引是:" + list.get(3));
    }
}

输出如下:

链表是否为空:true
链表长度:0
头插法建立链表!
5 4 3 2 1 
链表是否为空:false
链表长度:5
链表清空!
链表是否为空:true
链表长度:0
尾插法建立链表!
1 2 3 4 5 
链表是否为空:false
链表长度:5
在第一个元素和第二个元素之间插入元素10!
链表是否为空:false
链表长度:6
1 10 2 3 4 5 
第元素3的索引是:3

本文地址:https://blog.csdn.net/m0_45240384/article/details/107257730