plist.h
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}Node, *pNode, List, *pList;
void InitLinkList(pList* pplist);//初始化链表
pNode BuyNode(DataType d);
void DestroyLinkList(pList* pplist);//销毁链表
void PushBack(pList* pplist, DataType d);//尾插
void PopBack(pList* pplist);//尾部删除
void PushFront(pList* pplist, DataType d);//头部插入
void PopFront(pList* pplist);//头部删除
pNode Find(pList plist, DataType d);//查找元素
void Insert(pList* pplist, pNode pos, DataType d);//在指定位置之前插入一个值
void Erase(pList* pplist, pNode pos);//指定位置删除
void Remove(pList* pplist, DataType d);
void RemoveAll(pList* pplist, DataType d);
void EraseNotTailNode(pNode pos);
void PrintLinkList(pList plist);
int GetListLength(pList plist);
void PrintTailToHead(pList plist);//1. 逆序打印单项链表
#endif //__LINKLIST_H__
plist.c
#include"plist.h"
void InitLinkList(pList* pplist)
{
assert(pplist != NULL);
*pplist = NULL;
}
pNode BuyNode(DataType d)
{
pNode tmp = (pNode)malloc(sizeof(Node));
if (tmp == NULL)
{
perror("BuyNode::malloc");
return NULL;
}
tmp->data = d;
tmp->next = NULL;
return tmp;
}
void DestroyLinkList(pList* pplist)//销毁链表
{
assert(pplist != NULL);
pNode cur = *pplist;
while (cur != NULL)
{
pNode del = cur;
cur = cur->next;
free(del);
del = NULL;
}
*pplist = NULL;
}
void PushBack(pList* pplist, DataType d)
{
pNode newNode = BuyNode(d);
assert(pplist != NULL);
if (newNode == NULL)
{
exit(EXIT_FAILURE);
}
if (*pplist == NULL)
{
*pplist = newNode;
}
else
{
pNode cur = *pplist;
while ((cur->next) != NULL)
{
cur = cur->next;
}
cur->next = newNode;
}
}
void PopBack(pList* pplist)
{
assert(pplist != NULL);
pNode cur = *pplist;
if (*pplist ==NULL)
return;
if ((*pplist)->next == NULL)
{
free(*pplist);
*pplist = NULL;
}
else
{
while (cur->next->next != NULL)
{
cur = cur->next;
}
free(cur->next);
cur->next = NULL;
}
}
void PrintLinkList(pList plist)
{
pNode cur = plist;
while (cur)
{
printf("&d-->", cur->data);
cur = cur->next;
}
printf("over\n");
}
void PushFront(pList* pplist, DataType d)
{
assert(pplist != NULL);
pNode newNode = NULL;
newNode = BuyNode(d);
newNode->next = *pplist;
*pplist = newNode;
}
void PopFront(pList* pplist)
{
assert(pplist != NULL);
if (*pplist == NULL)
{
return;
}
else
{
pNode del = *pplist;
*pplist = del->next;
free(del);
del = NULL;
}
}
pNode Find(pList plist, DataType d)
{
pNode cur = NULL;
assert(plist != NULL);
cur = plist;
if (plist == NULL)
{
return NULL;
}
else
{
if (cur->data == d)
{
return cur;
cur = cur->next;
}
}
return NULL;
}
void Insert(pList* pplist, pNode pos, DataType d)
{
assert(*pplist != NULL);
assert(pplist != NULL);
assert(pos != NULL);
pNode newNode = NULL;
if (*pplist == pos)
{
newNode = BuyNode(d);
newNode->next = *pplist;
*pplist = newNode;
}
else
{
pNode cur = *pplist;
while (cur&&cur->next != pos)
{
cur = cur->next;
}
if (cur != NULL)
{
newNode = BuyNode(d);
newNode->next = pos;
cur->next = newNode;
}
}
}
void Erase(pList* pplist, pNode pos)
{
assert(pplist != NULL);
assert(pos != NULL);
pNode tmp = pos;
if (*pplist == NULL)
return;
if (*pplist == pos)
{
*pplist = tmp->next;
free(tmp);
tmp = NULL;
}
else
{
pNode cur = *pplist;
while (cur&&cur->next != NULL)
{
cur = cur->next;
}
if (cur != NULL)
{
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
}
void Remove(pList* pplist, DataType d)
{
pNode cur = NULL;
pNode del = NULL;
assert(pplist != NULL);
if(*pplist == NULL)
return;
cur = *pplist;
if (cur->data == d)
{
del = *pplist;
*pplist = cur->next;
free(del);
del = NULL;
}
else
{
while (cur&&cur->next->data != d)
{
cur = cur->next;
}
while (cur)
{
del = cur->next;
cur->next = del->next;
free(del);
del = NULL;
return;
}
}
}
void RemoveAll(pList* pplist, DataType d)
{
pNode cur = NULL;
pNode del = NULL;
assert(pplist != NULL);
if (*pplist == NULL)
{
return;
}
cur = *pplist;
if (cur->data == d)
{
return ;
}
else
{
while (cur&&cur->next->data != d)
{
cur = cur->next;
}
while (cur)
{
del = cur->next;
cur->next = del->next;
free(del);
del = NULL;
}
}
}
void EraseNotTailNode(pNode pos)
{
pNode del = NULL;
assert(pos != NULL&&pos->next != NULL);
del = pos->next;
pos->data = pos->next->data;
del->next = pos->next;
free(del);
del = NULL;
}
void PrintTailToHead(pList plist)
{
pNode cur = plist;
pNode tail = NULL;
if (plist == NULL)
{
return;
}
if (plist->next == NULL)
{
printf("%d", plist->data);
return;
}
while (plist != tail)
{
cur = plist;
while (cur->next!=tail)
{
cur = cur->next;
}
printf("%d", cur->data);
tail = cur;
}
}
int GetListLength(pList plist)
{
int count=0;
pNode cur = plist;
while (cur)
{
cur = cur->next;
count++;
}
return count;
}
test.c
#include"plist.h"
void test1()
{
pList List;
InitLinkList(&List);
PushFront(&List, 4);
PushFront(&List, 3);
PushFront(&List, 2);
PushFront(&List, 1);
PrintLinkList(List);
PopFront(&List);
PopFront(&List);
PrintLinkList(List);
DestroyLinkList(&List);
}
void test2()
{
pList List;
InitLinkList(&List);
PushBack(&List, 6);
PushBack(&List, 5);
PushBack(&List, 4);
PushBack(&List, 3);
PushBack(&List, 2);
PushBack(&List, 1);
PrintLinkList(List);
PopBack(&List);
PopBack(&List);
PopBack(&List);
PopBack(&List);
PrintLinkList(List);
DestroyLinkList(&List);
}
void test3()
{
pList List;
InitLinkList(&List);
PushFront(&List, 4);
PushFront(&List, 3);
PushFront(&List, 2);
PushFront(&List, 1);
PrintLinkList(List);
pNode ret = Find(List, 2);
{
if (ret == NULL)
{
printf("找不到");
}
else
{
printf("%d\n", ret->data);
}
}
DestroyList(&List);
}
void test4()
{
pList List;
InitLinkList(&List);
PushFront(&List, 4);
PushFront(&List, 3);
PushFront(&List, 2);
PushFront(&List, 1);
PrintLinkList(List);
Remove(&List, 1);
PrintLinkList(List);
DestroyLinkList(&List);
}
void test5()
{
pList List;
initLinkList(&List);
PushFront(&List, 4);
PushFront(&List, 3);
PushFront(&List, 2);
PushFront(&List, 1);
PrintLinkList(List);
Insert(&List,4, 2);
PrintLinkList(List);
DestroyLinkList(&List);
}
void test6()
{
pList List;
InitLinkList(&List);
PushFront(&List, 4);
PushFront(&List, 3);
PushFront(&List, 2);
PushFront(&List, 1);
PushFront(&List, 1);
RemoveAll(&List, 1);
PrintLinkList(List);
DestroyLinkList(&List);
}
int main()
{
//test1();
//test2();
//test3();
//test4();
//test5();
test6();
system("pause");
return 0;
}