前驱和后继:如果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++];
}
}
}
时间复杂度:查询,插入和删除
链表:
单向链表:头结点数据域为空,指针域指向下一个结点。尾结点的指针域为空。
代码:
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;
}
}
}