-
单链表
链表是一种常见的基础数据结构,链表的出现很大程度上弥补了数组的先天不足。链表是结构体变量和结构体变量连接在一起
单链表和数组相比较,单链表插入指定位置的效率要比数组高很多。
链表的一个结点拥有信息域(head)和指针域
- 单链表结点的声明
#include <stdio.h> struct Book { char title[128]; char author[40]; struct Book* next; }; int main(void) { return 0; }
- 动态申请一个链表(动态内存申请+模块化设计)
(1)创建链表(创建一个表头表示整个链表)
(2)创建结点
(3)插入结点
(4)删除结点
(5)打印遍历链表(测试)
创建一个简单的单链表:#include <stdio.h> #include <stdlib.h> struct Node { int data; //数据域 struct Node* next; //指针域 }; struct Node*creatlist() { struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); headNode->next = NULL; return headNode; }; int main(void) { struct Node* lis = creatlist(); return 0; }
然后为链表创建结点
struct Node*creatNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; };
- 在单链表中插入元素(头插法)
#include <stdio.h> #include <stdlib.h> struct Node { int data; //数据域 struct Node* next; //指针域 }; struct Node*creatlist() { struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); headNode->next = NULL; return headNode; }; struct Node*creatNode(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", pMove->data); pMove = pMove->next; } printf("\n"); } void insertNodeByHead(struct Node* headNode, int data) { //创建插入的结点 struct Node* newNode = creatNode(data); newNode->next = headNode->next; headNode->next = newNode; } int main(void) { struct Node* list = creatlist(); insertNodeByHead(list, 1); insertNodeByHead(list, 2); insertNodeByHead(list, 3); printList(list); return 0; }
- 链表的删除(指定位置删除)
#include <stdio.h> #include <stdlib.h> struct Node { int data; //数据域 struct Node* next; //指针域 }; struct Node*creatlist() { struct Node* headNode = (struct Node*)malloc(sizeof(struct Node)); headNode->next = NULL; return headNode; }; struct Node*creatNode(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 = creatNode(data); newNode->next = headNode->next; headNode->next = newNode; } void deleteNodeByAppin(struct Node* headNode, int posData) { struct Node* posNode = headNode->next; struct Node* posNodeFront = headNode; if (posNode==NULL) { printf("链表为空,无法删除\n"); } else { while (posNode->next != posNode) { posNodeFront = posNode; posNode = posNodeFront->next; if (posNode==NULL) { printf("没有相关信息,无法删除\n"); return; } } posNodeFront->next = posNode->next; free(posNode); } } int main(void) { struct Node* list = creatlist(); insertNodeByHead(list, 1); insertNodeByHead(list, 2); insertNodeByHead(list, 3); printList(list); deleteNodeByAppin(list, 4); printList(list); return 0; }