一、单链表(增,删,查,改,打印,销毁链表)
注意链表头的动态,需要改变头指针的先把指针保存起来
1,链表的创建。
2,链表的打印。(循环打印,遇到null则停止)
3,单链表的插入(包括头插,尾插,随机插)。
提示:①空链表,则直接插入;②只有一个节点和尾插,注意插入后 next=NULL;③其他情况就是正常插入。
3,单链表的删除(包括头删,尾删,随机删)。
提示:①空链表,直接返回NULL;②尾删时要注意把删除的前一个节点next=NULL;③其他情况都是正常删除。
4,单链表的查找(找到返回链表地址)。
提示:正常遍历查找,找到则返回地址,否则返回NULL.
5,单链表的修改(任意位置修改)。
提示:①先找到要修改节点的地址;②除去空的情况,其他的都是正常修改。
二、各函数代码:
1、链表的创建。
SListNode* BuySListNode(DataType x) //创建新节点
{
SListNode* tmp;
tmp=(SListNode*)malloc(sizeof(SListNode));
if (NULL==tmp)
return NULL;
else
{
tmp->_data=x;
tmp->_next=NULL;
return tmp;
}
}
2、链表的打印
void SListPrint(SListNode* pHead) //打印链表
{
if (NULL==pHead)
{
printf("空链表\n");
}
else
{
while (pHead)
{
printf("%d ",pHead->_data);
pHead=pHead->_next;
}
printf("\n");
}
}
3、链表的插入(①头插;②尾插;③随机插)
①头插
void SListPushFront(SListNode** ppHead, DataType x) //头插
{
SListNode *newNode;
newNode=BuySListNode(x);
if (NULL==*ppHead)
{
newNode->_next=NULL;
*ppHead=newNode;
}
else
{
newNode->_next=*ppHead;
*ppHead=newNode;
}
}
②尾插
void SListPushBack(SListNode** ppHead, DataType x) //尾插
{
SListNode*newNode;
SListNode*tmp;
tmp=*ppHead;
newNode=BuySListNode(x);
if (NULL==*ppHead)
{
*ppHead=newNode;
(*ppHead)->_next=NULL;
}
else
{
while (tmp->_next)
tmp=tmp->_next;
newNode->_next=NULL;
tmp->_next=newNode;
}
}
③随机插入
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x) //随机插入
{
SListNode*newNode;
SListNode*tmp=*ppHead;
newNode=BuySListNode(x);
if (*ppHead==pos)
{
newNode->_next=*ppHead;
*ppHead=newNode;
}
else
{
while (tmp->_next!=pos)
tmp=tmp->_next;
newNode->_next=pos;
tmp->_next=newNode;
}
}
4、链表的删除(①头删;②尾删;③随机删除)
①头删
void SListPopFront(SListNode** ppHead) //头删
{
SListNode *tmp;
tmp=*ppHead;
if (NULL==*ppHead || NULL==(*ppHead)->_next)
{
*ppHead=NULL;
free(*ppHead);
*ppHead=NULL;
}
else
{
*ppHead=(*ppHead)->_next;
free(tmp);
tmp=NULL;
}
}
②尾删
void SListPopBack(SListNode** ppHead) //尾删
{
SListNode *tmp=*ppHead;
SListNode *cur;
if (NULL==*ppHead || NULL==(*ppHead)->_next)
{
*ppHead=NULL;
free(*ppHead);
}
else
{
while (tmp->_next)
{
cur=tmp;
tmp=tmp->_next;
}
tmp=NULL;
cur->_next=tmp;
free(tmp);
}
}
③随机删除
void SListErase(SListNode** ppHead, SListNode* pos) //随机删除
{
SListNode*tmp;
tmp=*ppHead;
if (*ppHead==pos)
{
*ppHead=(*ppHead)->_next;
free(tmp);
tmp=NULL;
}
else
{
while (tmp->_next!=pos)
tmp=tmp->_next;
tmp->_next=pos->_next;
free(pos);
pos=NULL;
}
}
5、链表的查找
SListNode* SListFind(SListNode* pHead, DataType x) //查找
{
SListNode*tmp;
tmp=pHead;
if (NULL==tmp)
return NULL;
else
{
do
{
if (x==tmp->_data)
return tmp;
tmp=tmp->_next;
}while(NULL!=tmp);
return NULL;
}
}
6、链表的修改
void SListChange(SListNode**pphead,SListNode *pos,DataType x) //链表的修改
{
SListNode *tmp;
tmp=*pphead;
if (NULL==*pphead)
return;
else if (NULL==tmp->_next)
tmp->_data=x;
else
{
while (pos->_next!= tmp->_next)
tmp=tmp->_next;
tmp->_data=x;
}
}
7、链表的销毁
void SListDestory(SListNode** ppHead) //销毁链表
{
free(*ppHead);
*ppHead=NULL;
}
三、整体代码(后附带测试代码和测试运行结果)
1、SListNode.h
#ifndef __SLISTNODE_H__
#define __SLISTNODE_H__
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int DataType;
typedef struct SListNode
{
struct SListNode* _next;
DataType _data;
}SListNode;
SListNode* BuySListNode(DataType x); //创建新节点
void SListPrint(SListNode* pHead); //打印链表
void SListDestory(SListNode** ppHead); //销毁链表
void SListPushFront(SListNode** ppHead, DataType x); //头插
void SListPushBack(SListNode** ppHead, DataType x); //尾插
void SListPopBack(SListNode** ppHead); //尾删
void SListPopFront(SListNode** ppHead); //头删
SListNode* SListFind(SListNode* pHead, DataType x); //查找
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x); //插入
void SListChange(SListNode**pphead,SListNode *pos,DataType x); //链表的修改
void SListErase(SListNode** ppHead, SListNode* pos); //清空链表
#endif //__SLISTNODE_H__
2、SListNode.c
#include "SListNode.h"
SListNode* BuySListNode(DataType x) //创建新节点
{
SListNode* tmp;
tmp=(SListNode*)malloc(sizeof(SListNode));
if (NULL==tmp)
return NULL;
else
{
tmp->_data=x;
tmp->_next=NULL;
return tmp;
}
}
void SListPrint(SListNode* pHead) //打印链表
{
if (NULL==pHead)
{
printf("空链表\n");
}
else
{
while (pHead)
{
printf("%d ",pHead->_data);
pHead=pHead->_next;
}
printf("\n");
}
}
void SListPushFront(SListNode** ppHead, DataType x) //头插
{
SListNode *newNode;
newNode=BuySListNode(x);
if (NULL==*ppHead)
{
newNode->_next=NULL;
*ppHead=newNode;
}
else
{
newNode->_next=*ppHead;
*ppHead=newNode;
}
}
void SListPushBack(SListNode** ppHead, DataType x) //尾插
{
SListNode*newNode;
SListNode*tmp;
tmp=*ppHead;
newNode=BuySListNode(x);
if (NULL==*ppHead)
{
*ppHead=newNode;
(*ppHead)->_next=NULL;
}
else
{
while (tmp->_next)
tmp=tmp->_next;
newNode->_next=NULL;
tmp->_next=newNode;
}
}
void SListPopBack(SListNode** ppHead) //尾删
{
SListNode *tmp=*ppHead;
SListNode *cur;
if (NULL==*ppHead || NULL==(*ppHead)->_next)
{
*ppHead=NULL;
free(*ppHead);
}
else
{
while (tmp->_next)
{
cur=tmp;
tmp=tmp->_next;
}
tmp=NULL;
cur->_next=tmp;
free(tmp);
}
}
void SListPopFront(SListNode** ppHead) //头删
{
SListNode *tmp;
tmp=*ppHead;
if (NULL==*ppHead || NULL==(*ppHead)->_next)
{
*ppHead=NULL;
free(*ppHead);
*ppHead=NULL;
}
else
{
*ppHead=(*ppHead)->_next;
free(tmp);
tmp=NULL;
}
}
SListNode* SListFind(SListNode* pHead, DataType x) //查找
{
SListNode*tmp;
tmp=pHead;
if (NULL==tmp)
return NULL;
else
{
do
{
if (x==tmp->_data)
return tmp;
tmp=tmp->_next;
}while(NULL!=tmp);
return NULL;
}
}
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x) //随机插入
{
SListNode*newNode;
SListNode*tmp=*ppHead;
newNode=BuySListNode(x);
if (*ppHead==pos)
{
newNode->_next=*ppHead;
*ppHead=newNode;
}
else
{
while (tmp->_next!=pos)
tmp=tmp->_next;
newNode->_next=pos;
tmp->_next=newNode;
}
}
void SListErase(SListNode** ppHead, SListNode* pos) //随机删除
{
SListNode*tmp;
tmp=*ppHead;
if (*ppHead==pos)
{
*ppHead=(*ppHead)->_next;
free(tmp);
tmp=NULL;
}
else
{
while (tmp->_next!=pos)
tmp=tmp->_next;
tmp->_next=pos->_next;
free(pos);
pos=NULL;
}
}
void SListChange(SListNode**pphead,SListNode *pos,DataType x) //链表的修改
{
SListNode *tmp;
tmp=*pphead;
if (NULL==*pphead)
return;
else if (NULL==tmp->_next)
tmp->_data=x;
else
{
while (pos->_next!= tmp->_next)
tmp=tmp->_next;
tmp->_data=x;
}
}
void SListDestory(SListNode** ppHead) //销毁链表
{
free(*ppHead);
*ppHead=NULL;
}
3、test.c
#include "SListNode.h"
SListNode *SList;
int main()
{
SList=BuySListNode(2); //创建链表
SListPrint(SList); //打印链表
SListPushFront(&SList,5); //头插
SListPushFront(&SList,15);
SListPrint(SList);
SListPushBack(&SList,10); //尾插
SListPushBack(&SList,1);
SListPushBack(&SList,4);
SListPrint(SList);
SListPopBack(&SList); //尾删
SListPrint(SList);
SListPopFront(&SList); //头删
SListPrint(SList);
printf("%p\n",SListFind(SList,2)); //查找
printf("%p\n",SListFind(SList,5));
printf("%p\n",SListFind(SList,10));
printf("%p\n",SListFind(SList,1));
printf("%p\n",SListFind(SList,15));
SListChange(&SList,SListFind(SList,5),56); //修改
SListChange(&SList,SListFind(SList,1),46);
SListChange(&SList,SListFind(SList,2),36);
SListChange(&SList,SListFind(SList,10),26);
SListPrint(SList);
SListErase(&SList,SListFind(SList,56)); //随机删除
SListErase(&SList,SListFind(SList,36));
SListPrint(SList);
SListDestory(&SList); //销毁链表
SListPrint(SList);
return 0;
}
4、测试结果