公共接口
// 线性表公共接口IList
public interface IList {
// 清除数据为空表
public void clear();
// 判断是否是空表
public boolean isEmpty();
// 读取i位置上的值
public Object get(int i) throws Exception;
// 在i位置上插入x
public void insert(int i, Object x) throws Exception;
// 移除i位置的数据x
public void remove(int i) throws Exception;
// 返回x数据的位置
public int indexOf(Object x);
// 输出所有数据
public void display();
}
顺序表
public class SequenceList implements IList {
private int defaultCapacity = 10;
private Object[] listElem; // 线性表存储空间
private int curLen; // 此处是线性表长度。当前长度是线性表的长度, 数据长度是最大长度
// 初始化线性表
public SequenceList() {
curLen = 0;
listElem = new Object[defaultCapacity];
}
public SequenceList(Integer maxSize) {
curLen = 0;
listElem = new Object[maxSize];
}
@Override
public void clear() {
listElem = new Object[defaultCapacity];
curLen = 0;
}
@Override
public boolean isEmpty() {
return curLen == 0;
}
@Override
public Object get(int i) throws Exception {
if (i < 0 || i > curLen - 1) {
throw new Exception("第" + i + "元素不存在");
}
return listElem[i];
}
@Override
public void insert(int i, Object x) throws Exception {
if (curLen == listElem.length) {
throw new Exception("顺序表已满");
}
if (i < 0 || i > curLen) {
throw new Exception("插入位置不合理");
}
for (int j = curLen; j > i; j--) {
//插入位置及之后的元素后移
listElem[j] = listElem[j - 1];
}
listElem[i] = x;
curLen++;
}
@Override
public void remove(int i) throws Exception {
if (curLen == 0) {
throw new Exception("顺序表为空");
}
if (i < 0 || i > curLen) {
throw new Exception("删除位置不合理");
}
for (int j = i; j < curLen - 1; j++) {
listElem[j] = listElem[j + 1];
}
curLen--;
}
@Override
public int indexOf(Object x) {
int j = 0;
while (j < curLen && !listElem[j].equals(x)) {
j++;
}
if (j < curLen) {
return j;
} else
return -1;
}
@Override
public void display() {
for (int j = 0; j < curLen; j++) {
System.out.print(listElem[j] + " ");
}
System.out.println();// 换行
}
public Object[] getListElem() {
return listElem;
}
public void setListElem(Object[] listElem) {
this.listElem = listElem;
}
public int getCurLen() {
return curLen;
}
public void setCurLen(int curLen) {
this.curLen = curLen;
}
}
链表
public class LinkList implements IList {
private Object data;
private LinkList next;
public LinkList() {
next = null;
}
public LinkList(LinkList linkList) {
linkList = new LinkList();
linkList.next = null;
}
public void creatListHead(int n, String[] str) {
next = null;
for (int i = 0; i < n; i++) {
LinkList linkList = new LinkList();
linkList.data = str[i];
linkList.next = this.next;
this.next = linkList;
}
}
public void creatListTail(int n, String[] str) {
next = new LinkList();
LinkList p = next;
for (int i = 0; i < n; i++) {
LinkList linkList = new LinkList();
// linkList.data = str[i];
p.data = str[i];
p.next = linkList;
if ((i + 1) != n) {
p = linkList;
} else {
p.next = null;
}
}
}
@Override
public void clear() {
LinkList p = next;
while (p != null) {
LinkList q = p.next;
p.data = null;
p.next = null;
p = q;
}
data = null;
next = null;
}
@Override
public boolean isEmpty() {
if (next != null) {
return false;
} else
return true;
}
@Override
public void insert(int i, Object x) throws Exception {
int j = 0;
LinkList p, s;
p = next;
j = 2;
// 使用p != null,在最后也能插入
while (p != null && j < i) {
p = p.next;
++j;
}
if (p == null || j > i) {
System.out.println("不能插入");
}
s = new LinkList();
s.data = x;
s.next = p.next;
p.next = s;
}
@Override
public Object get(int i) throws Exception {
int j = 1;
LinkList p = next;
while (p != null && j < i) {
p = p.next;
j++;
}
if (p == null || j > i) {
System.out.println("没有此元素");
}
return p.data;
}
@Override
public void remove(int i) throws Exception {
int j = 0;
LinkList p, s;
p = next;
j = 2;
// 防止删除尾节点时指针指空
while (p.next != null && j < i) {
p = p.next;
j++;
}
if (p == null || j > i) {
System.out.println("没有此元素");
}
s = p.next;
p.next = s.next;
}
@Override
public int indexOf(Object x) {
int j = 0;
LinkList p = next;
j = 1;
while (p != null && p.data != x) {
p = p.next;
j++;
}
if (p != null || p.data == x) {
return j;
} else {
System.out.println("没有此元素");
return -1;
}
}
@Override
public void display() {
LinkList p = next;
if(p == null){
System.out.println("无元素");
}
while (p != null) {
System.out.print(p.data + " ");
p = p.next;
}
System.out.println();
}
}
输出结果
//顺序表(第一个结点从0开始)
public static void main(String[] args) {
System.out.println("顺序表输出结果如下:");
SequenceList sequenceList = new SequenceList();
System.out.println("初始化顺序表后:顺序表当前位置" + sequenceList.getCurLen());
try {
for (int j = 0; j < 5; j++)
sequenceList.insert(j, j + 1);
System.out.println("在顺序表的表头依次插入1〜5后:顺序表元素如下");
sequenceList.display();
System.out.println("顺序表当前位置" + sequenceList.getCurLen());
boolean empty = sequenceList.isEmpty();
System.out.println("顺序表是否空:" + empty);
int i = sequenceList.indexOf(3);
System.out.println("顺序表中元素3的位置是:" + i);
sequenceList.remove(2);
System.out.println("在顺序表中删除元素2后:顺序表元素如下");
sequenceList.display();
System.out.println("顺序表当前位置" + sequenceList.getCurLen());
sequenceList.clear();
System.out.println("清空顺序表后:顺序表当前位置" + sequenceList.getCurLen());
SequenceList sq2 = new SequenceList(20);
System.out.println("初始化20个元素的顺序表,顺序表当前位置" + sq2.getCurLen());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
顺序表输出结果如下:
初始化顺序表后:顺序表当前位置0
在顺序表的表头依次插入1〜5后:顺序表元素如下
1 2 3 4 5
顺序表当前位置5
顺序表是否空:false
顺序表中元素3的位置是:2
在顺序表中删除元素2后:顺序表元素如下
1 2 4 5
顺序表当前位置4
清空顺序表后:顺序表当前位置0
初始化20个元素的顺序表,顺序表当前位置0
//链表(第一个结点从1开始,可认为0指头结点)
public static void main(String[] args) {
System.out.println("链表输出结果如下:");
try {
LinkList linkList = new LinkList();
linkList.creatListTail(3, new String[]{"a", "b", "c"});
boolean empty = linkList.isEmpty();
System.out.println("插入元素a,b,c后,链表是否为空 " + empty);
System.out.println("插入元素a,b,c后,遍历链表如下");
linkList.display();
Object o = linkList.get(2);
System.out.println("获取第2个元素:" + o.toString());
linkList.insert(3, "w");
System.out.println("在3的位置上插入元素w,遍历链表如下");
linkList.display();
int w = linkList.indexOf("w");
System.out.println("元素w对应位置是 " + w);
linkList.remove(3);
System.out.println("删除元素w,遍历链表如下");
linkList.display();
linkList.remove(3);
System.out.println("删除元素c,遍历链表如下");
linkList.display();
linkList.remove(2);
System.out.println("删除元素b,遍历链表如下");
linkList.display();
System.out.println("无法在第1个位置插入及删除,详情如下");
linkList.insert(1, "Q");
System.out.println("在1的位置上插入元素Q,遍历链表如下");
linkList.display();
linkList.remove(1);
System.out.println("删除元素a,遍历链表如下");
linkList.display();
linkList.clear();
System.out.println("清空链表如下,再遍历如下");
linkList.display();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
链表输出结果如下:
插入元素a,b,c后,链表是否为空 false
插入元素a,b,c后,遍历链表如下
a b c
获取第2个元素:b
在3的位置上插入元素w,遍历链表如下
a b w c
元素w对应位置是 3
删除元素w,遍历链表如下
a b c
删除元素c,遍历链表如下
a b
删除元素b,遍历链表如下
a
无法在第1个位置插入及删除,详情如下
不能插入
在1的位置上插入元素Q,遍历链表如下
a Q
没有此元素
删除元素a,遍历链表如下
a
清空链表如下,再遍历如下
无元素