版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luffysk/article/details/82080585
线性表定义
- 多个元素组成的有限序列
- 第一个元素无前驱元素,最后一个无后继元素,剩余元素有且只有一个前驱和一个后继
- 存储方式有顺序存储和链式存储
数据类型定义
/**
* 数据类型类,其中可包括任意类型
*
*/
public class DataType {
public int data;
public DataType(int data) {
this.data = data;
}
}
顺序存储结构方式实现
/**
* 线性表-顺序存储结构
*List为线性表共有方法的抽象接口
*/
public class LineList implements List {
private DataType[] data;
private int length; //线性表实际长度
public LineList(int size) throws Exception {
if(size<0) {
throw new Exception("初始化失败,size参数错误");
}
data = new DataType[size];
length = 0;
}
//判断线性表是否为空
public boolean isEmpty() throws Exception {
return this.length==0? true:false;
}
//清空线性表
public void clearList() throws Exception {
for(int i=0; i<this.length; i++) {
this.data[i] = null;
}
this.length = 0;
}
//获取指定位置的元素,pos从零开始
public DataType getElement(int pos) throws Exception {
if(pos<0 || pos>this.length) {
throw new Exception("pos参数不正确");
}
return this.data[pos];
}
//判断元素ele是否在线性表中
public boolean locateElement(DataType ele) throws Exception {
for(int i=0; i<this.length; i++) {
if(this.data[i] == ele)
return true;
}
return false;
}
//在第pos个位置插入元素ele,pos从0开始
public void insert(int pos, DataType ele) throws Exception {
if(pos<0 || pos>this.length || pos>=this.data.length || this.data.length == this.length) {
throw new Exception("参数pos不正确");
}
for(int i=this.length; i!=pos; i--) {
this.data[i] = this.data[i-1];
}
this.data[pos] = ele;
this.length++;
}
//删除第i位的值并返回被删除的值,i从0开始
public DataType delete(int pos) throws Exception {
if(pos<0 || pos>this.length-1) {
throw new Exception("参数pos不正确");
}
DataType value = this.data[pos];
for(int i=pos; i<this.length; i++) {
this.data[i] = this.data[i+1];
}
this.length--;
return value;
}
//获取线性表的长度
public int size() {
return this.length;
}
}
链式存储结构方式实现
/**
* 单链表
*
*/
public class LinkedList implements List{
private Node head;
public LinkedList() {
head = new Node(new DataType(-1),null);
}
@Override
public boolean isEmpty() throws Exception {
return head.next == null? true : false;
}
//获取指定位置的元素,pos从1开始
public DataType getElement(int pos) throws Exception {
if(pos<1) {
throw new Exception("参数pos不正确");
}
int len = 1;
Node cur = head.next;
while(cur != null) {
if(len == pos) {
return cur.dt;
}
cur = cur.next;
len++;
}
return null;
}
//判断元素ele是否在表中
public boolean locateElement(DataType ele) throws Exception {
Node cur = head.next;
while(cur != null) {
if(cur.dt.equals(ele)) {
return true;
}
cur = cur.next;
}
return false;
}
//插入元素,pos从1开始
public void insert(int pos, DataType ele) throws Exception {
if(pos<1 || pos>size()+1) {
throw new Exception("参数pos不正确");
}
int len = 1;
Node cur = head.next;
Node pre = head;
Node node = new Node(ele, null);
if(cur == null) {
head.next = node;
return;
}
while(cur != null) { //2 1 3
if(len == pos) {
Node temp = pre.next;
pre.next = node;
node.next = temp;
return;
}
pre = cur;
cur = cur.next;
len++;
}
pre.next = node;
}
//删除指定位置的元素
public DataType delete(int pos) throws Exception {
if(pos<1 || pos>size()) {
throw new Exception("参数pos不正确");
}
int len = 1;
Node cur = head.next;
Node pre = head;
Node ret;
while(cur != null) { // 1 2 3 4
if(len == pos) {
ret = cur;
pre.next = ret.next;
return ret.dt;
}
len++;
pre = cur;
cur = cur.next;
}
throw new Exception("参数pos超出链表长度");
}
@Override
public int size() {
int size = 0;
Node cur = head.next;
while(cur != null) {
size++;
}
return size;
}
}
class Node{
public DataType dt;
public Node next;
public Node(DataType dt, Node next) {
this.dt = dt;
this.next = next;
}
}
循环链表实现
/**
*和单链表相比多了一个只向前驱的指针,头结点的pre只向末尾节点,末尾节点的next只向头结点
*/
public class LoopLinkedList implements List{
private Node head;
//创建头结点
public LoopLinkedList() {
head = new Node(null, new DataType(-1), null);
head.next = head;
head.pre = head;
}
@Override
public boolean isEmpty() throws Exception {
return head.next== head? true : false;
}
@Override
public void clearList() throws Exception {
}
@Override
public DataType getElement(int pos) throws Exception {
if(pos<1) {
throw new Exception("pos参数不正确");
}
int len = 1;
Node cur = head.next;
while(cur.next!=head) {
if(len == pos) {
return cur.dt;
}
cur = cur.next;
len++;
}
return null;
}
@Override
public boolean locateElement(DataType ele) throws Exception {
Node cur = head.next;
while(cur.next != head) {
if(cur.dt == ele) {
return true;
}
cur = cur.next;
}
return false;
}
@Override
public void insert(int pos, DataType ele) throws Exception {
if(pos<1 || pos>size()) {
throw new Exception("pos参数不正确");
}
Node cur = head.next;
int len = 1;
while(len != pos) {
cur = cur.next;
len++;
}
Node pre = cur.pre;
Node nNode = new Node(null, ele, null);
pre.next = nNode;
nNode.pre = pre;
nNode.next = cur;
cur.pre = nNode;
}
@Override
public DataType delete(int pos) throws Exception {
if(pos<1 || pos>size()) {
throw new Exception("pos参数不正确");
}
Node cur = head.next;
int len = 1;
while(len != pos) {
len++;
cur = cur.next;
}
cur.pre.next = cur.next;
cur.next.pre = cur.pre;
cur.next = null;
cur.pre = null;
return cur.dt;
}
@Override
public int size() {
int len = 0;
Node cur = head.next;
while(cur.next != head) {
len++;
cur = cur.next;
}
return len;
}
class Node{
public Node pre;
public DataType dt;
public Node next;
public Node(Node pre, DataType dt, Node next) {
this.pre = pre;
this.dt = dt;
this.next = next;
}
}
}