#define _CRT_SECURE_NO_WARNINGS 1
#include"test.h"
int main()
{
TestLinkList4();
system("pause");
return 0;
}
#include
#include
#include
typedef int DataType;
typedef struct ListNode
{
DataType data;
struct ListNode* next;
}ListNode;
void PrintList(ListNode* pList);
void PushBack(ListNode** ppList, DataType x);
void PopBack(ListNode** ppList);
void PushFront(ListNode** ppList, DataType x);
void PopFront(ListNode** ppList);
ListNode* Find(ListNode* pList, DataType x);
void Insert(ListNode** ppList,ListNode* pos, DataType x);
void Delete(ListNode** ppList, ListNode* pos);
#include"list.h"
ListNode* BuyNode(DataType x)
{
ListNode* pNode=(ListNode*)malloc(sizeof(ListNode));
if (pNode != NULL)
{
pNode->data = x;
pNode->next = NULL;
}
return pNode;
}
void PrintList(ListNode* pList)
{
assert(pList);
ListNode* Node = pList;
while (Node)
{
printf("%d ", Node->data);
Node = Node->next;
}
printf("\n");
}
void PushBack(ListNode** ppList, DataType x)
{
if (*ppList == NULL)
{
*ppList = BuyNode(x);
}
else
{
ListNode* list = *ppList;
while (list->next != NULL)
{
list = list->next;
}
list->next = BuyNode(x);
}
}
void PopBack(ListNode** ppList)
{
ListNode* Node = *ppList;
if (Node == NULL)
{
return;
}
else if (Node->next == NULL)
{
free(Node);
Node = NULL;
}
else
{
ListNode* prev = Node;
while (Node->next != NULL)
{
prev = Node;
Node = Node->next;
}
free(Node);
Node = NULL;
prev->next = NULL;
}
}
void PushFront(ListNode** ppList, DataType x)
{
if (*ppList == NULL)
{
*ppList = BuyNode(x);
}
else
{
ListNode* list = BuyNode(x);
list->next = *ppList;
*ppList = list;
}
}
void PopFront(ListNode** ppList)
{
assert(ppList);
ListNode* list = *ppList;
if (*ppList == NULL)
{
return;
}
else
{
*ppList = list->next;
free(list);
list = NULL;
}
}
ListNode* Find(ListNode* pList, DataType x)
{
assert(pList);
while (pList->next != NULL)
{
if (pList->data == x)
{
return pList;
}
pList = pList->next;
}
return NULL;
}
void Insert(ListNode** ppList, ListNode* pos, DataType x)
{
assert(pos);
if (pos == *ppList)
{
PushFront(ppList, x);
}
else
{
ListNode* tmp= BuyNode(x);
ListNode* prev = *ppList;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = tmp;
tmp->next = pos;
}
}
void Delete(ListNode** ppList, ListNode* pos)
{
assert(pos);
if (pos == *ppList)
{
PopBack(ppList);
}
else
{
ListNode* prev = *ppList;
ListNode* cur = *ppList;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void TestLinkList1()
{
ListNode* Node=NULL;
PushBack(&Node, 1);
PushBack(&Node, 2);
PushBack(&Node, 3);
PushBack(&Node, 4);
PushBack(&Node, 5);
PrintList(Node);
PopBack(&Node);
PopBack(&Node);
PrintList(Node);
}
void TestLinkList2()
{
ListNode* Node = NULL;
PushFront(&Node, 1);
PushFront(&Node, 2);
PushFront(&Node, 3);
PushFront(&Node, 4);
PushFront(&Node, 5);
PrintList(Node);
PopFront(&Node);
PopFront(&Node);
PopFront(&Node);
PopFront(&Node);
PrintList(Node);
}
void TestLinkList3()
{
ListNode* Node = NULL;
PushBack(&Node, 2);
PushBack(&Node, 3);
PushBack(&Node, 1);
PushBack(&Node, 4);
PushBack(&Node, 1);
PushBack(&Node, 8);
PrintList(Node);
printf("%s \n", Find(Node, 4));
}
void TestLinkList4()
{
ListNode* Node = NULL;
ListNode* pos ;
PushBack(&Node, 2);
PushBack(&Node, 3);
PushBack(&Node, 1);
PushBack(&Node, 4);
PushBack(&Node, 1);
PushBack(&Node, 8);
PrintList(Node);
pos = Node;
pos = pos->next;
Insert(&Node, pos, 4);
PrintList(Node);
Delete(&Node, pos);
PrintList(Node);
}