资源简介
链表的基本操作.c
代码片段和文件信息
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
typedef struct Node
{
int data;
struct Node *next;
}SLIST;
SLIST *SList_Create(); //创建链表
int SList_Print(SLIST *pHead); //遍历链表
int SList_NodeInsert(SLIST *pHead int x int y); //插入值 在x值之前 删除y
int SList_NodeDel(SLIST *pHead int y);
int SList_Destory(SLIST *pHead);
SLIST *SList_Create()
{
SLIST *pHead *pM *pCur;
int data;
//创建头节点 并初始化
pHead = (SLIST *)malloc(sizeof(SLIST));
if (pHead == NULL)
{
return NULL;
}
pHead->data = 0;
pHead->next = NULL;
printf(“\nplease enter you data: “);
scanf(“%d“ &data);
pCur = pHead;
while (data != -1)
{
//创建业务节点 并初始化 不断接受输入 malloc新结点
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
return NULL;
}
pM->data = data;
pM->next = NULL;
//2 新结点 入链表
pCur->next = pM;
//3 新结点变成当前节点
pCur = pM; //链表结点的尾部追加
printf(“\nplease enter you data: “);
scanf(“%d“ &data);
}
return pHead;
}
int SList_Print(SLIST *pHead)
{
SLIST *tmp = NULL;
if (pHead == NULL)
{
return -1;
}
tmp = pHead->next;
printf(“\nBegin\t“);
while (tmp)
{
printf(“%d “ tmp->data);
tmp = tmp->next;
}
printf(“\tEnd“);
return 0;
}
int SList_NodeInsert(SLIST *pHead int x int y)
{
SLIST *pM *pCur *pPre;
//创建新的业务节点pM
pM = (SLIST *)malloc(sizeof(SLIST));
if (pM == NULL)
{
return -1;
}
pM->next = NULL;
pM->data = y;
//遍历链表
pPre = pHead;
pCur = pHead->next;
while (pCur)
{
if (pCur->data == x)
{
break;
}
pPre = pCur;
pCur = pCur->ne
评论
共有 条评论