html个人网站模板,深圳网站优化提供商,北京南站官网,做网站的时候说需求的专业术语2.单循环链表 data|next——data|next——data|next——头节点 1.初始化链表 2.增加节点#xff08;头插法、尾插法#xff09; 3.删除节点 4.遍历链表
定义一个结构体#xff0c;存放data域和指针域#xff1a;
typedef struct Node {//定义一个结构体…
2.单循环链表 data|next——data|next——data|next——头节点 1.初始化链表 2.增加节点头插法、尾插法 3.删除节点 4.遍历链表
定义一个结构体存放data域和指针域
typedef struct Node {//定义一个结构体存放data域和指针域int data;//数据域类型struct Node* next;
}Node;
初始化链表
Node* initList() {//初始化链表Node* L (Node*)malloc(sizeof(Node));L-data 0;L-next L;return L;
}
头插法
void headInsert(Node* L, int data) {//头插法Node* node (Node*)malloc(sizeof(Node));node-data data;node-next L-next;L-next node;
}
尾插法
void tailInsert(Node* L, int data) {//尾插法Node* n L;Node* node (Node*)malloc(sizeof(Node));node-data data;while (n-next ! L) {n n-next;}node-next L;n-next node;
}
删除
int Delete(Node* L, int data)//删除
{Node* preNode L;Node* node L-next;while (node ! L){if (node-data data) {//deletepreNode-next node-next;free(node);return true;}preNode node;node node-next;}return false;
}
遍历链表
void printList(Node* L) {//遍历链表Node* node L-next;while (node ! L) {printf(%d-, node-data);node node-next;}printf(NULL\n);
}
main函数
int main()
{Node* L initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);headInsert(L, 4);headInsert(L, 5);tailInsert(L, 6);tailInsert(L, 7);tailInsert(L, 8);tailInsert(L, 9);tailInsert(L, 10);printList(L);Delete(L, 4);Delete(L, 5);printList(L);return 0;
}
单循环链表函数
typedef struct Node {//定义一个结构体存放data域和指针域int data;//数据域类型struct Node* next;
}Node;Node* initList() {//初始化链表Node* L (Node*)malloc(sizeof(Node));L-data 0;L-next L;return L;
}void headInsert(Node* L, int data) {//头插法Node* node (Node*)malloc(sizeof(Node));node-data data;node-next L-next;L-next node;
}void tailInsert(Node* L, int data) {//尾插法Node* n L;Node* node (Node*)malloc(sizeof(Node));node-data data;while (n-next ! L) {n n-next;}node-next L;n-next node;
}int Delete(Node* L, int data)//删除
{Node* preNode L;Node* node L-next;while (node ! L){if (node-data data) {//deletepreNode-next node-next;free(node);return true;}preNode node;node node-next;}return false;
}void printList(Node* L) {//遍历链表Node* node L-next;while (node ! L) {printf(%d-, node-data);node node-next;}printf(NULL\n);
}int main()
{Node* L initList();headInsert(L, 1);headInsert(L, 2);headInsert(L, 3);headInsert(L, 4);headInsert(L, 5);tailInsert(L, 6);tailInsert(L, 7);tailInsert(L, 8);tailInsert(L, 9);tailInsert(L, 10);printList(L);Delete(L, 4);Delete(L, 5);printList(L);return 0;
}