#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}Node;
//创建带头结点的单链表
Node* initNode() {
Node* Head = (Node*)malloc(sizeof(Node));//
Node *L = Head;//链表L申请内存
Node* LNew;
L->next = NULL;
for (int i = 0; i < 5; i++) {
LNew = (Node*)malloc(sizeof(Node));
LNew->next = NULL;
LNew->data = i;
L->next = LNew;
L = LNew;
}
return Head;//将头节点指针所带的地址传回去
}
//头插法(有头节点)
void headInsert(Node** L, int i) {
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = i;
temp->next = (*L)->next;
(*L)->next = temp;
}
//尾插法(有头节点)
void tailInsert(Node** L, int i) {
Node* p = *L;//创建指针p遍历链表,直到p->next==NULL时停止。
Node* temp = (Node*)malloc(sizeof(Node));
temp->data = i;
while (1) {
if (p->next == NULL)
break;
p = p->next;
}
p->next = temp;
temp->next = NULL;
}
//删除链表某数值对应的节点
void deleteNode(Node** L, int e) {
if ((*L == NULL))
return;