#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));
NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode)) ;
tempHeader->data = '\0';
tempHeader->next = NULL;
return tempHeader;
}// Of initLinkList
/**
* Print the list.
* @param paraHeader The header of the list.
输出所有的data数据,直到最后一个节点。
*/
void printList(NodePtr paraHeader){
NodePtr p = paraHeader->next;//跳过头节点;
while (p != NULL) {
printf("%c", p->data);
p = p->next;
}// Of while
printf("\r\n");
}
/*
末尾插入一个新节点,其数据为 paraChar
*/
void appendElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
// Step 1. Construct a new node.创建新节点q
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
//在参数+1 后插入新节点。
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
NodePtr p, q;
// Step 1. Search to the position.找到第 paraPosition+1 个节点 参数为 1则找到第二个节点。
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.创建新的节点,新的节点指向 参数+1 个节点
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
// Step 3. Now link.
printf("linking\r\n");
q->next = p->next;// 新节点指向 参数+2 节点
p->next = q;//参数+1 节点指向新节点。 头节点【节点2【节点3【.....【参数+1【新节点【参数+2
}// Of insertElement
/*
*/
void deleteElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
p = paraHeader;
while ((p->next != NULL) && (p->next->data != paraChar)){
p = p->next;
}// Of while 寻找目标节点前面的一个节点p;
if (p->next == NULL) {
printf("Cannot delete %c\r\n", paraChar);
return;
}// Of if 找到末尾没找到就返回错误。
q = p->next; //q为目标节点
p->next = p->next->next;//p指向目标节点o的指向。跳过 p-q-o
free(q);
}// Of deleteElement
/**
* Unit test.
*/
void appendInsertDeleteTest(){
// Step 1. Initialize an empty list.
LinkList tempList = initLinkList();//创建新的头指针。
printf("打印头指针") ;
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).
printf("删除,e,a,o \n");
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
printList(tempList);
// Step 4. Insert to a given position.
insertElement(tempList, 'o', 1);
printList(tempList);
printf("除去头节点后第二个节点后加入新节点,测试结束\n\r");
}// 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();
basicAddressTest();
}// Of main
本次构造单链表代码,使用分函数进行具体操作,用malloc进行内存分配,同时在设定结构体同时设定了指针变量名称,减少*使用。