1 实现静态链表模拟
1.1 代码实现
#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;
/**
* Initailize the list with header.
*@return The pointer to the header
*/
ListPtr initLinkedList() {
// The pointer to the whole list space.
ListPtr tempPtr = (ListPtr)malloc(sizeof(struct StaticLinkedList));
// Allocate total space.
tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
// The first node is the header.
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
// Noly the first node is used.
tempPtr->used[0] = 1;
for(int i = 0; i < DEFAULT_SIZE; i++) {
tempPtr->used[i] = 0;
}
return tempPtr;
}
/**
* Print the list.
*@param paraListPtr The pointer to the list.
*/
void printList(ListPtr paraListPtr) {
int p = 0;
while(p != -1) {
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}
printf("\r\n");
}
/**
*Insert an element to the given position
*@param paraListPtr The position of the list.
*@param paraChar The given char.
*@param paraPositon The given position.
*/
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition) {
int p, q, i;
// Step 1. Search to the position.
p = 0;
for(i = 0; i < paraPosition; i++) {
p =paraListPtr->nodes[p].next;
if(p == -1) {
printf("The positon %d is beyond the scope of the list.\r\n", paraPosition);
return;
}
}
// Step 2. Construct a new node.
for(i = 1; i < DEFAULT_SIZE; i++) {
if(paraListPtr->used[i] == 0) {
// The is identical to malloc.
printf("Space at %d alloccated.\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;
// Step 3. Now link.
printf("Linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = q;
}
/**
*Delete an element from the list.
*@param paraHeader The header of the list.
*@param paraChar The given char.
*/
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;
// The statement is identical to free(q)
paraListPtr->used[q] = 0;
}
/**
* Unit test
*/
void appendInsertDeleteTest() {
// Step 1. Initailize an empty list.
ListPtr tempList = initLinkedList();
printList(tempList);
// Step 2. Add some characters.
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
printList(tempList);
//Step 3. Delete some characters (the first occurrence).
printf("Deleting 'e'.\r\n");
deleteElement(tempList, 'e');
printf("Deleting 'a'.\r\n");
deleteElement(tempList, 'a');
printf("Deleting 'o'.\r\n");
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'x', 1);
printList(tempList);
}
/**
* The entrance.
*/
void main() {
appendInsertDeleteTest();
}
1.2 输出效果
2 代码分析
为了创建一个静态链表,首先在内存中申请了一块内存空间,内存空间实际可以分为三个部分,第一个部分是数据域,第二个部分是结点域,而第三个部分用来判断对应数据域是否空闲。将数组的下标当作静态链表中每一个内存空间的相对地址,通过这个相对地址实现对 c 语言 malloc函数以及 free 函数的模拟。
模拟 malloc 函数的具体思路如下:规定当判断域数组下标所对应的数据为 0 时,此下标对应的内存空间(数据域)为空,可以存放数据,当其下标为 1 时,则表示无法存放数据。在此基础上,通过遍历判断域数组,找到 0 所对应的下标,返回该下标,即可模拟 malloc 效果
模拟 free 函数,传入对应的下标给模拟的free函数,通过在free函数中遍历判断域,找到该下标对应的数据,使用 0 将其覆盖即可,表示这个下标对应的内存空间为空闲状态。