#include <stdio.h>
#include <malloc.h>//mallo函数使用
/**
* 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));//强制转化类型为NodePtr的指针并开辟数据大小为LNode的空间
tempHeader->data = '\0';//头节点
tempHeader->next = NULL;//头节点的指针
return tempHeader;
}// of initLinkList
/**
* Print the list.
* @param 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 a element to the tail.
* @param paraHeader The header of th 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空间。
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;
int i = 0;
for ( 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 paraHeader 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 if
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, '!');
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();
}// Of main
malloc函数
1 malloc是动态内存分配函数,用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址。
2 malloc函数头文件:#include<malloc.h>。
3 malloc函数返回值:如果分配成功则返回指向被分配内存的指针, 否则返回空指针NULL。
4 malloc函数使用注意事项:malloc函数的返回的是无类型指针,在使用时一定要强制转换为所需要的类型。在使用malloc开辟空间时,使用完成一定要释放空间,如果不释放会造内存泄漏。
在使用malloc函数开辟的空间中,不要进行指针的移动,因为一旦移动之后可能出现申请的空间和释放空间大小的不匹配。
5 指针自身 = (指针类型*)malloc(sizeof(指针类型)*数据数量)
free函数
释放malloc函数给指针变量分配的内存空间。(指针变量一定要重新指向NULL,防止悬空指针出现)。
心得
在思考代码问题时将自己智商降低,变得和计算机一样,根据代码要能将单链表添加,插入,删除通过内存或物理逻辑表示出来。这样才能牢记方法不容易遗忘。