对于单链表的基本操作
如果要讲到单链表的基本操作,首先我们就得知道什么是链表?下面我就给大家简单介绍一下什么是链表:链表是一种物理存储单元上非连续、非顺序的存储结构,而链表中的链接顺序考的是指针的链接次序来决定的。节点在运行时动态生成,而每个结点包括存储数据的数据域和指向下一个结点的指针域。而每个结点只有一个链域的链表则称为单链表。单链表中每个结点的存储地址是存放在其前趋结点next域中,而开始结点无前趋,故应设头指针head指向开始结点,但应该注意的是链表由头指针唯一确定,单链表可以由头指针的名字来命名,此外尾结点的next域应为NULL。对于单链表的操作一般就是增、删、查、找以及排序。下面便用代码来实现这些基本的操作:
PlinkList.h:(头文件)
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __LINKLIST_H__//防止头文件的重定义
#define __LINKLIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType;
typedef struct LinkNode
{
DataType data;
struct LinkNode* next;
}LinkNode,*pLinkNode;
typedef struct LinkList
{
LinkNode* pHead;
}LinkList,*pLinkList;
void InitLinkList(pLinkList pList);//链表的初始化
void DestoryList(pLinkList pList);//链表的释放
void PushBack(pLinkList pList,DataType x);//尾插
void PushFront(pLinkList pList,DataType x);//头插
void PopBack(pLinkList pList);//尾删
void PopFront(pLinkList pList);//头删
void PrintList(pLinkList pList);//打印链表
pLinkNode Find(pLinkList pList,DataType x);//查找
void Insert(pLinkList pList,pLinkNode pos,DataType x);//在指定位置插入数据
void Remove(pLinkList pList,DataType x);//删除指定数据
void RemoveAll(pLinkList pList,DataType x);//删除所有数据
void Erase(pLinkList pList,pLinkNode pos);//删除指定位置的数据
void BubbleSort(pLinkList pList);//链表的排序(冒泡)
#endif //__LINKLIST_H__
<pre name="code" class="cpp">#include"PlinkList.h"
void InitLinkList(pLinkList pList)//链表的初始化
{
assert(pList);
pList->pHead=NULL;
}
void DestoryList(pLinkList pList)//链表的释放
{
pLinkNode cur=NULL;
cur=pList->pHead;
assert(pList);
while(cur)
{
pLinkNode del=cur;
cur=cur->next;
free(del);
del=NULL;
}
pList->pHead=NULL;
}
void PushBack(pLinkList pList,DataType x)//尾插
{
pLinkNode newNode=NULL;
newNode=(pLinkNode)malloc(sizeof(LinkNode));
assert(pList);
if(NULL==newNode)
{
printf("out of memory\n");
exit(EXIT_FAILURE);
}
newNode->data=x;
newNode->next=NULL;
if(NULL==pList->pHead)
{
pList->pHead=newNode;
}
else
{
pLinkNode cur=pList->pHead;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=newNode;
}
}
pLinkNode BuyNode(DataType x) //创建一个结点
{
pLinkNode newNode=NULL;
newNode=(pLinkNode)malloc(sizeof(LinkNode));
if(NULL==newNode)
{
printf("out of memory\n");
exit(EXIT_FAILURE);
}
newNode->data=x;
newNode->next=NULL;
return newNode;
}
void PushFront(pLinkList pList,DataType x)//头插
{
pLinkNode newNode=NULL;
assert(pList);
newNode=BuyNode(x);
newNode->next=pList->pHead;
pList->pHead=newNode;
}
void PopBack(pLinkList pList)//尾删
{
pLinkNode cur=NULL;
assert(pList);
cur=pList->pHead;
if(NULL==cur)//空链表
{
printf("该链表为空\n");
}
else if(NULL==cur->next)//该链表只有一个结点
{
free(cur);
cur=NULL;
pList->pHead=NULL;
}
else//该链表有多个结点
{
while(cur->next!=NULL)
{
cur=cur->next;
}
free(cur->next);
cur->next=NULL;
}
}
void PopFront(pLinkList pList)//头删
{
pLinkNode cur=NULL;
assert(pList);
cur=pList->pHead;
if(NULL==cur)//空链表
{
printf("该链表为空\n");
}
else
{
pLinkNode newHead=NULL;
cur=cur->next;
newHead=cur;
free(cur);
cur=NULL;
pList->pHead=newHead;
}
}
void PrintList(pLinkList pList)//打印链表
{
pLinkNode cur=NULL;
cur=pList->pHead;
assert(pList);
while(cur)
{
printf("%d->",cur->data);
cur=cur->next;
}
printf("over\n");
}
pLinkNode Find(pLinkList pList,DataType x)//查找
{
pLinkNode cur=NULL;
assert(pList);
cur=pList->pHead;
if(NULL==cur)
{
printf("该链表为空链表\n");
}
while(cur->next!=NULL)
{
if(cur->data==x)
{
return cur;
}
cur=cur->next;
}
return NULL;
}
void Insert(pLinkList pList,pLinkNode pos,DataType x)//在指定位置之 前插入数据
{
pLinkNode newNode=NULL;
assert(pList);
assert(pos);
newNode=pList->pHead;
newNode=BuyNode(x);
if(pos == pList->pHead)
{
PushFront(pList, x);
}
else
{
pLinkNode cur=pList->pHead;
while(cur->next!=pos)
{
cur=cur->next;
}
cur->next=newNode;
newNode->next=pos;
}
}
void Remove(pLinkList pList,DataType x)//删除指定数据
{
pLinkNode Node=NULL;
pLinkNode pos=NULL;
assert(pList);
Node=Find(pList,x);
if(Node==NULL)
{
printf("该链表为空链表/n");
}
else if(Node=pList->pHead)
{
PopFront(pList);
}
else
{
pLinkNode cur=pList->pHead;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
void RemoveAll(pLinkList pList,DataType x)//删除所有数据
{
pLinkNode cur=NULL;
assert(pList);
cur=pList->pHead;
while(cur)
{
cur=cur->next;
free(cur);
cur=NULL;
}
cur->next=NULL;
}
void Erase(pLinkList pList,pLinkNode pos)//删除指定位置的数据
{
assert(pos);
assert(pList);
if(pos == pList->pHead)
{
PopFront(pList);
}
else
{
pLinkNode cur = pList->pHead;
while(cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
void BubbleSort(pLinkList pList)//链表的排序(冒泡)
{
pLinkNode cur=pList->pHead;
pLinkNode cmp=pList->pHead;
DataType flag=0;
while(cur->next != NULL)
{
flag=0;
while(cmp->next != NULL)
{
if(cmp->data > cmp->next->data) //升序排列
{
DataType tmp=cmp->data;
cmp->data=cmp->next->data;
cmp->next->data=tmp;
flag=1;
}
cmp=cmp->next;
}
cur=cur->next;
if(flag == 0)
break;
}
printf("排序成功\n");
}