前言:
单链表:
它是一种链式储存的线性表,用一组地址任意的储存单元存放线性表的数据元素;称储存单元为一个结点;
结点:
值域+指针域
链表:
二、代码实现
(1)结点定义:
typedef int DataType;
typedef struct ListNode
{
struct ListNode* next;
DataType data;
}Node,*PNode;
typedef struct List
{
PNode head;
}List,*PList;
(2)单链表主要功能:
void Remove(PList list,DataType x);//删除指定元素---✔
void RemoveAll(PList list,DataType x);//删除指定元素的全部
void Init(PList list);//初始化---✔
void PushBack(PList list,DataType x);//创建新结点---✔
void PushBack(PList list,DataType x);//尾插---✔
void PopBack(PList list);//尾删---✔
void PushFront(PList list,DataType x);//头插---✔
void PopFront(PList list);//头删---✔
void Insert(PList plist, PNode pos,DataType x);//插入--在节点之前插入一个节点---✔
void Erase(PList list,PNode pos);//删除指定位置的元素头插---✔
PNode Find(PList plist,DataType x);//查找---✔
void Printf(PList list);//打印---✔
void Destroy(PList list);//释放链表---✔
void Sort(PList list);//排序---✔
(3)完整代码实现:
//List.h
#include<stdio.h>
#include <stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct ListNode
{
struct ListNode* next;
DataType data;
}Node,*PNode;
typedef struct List
{
PNode head;
}List,*PList;
void Remove(PList list,DataType x);//删除指定元素---✔
void RemoveAll(PList list,DataType x);//删除指定元素的全部
void Init(PList list);//初始化---✔
void PushBack(PList list,DataType x);//创建新结点---✔
void PushBack(PList list,DataType x);//尾插---✔
void PopBack(PList list);//尾删---✔
void PushFront(PList list,DataType x);//头插---✔
void PopFront(PList list);//头删---✔
void Insert(PList plist, PNode pos,DataType x);//插入--在节点之前插入一个节点---✔
void Erase(PList list,PNode pos);//删除指定位置的元素头插---✔
PNode Find(PList plist,DataType x);//查找---✔
void Printf(PList list);//打印---✔
void Destroy(PList list);//释放链表---✔
void Sort(PList list);//排序---✔
//test.c
#include"List.h"
void Init(PList list)//初始化
{
assert(list);
list->head=NULL;
}
PNode BuyNode(DataType x)//创建新结点
{
PNode NewNode=(PNode)malloc(sizeof(Node));
if (NewNode==NULL)
{
printf("fail");
exit(EXIT_FAILURE);
}
NewNode->data=x;
NewNode->next=NULL;
return NewNode;
}
void Printf1(PNode list)//打印
{
PNode cur=NULL;
cur=list;
if (cur==NULL)
{
printf("list is mepty");
}
else
{
while (cur)
{
printf("%d->",cur->data);
cur=cur->next;
}
printf("over \n");
}
}
void Printf(PList list)//打印
{
PNode cur=NULL;
assert(list);
cur=list->head;
if (cur==NULL)
{
printf("list is mepty");
}
else
{
while (cur)
{
printf("%d->",cur->data);
cur=cur->next;
}
printf("over \n");
}
}
void PushBack(PList list,DataType x)//尾插
{
PNode cur=list->head;
PNode NewNode=NULL;
assert(list);
NewNode=BuyNode(x);
if (cur==NULL)
{
list->head=NewNode;
NewNode->next=NULL;
}
else
{
while (cur->next)
{
cur=cur->next;
}
cur->next=NewNode;
}
}
void PopBack(PList list)//尾删
{
PNode cur=NULL;
PNode prev=NULL;
assert(list);
cur=list->head;
if (cur==NULL)
{
printf("list ic empty");
exit(EXIT_FAILURE);
}
if (cur->next==NULL)//1.只有一个结点
{
free(cur);
cur=NULL;
}
else//2.有大于一个的结点
{
while (cur->next)
{
prev=cur;
cur=cur->next;
}
free(cur);
prev->next=NULL;
}
}
void PushFront(PList list,DataType x)//头插
{
PNode NewNode;
NewNode=BuyNode(x);
assert(list);
NewNode->next=list->head;
list->head=NewNode;
}
void PopFront(PList list)//头删
{
PNode del=NULL;
assert(list);
if (list->head==NULL)
{
printf("list is empty");
exit(EXIT_FAILURE);
}
del=list->head;
list->head=del->next;
free(del);
del=NULL;
}
//dest(目标),srt(源)
//prev(前一个),cur(当前),next(下一个)
void Insert(PList plist, PNode pos,DataType x)//插入--在某个节点之前插入一个节点
{
PNode cur=NULL;//当前结点
PNode prev=NULL;//当前结点的前一个结点
PNode NewNode=NULL;
assert(plist);
cur=plist->head;
NewNode=BuyNode(x);
if (pos==NULL)//1.如果位置为空
{
return;
}
if (cur==NULL)//2.如果链表为空
{
PushFront(plist,x);//头插
}
if (cur==pos)//3.1插入第一个结点
{
PushFront(plist,x);
}
else//3.2插入其它位置结点
{
while (cur && cur!=pos)
{
prev=cur;
cur=cur->next;
}
if (cur==NULL)
{
return ;
}
else
{
NewNode->next=cur;
prev->next=NewNode;
}
}
}
void Remove(PList list,DataType x)//删除第一个指定元素
{
PNode cur=NULL;
PNode prev=NULL;
PNode del=NULL;
assert(list);
cur=list->head;
if (list->head==NULL)//1.空链表
{
printf("list is empty");
exit(EXIT_FAILURE);
}
while (cur)//2.非空链表
{
if (cur->data==x)
{
if (cur==list->head)//2.1删除的结点为第一个结点
{
del=cur;
list->head=cur->next;
free(del);
}
else//2.2删除的不是第一个结点
{
del=cur;
prev->next=cur->next;
free(del);
}
return;
}
else
{
prev=cur;
cur=cur->next;
}
}
}
void RemoveAll(PList list,DataType x)//删除全部的指定元素
{
PNode cur=NULL;
PNode prev=NULL;
PNode del=NULL;
cur=list->head;
assert(list);
if (list->head=NULL)//1.空链表
{
printf("list is empty");
exit(EXIT_FAILURE);
}
while (cur)//2.非空链表
{
if (cur->data==x)
{
if (cur==list->head)//2.1删除的结点为第一个结点
{
del=cur;
list->head=cur->next;
free(del);
}
else//2.2删除的不是对个结点
{
del=cur;
prev->next=cur->next;
free(del);
}
cur=prev->next;
}
else
{
prev=cur;
cur=cur->next;
}
}
}
void Erase(PList list,PNode pos)//删除指定位置的元素
{
PNode cur=list->head;
PNode prev=NULL;
PNode del=NULL;
assert(list);
if (list->head==NULL)//1.链表为空
{
printf("list is empty");
exit(EXIT_FAILURE);
}
while(cur)//2.链表不为空
{
if (cur==pos)
{
if (cur==list->head)//2.1删除链表第一个结点
{
del=cur;
list->head=cur->next;
free(del);
}
else//2.2删除其他结点
{
del=cur;
prev->next=cur->next;
free(del);
}
return;
}
prev=cur;
cur=cur->next;
}
}
PNode Find(PList plist,DataType x)//查找某个元素,并返回指向该元素的结点的指针
{
PNode cur=plist->head;
assert(plist);
if (cur==NULL)//1.如果链表为空
{
printf("list is empty");
exit(EXIT_FAILURE);
}
while (cur)//2.链表不为空
{
if (cur->data==x)
{
return cur;
}
cur=cur->next;
}
printf("no exist");
return NULL;
}
void Sort(PList list)//排序--bubble sort
{
PNode cur=NULL;
PNode tail=NULL;
assert(list);
cur=list->head;
if ((list->head==NULL)||(list->head->next==NULL))//1.链表为空或者链表只有一个结点,不用排序
{
return;
}
while (cur!=tail)//2.链表有大于一个元素
{
while (cur->next!=tail)
{
if (cur->data > cur->next->data)
{
DataType tmp=cur->data;
cur->data=cur->next->data;
cur->next->data=tmp;
}
cur=cur->next;
}
tail=cur;
cur=list->head;
}
}
void Destroy(PList list)//释放链表
{
PNode cur=list->head;
PNode del=NULL;
assert(list);
while (cur)
{
del=cur;
cur=cur->next;
free(del);
del=NULL;
}
list->head=NULL;
}
测试代码:
//main.c
#include"List.h"
void Test()
{
List list;
PNode ret=NULL;
Init(&list);
PushBack(&list,1);
PushBack(&list,3);
PushBack(&list,5);
PushBack(&list,7);
PushBack(&list,2);
PushBack(&list1,4);
PushBack(&list1,6);
PushBack(&list1,8);
Printf1(ret);
// PushBack(&list,3);
//PushBack(&list,3);
//PushBack(&list,7);
PopBack(&list);
// //PopBack(&list);
//PushFront(&list,9);
//PushFront(&list,8);
//PushFront(&list,7);
/*ret=Find(&list,6);*/
//Insert(&list,ret,10);
//PopFront(&list);
//PopFront(&list);
/*Remove(&list,2);*/
/*Erase(&list,ret);*/
//Sort(&list);
//RemoveAll(&list,3);
/*InsretFrontNode(ret,99);*/
//ret=FindMidNode2(&list);
//printf("此链表中间数为:%d\n",ret->data);
/*Printf(&list);*/
Destroy(&list);
}
int main()
{
Test();
return 0;
}