单链表的各种操作(增删查改)
main函数自己写/
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct Node
{
DataType _Data;
struct Node* next;
}Node;
//创建一个节点
Node* BuyNode(DataType x)
{
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指向后一个节点,直到最后一个节点
}
tail->next = BuyNode(x);
}
}
void PopBack(Node** ppHead)
{
//空
if (*ppHead==NULL)
{
return;
//printf("this Node is kong");
}
//一个
else if ((*ppHead)->next==NULL)
{
free(*ppHead);
*ppHead = NULL;
}
//一个以上
else
{
Node* cur = *ppHead;
Node* prev = NULL;
while (cur->next)
{
prev = cur;
cur = cur->next;
}
free(cur);
prev->next = NULL;
}
}
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)
{
free(*ppHead);
*ppHead = NULL;
}
else
{
Node* next = (*ppHead)->next;
free(*ppHead);
*ppHead = next;
}
}
Node* Find(Node* pList, DataType x)
{
assert(pList);
Node* prev = pList;
while (prev != NULL)
{
if ((prev->_Data) == x)
{
printf("找到了");
return prev;
}
else
{
prev = prev->next;
}
}
return NULL;
}
//指定位置插入
void Insert(Node** ppHead, Node* pos,DataType x)
{
if (*ppHead == NULL)
{
*ppHead = BuyNode(x);
}
assert(pos);
if ((*ppHead)=pos)
{
PushFront(ppHead, x);
}
else
{
Node* tmp = BuyNode(x);
Node* prev = *ppHead;
//Node* cur = prev->next;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next=tmp;
tmp->next=pos;
}
}
//删除指定位置的节点
void Earse(Node**ppHead,Node*pos)
{
assert(*ppHead);
if (*ppHead == pos)
{
free(pos);
}
else
{
Node* prev = *ppHead;
while ((*ppHead)->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
//打印链表
void PrintList(Node* pHead)
{
Node* cur = pHead;
//
while (cur)
{
printf("%d ", cur->_Data);
cur = cur->next;
}
printf("\n");
}
void Test()
{
Node* List = NULL;
DataType x = 2;
PushBack(&List, 1);
PushBack(&List, 2);
PushBack(&List, 3);
PushBack(&List, 4);
PushBack(&List, 5);
//PushFront(&List, 4);
//PushFront(&List, 3);
//PushFront(&List, 2);
//PushFront(&List, 1);
Find(List,x);
//Insert(&List, List->next, 30);
PrintList(List);
}
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct Node
{
DataType _Data;
struct Node* next;
}Node;
//创建一个节点
Node* BuyNode(DataType x)
{
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指向后一个节点,直到最后一个节点
}
tail->next = BuyNode(x);
}
}
void PopBack(Node** ppHead)
{
//空
if (*ppHead==NULL)
{
return;
//printf("this Node is kong");
}
//一个
else if ((*ppHead)->next==NULL)
{
free(*ppHead);
*ppHead = NULL;
}
//一个以上
else
{
Node* cur = *ppHead;
Node* prev = NULL;
while (cur->next)
{
prev = cur;
cur = cur->next;
}
free(cur);
prev->next = NULL;
}
}
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)
{
free(*ppHead);
*ppHead = NULL;
}
else
{
Node* next = (*ppHead)->next;
free(*ppHead);
*ppHead = next;
}
}
Node* Find(Node* pList, DataType x)
{
assert(pList);
Node* prev = pList;
while (prev != NULL)
{
if ((prev->_Data) == x)
{
printf("找到了");
return prev;
}
else
{
prev = prev->next;
}
}
return NULL;
}
//指定位置插入
void Insert(Node** ppHead, Node* pos,DataType x)
{
if (*ppHead == NULL)
{
*ppHead = BuyNode(x);
}
assert(pos);
if ((*ppHead)=pos)
{
PushFront(ppHead, x);
}
else
{
Node* tmp = BuyNode(x);
Node* prev = *ppHead;
//Node* cur = prev->next;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next=tmp;
tmp->next=pos;
}
}
//删除指定位置的节点
void Earse(Node**ppHead,Node*pos)
{
assert(*ppHead);
if (*ppHead == pos)
{
free(pos);
}
else
{
Node* prev = *ppHead;
while ((*ppHead)->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
//打印链表
void PrintList(Node* pHead)
{
Node* cur = pHead;
//
while (cur)
{
printf("%d ", cur->_Data);
cur = cur->next;
}
printf("\n");
}
void Test()
{
Node* List = NULL;
DataType x = 2;
PushBack(&List, 1);
PushBack(&List, 2);
PushBack(&List, 3);
PushBack(&List, 4);
PushBack(&List, 5);
//PushFront(&List, 4);
//PushFront(&List, 3);
//PushFront(&List, 2);
//PushFront(&List, 1);
Find(List,x);
/st, List->nex