本文共 1577 字,大约阅读时间需要 5 分钟。
来源:https://www.bilibili.com/video/BV1Rb411F738?from=search&seid=2404768265956671653
讲的还可以,但是拿ps画图让我感觉有点难受,哈哈,代码风格很棒!值得学习!#include#include struct Node { int data; struct Node* next;};//创建链表 struct Node * createList(){ struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); //headNode成为了结构体变量,变量使用前必须被初始化,headNode->data = 1; headNode->next = NULL; return headNode;}//创建结点struct Node * createNode(int data){ struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode;}//打印结点void printList(struct Node* headNode){ struct Node* pMove = headNode->next; while (pMove) { printf("%d\t", pMove->data); pMove = pMove->next; } printf("\n");}//插入结点void insertNodeByHead(struct Node* headNode, int data){ struct Node* newNode = createNode(data); newNode->next = headNode->next; headNode->next = newNode;}//删除结点void deleteNodeByAppoin(struct Node* headNode, int posData){ struct Node* posNode = headNode->next; struct Node* posNodeFront = headNode; if (posNode == NULL) { printf("无法删除空链表\n"); } else { while (posNode->data != posData) { posNodeFront = posNode; posNode = posNodeFront->next; if (posNode == NULL) { printf("没有找到相关结点无法删除"); return; } } posNodeFront->next = posNode->next; free(posNode); }}int main(void){ struct Node* list = createList(); insertNodeByHead(list, 1); insertNodeByHead(list, 2); insertNodeByHead(list, 3); printList(list); deleteNodeByAppoin(list, 2); printList(list); system("pause"); return 0;}
转载地址:http://wemji.baihongyu.com/