前提代码
public class Main
{
public static final int OK = 1;
public static final int ERROR = 0;
public static final boolean TRUE = true;
public static final boolean FALSE = false;
public static final int MAXSIZE = 20;
public static class SqList
{
int[] data = new int[MAXSIZE];
int length;
}
}
//data用于存储顺序表的元素,length用于记录顺序表的当前长度
打印整数
public static int visit(int c)
{
System.out.print(c);
return OK;
}
初始化顺序线性表
public static void main(String[] args)
{
SqList myList = new SqList(MAXSIZE);
if (myList.length == 0)
{
System.out.println("List initialized successfully.");
}
else
{
System.out.println("Failed to initialize the list.");
}
}
顺序线性表L已存在,判定是否空表
public static boolean listEmpty(SqList L)
{
return L.length == 0;
}
顺序线性表L已存在,将表重置为空表
public static int clearList(SqList L)
{
L.length = 0;
return OK;
}
返回线性表中数据元素个数
public static int listLength(SqList L)
{
return L.length;
}
返回线性表中第i个数据元素的值
public static int getElem(SqList L, int i) throws Exception
{
if(L.length == 0 || i < 1 || i > L.length)
throw new Exception("ERROR");
return L.data[i-1];
}
返回线性表中第1个与e满足关系的数据元素的位序
public static int locateElem(SqList L, int e)
{
if (L.length == 0)
return 0;
int i;
for(i = 0; i < L.length; i++)
{
if (L.data[i] == e)
break;
}
if(i >= L.length)
return 0;
return i + 1;
}
在线性表中第i个位置之前插入新的数据元素e,L的长度加1
public static int listInsert(SqList L, int i, int e)
{
if (L.length == MAXSIZE) /* The list is full */
return ERROR;
if (i < 1 || i > L.length + 1) /* When i is smaller than the first position or larger than the last position */
return ERROR;
if (i <= L.length)
{
/* If the inserted data position is not at the end of the list */
for(int k = L.length - 1; k >= i - 1; k--)
/* Move the data elements after the insertion position back by one position */
L.data[k + 1] = L.data[k];
}
L.data[i - 1] = e; /* Insert the new element */
L.length++;
return OK;
}
public static void main(String[] args)
{
// Create a list and add some elements
SqList list = new SqList();
list.length = 5;
for (int i = 0; i < list.length; i++)
list.data[i] = i + 1;
// Insert an element
if (listInsert(list, 3, 10) == OK)
System.out.println("Element inserted successfully");
else
System.out.println("Error inserting element");
}
删除线性表的第i个数据元素,并用e返回其值,线性表的长度减1
public static int listDelete(SqList L, int i)
{
if (L.length == 0) /* The list is empty */
return ERROR;
if (i < 1 || i > L.length) /* The deletion position is incorrect */
return ERROR;
int e = L.data[i - 1]; // Assign the value of the i-th element in the list L to the variable e
if (i < L.length)
{
/* If the deletion is not in the last position */
for(int k = i; k < L.length; k++)
/* Move the successor elements of the deletion position forward */
L.data[k - 1] = L.data[k];
}
L.length--;
return e;
}
public static void main(String[] args)
{
// Create a list and add some elements
SqList list = new SqList();
list.data = new int[10];
list.length = 5;
list.maxSize = 10;
for (int i = 0; i < list.length; i++)
list.data[i] = i + 1;
// Delete an element
int e = listDelete(list, 3);
if (e != ERROR)
System.out.println("Deleted element: " + e);
else
System.out.println("Error deleting element");
}
依次对线性表的每个数据元素输出
public static void listTraverse(SqList L)
{
for(int i = 0; i < L.length; i++)
visit(L.data[i]);
System.out.println();
}
public static void visit(int e)
{
System.out.print(e + " ");
}
public static void main(String[] args)
{
// Create a list and add some elements
SqList list = new SqList();
list.data = new int[10];
list.length = 5;
for (int i = 0; i < list.length; i++)
list.data[i] = i + 1;
// Traverse the list
listTraverse(list);
}
将所有的在线性表Lb中但不在La中的数据元素插入到La中
public static void unionL(SqList La, SqList Lb)
{
int La_len, Lb_len, i;
La_len = La.length; /* Get the length of the list */
Lb_len = Lb.length;
for (i = 1; i <= Lb_len; i++)
{
int e = getElem(Lb, i);
/* Get the i-th element in Lb and assign it to e */
if (!locateElem(La, e))
/* If there is no element in La that is the same as e */
listInsert(La, ++La_len, e); /* Insert */
}
}
public static void main(String[] args)
{
SqList L = new SqList();
SqList Lb = new SqList();
int j, k, e;
initList(L);
System.out.println("After initializing L: L.length=" + L.length);
for(j = 1; j <= 5; j++)
listInsert(L, 1, j);
System.out.print("After inserting 1~5 at the beginning of L: L.data=");
listTraverse(L);
System.out.println("L.length=" + L.length);
boolean isEmpty = listEmpty(L);
System.out.println("Is L empty: " + isEmpty);
clearList(L);
System.out.println("After clearing L: L.length=" + L.length);
isEmpty = listEmpty(L);
System.out.println("Is L empty: " + isEmpty);
for(j = 1; j <= 10; j++)
listInsert(L, j, j);
System.out.print("After inserting 1~10 at the end of L: L.data=");
listTraverse(L);
System.out.println("L.length=" + L.length);
listInsert(L, 1, 0);
System.out.print("After inserting 0 at the beginning of L: L.data=");
listTraverse(L);
System.out.println("L.length=" + L.length);
e = getElem(L, 5);
System.out.println("The value of the 5th element is: " + e);
for(j = 3; j <= 4; j++)
{
k = locateElem(L, j);
if(k != 0)
System.out.println("The value of the " + k + "th element is " + j);
else
System.out.println("There is no element with the value " + j);
}
k = L.length; /* k is the length of the list */
for(j = k + 1; j >= k; j--)
{
e = listDelete(L, j); /* Delete the j-th element */
if(e == ERROR)
System.out.println("Failed to delete the " + j + "th element");
else
System.out.println("The value of the deleted " + j + "th element is: " + e);
}
System.out.print("Print the elements of L in order: ");
listTraverse(L);
j = 5;
e = listDelete(L, j); /* Delete the 5th element */
System.out.println("The value of the deleted " + j + "th element is: " + e);
System.out.print("Print the elements of L in order: ");
listTraverse(L);
// Construct a Lb with 10 numbers
initList(Lb);
for(j = 6; j <= 15; j++)
listInsert(Lb, 1, j);
unionL(L, Lb);
System.out.print("Print the elements of L that has merged with Lb in order: ");
listTraverse(L);
}