算法分析-线性表

本文详细介绍了顺序表(数组形式)和单向/双向链表的原理、操作(如查询、插入、删除)及其时间复杂度,包括头结点和尾结点的概念。通过实例代码演示了Java中的SequenceList和LinkedList实现。
摘要由CSDN通过智能技术生成

前驱和后继:如果A元素在B元素的前端,则称A为B的前驱元素。相反,B称为A的后继元素。

线性表的头部没有前驱元素的称为头结点,尾部没有后继元素的称为尾结点。

分类:

顺序表:在计算机内存中以数组形式保存的线性表,储存地址是一组连续的单元。

代码

package com.chenqing.test.linearList.sequence;

import java.util.Iterator;

public class Realization<T> implements Iterable<T>{
    private T[] arrs;
    private int n;

    public Realization(int capacity) {
        this.arrs = (T[]) new Object[capacity];
        this.n = 0;
    }

    public void clear() {
        this.n = 0;
    }

    public boolean isEmpty() {
        return this.n <= 0;
    }

    public int getLength() {
        return this.n;
    }

    public T get(int i) {
        return this.arrs[i];
    }

    public void insert(T obj) {
        if(this.arrs.length == this.n){
            this.resize(this.arrs.length*2);
        }
        this.arrs[this.n++] = obj;
    }

    public void insert(int i, T obj) {
        if(this.arrs.length == this.n){
            this.resize(this.arrs.length*2);
        }
        for (int j = this.n; j > i; j--) {
            this.arrs[j] = this.arrs[j - 1];
        }
        this.n++;
        this.arrs[i] = obj;
    }

    public T remove(int i) {
        T obj = this.arrs[i];
        for (int j = i; j < this.n-1; j++) {
            this.arrs[j] = this.arrs[j + 1];
        }
        this.n--;
        if(this.n <= this.arrs.length/4){
            this.resize(this.arrs.length/2);
        }
        return obj;
    }

    public int indexOf(T obj) {
        for (int i = 0; i < this.n; i++) {
            if (this.arrs[i] == obj) {
                return i;
            }
        }
        return -1;
    }

    public void resize(int newCapacity){
        T[] temp = this.arrs;
        this.arrs = (T[]) new Object[newCapacity];
        for (int i = 0; i < this.n; i++) {
            this.arrs[i] = temp[i];
        }
    }

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

    private class MyselfIterator implements Iterator<T>{
        private int cursor;

        public MyselfIterator() {
            this.cursor = 0;
        }

        @Override
        public boolean hasNext() {
            return this.cursor < Realization.this.n;
        }

        @Override
        public T next() {
            return Realization.this.arrs[this.cursor++];
        }
    }
}

时间复杂度:查询O(1),插入和删除O(n)

链表: 

单向链表:头结点数据域为空,指针域指向下一个结点。尾结点的指针域为空。

代码:

package com.chenqing.test.linearList.singleLinkedList;

import java.util.Iterator;

public class Realization<T> implements Iterable<T>{
    private Node head;
    private int n;

    public Realization() {
        this.head = new Node(null, null);
        this.n = 0;
    }

    public void clear() {
        this.head.setNext(null);
    }

    public boolean isEmpty() {
        return this.head.getNext() == null;
    }

    public int getLength() {
        return this.n;
    }

    public T get(int i) {
        Node pointer = head;
        for (int j = 0; j <= i-1; j++) {
            pointer = pointer.getNext();
        }
        return pointer.getNext().getData();
    }

    public void insert(T obj) {
        Node pointer = head;
        while (pointer.getNext() != null) {
            pointer = pointer.getNext();
        }
        Node newNode = new Node(obj, null);
        pointer.setNext(newNode);
        this.n++;
    }

    public void insert(int i, T obj) {
        Node pointer = head;
        for (int j = 0; j <= i-1; j++) {
            pointer = pointer.getNext();
        }
        Node newNode = new Node(obj, pointer.getNext());
        pointer.setNext(newNode);
        this.n++;
    }

    public T remove(int i) {
        Node pointer = head;
        for (int j = 0; j <= i-1; j++) {
            pointer = pointer.getNext();
        }
        Node delNode = pointer.getNext();
        pointer.setNext(delNode.getNext());
        this.n--;
        return delNode.getData();
    }

    public int indexOf(T obj) {
        Node pointer = head;
        for (int i = 0; pointer.getNext() !=null; i++) {
            pointer = pointer.getNext();
            if(pointer.getData().equals(obj)){
                return i;
            }
        }
        return -1;
    }

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

    private class MyselfIterator implements Iterator<T>{
        private Node pointer;

        public MyselfIterator() {
            this.pointer = head;
        }

        @Override
        public boolean hasNext() {
            return pointer.getNext() != null;
        }

        @Override
        public T next() {
            pointer = pointer.getNext();
           return pointer.getData();
        }
    }


    private class Node {
        private T data;
        private Node next;

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

        public T getData() {
            return data;
        }

        public void setData(T data) {
            this.data = data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }
}

双向链表:头结点数据域为空,指向上一个节点的指针域为空,指向下一个节点的指针域为下一个结点。尾结点的指向下一个结点的指针域为空。

代码:

package com.chenqing.test.linearList.doubleLinkedList;

import java.util.Iterator;

public class Realization<T> implements Iterable<T>{
    private Node head;
    private Node tail;
    private int n;

    public Realization() {
        this.head = new Node(null, null, null);
        this.tail = null;
        this.n = 0;
    }

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

    public boolean isEmpty() {
        return this.n == 0;
    }

    public int getLength() {
        return this.n;
    }

    public T getHead() {
        return this.head.next.data;
    }

    public T getTail() {
        if (isEmpty()) {
            return null;
        } else {
            return this.tail.data;
        }
    }

    public void insert(T obj){
        if(isEmpty()){
            Node newNode = new Node(head, obj, null);
            head.next = newNode;
            this.tail = newNode;
        }else{
            Node newNode = new Node(tail, obj, null);
            tail.next = newNode;
            this.tail = newNode;
        }
        this.n++;
    }

    public void insert(int i, T obj){
        if(i>this.n-1){
            return;
        }
        if(isEmpty()){
            Node newNode = new Node(head, obj, null);
            head.next = newNode;
            this.tail = newNode;
        }else{
            Node pre = this.head;
            for (int j = 0; j < i; j++) {
                pre = pre.next;
            }
            Node curr = pre.next;
            Node newNode = new Node(pre, obj, curr);
            pre.next = newNode;
            curr.pre = newNode;
        }
        this.n++;
    }

    public T remove(int i){
        if(i>this.n-1){
            return null;
        }
        if(isEmpty()){
            return null;
        }
        Node pre = this.head;
        for (int j = 0; j < i; j++) {
            pre = pre.next;
        }
        Node curr = pre.next;
        curr.next.pre = pre;
        pre.next = curr.next;
        this.n--;
        return curr.data;
    }

    public T get(int i){
        if(i>this.n-1){
            return null;
        }
        if(isEmpty()){
            return null;
        }
        Node pre = this.head;
        for (int j = 0; j < i; j++) {
            pre = pre.next;
        }
        Node curr = pre.next;
        return curr.data;
    }

    public int indexOf(T obj){
        Node pre = this.head;
        for (int j = 0; this.head.next != null; j++) {
            pre = pre.next;
            if(pre.data.equals(obj)){
                return j;
            }
        }
        return -1;
    }

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

    private class MyselfIterator implements Iterator<T>{
        private Node pointer;

        public MyselfIterator() {
            pointer=Realization.this.head;
        }

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

        @Override
        public T next() {
            pointer = pointer.next;
            return pointer.data;
        }
    }

    private class Node {
        public Node pre;
        public T data;
        public Node next;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值