单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。
1.正确理解前驱和后继的关系
一定要把握好前驱和后继之间的关系,只有得到前驱结点才能对后继结点进行操作,因为单链表是逐一相连的,前驱结点的指针域中存放着后继结点的地址,也就是说失去前驱也就意味着丢失后继,所以在对前驱结点的指针域做改动时一定要进行备份,不能让其丢失。
2.结点的插入过程
(1).创建一个结点:结点内存空间的分配、结点数据域的赋值。
(2).将结点插入链表:更改插入结点的指针域(此时指针域内的值来源于前驱结点指针域内的值)、更改前驱结点的指针域(此时指针域内的值为要插入结点的地址)。在插入数据时,一定要先将前驱的指针域的值先赋值给插入结的指针域,然后再更改前驱结点指针域的值,否则会造成后续结点的丢失。
3.结点删除的过程
(1).获取该结点的前驱结点。
(2).备份一份要删除结点的地址。
(3).断开结点与链表的连接:将要删除结点的指针域值赋给它前驱结点的指针域。
(4).释放要删除结点内存:这里就需要利用到备份的删除结点的地址,如果前面没有先对删除结点的地址进行备份,那么经过步骤(3)就会丢失删除结点的地址,将无法释放删除结点的内存空间。
————————————————
版权声明:本文为CSDN博主「红心火柴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
下见代码
#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;
}
/*
* Print the list.
* @param oaraHeader The header of the list.
*/
void printfList(NodePtr paraHeader) {
NodePtr p = paraHeader->next;
while (p != NULL) {
printf("%c", p->data);
p = p -> next;
}
printf("\r\n");
}
/*
* 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;
}
//step 3.now add/link.
p->next = q;
}
/*
* 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;
}
}
//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;
}
/*
* 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;
}
if (p->next == NULL) {
printf("Cannot delete %c\r\n", paraChar);
return;
}
q = p->next;
p->next = p->next->next;
free(q);
}
/*
*unit test.
*/
void appendInsertDeleteTest() {
//step 1.Initialize an empty list.
LinkList tempList = initLinkList();
printfList(tempList);
//step 2.add some characters.
appendElement(tempList, 'H');
appendElement(tempList, 'e');
appendElement(tempList, 'l');
appendElement(tempList, 'l');
appendElement(tempList, 'o');
appendElement(tempList, '!');
printfList(tempList);
//step 3.delete some characters(the first occurrence)
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
printfList(tempList);
//step 4.insert to a given position.
insertElement(tempList, 'o', 1);
printfList(tempList);
}
/*
* adderss 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;
}
/*
* the entrance.
*/
int main() {
appendInsertDeleteTest();
}
Hello!
Cannot delete a
Hll!
linking
Holl!
D:\数据算法\单链表\x64\Debug\单链表.exe (进程 41728)已退出,代码为 0。
按任意键关闭此窗口. . .