#pragma once
typedef int DataType;
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}Node;
{
DataType data;
struct Node* next;
}Node;
void PrintList(Node* pHead);
void PushBack(Node** ppHead, DataType x);
void PopBack(Node** ppHead);
void PushFront(Node** ppHead, DataType x);
void PopFront(Node** ppHead);
Node* Find(Node* pHead, DataType x);
void Insert(Node** ppHead, Node* pos, DataType x);
void Erase(Node** ppHead, Node* pos);
void PushBack(Node** ppHead, DataType x);
void PopBack(Node** ppHead);
void PushFront(Node** ppHead, DataType x);
void PopFront(Node** ppHead);
Node* Find(Node* pHead, DataType x);
void Insert(Node** ppHead, Node* pos, DataType x);
void Erase(Node** ppHead, Node* pos);
Node* BuyNode(DataType x)
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = x;
node->next = NULL;
return node;
}
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = x;
node->next = NULL;
return node;
}
void PushBack(Node** ppHead, DataType x)
{
if (*ppHead == NULL)
{
*ppHead = BuyNode(x);
}
else
{
Node* tail = *ppHead;
while (tail->next)
{
tail = tail->next;
}
tail->next = BuyNode(x);
}
}
void PopBack(Node** ppHead)
{
if (*ppHead == NULL)
{
return;
}
else if (*ppHead->next == NULL)
{
free(*ppHead);
*ppHead = NULL;
}
else
{
Node* cur = *ppHead;
Node* prev = NULL;
while (cur->next)
{
prev = cur;
cur = cur->next;
}
prev->next = NULL;
free(cur);
}
{
if (*ppHead == NULL)
{
return;
}
else if (*ppHead->next == NULL)
{
free(*ppHead);
*ppHead = NULL;
}
else
{
Node* cur = *ppHead;
Node* prev = NULL;
while (cur->next)
{
prev = cur;
cur = cur->next;
}
prev->next = NULL;
free(cur);
}
}
void PushFront(Node** ppHead, DataType x)
{
if (*ppHead == NULL)
{
*ppHead = BuyNode(x);
}
else
{
Node* tmp = BuyNode(x);
tmp->next = *ppHead;
*ppHead = tmp;
}
}
void PopFront(Node** ppHead)
{
if (*ppHead == NULL)
{
return;
}
else if (*ppHead -> next == NULL)
{
free(*ppHead);
*ppHead = NULL;
}
else
{
Node* next = (*ppHead)->next;
free(*ppHead);
*ppHead = next;
}
}
void PushFront(Node** ppHead, DataType x)
{
if (*ppHead == NULL)
{
*ppHead = BuyNode(x);
}
else
{
Node* tmp = BuyNode(x);
tmp->next = *ppHead;
*ppHead = tmp;
}
}
void PopFront(Node** ppHead)
{
if (*ppHead == NULL)
{
return;
}
else if (*ppHead -> next == NULL)
{
free(*ppHead);
*ppHead = NULL;
}
else
{
Node* next = (*ppHead)->next;
free(*ppHead);
*ppHead = next;
}
}
void Insert(Node** ppHead, Node* pos, DataType x)
{
assert(pos);
if (pos == *ppHead)
{
PushFront(ppHead, x);
}
else
{
Node* prev = *ppHead;
while (prev->next != pos)
{
prev = prev->next;
}
Node* tmp = BuyNode(x);
prev->next = tmp;
tmp->next = pos;
}
}
void Erase(Node** ppHead, Node* pos)
{
assert(pos);
if (pos == *ppHead)
{
PopFront(ppHead);
}
else if (pos->next == NULL)
{
PopBack(ppHead);
}
else
{
Node* prev = *ppHead, *next = pos->next;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = next;
free(pos);
}
}
{
assert(pos);
if (pos == *ppHead)
{
PushFront(ppHead, x);
}
else
{
Node* prev = *ppHead;
while (prev->next != pos)
{
prev = prev->next;
}
Node* tmp = BuyNode(x);
prev->next = tmp;
tmp->next = pos;
}
}
void Erase(Node** ppHead, Node* pos)
{
assert(pos);
if (pos == *ppHead)
{
PopFront(ppHead);
}
else if (pos->next == NULL)
{
PopBack(ppHead);
}
else
{
Node* prev = *ppHead, *next = pos->next;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = next;
free(pos);
}
}
void PrintList(Node* pHead)
{
Node* cur = pHead;
while (cur)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
{
Node* cur = pHead;
while (cur)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
void TestList1() // PushBack & PopBack
{
Node* list = NULL;
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PushBack(&list, 4);
PushBack(&list, 5);
PushBack(&list, 6);
{
Node* list = NULL;
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PushBack(&list, 4);
PushBack(&list, 5);
PushBack(&list, 6);
PrintList(list);
PopBack(&list, 1);
PopBack(&list, 2);
PopBack(&list, 3);
PopBack(&list, 4);
PopBack(&list, 2);
PopBack(&list, 3);
PopBack(&list, 4);
PrintList(list);
}
}
void TestList2() //PushFront & PopFront
{
Node* list = NULL;
PushFront(&list, 1);
PushFront(&list, 2);
PushFront(&list, 3);
PushFront(&list, 4);
PushFront(&list, 5);
{
Node* list = NULL;
PushFront(&list, 1);
PushFront(&list, 2);
PushFront(&list, 3);
PushFront(&list, 4);
PushFront(&list, 5);
PrintList(list);
PopFront(&list, 1);
PopFront(&list, 2);
PopFront(&list, 3);
PopFront(&list, 4);
PopFront(&list, 1);
PopFront(&list, 2);
PopFront(&list, 3);
PopFront(&list, 4);
PrintList(list);
}
void TestList3() //
{
Node* list = NULL;
PushFront(&list, 1);
PushFront(&list, 2);
PushFront(&list, 3);
PushFront(&list, 4);
PushFront(&list, 5);
}
void TestList3() //
{
Node* list = NULL;
PushFront(&list, 1);
PushFront(&list, 2);
PushFront(&list, 3);
PushFront(&list, 4);
PushFront(&list, 5);
PrintList(list);
Node* pos = list;
pos = pos->next;
Insert(&list, 3, 30);
PrintList(list);
Node* pos = list;
pos = pos->next;
Insert(&list, 3, 30);
PrintList(list);
}
#include<stdio.h>
#include<stdlib.h>
#include"test.h"
#include<stdlib.h>
#include"test.h"
int main()
{
//void TestList1();
//void TestList2();
void TestList3();
}
{
//void TestList1();
//void TestList2();
void TestList3();
}