有头单链表的一般写法
#include <stdio.h>
#include <stdlib.h>
//数据可以单独封装
typedef struct Node {
int data; //数据可以是任何类型
struct Node* next; //指针域
}NODE,*LPNODE;
//有表头的链表, 创建表头 --> 创建结构体变量
LPNODE createHead() {
LPNODE headNode = (LPNODE)malloc(sizeof(NODE));
if (NULL == headNode) {
printf("头节点内存申请失败!\n");
return NULL;
}
//初始化
//headNode->data = 0; //表头数据域无需初始化
headNode->next = NULL;
return headNode;
}
//创建数据节点
LPNODE createNode(int data) {
LPNODE newNode = (LPNODE)malloc(sizeof(NODE));
if (NULL == newNode) {
printf("数据节点内存申请失败!\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
//头插
void insertByHead(LPNODE headNode,int data) {
LPNODE newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
//尾插
void insertByTail(LPNODE headNode, int data) {
LPNODE newNode = createNode(data);
LPNODE curNode = headNode;
while (curNode->next != NULL) {
curNode = curNode->next;
}
curNode->next = newNode;
}
//指定插
void insertByAppoint(LPNODE headNode, int data, int pos) {
LPNODE newNode = createNode(data);
LPNODE curNode = headNode->next;
while (curNode->next != NULL && --pos) {
curNode = curNode->next;
}
newNode->next = curNode->next;
curNode->next = newNode;
}
//头删
void deleteByHead(LPNODE headNode) {
LPNODE delNode = headNode->next;
if (NULL == delNode) {
printf("链表为空, 删除失败!\n");
return;
}
headNode->next = delNode->next;
free(delNode);
delNode = NULL;
}
//尾删
void deleteByTail(LPNODE headNode) {
LPNODE curNode = headNode->next;
LPNODE preNode = headNode;
if (NULL == curNode) {
printf("链表为空, 删除失败!\n");
return;
}
while (curNode->next != NULL) {
preNode = curNode;
curNode = curNode->next;
}
free(curNode);
curNode = NULL;
preNode->next = NULL;
}
//尾删2
void deleteByTail2(LPNODE headNode) {
LPNODE delNode = NULL;
LPNODE curNode = headNode;
if (NULL == headNode->next) {
printf("链表为空,删除失败!\n");
return;
}
while (curNode->next->next != NULL) {
curNode = curNode->next;
}
delNode = curNode->next;
curNode->next = NULL;
free(delNode);
delNode = NULL;
}
//指定删
void deleteByAppoint(LPNODE headNode, int posData) {
LPNODE preNode = headNode;
LPNODE curNode = headNode->next;
if (NULL == preNode->next) {
printf("链表为空,无数据可以删除!\n");
return;
}
//此处顺序不能变 NULL->data 会引发冲突
while (curNode != NULL && curNode->data != posData) {
//preNode = preNode->next;
preNode = curNode;
curNode = curNode->next;
}
if (NULL == curNode) {
printf("未找到指定数据!\n");
return;
}
else {
preNode->next = curNode->next;
free(curNode);
curNode = NULL;
}
}
//查找节点
LPNODE searchByData(LPNODE headNode, int posData) {
LPNODE curNode = headNode->next;
while (curNode != NULL && curNode->data != posData) {
curNode = curNode->next;
}
return curNode;
}
//需要删除相同的元素
void deleteAll(LPNODE headNode, int posData) {
while (searchByData(headNode, posData) != NULL) {
deleteByAppoint(headNode, posData);
}
}
//链表打印
void printList(LPNODE headNode) {
LPNODE curNode = headNode->next;
while (curNode != NULL) {
printf("%d\t", curNode->data);
curNode = curNode->next;
}
printf("\n");
}
int main()
{
//头插
printf("---- 头插 -----: \n");
LPNODE list = createHead();
for (int i = 0; i < 3; i++) {
insertByHead(list, 520 + i);
insertByHead(list, 520 + i);
}
printList(list);
//尾插
printf("---- 尾插 -----: \n");
insertByTail(list, 1314);
printList(list);
//指定位置插入
printf("---- 指定位置插入 -----: \n");
insertByAppoint(list, 666, 3);
printList(list);
//头删
printf("---- 头删 -----: \n");
deleteByHead(list);
printList(list);
//尾删
printf("---- 尾删 -----: \n");
deleteByTail(list);
printList(list);
//尾删2
printf("---- 尾删2 -----: \n");
deleteByTail2(list);
printList(list);
//指定数据删除
printf("---- 指定数据521删除 -----: \n");
deleteByAppoint(list, 521);
printList(list);
//删除全部指定数据
printf("---- 指定数据522全部删除 -----: \n");
deleteAll(list, 522);
printList(list);
system("pause");
return 0;
}