头文件
#ifndef __LIST_H__
#define __LIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node *next;
}Node,*pNode,*pList;
void Initlist(pList *pplist);
pNode BuyNode(DataType data);
void PushBack(pList *pplist,DataType data);
void PushFront(pList *pplist, DataType data);
void Print(pList plist);
void Destroylist(pList* pplist);
void PopBack(pList* pplist,DataType data);
void PopFront(pList* pplist,DataType data);
DataTypeFind(pList* pplist, DataType data);
void Remove(pList*pplist, DataType data);
void RemoveAll(pList* pplist, DataType data);
void Insert(pList* pplist,pNode pos,DataType data);
void Erase(pList*pplist,pNode pos, DataType data);
#endif
测试文件
#define _CRT_SECURE_NO_WARNINGS 1
#include"list.h"
void test()
{
pList plist;
Initlist(&plist);
PushBack(&plist, 1);
Print(plist);
Destroylist(&plist);
Print(plist);
PopBack(&plist, 1);
Print(plist);
PopFront(&plist, 2);
Print(plist);
find(&plist, 1);
Remove(&plist, 1);
}
int main()
{
test();
system("pause");
return 0;
}
函数实现
#define _CRT_SECURE_NO_WARNINGS 1
#include "list.h"
void Initlist(pList *pplist)
{
assert(pplist != NULL);
*pplist = NULL;
}
pNode BuyNode(DataType data)
{
pNode pnode = NULL;
pnode = malloc(sizeof(Node));
if (pnode == NULL)
{
perror("malloc");
return NULL;
}
else
{
pnode->data = data;
pnode->next = NULL;
return pnode;
}
}
void PushBack(pList *pplist, DataType data)
{
assert(pplist != NULL);
pNode pnewnode = NULL;
pnewnode = BuyNode(data);
if (*pplist == NULL)
{
*pplist = pnewnode;
}
else
{
pNode cur = NULL;
cur = *pplist;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = pnewnode;
}
}
void Print(pList plist)
{
if (plist == NULL)
{
printf("链表为空\n");
}
else
{
pList cur = NULL;
cur = plist;
printf("list:");
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL");
}
}
void PushFront(pList *pplist, DataType data)
{
pNode pnewnode = NULL;
assert(pplist!=NULL);
pnewnode = BuyNode(data);
if (*pplist == NULL)
{
*pplist = pnewnode;
pnewnode->next = NULL;
}
else
{
pNode cur = *pplist;
*pplist = pnewnode;
pnewnode->next =cur;
}
}
void Destroylist(pList* pplist)
{
pNode cur = *pplist;
assert(pplist != NULL);
while (cur)
{
pNode del = cur;
cur = cur->next;
free(del);
}
*pplist = NULL;
}
void PopBack(pList* pplist, DataType data)
{
assert(pplist != NULL);
pNode cur = *pplist;
if (cur == NULL)
{
printf("链表为空\n");
}
else if (cur->next == NULL)
{
free(cur);
*pplist = NULL;
}
else
{
while (cur->next->next != NULL)
{
cur = cur->next;
}
free(cur->next->next);
cur->next = NULL;
}
}
void PopFront(pList* pplist, DataType data)
{
assert(pplist != NULL);
pNode cur = NULL;
cur = *pplist;
if (cur == NULL)
{
printf("链表为空\n");
}
else if (cur->next == NULL)
{
free(cur);
*pplist = NULL;
}
else
{
pNode cur = NULL;
cur = *pplist;
*pplist = cur->next;
free(cur);
}
}
DataType Find(pList* pplist, DataType data)
{
assert(pplist != NULL);
pNode cur = *pplist;
if (cur == NULL)
{
printf("链表为空\n");
}
else if (cur->next==NULL)
{
if (cur->data == data)
{
return cur->data;
}
else
{
return -1;
}
}
else
{
while (cur)
{
if (cur->data == data)
{
return cur->data;
}
else
{
cur = cur->next;
}
}
printf("查找元素不存在\n");
return -1;
}
}
void Insert(pList* pplist, DataType data)
{
}
void Remove(pList*pplist, DataType data)
{
assert(pplist != NULL);
pNode cur = *pplist;
if (cur == NULL)
{
printf("链表为空\n");
}
else if (cur->next == NULL)
{
if (cur->data == data)
{
free(cur);
*pplist == NULL;
}
else
{
printf("指定删除的元素不存在\n");
}
}
else
{
pNode prev = NULL;
while (cur)
{
if (cur->data == data)
{
if (cur==*pplist)
{
*pplist = cur->next;
free(cur);
}
else
{
prev->next = cur->next;
free(cur);
}
}
else
{
prev = cur;
cur = cur->next;
}
}
}
}