线性表 是编程里常用的数据结构,通常以数组,字符串,栈,队列的形式在程序中出现。
线性表 有两种实现方式,一种是以可增长数组的实现,另一种以链表的方式实现。
首先我们用代码来分别实现:
数组线性表:
package com.wanq.datastructure.list;
/**
* @author-WanQ
* @创建时间:2013-3-20 下午11:38:27
* @类说明: 线性表 数组实现
*/
public class ArrayList<E> {
Object[] array = null;// 用来保存这线性表的数据的数组
int current;// 用来保存当前位置的参数
int size;// 线性表的容量大小
/**
* 线性表初始化
*
* @return
*/
public ArrayList() {
// 首次初始化线性表的容量为10
this(10);
}
/**
* 设置线性表容量的构造函数
*
* @param arrayLength
* @return
*/
public ArrayList(int arrayLength) {
if (arrayLength < 0) {
throw new RuntimeException("数组大小错误:不能为" + arrayLength);
} else {
this.array = new Object[arrayLength];
this.current = 0;
this.size = arrayLength;
}
}
/**
* 判断线性表当前容量是否满了,如果满了就扩大
*
* @param cur
*/
public void ensureLength(int cur) {
// 判断是不是已经满额了,满了就将容量扩大一倍
if (cur == size) {
this.size = this.size * 2;
Object[] newArray = new Object[size];
for (int i = 0; i < cur; i++) {
newArray[i] = this.array[i];
}
this.array = newArray;
}
}
public void validateIndex(int index) {
if (index < 0 || index > current) {
throw new RuntimeException("数组 index 无效:" + index);
}
}
/**
* 线性表添加元素方法 添加之前,判断线性表的容量
*
* @param e
* @return
*/
public boolean add(E e) {
ensureLength(current);
this.array[current] = e;
current++;
return true;
}
/**
* 获取指定index下标的数据
*
* @param index
* @return
*/
public E get(int index) {
validateIndex(index);
return (E) this.array[index];
}
/**
* 更改指定index下标的数据为e
*
* @param index
* @param e
* @return
*/
public boolean set(int index, E e) {
validateIndex(index);
this.array[index] = e;
return true;
}
/**
* 在指定index下标插入数据e
*
* @param index
* @param e
* @return
*/
public boolean inset(int index, E e) {
validateIndex(index);
Object[] tempArr = this.array;
for (int i = 0; i < current; i++) {
if (i < index) {
tempArr[i] = array[i];
} else if (i == index) {
tempArr[i] = e;
} else if (i > index) {
tempArr[i] = array[i - 1];
}
}
this.array = tempArr;
return true;
}
/**
* 删除指定index下标的数据
*
* @param index
* @return
*/
public boolean delete(int index) {
validateIndex(index);
Object[] tempArr = this.array;
for (int i = 0; i < current; i++) {
if (i < index) {
tempArr[i] = array[i];
} else if (i >= index) {
tempArr[i] = array[i + 1];
}
}
return true;
}
/**
* 返回当前列表的长度
*
* @return
*/
public int length() {
return this.current;
}
}
链表线性表
package com.wanq.datastructure.list;
/**
* @author-WanQ
* @创建时间:2013-3-21 上午12:37:11
* @类说明: 链式线性表
*/
public class LinkedList<E> {
private Node<E> header = null;// 头结点
private Node<E> laster = null;// 尾结点
int size = 0;// 表示链表大小
public LinkedList() {
header = new Node<E>();
laster = header;
}
/**
* 增加数据
*
* @param e
* @return
*/
public boolean add(E e) {
if (size == 0) {
header.e = e;
} else {
// 根据需要添加的内容,封装为结点
Node<E> newNode = new Node<E>(e);
// 在最后一个结点后加上新结点
laster.addNext(newNode);
// 尾部指针移动到最后
laster = newNode;
}
size++;// 当前大小自增加1
return true;
}
/**
* 得到指定下标的结点
*
* @param index
* @return
*/
public Node<E> getNode(int index) {
// 先判断索引正确性
if (index > size || index < 0) {
throw new RuntimeException("索引值有错:" + index);
}
Node<E> tempNode = new Node<E>();
tempNode = header;
int count = 0;
while (count != index) {
tempNode = tempNode.next;
count++;
}
return tempNode;
}
/**
* 在指定的下边插入数据
*
* @param index
* @param e
* @return
*/
public boolean inset(int index, E e) {
Node<E> newNode = new Node<E>(e);
// 得到第N个结点
Node<E> cNode = getNode(index);
newNode.next = cNode.next;
cNode.next = newNode;
size++;
return true;
}
/**
* 删除方法
*
* @param index
* @return
*/
public boolean delete(int index) {
if (size == 0) {
header.next = null;
} else if (size > 0) {
if (index == 0) {
Node<E> cNode = header;
header.next = cNode.next;
cNode.next = null;
} else if (index > 0) {
Node<E> cNode = getNode(index);
Node<E> pNode = getNode(index - 1);
pNode.next = cNode.next;
cNode.next = null;
}
}
size--;
return true;
}
/**
* 根据索引,取得该索引下的数据
*
* @param index
* @return
*/
public E get(int index) {
// 先判断索引正确性
if (index >= size || index < 0) {
throw new RuntimeException("索引值有错:" + index);
}
Node<E> tempNode = new Node<E>();
tempNode = header;
int count = 0;
while (count != index) {
tempNode = tempNode.next;
count++;
}
E e = tempNode.e;
return e;
}
public int size() {
return size;
}
/**
* 设置第N个结点的值
*
* @param x
* @param e
* @return
*/
public boolean set(int index, E e) {
Node<E> newNode = new Node<E>(e);
// 得到第x个结点
Node<E> cNode = getNode(index);
cNode.e = newNode.e;
return true;
}
/**
* 用来存放数据的内部类
*
* @author WanQ
*
* @param <E>
*/
class Node<e> {
private E e;// 结点存放的数据
Node() {
}
Node(E e) {
this.e = e;
}
Node<E> next;// 用来存放下个结点
/**
* 在此结点后加一个结点
*/
void addNext(Node<E> node) {
next = node;
}
}
}
栈与队列 也是线性结构,所以可以用线性表简单的实现。代码如下:
栈
package com.wanq.datastructure.list;
/**
* @author-WanQ
* @创建时间:2013-3-21 上午01:28:48
* @类说明: 用数组实现栈
*/
public class ArrayStack<E> {
private ArrayList<E> list = new ArrayList<E>();// 用来保存数据的数组
private int size;// 当前队列的大小
/**
* 入栈
*
* @param e
*/
public void push(E e) {
list.add(e);
size++;
}
/**
* 出栈
*
* @return
*/
public E pop() {
E e = list.get(size - 1);
size--;
return e;
}
public boolean isEmpty() {
if (size == 0) {
return true;
} else {
return false;
}
}
}
队列
package com.wanq.datastructure.list;
/**
* @author-WanQ
* @创建时间:2013-3-21 上午01:16:30
* @类说明: 用数组线性表实现队列
*/
public class ArrayQueue<E> {
private ArrayList<E> list = new ArrayList<E>();// 用来保存数据的数组
private int size;// 当前队列的大小
/**
* 入队
*
* @param e
*/
public void enQueue(E e) {
list.add(e);
size++;
}
/**
* 出队
*
* @return
*/
public E deQueue() {
if (size > 0) {
E e = list.get(0);
list.delete(0);
return e;
} else {
throw new RuntimeException("已经到达队列顶部");
}
}
/**
* 判断是否为空
*
* @return
*/
public boolean isEmpty() {
if (size == 0) {
return true;
} else {
return false;
}
}
}
栈与队列也可以用链式表来实现。