数据结构--单链表,环形链表,双向链表

单链表

import java.util.ArrayList;

/**
 * @ClassName LinkList
 * @Description 单链表基本操作增、删、改、查
 * @Author lzq
 * @Date 2018/11/21 20:25
 * @Version 1.0
 **/
public class LinkList<T> {
    public class Entry {
        T data;
        Entry next;

        //头节点
        public Entry() {
            this.data = null;
            this.next = null;
        }
        //数据节点
        public Entry(T val) {
            this.data = val;
            this.next = null;
        }
    }

    public Entry head;  //头节点的引用
    private Entry entry; //声明一个数据节点
    private Entry count; //一个移动的标记指针

    public LinkList() {
        this.head = new Entry();
    }

    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty() {
        return head.next == null;
    }
/**
     * 头插法,从链表头部进行插入
     * @param val
     */
    public void head_insert(T val) {
        this.entry = new Entry(val);
        entry.next = head.next;
        head.next = entry;
    }

    /**
     * 尾插法,从链表尾部插入
     * @param val
     */
    public void tail_insert(T val) {
        count = this.head;
        while(count.next != null) {
            count = count.next;
        }
        entry = new Entry(val);
        count.next = entry;
    }

    /**
     * 指定位置插入元素
     * @param pos
     * @param val
     * @return
     */
    public boolean insert(int pos,T val) {
        if(pos < 0 || pos >= get_length()) {
            return false;
        }
        entry = new Entry(val);
        count = this.head.next;
        Entry cur = this.head;
        int i = 0;
        while(i != pos) {
            i++;
            count = count.next;
            cur = cur.next;
        }
        cur.next = entry;
        entry.next = count;
        return true;
    }
 /**
     * 删除指定元素(删除第一次出现的该元素)
     * @param val
     * @return
     */
    public boolean delete(T val) {
        if(isEmpty()) {
            return false;
        }
        count = this.head;
        while(count.next != null) {
            if (count.next.data == val) {
                count.next = count.next.next;
                return true;
            }
            count = count.next;
        }
        return false;
    }

    /**
     * 删除所有的指定元素
     * @param val
     * @return
     */
    public boolean delete1(T val) {
        if(isEmpty()) {
            return false;
        }
        count = this.head;
        while(count.next != null) {
            if (count.next.data == val) {
                count.next = count.next.next;
                continue;
            }
            count = count.next;
        }
        return true;
    }

    /**
     * 删除指定位置元素
     * @param pos
     * @return
     */
    public boolean remove(int pos) {
        if(pos < 0 || pos >= get_length() || isEmpty()) {
            return false;
        }
        count = this.head;
        int i = 0;
        while(i != pos) {
            i++;
            count = count.next;
        }
        count.next = count.next.next;
        return true;
    }
 /**
     * 修改指定位置元素
     * @param pos
     * @param val
     * @return
     */
    public boolean setIndex(int pos,T val){
        if(pos < 0 || pos >= get_length()) {
            return false;
        }
        count = this.head;
        int i = 0;
        while(i != pos) {
            i++;
            count = count.next;
        }
        count.next.data = val;
        return true;
    }

    /**
     * 替换所有指定元素
     * @param x
     * @param val
     * @return
     */
    public boolean setAll(T x,T val) {
        count = this.head;
        while(count.next != null) {
            if (count.next.data == x) {
                count.next.data = val;
            }
            count = count.next;
        }
        return true;
    }
 /**
     * 得到链表长度
     * @return
     */
    public int get_length() {
        count = head;
        int length = 0;
        while(count.next != null) {
            count = count.next;
            length++;
        }
        return length;
    }

    /**
     * 返回指定元素的下标(默认找到的是第一个)
     * @param val
     * @return
     */
    public int getIndex(T val) {
        count = this.head;
        int i = 0;
        while(count.next != null) {
            if (count.next.data == val) {
                return i;
            }
            i++;
            count = count.next;
        }
        return -1;  //没找到
    }

    /**
     * 返回元素的所有下标
     * @param val
     * @return
     */
    public Integer[] getAll(T val) {
        count = this.head;
        int i = 0;
        boolean cur = false;  //一个标记,判断该链表是否有该元素
        ArrayList<Integer> list = new ArrayList<>();
        while(count.next != null) {
            if (count.next.data == val) {
                cur = true;
                list.add(i);
            }
            i++;
            count = count.next;
        }
        if(cur == true) {
            Integer[] array = (Integer[])list.toArray(new Integer[list.size()]);
            return array;
        }
        return new Integer[] {-1};
    }
    
    /**
     * 获得链表头节点
     * @returnpublic class TestDemo1 {

    /**
     * 合并两个有序的单链表
     * @param x1
     * @param x2
     * @return
     */
    public static Node1 mergeTable(Node1 x1,Node1 x2) {
        if(x1 == null) {
            return x2;
        }
        if(x2 == null) {
            return x1;
        }
        Node1 x = new Node1();
        Node1.Entry cur1 = x1.head.next;
        Node1.Entry cur2 = x2.head.next;
        Node1.Entry cur = x.head;
        while(cur1 != null && cur2 != null) {
            if(cur1.data > cur2.data) {
                cur.next = cur2;
                cur2 = cur2.next;
            }else {
                cur.next = cur1;
                cur1 = cur1.next;
            }
            cur = cur.next;
        }

        if(cur1 == null) {
            cur.next = cur2;
        }

        if(cur2 == null) {
            cur.next = cur1;
        }

        return x;
    }
     */
    public Entry getHead() {
        return this.head;
    }
    
 /**
     * 打印链表
     */
    public void show() {
        count = this.head;
        while(count.next != null) {
            count = count.next;
            System.out.print(count.data+"\t");
        }
        System.out.println();
    }


    /**
     * 逆序打印链表
     * @param head
     */
    public void inverseOrderShow(Entry head) {
        if(isEmpty()) {
            return ;
        }
        Entry cur = head.next;
        if(cur != null) {
            inverseOrderShow(cur);
            System.out.print(cur.data+"\t");
        }
    }
    //测试
public class TestDemo {
    public static void main(String[] args) {
        LinkList<Integer> list = new LinkList<>();
        list.head_insert(35);
        list.head_insert(45);
        list.tail_insert(15);
        list.tail_insert(5);
        list.show();
         System.out.println("链表长度:"+list.get_length());
        list.insert(2,25);
        list.insert(1,5);
        list.insert(3,5);
        list.show();
        System.out.println("替换5号元素:");
        list.setIndex(5,9);
        list.show();
        System.out.println("替换所有的5为20:");
        list.setAll(5,20);
        list.show();
        System.out.println("删除25这个元素:");
        list.delete(25);
        list.show();
        System.out.println("找到35的下标:\n"+list.getIndex(35));
        System.out.println("找到20的下标:\n"+ Arrays.toString(list.getAll(20)));
        System.out.println("删除3号下标位置元素:");
        list.remove(3);
        list.show();
        System.out.println("删除所有的20:");
        list.delete1(20);
        list.show();
        System.out.println("逆序打印:");
        list.inverseOrderShow(list.head);
    }
}

环形链表

/**
 * @ClassName Annular
 * @Description 环形链表
 * @Author lmx
 * @Date 2018/11/3 20:46
 * @Version 1.0
 **/
public class Annular {
    /**
     * 节点类
     */
    class Entry {
        int data;
        Entry next;

        public Entry() {
            this.next = null;
            this.data = 0;
        }

        public Entry(int data) {
            this.data = data;
            this.next = null;
        }
    }


    private Entry head = new Entry();  //头节点,列表首指针


    /**
     *  头结点的初始化
     */
    public Annular() {
        this.head = new Entry();
        this.head.next = this.head;
    }

    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty(){
        Entry cur = head;
        if(cur.next != this.head){
            return false;
        }else {
            return true;
        }
    }


    /**
     * 头插法
     * @param val
     */
    public void insertHead(int val) {
        Entry entry = new Entry(val);
        if(this.head.next == null) {
            head.next = entry;
            entry.next = this.head;
            return;
        }
        entry.next = this.head.next;
        this.head.next = entry;
    }


    /**
     * 尾插法
     * @param val
     */
    public void insertTail(int val) {
        Entry entry = new Entry(val);
        Entry cur = this.head;         //找到尾巴
        while(cur.next != head) {
            cur = cur.next;
        }
        entry.next =  cur.next;
        cur.next = entry;
    }

    /**
     * 删除一个元素
     * @param data
     */
    public void deleteEntry(int data) {
        if(isEmpty()) {
            return ;
        }
        Entry p1 = head;
        Entry p2 = head.next;         //前指针

        /*while(p2.data!=data){
			p1=p1.next;
			p2=p2.next;
		}
			p1.next=p2.next;
		}*/

        while(p2 != this.head) {
            if(p2.data == data) {
                p1.next = p2.next;
                p2 = p1.next;
            }else {
                p1 = p2;
                p2 = p2.next;
            }
        }
    }

    /**
     * 得到链表的长度
     * @return
     */
    public int getlength() {
        if(isEmpty()) {
            return 0;
        }
        Entry pwd = head;
        int length = 0;
        while(pwd.next != head) {
            length++;
            pwd = pwd.next;
        }
        return length;
    }


    /**
     * 输出链表
     */
    public void show() {
        if(isEmpty()) {
            return ;
        }
        Entry cur = this.head.next;
        while(cur != this.head) {
            System.out.print(cur.data+" ");
            cur = cur.next;
        }
        System.out.println();
    }

}

双向链表
每个节点即有指向前一个节点的地址,还有指向后一个节点的地址;

/**
 * @ClassName BothwayList
 * @Description 双向链表
 * @Author lmx
 * @Date 2018/11/7 20:14
 * @Version 1.0
 **/
public class BothwayList {
    class Entry {
        private int data;
        private Entry prev;  //前驱
        private Entry next;  //后继

        public Entry() {
            this.data = -1;
            this.next = null;
            this.prev = null;
        }

        public Entry(int val) {
            this.data = val;
            this.next = null;
            this.prev = null;
        }
    }
    private Entry head;

    public BothwayList(){
        this.head = new Entry();
    }

    /**
     * 头插法
     * @param val
     */
    public void head_insert(int val) {
        Entry entry = new Entry(val);
        entry.next = this.head.next;
        this.head.next = entry;
        entry.prev = this.head;
        if(entry.next != null) {
            entry.next.prev = entry;
        }
    }

    /**
     * 尾插法
     * @param val
     */
    public void tail_insert(int val) {
        Entry entry = new Entry(val);
        Entry cur = this.head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = entry;
        entry.prev = cur;
    }

    /**
     * 删除所有值为val的节点
     * @param val
     */
    public void delete(int val) {
        Entry cur = this.head;
        while(cur.next != null) {
            if(cur.next.data == val) {
                cur.next = cur.next.next;
                if(cur.next != null) {
                    cur.next.prev = cur;
                }
                continue;
            }
            cur = cur.next;
        }
    }

    /**
     * 打印
     */
    public void show() {
        Entry cur = this.head;
        while(cur.next != null) {
            cur = cur.next;
            System.out.print(cur.data+"\t");
        }
        System.out.println();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值