#pragma once
#include<stdio.h>
#include<stdlib.h>
typedef int SLTDateType;
typedef struct SListNode {
SLTDateType data;
struct SListNode* next;
}SLTNode;
void SListPrint(SLTNode* phead);//create一个头节点
void SListPushBack(SLTNode** phead, SLTDateType x);
void SListPushFront(SLTNode** phead, SLTDateType x);
void SListPopBack(SLTNode** phead);
void SListPopFront(SLTNode** phead);
SLTNode* SListFind(SLTNode* phead, SLTDateType x);
//在pos 之前插入一个节点
void SListInsert(SLTNode** phead, SLTNode* pos, SLTDateType x);
void SListEsrae(SLTNode** phead, SLTNode* pos);
void SListDestory(SLTNode** phead);
#include"SList.h"
#include<assert.h>
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) {
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;*/
SLTNode* newnode = BuyListNode(x);
if (*phead == NULL) {//如果头为空就没办法玩了
*phead = newnode;
}
else {
SLTNode* tail = *phead;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newnode;
}
}//头插
void SListPushFront(SLTNode** phead, SLTDateType x) {
SLTNode* newnode = BuyListNode(x);
newnode->next = *phead;//plist在外部只能用phead
*phead = newnode;
}
void SListPopBack(SLTNode** phead) {//尾删tail ,头节点phead为NULL就没办法玩了
assert(*phead != NULL);//头节点可能也是空的
if ((*phead)->next == NULL) {
free(*phead);
*phead = NULL;
}
else
{
SLTNode* prv = NULL;
SLTNode* tail = *phead;
while (tail->next != NULL) {
prv = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
prv->next = NULL;
}
}
void SListPopFront(SLTNode** phead) {
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) {
newnode->next = *phead;//不要写反了
*phead = newnode;
}
else {
//newnode->next = *phead;
//*phead = newnode;
SLTNode* posPrev = *phead;
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) {
}
#include"SList.h"
void Test() {
SLTNode* plist = NULL;//外部plist头指针对接**plist
SListPushBack(&plist,1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SListPushFront(&plist,1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SListPrint(plist);
}
void test() {
SLTNode* plist = NULL;
SListPushFront(&plist, 1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SListPrint(plist);
SListPopBack(&plist);
SListPrint(plist);
}
void test3() {
SLTNode* plist = NULL;
SListPushFront(&plist, 1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);
SListPopFront(&plist);
SListPrint(plist);
}
void test4() {
SLTNode* plist = NULL;
SListPushFront(&plist, 1);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SListPushFront(&plist, 2);
SListPushFront(&plist, 3);
SListPushFront(&plist, 4);
SLTNode* pos1 = SListFind(plist, 2);
if (pos1==NULL) {
return 0;
}
else {
int i = 0;
while (pos1) {
printf("%d pos节点:%p->%d\n", i++, pos1, pos1->data);
pos1 = SListFind(pos1->next, 2);
}
}
/*SLTNode* pos1 = SListFind(plist, 2);
int i = 1;
while (pos1) {
printf("%d pos节点:%p->%d\n", i++, pos1, pos1->data);
pos1 = SListFind(pos1->next, 2);
}*/
}
int main() {
//Test();
//test();
//test3();
test4();
return 0;
}