//头插
void SListPushFront(SListNode** pplist, SLTDateType x)
{
SListNode* pcurr = BuySListNode(x);
if(*pplist==NULL)
{
*pplist = pcurr;
}
else
{
pcurr->next = *pplist;//这里会写成pcurr->next = pplist->next;
*pplist = pcurr;
}
}
//调用
#include "slist.h"
#include<Windows.h>
int main()
{
SListNode *plist=NULL;
SListPushFront(&plist,9);
SListPushFront(&plist,8);
SListPrint(plist);
system("pause");
return 0;
}
注意: 指向下一个节点时,不要写成pcur->next = pplist->next;如上图绿色 箭头方向
完整代码
//1.test.c
#include "slist.h"
#include<Windows.h>
int main()
{
SListNode *plist=NULL;
SListPushBack(&plist,10);
SListPushBack(&plist,11);
SListPushBack(&plist,12);
SListPushBack(&plist,13);
SListPushFront(&plist,9);
SListPushFront(&plist,8);
//SListPopBack(&plist);
//SListPopBack(&plist);
SListPopFront(&plist);
SListDestory(plist);
plist=NULL;//销毁后需要将结构体指针变量置空
SListPrint(plist);
system("pause");
return 0;
}
//slist.h
#include<stdio.h>
#include<stdlib.h>
typedef int SLTDateType;
typedef struct SListNode
{
SLTDateType data;
struct SListNode* next;
}SListNode;
// 动态申请一个末节点
SListNode* BuySListNode(SLTDateType x);
// 单链表打印
void SListPrint(SListNode* plist);
// 单链表尾插
void SListPushBack(SListNode** pplist, SLTDateType x);
// 单链表的头插
void SListPushFront(SListNode** pplist, SLTDateType x);
// 单链表的尾删
void SListPopBack(SListNode** pplist);
// 单链表头删
void SListPopFront(SListNode** pplist);
// 单链表查找
SListNode* SListFind(SListNode* plist, SLTDateType x);
// 单链表在pos位置之后插入x
// 分析思考为什么不在pos位置之前插入?
void SListInsertAfter(SListNode* pos, SLTDateType x);
// 单链表删除pos位置之后的值
// 分析思考为什么不删除pos位置?
void SListEraseAfter(SListNode* pos);
// 单链表的销毁
void SListDestory(SListNode* plist);
//3.slist.c
#include "slist.h"
SListNode* BuySListNode(SLTDateType x)
{
SListNode* ps = (SListNode*)malloc(sizeof(SListNode));
ps->data=x;
ps->next=NULL;
return ps;
}
void SListPrint(SListNode* plist)
{
SListNode* cur = plist;
while(cur!=NULL)
{
printf("%d ",cur->data);
cur = cur->next;
}
}
void SListPushBack(SListNode** pplist, SLTDateType x)
{
SListNode*list = BuySListNode(x);
if(*pplist==NULL)
{
*pplist =list;
}
else
{
SListNode* tail=*pplist;
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next=list;
}
}
void SListPushFront(SListNode** pplist, SLTDateType x)
{
SListNode* pcurr = BuySListNode(x);
if(*pplist==NULL)
{
*pplist = pcurr;
}
else
{
pcurr->next = *pplist;//这里会写成pcurr->next = pplist->next;
*pplist = pcurr;
}
}
void SListPopBack(SListNode** pplist)
{
/*SListNode* tail = *pplist;
while(tail->next->next!=NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next=NULL;*/
/***********方法二********/
SListNode* tailPrev = NULL;
SListNode* tail = *pplist;
if(tail->next==NULL)
{
free(tail);
*pplist=NULL;//这里如果使用tail=NULL;会置空失败
}else
{
while (tail->next != NULL)
{
tailPrev = tail;
tail = tail->next;
}
free(tail);//释放尾结点所在节点内存,地址在上一节点next中寻找
tailPrev->next = NULL;
}
}
void SListPopFront(SListNode** pplist)
{
SListNode* plist = (*pplist)->next;
free(*pplist);
*pplist = plist;
}
SListNode* SListFind(SListNode* plist, SLTDateType x)
{
SListNode* cur = plist;
while (cur)
{
if (cur->data == x)
return cur;
cur = cur->next;
}
return NULL;
}
void SListDestory(SListNode* plist)
{
SListNode* next=NULL;
while(plist!=NULL)
{
next = plist->next;
free(plist);
plist = next;
}
}
|