静态链表的应用范围只限于本函数,有局限性,无法跨函数调用,所以在子函数中要malloc分配内存。
链表的数据类型是结构体。
#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct Node
{
int data;
struct Node *next;
}SLIST;
SLIST *Creat_SList();//增
int SList_NodeDel(SLIST *pHead, int y);//删
int SList_NodeInsrt(SLIST *pHead, int x, int y);//查,改
int SList_reverse(SLIST *pHead);//逆置
int SList_print(SLIST *pHead);//显示
int SList_Destory(SLIST *pHead);//free节点
//在结点数为x的前面插入y
int SList_NodeInsrt(SLIST *pHead, int x, int y)
{
SLIST *pCur = NULL;
SLIST *pPre = NULL;
SLIST *pM = NULL;
if (pHead == NULL)
{
return -1;
}
pPre = pHead;
pCur = pHead->next;
//不断的malloc新结点,并且数据域赋值
pM = (SLIST *)malloc(sizeof(SLIST));
pM->data = y;
pM->next = NULL;
while (pCur)
{
if (pCur->data == x)
{
break;
}
//让pPre下移
pPre = pCur;
//让当前节点下移
pCur = pCur->next;
}
//让新节点连接后继节点
pM->next = pPre->next;
//让前驱节点连接新节点
pPre->next = pM;
return 0;
}
//删除结点为y的链表结点
int SList_NodeDel(SLIST *pHead, int y)
{
SLIST *pCur = NULL;
SLIST *pPre = NULL;
SLIST *pM = NULL;
if (pHead == NULL)
{
return -1;
}
pPre = pHead;
pCur = pHead->next;
while (pCur)
{
if (pCur->data == y)
{
break;
}
pPre = pCur;
pCur = pCur->next;
}
if (pCur == NULL)
{
printf("\n没有找到data:%d的节点\n",y);
return -2;
}
//让新节点连接后继节点
pPre->next = pCur->next;
//让前驱节点连接新节点
free(pCur);
return 0;
}
int SList_Destory(SLIST *pHead)
{
if (pHead == NULL)
{
return -1;
}
SLIST *p = pHead,*tmp = NULL;
while (p)
{
tmp = p->next;
free(p);
p = tmp;
}
return 0;
}
SLIST *Creat_SList()
{
//1、创建头结点并初始化
int data = 0;
SLIST *pHead = NULL, *pM = NULL, *pCur;
pHead = (SLIST *)malloc(sizeof(SLIST));
if (pHead == NULL)
{
return NULL;
}
pHead->data = 0;
pHead->next = NULL;
//2、循环创建结点,结点数据域的数值从键盘输入。
//以-1作为输入结束标志。
printf("\nPlease input the date of node:\n(Noting:-1:quit)");
scanf("%d", &data);
//初始化,使辅助指针指向pHead
pCur = pHead;
while (data != -1)
{
//不断的malloc新结点,并且数据域赋值
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
SList_Destory(pHead);
return NULL;
}
pM->data = data;
pM->next = NULL;
//新结点入链表
pCur->next = pM;
//当前结点下移(新节点变成当前结点)
pCur = pM;//or pCur=pCur->next;
printf("\nPlease input the date of node:\n(Noting:-1:quit)");
scanf("%d", &data);
}
//printf("\npHead :%x\n", pHead);
return pHead;
}
int SList_print(SLIST *pHead)
{
SLIST *p = NULL;
if (pHead == NULL)
{
return -1;
}
//准备环境
p = pHead->next;
printf("\n开始");
while (p)
{
printf("data:%d ->", p->data);
//不断的让当前指针下移
p = p->next;
}
printf("结束\n");
return 0;
}
int SList_reverse(SLIST *pHead)//逆置
{
SLIST *t = NULL;
SLIST *p = NULL;
SLIST *q = NULL;
if (pHead == NULL)
{
return -1;
}
if (pHead->next == NULL || pHead->next->next == NULL)
{
return 0;
}
p = pHead->next;
q = pHead->next->next;
while (q != NULL)
{
t = q->next;
q->next = p;
p = q;
q = t;
}
pHead->next->next = NULL;
pHead->next = p;
return 0;
}
void main()
{
int ret = 0;
SLIST *pHead = NULL;
pHead = Creat_SList();
ret=SList_print(pHead);
/*ret = SList_NodeInsrt(pHead, 20, 19);
ret = SList_print(pHead);*/
/*SList_NodeDel(pHead, 19);
ret = SList_print(pHead);*/
/*ret = SList_NodeInsrt(pHead, 100, 29);
ret = SList_print(pHead);*/
ret = SList_reverse(pHead);//逆置
ret = SList_print(pHead);
SList_Destory(pHead);
system("pause");
}