单链表完整代码
- LinkList.h
- LinkList.c
- test.c
LinkList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
struct LNode* next;
}LNode;
void LinkListPrint(LNode* phead);
void LinkListPushBack(LNode** phead,ElemType x);
void LinkListPushFront(LNode** phead, ElemType x);
void LinkListPopBack(LNode** phead);
void LinkListPopFront(LNode** phead);
LNode* LinkListFind(LNode* pphead, ElemType x);
void LinkListInsert(LNode** phead, LNode* pos, ElemType x);
void LinkListErase(LNode** phead,LNode* pos);
LinkList.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "LinkList.h"
void LinkListPrint(LNode* phead)
{
LNode* cur = phead;
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void LinkListPushBack(LNode** pphead, ElemType x)
{
LNode* newNode = (LNode*)malloc(sizeof(LNode));
if (newNode == NULL)
{
printf("%s", strerror(errno));
}
else
{
newNode->data = x;
newNode->next = NULL;
}
if (*pphead == NULL)
{
*pphead = newNode;
}
else
{
LNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newNode;
}
}
void LinkListPushFront(LNode** pphead, ElemType x)
{
LNode* newNode = (LNode*)malloc(sizeof(LNode));
newNode->data = x;
newNode->next = NULL;
newNode->next = *pphead;
*pphead = newNode;
}
void LinkListPopBack(LNode** pphead)
{
if (*pphead == NULL)
return;
else if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
LNode* prev = NULL;
LNode* tail = *pphead;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
void LinkListPopFront(LNode** pphead)
{
if (*pphead == NULL)
return;
LNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
LNode* LinkListFind(LNode* pphead, ElemType x)
{
LNode* cur = pphead;
while (cur != NULL)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void LinkListInsert(LNode** pphead, LNode* pos, ElemType x)
{
if (pos == *pphead)
{
LinkListPushFront(pphead, x);
}
else
{
LNode* newNode = (LNode*)malloc(sizeof(LNode));
newNode->data = x;
newNode->next = NULL;
LNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = newNode;
newNode->next = pos;
}
}
void LinkListErase(LNode** pphead, LNode* pos)
{
if (pos == *pphead)
{
LinkListPopFront(pphead);
}
else
{
LNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "LinkList.h"
void LinkListTest1()
{
LNode* plist = NULL;
LinkListPushBack(&plist, 1);
LinkListPushBack(&plist, 2);
LinkListPushBack(&plist, 3);
LinkListPushBack(&plist, 4);
LinkListPushFront(&plist, 0);
LinkListPrint(plist);
LinkListPopFront(&plist);
LinkListPopFront(&plist);
LinkListPopFront(&plist);
LinkListPopFront(&plist);
LinkListPrint(plist);
LinkListPopFront(&plist);
LinkListPrint(plist);
}
void LinkListTest2()
{
LNode* plist = NULL;
LinkListPushBack(&plist, 1);
LinkListPushBack(&plist, 2);
LinkListPushBack(&plist, 3);
LinkListPushBack(&plist, 4);
LinkListPushFront(&plist, 0);
LinkListPrint(plist);
LinkListPopBack(&plist);
LinkListPopBack(&plist);
LinkListPrint(plist);
LinkListPopBack(&plist);
LinkListPopBack(&plist);
LinkListPrint(plist);
}
void LinkListTest3()
{
LNode* plist = NULL;
LinkListPushBack(&plist, 1);
LinkListPushBack(&plist, 2);
LinkListPushBack(&plist, 3);
LinkListPushBack(&plist, 4);
LinkListPushFront(&plist, 0);
LinkListPrint(plist);
LNode* pos = LinkListFind(plist, 0);
if (pos)
{
LinkListInsert(&plist, pos, 30);
}
LinkListPrint(plist);
}
void LinkListTest4()
{
LNode* plist = NULL;
LinkListPushBack(&plist, 1);
LinkListPushBack(&plist, 2);
LinkListPushBack(&plist, 3);
LinkListPushBack(&plist, 4);
LinkListPushFront(&plist, 0);
LinkListPrint(plist);
LNode* pos = LinkListFind(plist, 0);
if (pos)
{
LinkListErase(&plist, pos);
}
pos = LinkListFind(plist, 4);
if (pos)
{
LinkListErase(&plist, pos);
}
pos = LinkListFind(plist, 2);
if (pos)
{
LinkListErase(&plist, pos);
}
LinkListPrint(plist);
}
int main()
{
LinkListTest4();
return 0;
}