本文使用Java实现线性表的顺序存储结构,虽然Java提供了动态数组ArrayList,但是自己动手做做是不一样的,欢迎交流想法!
一、源代码
在Eclipse中新建一个Java project,包括两个java源文件,SqList.java和SqListTest.java。
// SqList.java
/**
* 线性表的动态分配顺序存储结构
*
* @author YangYong
*
*/
public classSqList
private final int LIST_INT_SIZE = 10; // 线性表存储空间的初始分配量
private Object[] elem = null; // 存储空间基址
private int size; // 当前表长
/**
* 操作结果:构造一个空的顺序表
*/
public SqList() {
this.elem = new Object[LIST_INT_SIZE];// 存储分配
this.size = 0;// 空表长度为0
}
/**
* 初始条件:顺序表L已存在操作结果:销毁顺序表
*
* @param L
* 待销毁的顺序表
* @return null
*/
public SqList
// 提醒GC工作
L = null;
return L;
}
/**
* 初始条件:顺序表已存在操作结果:将顺序表置为空表
*/
public void clearList() {
// 提醒GC工作
for (int i = 0; i < elem.length; i++)
elem[i] = null;
this.size = 0;
}
/**
* 初始条件:顺序表已存在
*
* @return操作结果:若顺序表为空,则返回true,否则返回false
*/
public boolean listEmpty() {
return this.size == 0;
}
/**
* 初始条件:顺序表已存在
*
* @return操作结果:返回顺序表中元素个数
*/
public int listSize() {
return this.size;
}
/**
* 初始条件:顺序表已存在操作结果:将顺序表空间扩充为原来的2倍
*/
private void expandCapacity() {
// 扩充存储空间位原来的2倍
Object[]temp= newObject[this.size * 2];
// 复制原有的顺序表
int i;
for (i = 0; i < this.size; i++) {
temp[i] = this.elem[i];
// 提醒GC工作
this.elem[i] = null;
}
// 提醒GC工作
this.elem = null;
this.elem = temp;
}
/**
* 初始条件:顺序表已存在操作结果:将一个元素添加到顺序表尾,表长加1
*
* @param e
* 待添加的元素
*/
public void add(T e) {
if (this.size == this.elem.length) {
// 扩充存储空间位原来的2倍
this.expandCapacity();
this.elem[this.size++] = e;
}else
this.elem[this.size++] = e;
}
/**
* 初始条件:顺序表已存在,1<=i<=length
*
* @param i
* @return顺序表中第i个元素的值
*/
@SuppressWarnings("unchecked")
public T getElem(int i) {
if (i >= 1 && i <= this.size) {
return (T) elem[i - 1];
}else
return null;
}
/**
* 初始条件:顺序表已存在,1<=i<=length+1 操作结果:在顺序表中第i个位置之前插入新元素e,表长加1
*
* @param i
* @param e
* @return插入成功返回true,否则返回false
*/
public boolean listInsert(int i, T e) {
int j;
if (i >= 1 && i <= this.size + 1) {
if (this.size == this.elem.length) {
// 扩充存储空间位原来的2倍
this.expandCapacity();
// 移动原有元素
for (j = this.size; j >= i; j--)
this.elem[j] = this.elem[j - 1];
this.elem[i - 1] = e; // 插入新元素
this.size++; // 表长加1
}else{
// 移动原有元素
for (j = this.size; j >= i; j--)
this.elem[j] = this.elem[j - 1];
this.elem[i - 1] = e; // 插入新元素
this.size++; // 表长加1
}
return true;
}else
return false;
}
/**
* 初始条件:顺序表已存在且非空,1<=i<=length 操作结果,删除顺序表的第i个元素,并返回其值,表长减1
*
* @param i
* @return
*/
@SuppressWarnings("unchecked")
public T listDelete(int i) {
Objecttemp= null;
int j;
if (!this.listEmpty() &&(i >= 1&& i <= this.size)) {
temp = this.elem[i - 1];
for (j = i; j < this.size; j++)
this.elem[j - 1] = this.elem[j];
this.elem[this.size - 1] = null;
this.size--;
}
return (T) temp;
}
/**
* 初始条件:顺序表已存在操作结果:遍历顺序表
*
* @return遍历结果
*/
public String traversalList(){
Stringresult= newString();
for (Object temp : this.elem)
result += temp + " ";
return result;
}
}
// SqListTest.java
/**
* 测试SqList
*
* @author YangYong
*
*/
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.*;
public classSqListTest {
private JFrame frame = null;
private JPanel[] panel = null;
private JScrollPane sc = null;
private JButton[] button = null;
private JLabel[] label = null;
private JTextField[] textField = null;
private JTextArea textArea = null;
SqList
public static void main(String[] args) {
newSqListTest().myHandler();
}
public SqListTest() {
frame = new JFrame("顺序表的基本操作");
frame.setLayout(new GridLayout(2, 1));
int i;
panel = new JPanel[2];
button = new JButton[10];
label = new JLabel[4];
textField = new JTextField[4];
for (i = 0; i < textField.length; i++) {
textField[i] = new JTextField();
textField[i].setFont(new Font(Font.DIALOG, Font.BOLD, 15));
}
textArea = new JTextArea();
textArea.setFont(new Font(Font.DIALOG, Font.BOLD, 15));
textArea.setEditable(false);
panel[0] = new JPanel();
panel[0].setLayout(new GridLayout(6, 3));
button[0] = new JButton("顺序表初始化");
panel[0].add(button[0]);
button[1] = new JButton("销毁顺序表");
panel[0].add(button[1]);
button[2] = new JButton("顺序表置空");
panel[0].add(button[2]);
button[3] = new JButton("顺序表判空");
panel[0].add(button[3]);
button[4] = new JButton("求表长");
panel[0].add(button[4]);
button[5] = new JButton("遍历顺序表");
panel[0].add(button[5]);
label[0] = new JLabel("添加的元素值:");
button[6] = new JButton("添加元素");
panel[0].add(label[0]);
panel[0].add(textField[0]);
panel[0].add(button[6]);
label[1] = new JLabel("取的位置:");
button[7] = new JButton("取元素");
panel[0].add(label[1]);
panel[0].add(textField[1]);
panel[0].add(button[7]);
label[2] = new JLabel("插入的元素值(插入位置输入“取的位置”文本框中):");
button[8] = new JButton("插入元素");
panel[0].add(label[2]);
panel[0].add(textField[2]);
panel[0].add(button[8]);
label[3] = new JLabel("删除位置:");
button[9] = new JButton("删除元素");
panel[0].add(label[3]);
panel[0].add(textField[3]);
panel[0].add(button[9]);
panel[1] = new JPanel();
panel[1].setLayout(new BorderLayout());
sc = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel[1].add(sc);
frame.add(panel[0]);
frame.add(panel[1]);
int width, height;
width = Toolkit.getDefaultToolkit().getScreenSize().width;
height = Toolkit.getDefaultToolkit().getScreenSize().height;
frame.setBounds((width - 1000) >>> 2,(height- 808) >>> 2, 1000, 808);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
public void myHandler() {
button[0].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
list = new SqList<>();
textArea.append("\n初始化成功!");
}
});
button[1].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
list = list.destoryList(list);
textArea.append("\n销毁成功!");
}
});
button[2].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
list.clearList();
textArea.append("\n置空成功!");
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
button[3].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
if (list.listEmpty())
textArea.append("\n顺序表为空!");
else
textArea.append("\n顺序表非空!");
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
button[4].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
textArea.append("\n表长为:" + list.listSize());
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
button[5].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
textArea.append("\n遍历结果为:" + list.traversalList());
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
button[6].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
int t = Integer.valueOf(textField[0].getText());
list.add(t);
textArea.append("\n添加成功!");
}catch(NumberFormatException e1) {
textArea.append("\n数据格式不对!");
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
button[7].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
int t = Integer.valueOf(textField[1].getText());
Objectobj= list.getElem(t);
if (obj != null)
textArea.append("\n读取成功!数据为:" + obj);
else
textArea.append("\n读取位置不合法!");
}catch(NumberFormatException e1) {
textArea.append("\n数据格式不对!");
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
button[8].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
int t1 = Integer.valueOf(textField[1].getText());
int t2 = Integer.valueOf(textField[2].getText());
if (list.listInsert(t1, t2))
textArea.append("\n插入成功!");
else
textArea.append("\n插入失败!");
}catch(NumberFormatException e1) {
textArea.append("\n数据格式不对!");
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
button[9].addActionListener(new ActionListener() {
@Override
public voidactionPerformed(ActionEvent e) {
try {
int t = Integer.valueOf(textField[3].getText());
Objectobj= list.listDelete(t);
if (obj != null)
textArea.append("\n删除成功!数据为:" + obj);
else
textArea.append("\n删除位置不合法!");
}catch(NumberFormatException e1) {
textArea.append("\n数据格式不对!");
}catch(NullPointerException e1) {
textArea.append("\n空指针异常!");
}
}
});
}
}
二、运行结果与测试程序启动
测试