#include"SList.h"
#include<assert.h>
一般增加才有的节点才会用newnode,看push的话就直接newnode,删的话就不用增加newnode
看到POP的带有*都要free,要分tail头和tail ->next
SLTNode* BuyListNode(SLTDateType x) {
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL) {
printf("over");
exit(-1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
void SListPrint(SLTNode* phead) {
解析为什么要写cur ,cur就是个后驱把phead连接上,为什么连接不是cur->next,因为断就要断要头,直接把cur->next断了,剩下的就只有cur=NULL,因为while循环要zhui后面开始推,所以while(cur!=NULL)->退出来 cur->next的前一个就是cur,关于,cur->data,data本来就是下,也就是说cur->date就等于cur->x,重点是cur!=NULL
SLTNode* cur = phead;
while (cur != NULL) {
printf("%d->", cur->data);
cur = cur -> next;
}
printf("NULL\n");
}
void SListPushBack(SLTNode** phead, SLTDateType x) {
/*SLTNode* tail = phead;
while (tail->next != NULL) {
tail = tail->next;
}*/
/*SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;*/
//尾插分2种,第一种先要断定头是不是空的,是空的就和新节点连接上
//第2种尾插tail节点对接*phead,然后开始循环,找到开始循环tail->nixt!=NULL;
tail->nixtde 前面是tail,然后看循环tail->nextd 对接上newnode,要看外面的{}号
SLTNode* newnode = BuyListNode(x);
if (*phead == NULL) {//如果头为空就没办法玩了
*phead = newnode;
}
else {
SLTNode* tail = *phead;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newnode;
}
}//头插
//头部插入新节点有newnode,和newnode->next,所以只能newnode->next=就是对接*phead,然后*phead=对接newnode
void SListPushFront(SLTNode** phead, SLTDateType x) {
SLTNode* newnode = BuyListNode(x);
newnode->next = *phead;//plist在外部只能用phead
*phead = newnode;
}
//记住一点删的话,所有的节点都要删的,只有xx->next=NULL;,带有*的free了都要为空
//删除尾部节点的话要考虑*phead 是不是空的直接return 0,要是删删的话就删到(*phead)==NULL,直接free
//后删的话前后不能对接上,所以要是搞个前驱prev=NULL,然后tail对接上*phead,while(tail->next!=NULL),删除的目的就是把tail为NULL ,prev->next为NULL;
画图就可以清楚很多的,prev=tail 的,然后往前面推,prev在前面tail在后面 就等于prev=tail;tail在前面tial在后面所以就等于tail=tail->next,直接free(tail),tail节点为NULL,tail前面还有prev->next也要为NULL,prev一开始就为空了,反推过去就行了,反的*号的都要NULL,重要的是tail->next!=NULL就可以了循环了
void SListPopBack(SLTNode** phead) {//要删除节点SListPopBack,当然要FREE
//节点都是插入的时候 malloc好的 现在删除节点 就需要free掉
//assert(*phead != NULL);//删尾的话头节点,只能是(*phead)->next为空
if (*phead == NULL) {
return 0;
}
if ((*phead)->next == NULL) {
free(*phead);
*phead = NULL;
}
else
{
SLTNode* prv = NULL;//可能tial的话全部删除所以前驱要写NULL
SLTNode* tail = *phead;
while (tail->next != NULL) {
prv = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prv->next = NULL;
}
}
//头删节点要free为空的,
//头删的第一步就把*phead删了,剩下的就只有*phead->next,create一个新的SLTNode*next=*phead->next,*phead->next的头是*phead对接next
void SListPopFront(SLTNode** phead) {
//节点都是插入的时候 malloc好的 现在删除节点 就需要free掉
assert(*phead);
SLTNode* next = (*phead)->next;
free(*phead);
*phead = next;
}
SLTNode* SListFind(SLTNode* phead, SLTDateType x) {
SLTNode* cur = phead;
while (cur) {
if (cur->data == x) {
return cur;
}
else {
cur = cur->next;
}
}
return NULL;
}
//在pos 之前插入一个节点
void SListInsert(SLTNode** phead, SLTNode*pos, SLTDateType x){
SLTNode* newnode = BuyListNode(x);
找到pos前一个位置
//SLTNode* posPrev = *phead;
//while (posPrev->next != pos) {
// posPrev = posPrev->next;//不懂就画图,自己就会明白的
//}
//posPrev->next = newnode;
//newnode->next = pos;
if (*phead == pos) {//插入之前看下pos 和*phead是一样的
newnode->next = *phead;//不要写反了
*phead = newnode;
}
else {
//newnode->next = *phead;
//*phead = newnode;
SLTNode* posPrev = *phead;//单链就是有个缺点就是没有前驱,断了就链接不了,所以要找个前驱POSPrev
while (posPrev->next != pos) {
posPrev = posPrev->next;//不懂就画图,自己就会明白的
}
posPrev->next = newnode;
newnode->next = pos;
}
}
void SListEsrae(SLTNode** phead, SLTNode* pos) {//删除
if (*phead == pos) {
void SListPopFront(phead);
}
else {
SLTNode* prev = *phead;
while (prev->next != pos) {
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void SListDestory(SLTNode** phead) {
}