问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
typedef int SListDataType;
typedef struct SListNode
{
SListDataType data;
struct SListNode* next;
}SListNode;
SListNode* CreatSListNode(SListDataType x)
{
SListNode* Newnode = (SListNode*)malloc(sizeof(SListNode));
if (Newnode == NULL)
{
printf("申请内存失败!");
exit(-1);
}
Newnode->data = x;
Newnode->next = NULL;
return Newnode;
}
//void InitSList(SListNode *L)
//{
// L = (SListNode*)malloc(sizeof(SListNode));
// L->data = 0;
// L->next = NULL;
//
//}
SListNode * InitSList()
{
SListNode* L = (SListNode*)malloc(sizeof(SListNode));
L->data = 0;
L->next = NULL;
return L;
}
void HeadInsertSList(SListNode* L,SListDataType x)
{
SListNode* Newnode = (SListNode*)malloc(sizeof(SListNode));
Newnode->data = x;
Newnode->next = L->next;
L->next = Newnode;
L->data++;
}
void TailInsertSList(SListNode* L, SListDataType x)
{
SListNode* p=CreatSListNode(x);
SListNode* cur = L;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next= p;
L->data++;
}
void EraseSList(SListNode* L, SListDataType x)
{
SListNode* pre = L;
SListNode* cur = L->next;
while (cur)
{
if (cur->data == x)
{
pre->next = cur->next;
free(cur);
}
pre = cur;
cur = cur->next;
}
}
void PrintSList(SListNode *L)
{
SListNode* p = L->next;
while (p!=NULL)
{
printf("%d->", p->data);
p = p->next;
}
printf("NULL");
}
int main()
{
SListNode* L=InitSList();
L->next = NULL;
L->data = 0;
InitSList(L);
HeadInsertSList(L, 1);
HeadInsertSList(L, 2);
HeadInsertSList(L, 3);
HeadInsertSList(L, 4);
TailInsertSList(L, 5);
TailInsertSList(L, 6);
TailInsertSList(L, 7);
TailInsertSList(L, 8);
EraseSList(L, 3);
PrintSList(L);
return 0;
}