这些基本的操作在代码里都有注释,任意位置插入有图解,有了这些应该可以看懂了,看不懂的话请留言阿鲤给你解释。希望可以对大家有一点帮助(文件在最后)
首先任意位置插入的图解:
首先总代码是这样的
void SListInsert(pNode pos, SDataType data)//任意位置插入
{
if (NULL == pos)
return;
else
{
pNode pNewNode = BuySListNode(data);
pNewNode->_pNext = pos->_pNext;
pos->_pNext = pNewNode;
}
}
图解:
我们假设原来的链表数据为1,2,3,4,5;地址分别为111,fff,666,329,888 然后我们建立一个new节点 将它插到3位置
执行图:
1:原链表
2:将新链表指向3位置(3的后面)
pNewNode->_pNext = pos->_pNext;
3:打断3到4的指向并将3的指向new
pNewNode->_pNext = pos->_pNext;
这样一个简单的插入就完成了。
实现代码文件
PList.h//头文件
#ifndef _LIST_H_
#define _LIST_H_
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<malloc.h>
typedef int SDataType;
typedef struct SListNode //链表的节点
{
SDataType _data; //当前节点的值
struct SListNode *_pNext;//保存下一个节点的地址
}Node, *pNode;
typedef struct SList //保存链表的第一个地址,类似于有头单链表的头
{
pNode _pHead; //指向链表中的第一个节点
}SList;
void SListInit(SList *s);//初始化
void SListPushBack(SList *s, SDataType data);//尾插
void SListPopBack(SList *s);//尾删
void SListPushFront(SList *s, SDataType data);//头插
void SListPopFront(SList *s);//头删
void SListInsert(pNode pos, SDataType data);//任意位置插入
void SListErase(pNode pos);//任意位置删除
void PrintSList(SList *s);//打印
void Testlist();//测试
pNode SListFind(SList *s, SDataType data);//查找位置
size_t SListSize(SList *s);//统计节点数
int SListEmpty(SList *s);//判空
#endif
test.c//测试文件
#include"PList.h"
void Testlist()
{
SList s;
SListInit(&s);
SListPushBack(&s, 1);//尾插
SListPushBack(&s, 2);
SListPushBack(&s, 3);
SListPushBack(&s, 4);
SListPushBack(&s, 5);
PrintSList(&s);//打印
SListPopBack(&s);//尾删
PrintSList(&s);
SListPopBack(&s);
SListPopBack(&s);
SListPopBack(&s);
PrintSList(&s);
SListPopBack(&s);
PrintSList(&s);
SListPushFront(&s, 5);//头插
SListPushFront(&s, 4);
SListPushFront(&s, 3);
SListPushFront(&s, 2);
SListPushFront(&s, 1);
PrintSList(&s);
SListPopFront(&s);//头删
SListPopFront(&s);
SListPopFront(&s);
PrintSList(&s);
SListPopFront(&s);
SListPopFront(&s);
PrintSList(&s);
SListPushFront(&s, 5);//头插
SListPushFront(&s, 4);
SListPushFront(&s, 3);
SListPushFront(&s, 2);
SListPushFront(&s, 1);
PrintSList(&s);
SListInsert(SListFind(&s,3), 8);//任意位置插入(先找到3的位置再传给SListInsert)
PrintSList(&s);
SListErase(&s,SListFind(&s, 4));//任意位置删除(先找到4的位置再传给SListInsert)
PrintSList(&s);
}
int main()
{
Testlist();
system("pause");
return 0;
}
list.c//实现文件
#include"PList.h"
void SListInit(SList *s)//初始化
{
assert(s);
s->_pHead = NULL;
}
pNode BuySListNode(SDataType data)
{
pNode pNewNode = (pNode)malloc(sizeof(Node)); //建立一个新节点
if (NULL == pNewNode) //判空 确保新建立的节点pNewNode创建成功
{
assert(0);
return NULL;
}
pNewNode->_data = data; //赋值
pNewNode->_pNext = NULL;
return pNewNode;
}
void SListPushBack(SList *s, SDataType data)//尾插
{
assert(s);
pNode pNewNode = BuySListNode(data);
if (NULL == s->_pHead)
{
//空链表
s->_pHead = pNewNode;
}
else
{
//链表非空 找最后一个节点
pNode pCur = s->_pHead;
while (pCur->_pNext)
{
pCur = pCur->_pNext;
}
pCur->_pNext = pNewNode;
}
}
void SListPopBack(SList *s)//尾删
{
assert(s);
if (NULL == s->_pHead)//空链表
return;
else if (NULL == s->_pHead->_pNext)//只有一个节点
{
free(s->_pHead);
s->_pHead = NULL;
}
else//多个节点
{
pNode pCur = s->_pHead;
pNode pCre = NULL;
while (pCur->_pNext)
{
pCre = pCur;
pCur = pCur->_pNext;
}
free(pCur);
pCre->_pNext = NULL;
}
}
void SListPushFront(SList *s, SDataType data)//头插
{
assert(s);
pNode pNewNode = BuySListNode(data);
pNewNode->_pNext = s->_pHead;
s->_pHead = pNewNode;
}
void SListPopFront(SList *s)//头删
{
assert(s);
if (NULL == s->_pHead)
return;
else
{
pNode pDelNode = s->_pHead;
s->_pHead = pDelNode->_pNext;
free(pDelNode);
}
}
void SListInsert(pNode pos, SDataType data)//任意位置插入
{
if (NULL == pos)
return;
else
{
pNode pNewNode = BuySListNode(data);
pNewNode->_pNext = pos->_pNext;
pos->_pNext = pNewNode;
}
}
void SListErase(SList *s, pNode pos)//任意位置删除
{
assert(s);
if (NULL == pos || NULL == s->_pHead)
return;
if (pos == s->_pHead)
{
s->_pHead = pos->_pNext;
}
else
{
pNode pPrePos = s->_pHead;
while (NULL != pPrePos && pPrePos->_pNext != pos)
{
pPrePos = pPrePos->_pNext;
}
if (pPrePos)
{
pPrePos->_pNext = pos->_pNext;
}
}
free(pos);
}
void PrintSList(SList *s)//打印
{
assert(s);
pNode pCur = s->_pHead;
while (pCur)
{
printf("%d--->", pCur->_data);
pCur = pCur->_pNext;
}
printf("NULL\n");
}
pNode SListFind(SList *s, SDataType data)//查找位置
{
assert(s);
pNode pCur = s->_pHead;
while (pCur)
{
if (pCur->_data == data)
{
return pCur;
}
pCur = pCur->_pNext;
}
return NULL;
}
size_t SListSize(SList *s)//统计链表有多少个节点
{
assert(s);
pNode pCur = s->_pHead;
int count = 0;
while (pCur)
{
count++;
pCur = pCur->_pNext;
}
return count;
}
int SListEmpty(SList *s)//判空
{
assert(s);
return NULL == s->_pHead;
}