#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{
int data;
struct node *next;
}SLIST;
//创建链表
SLIST *create_Slist(SLIST *pHead);
int print_Slist();
//在节点数值为x的前面插入y
int Slist_NodeInsert(SLIST *pHead, int x, int y);
//删除节点y
int Slist_NodeDel(SLIST *phead, int y);
int Slist_Destory(SLIST *phead);
int Slist_revse(SLIST *phead);
int Slist_NodeInsert(SLIST *pHead, int x, int y)
{
int ret = 0;
SLIST *pPior = NULL;
SLIST *pCur = NULL;
SLIST *pNode = NULL;
//环境准备
pCur = pHead->next;
pPior = pHead;
pNode = (SLIST *)malloc(sizeof(SLIST));
pNode->data = y;
pNode->next = NULL;
if (NULL == pHead)
{
ret = -1;
printf("func Slist_NodeInsert err: %d", ret);
return ret;
}
while (pCur)
{
if (x == pCur ->data)
{
break;
}
pPior = pCur;
pCur = pCur->next;
}
pNode->next = pCur;
pPior->next = pNode;
return ret;
}
int print_Slist(SLIST *pHead)
{
SLIST *pCur = NULL;
int ret = 0;
if (NULL == pHead)
{
ret = -1;
printf("func print_Slist() err:%d", ret);
return ret;
}
pCur = pHead->next;
printf("\nBegin\n");
while (pCur)
{
printf("%d ", pCur->data);
pCur = pCur->next;
}
printf("\nEnd\n");
return ret;
}
SLIST *create_Slist()
{
//建立带有头结点,判断输入是否为-1,以-1作为结束符,否则,新建节点,malloc新入链表
SLIST *pHead = NULL;
SLIST *pCur = NULL;
SLIST *pM;
int data = 0;
pHead = (SLIST *)malloc(sizeof(SLIST));
if (NULL == pHead)
{
return NULL;
}
pHead->data = 0;
pHead->next = NULL;
printf("please enter the data of node:(-1: quit)\n");
scanf("%d", &data);
//不断的malloc节点
//不断的让新pM节点入链表
//1、新节点入链表(pCur -> next = pM)
//2、当前节点下移,Pcur = pM
//我们需要不断的记录下来最后一个节点的位置,pcur
pCur = pHead;//准备环境,让pCur指向phead
while (data != -1)
{
pM = (SLIST *)malloc(sizeof(SLIST));
if (NULL == pM)
{
Slist_Destory(pHead);
return NULL;
}
pM->data = data;
pM->next = NULL;
pCur->next = pM;
pCur = pM;
printf("please enter the data of node:(-1: quit)\n");
scanf("%d", &data);
}
pCur->next = NULL;//可以 不用已经构件节点的时候已经复制了
return pHead;
}
int Slist_NodeDel(SLIST *phead, int y)
{
SLIST *pPrior = NULL;
int ret = 0;
pPrior = phead;
phead = phead->next;
if (NULL == phead)
{
ret = -1;
printf("func Slist_NodeDel() err: %d", ret);
return ret;
}
while (phead)
{
if (phead->data == y)
{
pPrior->next = phead->next;
free(phead);
break;
}
pPrior = phead;
phead = phead->next;
}
if (NULL == phead)
{
ret = -2;
printf("没有找到节点:%d", y);
return ret;
}
return ret;
}
int Slist_Destory(SLIST *phead)
{
int ret = 0;
SLIST *p = NULL;
p = phead;
if (NULL == phead)
{
ret = -1;
printf("func Slist_Destory() err :%d", ret);
return ret;
}
while (phead)
{
p = phead;
phead = phead->next;
free(p);
}
return ret;
}
int Slist_reseve(SLIST *phead)
{
int ret = 0;
SLIST *ptemp = NULL;
SLIST *p = NULL;
if (NULL == phead)
{
ret = -1;
printf("func Slist_reseve() err: %d", ret);
return ret;
}
p = phead->next;
//ptemp = p;
phead->next = NULL;
while (p)
{
ptemp = p ->next;
p->next = phead->next;
phead->next = p;
p = ptemp;
}
}
void main()
{
printf("------------创建节点--------------------\n");
SLIST *pHead = create_Slist();
print_Slist(pHead);
printf("------------插入后--------------------\n");
Slist_NodeInsert(pHead, 3, 6);
Slist_NodeInsert(pHead, 33, 100);
print_Slist(pHead);
printf("------------删除后--------------------\n");
Slist_NodeDel(pHead, 3);
print_Slist(pHead);
printf("------------逆置链表--------------------\n");
Slist_reseve(pHead);
print_Slist(pHead);
printf("------------释放内存--------------------\n");
Slist_Destory(pHead);
system("pause");
}