有头单链表的再封装写法
#include <stdio.h>
#include <stdlib.h>
//封装节点
typedef struct Node {
int data;
struct Node* next;
}NODE, * LPNODE;
//再封装的方式
typedef struct list {
//描述结构的特性
LPNODE headNode;
int listSize; //记录当前链表节点个数
}LIST, * LPLIST;
LPNODE creatHead() {
LPNODE headNode = (LPNODE)malloc(sizeof(NODE));
if (NULL == headNode) {
printf("链表头内存申请失败!\n");
return NULL;
}
headNode->next = NULL;
return headNode;
}
LPNODE creatNode(int data) {
LPNODE newNode = (LPNODE)malloc(sizeof(NODE));
if (NULL == newNode) {
printf("数据节点内存申请失败!\n");
return NULL;
}
newNode->next = NULL;
newNode->data = data;
return newNode;
}
LPLIST createList() {
//创建过程就是描述最初的状态
LPLIST list = (LPLIST)malloc(sizeof(LIST));
if (NULL == list) {
printf("链表内存申请失败!\n");
return NULL;
}
list->headNode = creatHead();
list->listSize = 0;
return list;
}
//头插
void insertByHead(LPLIST list, int data) {
if (NULL != list) {
LPNODE newNode = creatNode(data);
newNode->next = list->headNode->next;
list->headNode->next = newNode;
list->listSize++;
}
}
//尾插
void insertByTail(LPLIST list, int data) {
if (NULL != list) {
LPNODE newNode = creatNode(data);
LPNODE curNode = list->headNode;
while (curNode->next != NULL) {
curNode = curNode->next;
}
curNode->next = newNode;
list->listSize++;
}
}
//指定位置插入
void insertByAppoint(LPLIST list, int data, int pos) {
if (NULL != list) {
LPNODE newNode = creatNode(data);
LPNODE curNode = list->headNode;
while (curNode->next != NULL && pos--) {
curNode = curNode->next;
}
newNode->next = curNode->next;
curNode->next = newNode;
list->listSize++;
}
}
//头删
void deleteByHead(LPLIST list) {
if (NULL != list) {
LPNODE delNode = list->headNode->next;
if (NULL != delNode) {
list->headNode->next = delNode->next;
free(delNode);
delNode = NULL;
list->listSize--;
}
else {
printf("链表为空,删除失败!\n");
}
}
}
//尾删
void deleteByTail(LPLIST list) {
if (NULL != list) {
LPNODE curNode = list->headNode->next;
LPNODE delNode = NULL;
if (NULL != curNode) {
while (curNode->next->next != NULL) {
curNode = curNode->next;
}
delNode = curNode->next;
free(delNode);
delNode = NULL;
curNode->next = NULL;
list->listSize--;
}
else {
printf("链表为空,尾删失败!\n");
}
}
}
//指定数据删除
void deleteByAppoint(LPLIST list, int posData) {
if (NULL != list) {
LPNODE curNode = list->headNode->next;
LPNODE preNode = list->headNode;
if (NULL != curNode) {
while (curNode != NULL && curNode->data != posData) {
preNode = curNode;
curNode = curNode->next;
}
if (curNode == NULL) {
printf("没有指定数据节点,删除失败!\n");
return;
}
else {
preNode->next = curNode->next;
free(curNode);
curNode = NULL;
list->listSize--;
}
}
else {
printf("链表为空,指定数据删除失败!\n");
}
}
}
//查找节点
LPNODE searchByData(LPLIST list, int posData) {
LPNODE curNode = list->headNode->next;
while (curNode != NULL && curNode->data != posData) {
curNode = curNode->next;
}
return curNode;
}
//删除指定数据全部节点
void deleteAllByData(LPLIST list, int posData) {
while (searchByData(list, posData) != NULL) {
deleteByAppoint(list, posData);
}
}
//打印链表
void printList(LPLIST list) {
if (NULL != list) {
LPNODE curNode = list->headNode->next;
while (curNode != NULL) {
printf("%d\t", curNode->data);
curNode = curNode->next;
}
printf("\n");
printf("链表长度: %d\n\n", Size(list));
}
}
//万金油函数
int empty(LPLIST list) {
return list->listSize == 0;
}
int Size(LPLIST list) {
return list->listSize;
}
int main()
{
LPLIST list = createList();
//头插
for (int i = 0; i < 3; i++) {
insertByHead(list, 520 + i);
insertByHead(list, 520 + i);
}
printList(list);
//尾插
insertByTail(list, 1314);
printList(list);
//指定位置插入
insertByAppoint(list, 666, 2);
printList(list);
//头删
deleteByHead(list);
printList(list);
//尾删
deleteByTail(list);
printList(list);
//指定数据删
deleteByAppoint(list, 521);
printList(list);
//删除全部指定数据
deleteAllByData(list, 520);
printList(list);
system("pause");
return 0;
}