无头链表再封装方式
#include <stdio.h>
#include <stdlib.h>
//节点数据类型
typedef struct Node {
int data;
struct Node* next;
}NODE,*LPNODE;
//链表数据类型
typedef struct List {
LPNODE frontNode; //指向表头
LPNODE tailNode; //指向表尾
int listSize;
}LIST, * LPLIST;
//创建数据节点函数
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;
}
//创建链表
LPLIST createList() {
LPLIST list = (LPLIST)malloc(sizeof(LIST));
if (NULL == list) {
printf("链表节点申请失败!\n");
return NULL;
}
list->tailNode = NULL;
list->frontNode = NULL;
list->listSize = 0;
return list;
}
//判断链表是否为空
int empty(LPLIST list) {
return list->listSize == 0;
}
//返回链表长度
int size(LPLIST list) {
return list->listSize;
}
//头插
void insertByHead(LPLIST list, int data) {
LPNODE newNode = createNode(data);
if (empty(list)) {
list->frontNode = newNode;
list->tailNode = newNode;
}
else {
newNode->next = list->frontNode;
list->frontNode = newNode;
}
list->listSize++;
}
//尾插
void insertByTail(LPLIST list, int data) {
LPNODE newNode = createNode(data);
if (empty(list)) {
list->frontNode = newNode;
list->tailNode = newNode;
}
else {
list->tailNode->next = newNode;
list->tailNode = newNode; //表尾移动到新的表尾
}
list->listSize++;
}
//指定插
void insertByAppoint(LPLIST list, int data, int pos) {
LPNODE newNode = createNode(data);
if (empty(list)) {
list->frontNode = newNode;
list->tailNode = newNode;
}
else {
LPNODE curNode = list->frontNode;
while (curNode->next != NULL && --pos) {
curNode = curNode->next;
}
newNode->next = curNode->next;
curNode->next = newNode;
if (newNode->next == NULL) { //若插入到表尾, 表尾指针指向新节点
list->tailNode = newNode;
}
}
list->listSize++;
}
//头删
void deleteByHead(LPLIST list) {
LPNODE delNode = list->frontNode;
if (empty(list)) {
printf("链表为空, 数据删除失败!\n");
return;
}
else {
if (delNode->next == NULL)
list->tailNode = NULL;
list->frontNode = delNode->next;
}
free(delNode);
delNode = NULL;
list->listSize--;
}
//尾删
void deleteByTail(LPLIST list) {
LPNODE preNode = list->frontNode;
LPNODE delNode = list->frontNode;
if (empty(list)) {
printf("链表为空,数据删除失败!\n");
return;
}
else {
if (delNode->next == NULL)
list->frontNode = NULL;
while (delNode->next != NULL) {
preNode = delNode;
delNode = delNode->next;
}
if (list->frontNode == NULL) {
list->tailNode = NULL;
}
else {
list->tailNode = preNode;
}
}
preNode->next = NULL;
free(delNode);
delNode = NULL;
list->listSize--;
}
//指定数据删除
void deleteByAppoint(LPLIST list, int posData) {
LPNODE curNode = list->frontNode;
LPNODE preNode = list->frontNode; //保存删除节点的前一个节点
if (empty(list)) {
printf("链表为空,数据删除失败!\n");
return;
}
if (curNode->data == posData) { //第一个节点且就是指定删除数据
if (curNode->next == NULL) { //只有一个节点
list->frontNode = NULL;
list->tailNode = NULL;
}
else { //后面还有节点
list->frontNode = curNode->next;
}
preNode = NULL;
}
else { //删除节点在第一个节点之后
while (curNode->next != NULL && curNode->data != posData) {
preNode = curNode;
curNode = curNode->next;
}
if(curNode->next == NULL)
list->tailNode = preNode;
preNode->next = curNode->next;
}
free(curNode);
curNode = NULL;
list->listSize--;
}
//查找
LPNODE searchNodeByData(LPLIST list, int posData) {
LPNODE curNode = list->frontNode;
while (curNode != NULL && curNode->data != posData) {
curNode = curNode->next;
}
return curNode;
}
void deleteAllAppointData(LPLIST list, int posData) {
while (searchNodeByData(list,posData) != NULL) {
deleteByAppoint(list, posData);
}
}
//打印
void printList(LPLIST list) {
LPNODE curNode = list->frontNode;
while (curNode != NULL) {
printf("%d\t", curNode->data);
curNode = curNode->next;
}
printf("\n");
}
//销毁链表
void destroyList(LPLIST* pList) {
if (*pList != NULL) {
for (int i = 0; i < (*pList)->listSize; i++) {
deleteByHead(*pList);
}
free(*pList);
*pList = NULL;
}
}
int main()
{
LPLIST list = createList();
//头插
for (int i = 0; i < 5; i++)
{
insertByHead(list, 520 + i);
insertByHead(list, 520 + i);
}
printList(list);
//尾插
insertByTail(list, 100);
printList(list);
//指定位置插入
insertByAppoint(list, 1001, 2);
printList(list);
//头删
deleteByHead(list);
printList(list);
//尾删
deleteByTail(list);
printList(list);
//指定数据删除
deleteByAppoint(list, 1001);
printList(list);
//全部删除
deleteAllAppointData(list, 522);
printList(list);
if (list != NULL) {
printf("链表未销毁!\n");
}
//链表销毁
destroyList(&list);
if (list == NULL) {
printf("链表已销毁!\n");
}
system("pause");
return 0;
}