《数据结构》(严蔚敏 吴伟民)学习笔记 - 第二章 之 静态链表
作者:互联网
有时,可以用数组来描述线性链表。这种描述方法便于在不设“指针”类型的高级程序设计语言中使用链表结构。数组的一个分量表示节点,同时用游标代替指针指示节点在数组中的相对位置。数组的第零个分量可看成头节点,其指针域指示链表的第一个节点。这种存储结构仍需要预先分配一个较大的空间,但在作线性表的插入和删除操作时不需要移动元素,故仍具有链式存储结构的主要优点。为了和指针型描述的线性链表相区别,称这种用数组描述的链表为静态链表。
下图显示在线性表ABCD中删除B和插入Test之后的状况。
import java.util.*;
public class TDS0203_ArrayForPoint
{
final static int MAXSIZE = 100; //Default data number
public static void printArray(String[] data, int[] cur)
{
int pos = cur[0];
System.out.println("All the elements are: ");
while(pos != 0)
{
System.out.print(data[pos] + " ");
pos = cur[pos];
}
System.out.println();
}
public static void insertElement(String[] data, int[] cur, String newElement)
{
//insert the new element at the end of the list
int i = cur[0];
int j = 0;
while(i != 0)
{
j = i;
i = cur[i];
}
data[j + 1] = newElement;
cur[j] = j + 1;
cur[j + 1] = 0;
}
public static void removeElement(String[] data, int[] cur, String element)
{
int j = 0;
int i = cur[0];
String currentElement = data[i];
while((i != 0) && !currentElement.equals(element))
{
j = i;
i = cur[i];
currentElement = data[i];
}
if(i == 0)
{
System.out.println("Not found " + element);
}
else
{
cur[j] = cur[i];
}
}
public static int getLoc(String[] data, int[] cur, String element)
{
int pos = cur[0];
String currentElement = data[pos];
while(pos != 0 && (!currentElement.equals(element)))
{
pos = cur[pos];
currentElement = data[pos];
}
if(pos == 0)
{
return -1;
}
else
{
return pos;
}
}
public static String getElement(String[] data, int[] cur, int loc)
{
if(loc == 0)
return "Error Index";
int counter = 1;
int pos = cur[0];
while(pos != 0 && counter < loc)
{
pos = cur[pos];
counter++;
}
if(pos == 0 )
{
return "Error index";
}
else
{
return data[pos];
}
}
public static void main(String[] args)
{
String[] data = new String[MAXSIZE];
int[] cur = new int[MAXSIZE];
Scanner scanner = new Scanner(System.in);
System.out.print("Input the data number:"); //The number of the list
int datanumber = scanner.nextInt();
cur[0] = 1;
for(int j = 1; j <= datanumber; j++) //For example, "A, B, C, D"
{
System.out.print("Input the data: ");
data[j] = scanner.next();
cur[j] = j + 1;
}
cur[datanumber] = 0;
printArray(data, cur);
//Just for test
removeElement(data, cur, "B");
insertElement(data, cur, "Test");
removeElement(data, cur, "Sun");
printArray(data, cur);
String name = getElement(data, cur, 4);
System.out.println("The name is " + name);
}
}
与指针不同,在静态链表中插入数据和删除数据时,用户需要自己实现分配和回收这两个函数。为了辨明数组中哪些分量未被使用,需要将使用过以及被删除的分量用游标链成一个备用链表,每当进行插入时可从备用链表上取得第一个节点作为待插入的新节点;反之,在删除时将从链表中删除的节点链接到备用链表上。
以集合运算(A - B)U(B - A)为例:
import java.util.*;
public class TDS0204_Difference
{
final static int MAXSIZE = 100; //Default size of the space
public static void Init_Space(int[] spa, int[] cur)
{
for(int i = 0; i < MAXSIZE; i++)
{
spa[i] = i + 1;
cur[i] = i + 1;
}
spa[MAXSIZE - 1] = 0;
cur[MAXSIZE - 1] = 0;
}
public static int Malloc_Space(int[] spa)
{
int i = spa[0]; //Get the availabe space index
if(spa[0] != 0)
{
spa[0] = spa[i];
}
return i;
}
public static void Free_Space(int[] spa, int k)
{
spa[k] = spa[0]; //Free the space, add it to the space
spa[0] = k;
}
public static void printArray(String[] data, int[] cur)
{
int pos = cur[0];
while(pos != 0)
{
System.out.print(data[pos] + " ");
pos = cur[pos];
}
System.out.println();
}
public static void main(String[] args)
{
int[] space = new int[MAXSIZE];
int[] cur = new int[MAXSIZE];
String[] data = new String[MAXSIZE];
Init_Space(space, cur);
Scanner scanner = new Scanner(System.in);
System.out.print("Input Set A number: "); //The number of Set A: 5
int numberA = scanner.nextInt();
System.out.print("Input Set B number: ");
int numberB = scanner.nextInt(); //The number of Set B: 6
int startpos = 0;
data[0] = "Orig_Null"; //Default is null, set "Orig_Null"
int pos = 0;
System.out.println("Input Set A data: "); //For example, "c, b, e, g, f"
for(int i = 0; i < numberA; i++)
{
pos = Malloc_Space(space);
data[pos] = scanner.next();
}
cur[pos] = 0;
printArray(data, cur);
System.out.println("Input Set B data: ");
String dataB;
int p;
int k;
for(int i = 0; i < numberB; i++) //For example, "a, b, n, h, f, m"
{
dataB = scanner.next();
p = startpos;
k = cur[p];
while((k != 0) && (!data[k].equals(dataB)))
{
p = k;
k = cur[k];
}
if(k != 0) //Found dataB, delete it, and free the space
{
cur[p] = cur[k];
Free_Space(space, k);
}
else //Not found dataB, insert it into Set A
{
pos = Malloc_Space(space);
cur[p] = pos;
data[pos] = dataB;
cur[pos] = 0;
}
}
printArray(data, cur); //Display the final info
for(int i = 0; i < 10; i++) //Display the space info
{
System.out.print(space[i] + " ");
}
System.out.println();
for(int i = 0; i < 10; i++) //Display the link info
{
System.out.print(cur[i] + " ");
}
System.out.println();
}
}
标签:cur,int,pos,System,链表,严蔚敏,数据结构,data,String 来源: https://www.cnblogs.com/Tom-1103/p/12018464.html