链表:相对于顺序表,链表主打的就是一个动态内存分配,可以在针对不确定的数据的存储时,具有空间利用率高等优势。其中最关键的就是malloc函数的调用。
malloc:malloc函数相当于在栈区开辟一个新的空间,用指针来存储这个空间的位置,所以,malloc函数一般用指针来进行接收。下面用int类型举例一般格式为
int*p = (int*) malloc ( sizeof(int) );
这样就已经在堆区开辟了一块由自己管理的空间(记得不用了要用free来回收,没有人希望自己的电脑里的内存里塞满没用的垃圾);
链表另一个关键所在就是指针——只向下一个结构体数据的指针,链表就是靠这来连接起来的。下面先看代码
#include <stdio.h>
#include <malloc.h>
/**
* Linked list of characters. The key is data.
*/
typedef struct LinkNode{
char data;
struct LinkNode *Next;
} LNode, *LinkList, *NodePtr;
/**
* Initialize the list with a header.
* @return The pointer to the header.
*/
LinkList initLinkList(){
NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
tempHeader->data = '\0';
tempHeader->next = NULL;
return tempHeader;
}//Of initLinkList
/**
* Print the List
* @parm paraHeader The header of the List.
*/
void printList(NodePtr paraHeader){
NodePtr p = paraHeader->next;
while (p != NULL) {
printf("%c", p->data);
p = p->next;
}//Of while
printf("\r\n");
}//Of printList
/**
* Add an element to the tail.
* @param paraHeader The header of the list.
* @param paraChar The given char.
*/
void appendElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
// Step 1. Construct a new node.
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
q->next = NULL;
// Step 2. Search to the tail.
p = paraHeader;
while (p->next != NULL) {
p = p->next;
}//Of while
// Step 3. Now add/link.
p->next = q;
}// Of appendElement
/**
* Insert an element to the given position.
* @param paraHeader The header of the list.
* @param paraChar The given char.
* @param paraPosition The given position.
*/
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
NodePtr p, q;
// Step 1. Search to the position.
p = paraHeader;
for (int i = 0; i < paraPosition; i ++) {
p = p->next;
if (p == NULL) {
printf("The position %d is beyond the scope of the list.", paraPosition);
return ;
}// Of if
}// Of for i
// Step 2. Construct a new node.
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
// Step 3. Now link.
printf("linking\r\n");
q->next = p->next;
p->next = q;
}// of insertElement
/**
* Delete an element from the list.
* @param paraHearder The header of the list.
* @param paraChar The given char.
*/
void deleteElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
p = paraHeader;
while ((p->next != NULL) && (p->next->data != paraChar)) {
p = p->next;
}//Of while
if (p->next == NULL) {
printf("Cannot delete %c\r\n", paraChar);
return ;
}// Of if
q = p->next;
p->next = p->next->next;
free(q);
}//Of deleteElement
/**
* Unit test
*/
void appendInsertDeleteTest(){
// Step 1.Initialize an empty list.
LinkList tempList = initLinkList();
printList(tempList);
// Step 2. Add some characters.
appendElement(tempList, 'H');
appendElement(tempList, 'e');
appendElement(tempList, 'l');
appendElement(tempList, 'l');
appendELement(tempList, 'o');
appendElement(tempList, 'l');
printList(tempList);
// Step 3. Delete some characters (the first occurrence).
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
printList(tempList);
// Step 4. Insert to a given position.
insertElement(tempList, 'o', 1);
printList(tempList);
}//Of appendInsertDeleteTest
/**
* Address test: beyond the book.
*/
void basicAddressTest(){
LNode tempNode1, tempNode2;
tempNode1.data = 4;
tempNode1.next = NULL;
tempNode2.data = 6;
tempNode2.next = NULL;
printf("The first node: %d, %d, %d\r\n", &tempNode1, &tempNode1.data, &tempNode1.next);
printf("The second node: %d, %d, %d\r\n",&tempNode2, &tempNode2.data, &tempNode2.next);
tempNode1.next = &tempNode2;
}//Of basicAddressTest
/**
* The entrance.
*/
int main(){
appendInsertDeleteTest();
return 0;
}//Of main.
下面是代码结果展示:
下面是图示: