头插法代码如下
typedef struct Node {
struct Node* next;
int data;
}Node;
Node* head = (Node*)malloc(sizeof(Node));
void heading(Node* head) {
head->next = NULL;
head->data = 000;
for (int i = 0; i < 10; i++) {
//*********************************************
Node* p = (Node*)malloc(sizeof(Node));
p->next = NULL;
p->data = i;
p->next = head->next;
head->next = p;
//*********************************************
}
}
尾插法代码如下
typedef struct Node {
struct Node* next;
int data;
}Node;
Node* head = (Node*)malloc(sizeof(Node));
//*********************************************
Node* temp = head;
//*********************************************
void tailing(Node* head) {
head->next = NULL;
head->data = 000;
for (int i = 0; i < 10; i++) {
//*********************************************
Node* p = (Node*)malloc(sizeof(Node));
p->next = NULL;
p->data = i;
temp->next = p;
temp = temp->next;
//*********************************************
}
}
本文介绍了在C语言中使用头插法和尾插法创建动态链表的过程,包括结构体定义、内存分配以及插入节点的关键代码段。

被折叠的 条评论
为什么被折叠?



