point_list.h
测试代码仅测试插入,删除请自行测试
typedef int ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
}PointList;
void InitPointList(PointList** pp); // 初始化
bool InsertPointList(PointList **pp, ElemType val, int pos); // 按位置插入
bool InsertHead(PointList **pp, ElemType val); // 头插
bool InsertTail(PointList **pp, ElemType val); // 尾插
bool DeletePonitList(PointList **pp, int pos); // 按位置删除
bool DeleteHead(PointList **pp); // 头删
bool DeleteTail(PointList **pp); // 尾删
bool DeleteValue(PointList **pp, ElemType val); // 按值删除
void DestroyPointList(PointList **pp); // 销毁
void ShowList(PointList** head);
PointList* ListQueryByVal(PointList** head, int val);
point_list.c
#include "point_list.h"
#include <stdlib.h>
#include <stdio.h>
static PointList* ApplyNode(ElemType val, PointList* nt)
{
PointList* s = (PointList*)malloc(sizeof(PointList));
if (s == NULL) return NULL;
s->data = val;
s->next = nt;
return s;
}
void InitPointList(PointList** pp)
{
if (pp == NULL) exit(0);
*pp = NULL;
}
bool InsertHead(PointList** pp, ElemType val)
{
if (pp == NULL) exit(0);
*pp = ApplyNode(val, *pp);
if (*pp == NULL) return false;
return true;
}
bool InsertPointList(PointList** pp, ElemType val, int pos)
{
if (pp == NULL) exit(0);
if (pos < 0) return false;
if (*pp == NULL || pos == 0) return InsertHead(pp, val);
PointList* p = *pp;
while (pos > 1 && p != NULL)
{
pos--;
p = p->next;
}
if (p == NULL) return false;
PointList* newNode = ApplyNode(val, p->next);
if (newNode == NULL) return false;
p->next = newNode;
return true;
}
bool InsertTail(PointList** pp, ElemType val)
{
if (pp == NULL) exit(0);
if (*pp == NULL) return InsertHead(pp, val);
PointList * p = *pp;
while (p->next != NULL) p = p->next;
p->next = ApplyNode(val, NULL);
return false;
}
bool DeleteHead(PointList** pp)
{
if (pp == NULL) exit(0);
if (*pp == NULL) return false;
PointList * q = *pp;
* pp = q->next;
free(q);
return true;
}
bool DeletePonitList(PointList * *pp, int pos)
{
if (pp == NULL) exit(0);
if (*pp == NULL || pos < 0) return false;
if (pos == 0)
{
return DeleteHead(pp);
}
PointList * p = *pp;
while (pos > 1 && p->next != NULL)
{
pos--;
p = p->next;
}
if (p->next == NULL) return false; // pos越界
PointList *q = p->next;
p->next = q->next;
free(q);
return true;
}
bool DeleteTail(PointList **pp)
{
if (pp == NULL) exit(0);
if (*pp == NULL) return false;
if ((*pp)->next == NULL) return DeleteHead(pp);
PointList *p = *pp;
PointList *q = p->next;
while (q->next != NULL)
{
p = q;
q = q->next;
}
p->next = NULL;
free(q);
return true;
}
bool DeleteValue(PointList * *pp, ElemType val)
{
if (pp == NULL) exit(0);
if (*pp == NULL) return false; // 进来就是空链表
while (*pp != NULL && (*pp)->data == val)
{
DeleteHead(pp);
}
if (*pp == NULL) return true; // 将链表删除成空链
PointList *p = *pp;
PointList *q = p->next;
while (q != NULL)
{
if (q->data == val)
{
p->next = q->next;
free(q);
q = p->next;
}
else
{
p = q;
q = q->next;
}
}
return true;
}
void DestroyPointList(PointList** pp)
{
if (pp == NULL) exit(0);
while (*pp != NULL)
{
DeleteHead(pp);
}
}
void ShowList(PointList** head)
{
if (head == NULL) exit(0);
PointList* p = (*head);
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
PointList* ListQueryByVal(PointList** head, int val)
{
if (head == NULL) exit(0);
PointList* p = *head;
while (p->data != val && p)
{
p = p->next;
}
return p;
}
test_main.c
#include "point_list.h"
void InsertPointListTest()
{
PointList* head;
InitPointList(&head);
for (int i = 0; i < 10; ++i)
{
InsertPointList(&head, i, 0);
}
ShowList(&head);
}
void InsertHeadTest()
{
PointList* head;
InitPointList(&head);
for(int i = 1; i <= 10; ++i)
{
InsertHead(&head, i);
}
ShowList(&head);
}
void InsertTailTest()
{
PointList* head;
InitPointList(&head);
for (int i = 1; i <= 10; ++i)
{
InsertTail(&head, i);
}
ShowList(&head);
}
int main()
{
InsertPointListTest();
InsertHeadTest();
InsertTailTest();
}
O(1)时间下删除链表的结点.c
#include "point_list.h"
#include <stdlib.h>
void DeleteNode(PointList** head, PointList* node);
void InsertTailTest()
{
PointList* head;
InitPointList(&head);
for (int i = 1; i <= 10; ++i)
{
InsertTail(&head, i);
}
ShowList(&head);
PointList* node = ListQueryByVal(&head, 6);
DeleteNode(&head, node);
ShowList(&head);
}
void DeleteNode(PointList** head, PointList* node)
{
if (!head || !node)
return;
if (node->next != NULL)
{
PointList* pNext = node->next;
node->data = pNext->data;
node->next = pNext->next;
free(pNext);
pNext = NULL;
}
else if (node == *head)
{
free(node);
node = NULL;
*head = NULL;
}
else {
DeleteTail(head);
}
}
int main()
{
InsertTailTest();
}