线性表

线性表

1 顺序表

  • 使用数组实现,代码如下:
//顺序表
public class SequenceList<T> implements Iterable<T> {
    //存储元素的数组
    private T[] eles;
    //记录当前顺序表中元素的个数
    private int N;

    //构造方法
    public SequenceList(int capacity) {
        //初始化数组
        this.eles = (T[]) new Object[capacity];
        //初始化长度
        this.N = 0;
    }

    //将一个线性表置为空表
    public void clear() {
        for (int i = 0; i < this.eles.length; i++) {
            this.eles[i] = null;
        }
        this.N = 0;
    }

    //获取线性表的长度
    public int length() {
        return this.N;
    }

    //判断当前线性表是否为空表
    public boolean isEmpty() {
        return this.N == 0;
    }

    //获取指定位置的元素
    public T get(int i) {
        return this.eles[i];
    }

    //向线性表中添加元素t
    public void insert(T t) {

        if (this.N == this.eles.length) {
            this.resize(2 * this.eles.length);
        }
        this.eles[this.N++] = t;
    }

    //在i元素处插入元素t
    public void insert(int i, T t) {

        if (this.N == this.eles.length) {
            this.resize(2 * this.eles.length);
        }
        //先把i索引处的元素及其后面的元素依次向后移动一位
        for (int index = this.N; index > i; index--) {
            this.eles[index] = this.eles[index - 1];
        }
        //再把t元素放到i索引处即可
        this.eles[i] = t;
        //元素个数加一
        this.N++;
    }

    //删除指定位置i处的元素,并返回该元素
    public T remove(int i) {
        //记录索引i处的值
        T current = this.eles[i];
        //索引i后面的元素依次向前移动一位即可
        for (int index = i; index < this.N - 1; index++) {
            this.eles[index] = this.eles[index + 1];
        }
        //元素个数减一
        this.N--;

        if (this.N < this.eles.length / 4) {
            this.resize(this.eles.length / 2);
        }

        return current;
    }

    //返回线性表中首次出现该元素的序号,不存在则返回-1
    public int indexOf(T t) {
        for (int i = 0; i < this.N; i++) {
            if (t.equals(this.eles[i])) {
                return i;
            }
        }
        return -1;
    }

    //根据参数newsize重置eles数组的大小
    public void resize(int newsize) {
        //定义一个临时数组,指向原数组
        T[] temp = this.eles;
        //创建新数组
        this.eles = (T[]) new Object[newsize];
        //把原数组的数据拷贝到新数组即可
        for (int i = 0; i < this.N; i++) {
            this.eles[i] = temp[i];
        }

    }

	//提供迭代器遍历顺序表
    @Override
    public Iterator<T> iterator() {
        return new Siterator();
    }

    private class Siterator implements Iterator {
        private int cusor;

        public Siterator() {
            this.cusor = 0;
        }

        @Override
        public boolean hasNext() {
            return this.cusor < SequenceList.this.N;
        }

        @Override
        public Object next() {
            return SequenceList.this.eles[this.cusor++];
        }
    }
}

2 单向链表

  • 使用Node结点类实现
  • 链表中记录了头结点且每个结点中记录了下一个结点
  • 代码实现:
//单链表
public class LinkList<T> implements Iterable<T> {
    //记录头结点
    private Node head;
    //记录链表的长度
    private int N;

    public LinkList() {
        //初始化头结点
        this.head = new Node(null, null);
        //初始化元素个数
        this.N = 0;
    }

    public void clear() {
        this.head.next = null;
        this.N = 0;
    }

    //清空链表

    public int length() {
        return this.N;
    }
    //获取链表的长度

    public boolean isEmpty() {
        return this.N == 0;
    }
    //判断链表是否为空

    public T get(int i) {
        //通过循环,从头结点开始往后找,依次找i次,就可以找到对应的元素
        Node n = this.head.next;
        for (int index = 0; index < i; index++) {
            n = n.next;
        }
        return n.item;
    }
    //获取指定位置i处的元素

    //向链表中添加元素t
    public void insert(T t) {

        //找到当前最后一个结点
        Node n = this.head;
        while (n.next != null) {
            n = n.next;
        }
        //创建新结点,保存元素t
        Node newNode = new Node(t, null);
        //让当前最后一个结点指向新结点
        n.next = newNode;
        //元素个数+1
        this.N++;

    }

    public void insert(int i, T t) {

        //找到i位置前一个结点
        Node pre = this.head;
        for (int index = 0; index < i - 1; index++) {
            pre = pre.next;
        }
        //找到i位置的结点
        Node curr = pre.next;
        //创建新结点,并且新结点需要指向原来i位置的结点
        Node newNode = new Node(t, curr);
        //原来i位置的前一个结点指向新结点即可
        pre.next = newNode;
        //元素个数+1
        this.N++;
    }
    //向指定位置i处添加元素t

    public T remove(int i) {

        //找到i位置前一个结点
        Node pre = this.head;
        for (int index = 0; index < i - 1; index++) {
            pre = pre.next;
        }

        //找到i位置的结点
        Node curr = pre.next;

        //找到i位置的下一个结点
        Node nextNode = curr.next;

        //前一个结点指向下一个结点
        pre.next = nextNode;

        //元素个数减一
        this.N--;

        return curr.item;
    }
    //删除指定位置i处的元素,并返回被删除的元素

    public int indexOf(T t) {

        //从头结点开始,依次找到每一个结点,取出item,和t比较,如果相同,返回index
        Node n = this.head;
        for (int i = 0; n.next != null; i++) {
            if (n.next.item.equals(t)) {
                return i;
            }
        }

        return -1;
    }
    //查找元素在链表中第一次出现的位置

    //对整个链表反转
    public void reverse() {
        //判断当前链表是否为空链表,如果是空链表,则结束运行,如果不是,则调用重载的reverse方法完成反转
        if (this.isEmpty()) {
            return;
        }
        this.reverse(this.head.next);
    }

    //反转链表中的某个结点curr,并把反转后的结点返回
    public Node reverse(Node curr) {
        if (curr.next == null) {
            this.head.next = curr;
            return curr;
        }
        //递归的反转当前结点curr的下一个结点,返回值就是链表反转后,当前结点的上一个结点
        Node pre = this.reverse(curr.next);
        //让返回的结点的下一个结点指向当前结点curr
        pre.next = curr;
        //把当前结点的下一个结点变为null
        curr.next = null;
        return curr;
    }

    @Override
    public Iterator<T> iterator() {
        return new Litrator();
    }

    //结点类
    private class Node {

        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }

    }

	//提供迭代器遍历链表
    private class Litrator implements Iterator<T> {

        private Node n;

        public Litrator() {
            this.n = LinkList.this.head;
        }

        @Override
        public boolean hasNext() {
            return this.n.next != null;
        }

        @Override
        public T next() {
            this.n = this.n.next;
            return this.n.item;
        }
    }
}

3 双向链表

  • 使用Node结点类实现
  • 链表中记录了头结点和尾结点且每个结点中记录了当前结点的前驱和后继
  • 代码实现:
//双向链表
public class TwoWayLinkList<T> implements Iterable<T> {

    //首结点
    private Node head;
    //最后一个结点
    private Node last;
    //链表的长度
    private int N;

    public TwoWayLinkList() {
        //初始化头结点和为结点
        this.head = new Node(null, null, null);
        this.last = null;
        //初始化元素个数
        this.N = 0;
    }

    //清空链表
    public void clear() {
        this.head.next = null;
        this.last = null;
        this.N = 0;
    }

    //获取链表长度
    public int length() {
        return this.N;
    }

    //判断链表是否为空
    public boolean isEmpty() {
        return this.N == 0;
    }

    //获取第一个元素
    public T getFirst() {
        if (this.isEmpty()) {
            return null;
        }
        return this.head.next.item;
    }

    //获取最后一个元素
    public T getLast() {
        if (this.isEmpty()) {
            return null;
        }
        return this.last.item;
    }

    //插入元素t
    public void insert(T t) {
        //如果链表为空
        if (this.isEmpty()) {
            //创建新的结点
            Node newNode = new Node(t, this.head, null);
            //让新结点成为尾结点
            this.last = newNode;
            //让头结点指向尾结点
            this.head.next = this.last;
        } else {//如果链表不为空
            //创建新的结点
            Node newNode = new Node(t, this.last, null);
            //让当前的尾结点指向新结点
            this.last.next = newNode;
            //让新结点成为尾结点
            this.last = newNode;
        }

        //元素个数+1
        this.N++;
    }

    //想指定位置i插入元素t
    public void insert(int i, T t) {
        //找到i位置的前一个结点
        Node pre = this.head;
        for (int index = 0; index < i; index++) {
            pre = pre.next;
        }
        //找到i位置的结点
        Node curr = pre.next;
        //创建新结点
        Node newNode = new Node(t, pre, curr);
        //让i位置的前一个结点的下一个结点指向新结点
        pre.next = newNode;
        //让i位置的前一个结点指向新结点
        curr.pre = newNode;
        //元素个数+1
        this.N++;
    }

    //获取指定位置i处的元素
    public T get(int i) {
        Node n = this.head.next;
        for (int index = 0; index < i; index++) {
            n = n.next;
        }
        return n.item;
    }

    //找到元素t在链表中第一次出现的位置
    public int indexOf(T t) {
        Node n = this.head;
        for (int index = 0; n.next != null; index++) {
            n = n.next;
            if (n.item.equals(t)) {
                return index;
            }
        }
        return -1;
    }

    //删除位置i处的元素,并返回该元素
    public T remove(int i) {
        //找到i位置的前一个结点
        Node pre = this.head;
        for (int index = 0; index < i; index++) {
            pre = pre.next;
        }
        //找到i位置的结点
        Node curr = pre.next;
        //找到i位置的下一个结点
        Node nextNode = curr.next;
        //让i位置的前一个结点的下一个结点指向i位置的下一个结点
        pre.next = nextNode;
        //让i位置的下一个结点的上一个结点指向i位置的上一个结点
        nextNode.pre = pre;
        //元素个数-1
        this.N--;
        return curr.item;
    }

    @Override
    public Iterator iterator() {
        return new TIterator();
    }

    //结点类
    private class Node {
        public T item;//存储数据
        public Node pre;//指向前一个结点
        public Node next;//指向下一个结点

        public Node(T item, Node pre, Node next) {
            this.item = item;
            this.pre = pre;
            this.next = next;
        }
    }

	//提供迭代器遍历双向链表
    private class TIterator implements Iterator {
        private Node n;

        public TIterator() {
            this.n = TwoWayLinkList.this.head;
        }

        @Override
        public boolean hasNext() {
            return this.n.next != null;
        }

        @Override
        public Object next() {
            this.n = this.n.next;
            return this.n.item;
        }
    }
}

4 队列

  • 只允许在一端进行插入操作,而在另一端进行删除操作的线性表。
  • FIFO:先进先出
  • 用顺序表和链表都可实现,以下使用链表实现:
/**
 * 队列:链表实现
 *
 * @author Richard
 * 2020/12/9 18:49
 */
public class Queue<T> implements Iterable<T> {
    //记录首结点
    private Node head;
    //记录最后一个结点
    private Node last;
    //记录队列中元素的个数
    private int N;

    public Queue() {
        this.head = new Node(null, null);
        this.last = null;
        this.N = 0;
    }

    @Override
    public Iterator<T> iterator() {
        return new Qitrator();
    }

    //判断队列是否为空
    public boolean isEmpty() {
        return this.N == 0;
    }

    //返回队列中元素的个数
    public int size() {
        return this.N;
    }

    //向队列中插入元素t
    public void enqueue(T t) {
        //当前尾结点为null
        Node newNode = new Node(t, null);
        if (this.last == null) {
            this.head.next = newNode;
            this.last = newNode;
        } else {
            //当前尾结点不为null
            this.last.next = newNode;
            this.last = newNode;
        }
        //元素个数+1
        this.N++;
    }

    //从队列中拿出一个元素
    public T dequeue() {
        if (this.isEmpty()) {
            return null;
        }

        Node oldFirst = this.head.next;
        this.head.next = oldFirst.next;
        this.N--;

        if (this.isEmpty()) {
            this.last = null;
        }
        return oldFirst.item;
    }

    //结点类
    private class Node {

        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

	//提供迭代器遍历队列
    private class Qitrator implements Iterator<T> {
        private Node n;

        public Qitrator() {
            this.n = Queue.this.head;
        }

        @Override
        public boolean hasNext() {
            return this.n.next != null;
        }

        @Override
        public T next() {
            this.n = this.n.next;
            return this.n.item;
        }
    }
}

5 栈

  • 栈的插入和删除只能在栈顶进行,每次删除的元素都是最后进栈的元素
  • LIFO:后进先出
  • 用顺序表和链表都可实现,以下使用链表实现:
//栈:链表实现
public class Stack<T> implements Iterable<T> {
    //记录首结点
    private Node head;
    //栈中元素的个数
    private int N;

    public Stack() {
        this.head = new Node(null, null);
        this.N = 0;
    }

    @Override
    public Iterator<T> iterator() {
        return new Sitrator();
    }

    //判断当前栈中元素是否为0
    public boolean isEmpty() {
        return this.N == 0;
    }

    //获取当前栈中元素个数
    public int size() {
        return this.N;
    }

    //把t元素压入栈中
    public void push(T t) {
        //找到首结点指向的第一个结点
        Node oldFirst = this.head.next;
        //创建新结点
        Node newNode = new Node(t, null);
        //让首结点指向新结点
        this.head.next = newNode;
        //让新结点指向原来的第一个结点
        newNode.next = oldFirst;
        //元素个数+1
        this.N++;
    }

    //弹出栈顶元素
    public T pop() {
        //找到首结点指向的第一个结点
        Node oldFirst = this.head.next;
        if (oldFirst == null) {
            return null;
        }
        //让首结点指向原来第一个结点的下一个结点
        this.head.next = oldFirst.next;
        //元素个数-1
        this.N--;
        return oldFirst.item;
    }

    //结点类
    private class Node {

        //存储数据
        T item;
        //下一个结点
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }
	
	//提供迭代器遍历栈
    private class Sitrator implements Iterator<T> {
        private Node n;

        public Sitrator() {
            this.n = Stack.this.head;
        }

        @Override
        public boolean hasNext() {
            return this.n.next != null;
        }

        @Override
        public T next() {
            this.n = this.n.next;
            return this.n.item;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值