用c语言实现单链表
Node.h
#pragma once
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}Node;
Node* BuyNode(DataType x); //增容
void PrintList(Node* pHead); //打印
void PushBack(Node** ppHead, DataType x); //尾插
void PopBack(Node** ppHead); //尾删
void PushFront(Node** ppHead, DataType x); //头插
void PopFront(Node** ppHead); //头删
void Insert(Node** ppHead,Node* pos, DataType x); //插入元素
void Erase(Node** ppHead,Node* pos); //删除任意
Node* Find(Node* pHead, DataType x); //查找
Node* BuyNode(DataType x) //增容
{
Node* node = (Node*)malloc(sizeof(Node));
node->data = x;
node->next = NULL;
return node;
}
void PrintList(Node* pHead) //打印
{
Node* cur = pHead;
while (cur)
{
printf("%d ", cur->data);
cur = cur->next;
}
printf("\n");
}
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);
}
}
void PushFront(Node** ppHead, DataType x) //头插
{
/*if (*ppHead == NULL)
{
*ppHead = BuyNode(x);
}
else
{
Node* tmp = BuyNode(x);
tmp->next = *ppHead;
*ppHead = tmp;
}*/
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* tmp=BuyNode(x);
Node* prev = *ppHead;
while ((prev->next)!=pos) //prev记录的是被插位置的前一位置元素
{
prev = prev->next;
}
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);
}
}
Node* Find(Node* pHead, DataType x) //查找
{
Node* cur = pHead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void Testlist1()
{
Node* list = NULL;
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PushBack(&list, 4);
PrintList(list);
PopBack(&list);
PrintList(list);
PushFront(&list,4);
PrintList(list);
PopFront(&list);
PrintList(list);
}
void Testlist2()
{
Node* list = NULL;
Node* pos = NULL;
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PushBack(&list, 4);
PrintList(list);
pos = Find(list, 3);
/*pos = list;
pos = pos->next;*/
Insert(&list, pos, 5);
PrintList(list);
Erase(&list, pos);
PrintList(list);
}
Node.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "Node.h"
int main()
{
Testlist2();
system("pause");
return 0;
}
用c语言实现单链表
最新推荐文章于 2022-10-11 12:23:16 发布