在静态链表中,每一个结点包含两部分内容,一部分是data(即有意义的数据),另一部分是cur(存储该元素下一个元素所在数组对应的下标)。
有几个特殊的结点:
首先是下标为0的结点中不包含有意义的数据,它的cur存储的是备用链表(即没有存储的数据的那些结点)第一个结点的下标。
最后就是链表的最后一个元素(并不一定是数组的最后一个元素),链表最后一个元素的cur一般存放-1,表示它后面的结点为空了。
1.创建结点
typedef struct StaticLinkedNode{
char data;
int next;
} *NodePtr;
typedef struct StaticLinkedList{
NodePtr nodes;
int* used;
} *ListPtr;
2.创建链表
ListPtr initLinkedList(){
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct StaticLinkedList));
tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
tempPtr->used[0] = 1;
int i;
for (i = 1; i < DEFAULT_SIZE; i ++){
tempPtr->used[i] = 0;
}
return tempPtr;
}
3.打印链表
void printList(ListPtr paraListPtr){
int p = 0;
while (p != -1) {
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}// Of while
printf("\r\n");
}
4.插入结点
ListPtr initLinkedList(){
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct StaticLinkedList));
tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
tempPtr->used[0] = 1;
int i;
for (i = 1; i < DEFAULT_SIZE; i ++){
tempPtr->used[i] = 0;
}
return tempPtr;
}
5.删除结点
void deleteElement(ListPtr paraListPtr, char paraChar){
int p, q;
p = 0;
while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)){
p = paraListPtr->nodes[p].next;
}
if (paraListPtr->nodes[p].next == -1) {
printf("Cannot delete %c\r\n", paraChar);
return;
}
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
paraListPtr->used[q] = 0;
}
6.测试
void appendInsertDeleteTest(){
ListPtr tempList = initLinkedList();
printList(tempList);
insertElement(tempList, 'a', 0);
insertElement(tempList, 'p', 1);
insertElement(tempList, 'p', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'e', 4);
printList(tempList);
printf("Deleting 'e'.\r\n");
deleteElement(tempList, 'e');
printf("Deleting 'a'.\r\n");
deleteElement(tempList, 'a');
printf("Deleting 'p'.\r\n");
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'x', 1);
printList(tempList);
}
7.总程序和运行结果
#include <stdio.h>
#include <malloc.h>
#define DEFAULT_SIZE 5
typedef struct StaticLinkedNode{
char data;
int next;
} *NodePtr;
typedef struct StaticLinkedList{
NodePtr nodes;
int* used;
} *ListPtr;
ListPtr initLinkedList(){
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct StaticLinkedList));
tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
tempPtr->used[0] = 1;
int i;
for (i = 1; i < DEFAULT_SIZE; i ++){
tempPtr->used[i] = 0;
}
return tempPtr;
}
void printList(ListPtr paraListPtr){
int p = 0;
while (p != -1) {
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}// Of while
printf("\r\n");
}// Of printList
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
int p, q, i;
p = 0;
for (i = 0; i < paraPosition; i ++) {
p = paraListPtr->nodes[p].next;
if (p == -1) {
printf("The position %d is beyond the scope of the list.\r\n", paraPosition);
return;
}
}
for (i = 1; i < DEFAULT_SIZE; i ++){
if (paraListPtr->used[i] == 0){
printf("Space at %d allocated.\r\n", i);
paraListPtr->used[i] = 1;
q = i;
break;
}
}
if (i == DEFAULT_SIZE){
printf("No space.\r\n");
return;
}
paraListPtr->nodes[q].data = paraChar;
printf("linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
}
void deleteElement(ListPtr paraListPtr, char paraChar){
int p, q;
p = 0;
while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)){
p = paraListPtr->nodes[p].next;
}
if (paraListPtr->nodes[p].next == -1) {
printf("Cannot delete %c\r\n", paraChar);
return;
}
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
paraListPtr->used[q] = 0;
}
void appendInsertDeleteTest(){
ListPtr tempList = initLinkedList();
printList(tempList);
insertElement(tempList, 'a', 0);
insertElement(tempList, 'p', 1);
insertElement(tempList, 'p', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'e', 4);
printList(tempList);
printf("Deleting 'e'.\r\n");
deleteElement(tempList, 'e');
printf("Deleting 'a'.\r\n");
deleteElement(tempList, 'a');
printf("Deleting 'p'.\r\n");
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'x', 1);
printList(tempList);
}
void main(){
appendInsertDeleteTest();
}