代码如下:
#include <stdio.h>
#include <malloc.h>
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 = ' ';
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 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;
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
q->next = NULL;
p = paraHeader;
while (p->next != NULL) {
p = p->next;
}// of while
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;
p = paraHeader;
int i;
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
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
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 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(){
LinkList tempList = initLinkList();
printList(tempList);
appendElement(tempList, 'H');
appendElement(tempList, 'e');
appendElement(tempList, 'l');
appendElement(tempList, 'l');
appendElement(tempList, 'o');
appendElement(tempList, '!');
printList(tempList);
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'o', 1);
printList(tempList);
}// of apppendInsertDeleteTest
/**
* Address test: beyond the book.
*/
void basicAddressTest(){
LNode tempNode1, tempNode2;
tempNode1.data = 4;
tempNode1.next = NULL;
tempNode1.data = 6;
tempNode1.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);
}//of basicAddressTest
/**
* The entrance.
*/
int main(){
appendInsertDeleteTest();
basicAddressTest();
}//of main
运行结果:
函数1:打印链表
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
函数2:初始化链表,将元素逐一插入。
void appendElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
q->next = NULL;
p = paraHeader;
while (p->next != NULL) {
p = p->next;
}// of while
p->next = q;
}// of appendElement
函数3:向指定位置插入
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
NodePtr p, q;
p = paraHeader;
int i;
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
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
printf("linking\r\n");
q->next = p->next;
p->next = q;
}//of insertElement
函数4:删除指定元素
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
单链表的优点:①不会浪费太多内存。②元素个数不会受到限制。
收获总结:单链表是数据结构的储存结构中的链式结构的代表,通过对单链表的学习使我对数据结构的储存方式有了进一步的了解,更加充分的对链式结构有了了解。通过对闵帆老师代码的抄写,对代码的规范有了加强和提升了自己的编码能力。
通过上了三次数据结构课,让我对数据结构这门学科有了进一步的了解,同时也产生了一定的兴趣。
展望:希望在以后跟随闵帆老师学习可以更好的掌握数据结构的知识,提升自己的编码能力。