LinkList.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW -1
typedef int ElementType;
typedef struct ListNode
{
ElementType data;
struct ListNode* next;
}ListNode;
void ListInit(ListNode** pphead);
ListNode* BuyNewnode(ElementType e);
void ListPrint(ListNode* phead);
void ListPushBack(ListNode* phead, ElementType e);
void ListPushFront(ListNode* phead, ElementType e);
void CreatList_T(ListNode* phead, int n);
void CreatList_H(ListNode* phead, int n);
int ListEmpty(ListNode* phead);
int ListDestroy(ListNode** pphead);
int ListClear(ListNode* phead);
int ListLength(ListNode* phead);
ListNode* ListGetElem(ListNode* phead, int pos);
ListNode* ListSearchByVal(ListNode* phead, ElementType e);
int ListSearchByVal_pos(ListNode* phead, ElementType e);
int ListDeleteByPos(ListNode** pphead, int pos, ElementType* e);
int ListDeleteByPos_second(ListNode* phead, int pos);
int ListDeleteByNode(ListNode* phead, ListNode* p);
int ListDeleteByVal(ListNode* phead, ElementType e);
int ListInsert(ListNode* phead, int pos, ElementType e);
LinkList.c
#define _CRT_SECURE_NO_WARNINGS
#include "LinkList.h"
void ListInit(ListNode** pphead)
{
*pphead = (ListNode*)malloc(sizeof(struct ListNode));
if (*pphead == NULL)
{
exit(OVERFLOW);
}
(*pphead)->next = NULL;
}
ListNode* BuyNewnode(ElementType e)
{
ListNode* newnode = (ListNode*)malloc(sizeof(struct ListNode));
if (newnode == NULL)
{
exit(OVERFLOW);
}
newnode->data = e;
newnode->next = NULL;
return newnode;
}
void ListPrint(ListNode* phead)
{
assert(phead);
ListNode* cur = phead->next;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
void ListPushBack(ListNode* phead, ElementType e)
{
assert(phead);
ListNode* newnode = BuyNewnode(e);
ListNode* tail = phead;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
void ListPushFront(ListNode* phead, ElementType e)
{
assert(phead);
ListNode* newnode = BuyNewnode(e);
newnode->next = phead->next;
phead->next = newnode;
}
void CreatList_T(ListNode* phead, int n)
{
ListNode* tail = phead;
int i = 0;
for (i = 0; i < n; i++)
{
ElementType e;
scanf("%d", &e);
ListNode* newnode = BuyNewnode(e);
tail->next = newnode;
tail = newnode;
}
}
void CreatList_H(ListNode* phead, int n)
{
int i = 0;
for (i = 0; i < n; i++)
{
ElementType e;
scanf("%d", &e);
ListNode* newnode = BuyNewnode(e);
newnode->next = phead->next;
phead->next = newnode;
}
}
int ListEmpty(ListNode* phead)
{
if (phead->next)
{
return FALSE;
}
else
{
return TRUE;
}
}
int ListDestroy(ListNode** pphead)
{
assert(pphead);
ListNode* p;
while (*pphead)
{
p = *pphead;
*pphead = (*pphead)->next;
free(p);
p = NULL;
}
return OK;
}
int ListClear(ListNode* phead)
{
assert(phead);
ListNode* p, *q;
p = phead->next;
while (p)
{
q = p->next;
free(p);
p = q;
}
phead->next = NULL;
return OK;
}
int ListLength(ListNode* phead)
{
assert(phead);
ListNode* p = phead->next;
int i = 0;
while (p)
{
i++;
p = p->next;
}
return i;
}
ListNode* ListGetElem(ListNode* phead, int pos)
{
assert(phead);
assert(pos >= 1 && pos <= ListLength(phead));
ListNode* p = phead->next;
int i = 1;
while (i != pos)
{
p = p->next;
i++;
}
return p;
}
ListNode* ListSearchByVal(ListNode* phead, ElementType e)
{
ListNode* p = phead->next;
while (p && p->data!=e)
{
p = p->next;
}
return p;
}
int ListSearchByVal_pos(ListNode* phead, ElementType e)
{
ListNode* p = phead->next;
int i = 1;
while (p && p->data != e)
{
p = p->next;
i++;
}
if (p)
{
return i;
}
else
{
return 0;
}
}
int ListDeleteByPos(ListNode** pphead, int pos, ElementType* e)
{
assert(pphead);
assert(pos >= 1 && pos <= ListLength(*pphead));
ListNode* p = *pphead, *q;
while (pos-1)
{
p = p->next;
pos--;
}
q = p->next;
p->next = q->next;
*e = q->data;
free(q);
q = NULL;
return OK;
}
int ListDeleteByPos_second(ListNode* phead, int pos)
{
ListNode* p = phead, *q;
int i = 0;
while (p->next && i < pos - 1)
{
p = p->next;
i++;
}
if (p->next == NULL || i > pos - 1)
{
return ERROR;
}
q = p->next;
p->next = q->next;
free(q);
q = NULL;
return OK;
}
int ListDeleteByNode(ListNode* phead, ListNode* pos)
{
assert(phead);
ListNode* prev = phead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
return OK;
}
int ListDeleteByVal(ListNode* phead, ElementType e)
{
ListNode* prev = phead, *p;
while (prev->next && prev->next->data != e)
{
prev = prev->next;
}
if (prev->next == NULL)
{
return 0;
}
p = prev->next;
prev->next = p->next;
free(p);
p = NULL;
return OK;
}
int ListInsert(ListNode* phead, int pos, ElementType e)
{
ListNode* p = phead;
int i = 0;
while (p && i < pos - 1)
{
p = p->next;
i++;
}
if (p == NULL || i > pos - 1)
{
return ERROR;
}
ListNode* newnode = BuyNewnode(e);
newnode->next = p->next;
p->next = newnode;
return OK;
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include "LinkList.h"
void Test01()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
}
void Test02()
{
ListNode* list;
ListInit(&list);
ListPushFront(list, 1);
ListPushFront(list, 2);
ListPushFront(list, 3);
ListPushFront(list, 4);
ListPrint(list);
}
void Test03()
{
ListNode* list;
ListInit(&list);
CreatList_T(list, 5);
ListPrint(list);
}
void Test04()
{
ListNode* list;
ListInit(&list);
CreatList_H(list, 5);
ListPrint(list);
}
void Test05()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
ListDestroy(&list);
ListPrint(list);
}
void Test06()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
ListClear(list);
ListPrint(list);
}
void Test07()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
ListNode* ret = ListGetElem(list, 4);
printf("%d\n", ret->data);
}
void Test08()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
ListNode* p = ListSearchByVal(list, 1);
if (p == NULL)
{
printf("没有找到!\n");
}
else
{
printf("%d\n", p->data);
}
}
void Test09()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
int pos = ListSearchByVal_pos(list, 3);
if (pos)
{
printf("%d\n",pos);
}
else
{
printf("没有找到\n");
}
}
void Test10()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
ElementType e;
ListDeleteByPos(&list, 3, &e);
printf("删除的元素是:%d\n", e);
ListPrint(list);
}
void Test11()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
ListDeleteByPos_second(list, 3);
ListPrint(list);
}
void Test12()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
ListNode* pos = ListSearchByVal(list, 3);
if (pos)
{
printf("%d\n", pos->data);
ListDeleteByNode(list, pos);
ListPrint(list);
}
else
{
printf("要删除的节点不存在!\n");
}
}
void Test13()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
int ret = ListDeleteByVal(list, 3);
if (ret)
{
ListPrint(list);
}
else
{
printf("要删除的值不存在!\n");
}
}
void Test14()
{
ListNode* list;
ListInit(&list);
ListPushBack(list, 1);
ListPushBack(list, 2);
ListPushBack(list, 3);
ListPushBack(list, 4);
ListPrint(list);
int ret = ListInsert(list, 5, 22);
if (ret)
{
ListPrint(list);
}
else
{
printf("插入位置不合法!\n");
}
}
int main()
{
Test14();
return 0;
}