链表的基本操作:
链表的创建、遍历、插入、删除、销毁、逆置
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node{
int node;
struct Node *next;
}SList;
SList *SList_Creat(); //创建链表
int SList_print(SList *PHead); //遍历链表
int SList_Insert(SList *PHead,int x,int y); //插入元素
int SList_Delete(SList *PHead,int x); //删除元素
void SList_Destory(SList *PHead); //销毁链表
int SList_Reverse(SList *PHead); //逆置链表
SList *SList_Creat()
{
SList *pHead = NULL,*pCur,*pM;
int data;
//创建头结点并初始化
pHead = (SList *)malloc(sizeof(SList));
if( pHead == NULL )
{
return NULL;
}
pHead->node = 0;
pHead->next = NULL;
//接收用户输入
printf("\nplease enter your data:");
scanf("%d",&data);
pCur = pHead;
while( data != -1 )
{
//1.malloc空间
pM = (SList *)malloc(sizeof(SList));
if( pM == NULL )
{
return NULL;
}
pM->next = NULL;
pM->node = data;
//2.新节点入链表
pCur->next = pM;
pCur = pM;
printf("\nplease enter your data:");
scanf("%d",&data);
}
return pHead;
}
int SList_Creat2(SList **myList)
{
SList *pHead = NULL,*pCur,*pM;
int data,ret = 0;
//创建头结点并初始化
pHead = (SList *)malloc(sizeof(SList));
if( pHead == NULL )
{
ret = -1;
goto END;
}
pHead->node = 0;
pHead->next = NULL;
//接收用户输入
printf("\nplease enter your data:");
scanf("%d",&data);
pCur = pHead;
while( data != -1 )
{
//1.malloc空间
pM = (SList *)malloc(sizeof(SList));
if( pM == NULL )
{
ret = -2;
goto END;
}
pM->next = NULL;
pM->node = data;
//2.新节点入链表
pCur->next = pM;
pCur = pM;
printf("\nplease enter your data:");
scanf("%d",&data);
}
END:
if( ret != 0 )
SList_Destory(pHead);
else
*myList = pHead;
return 0;
}
int SList_print(SList *PHead)
{
int ret = 0;
SList *tmp = NULL;
if( PHead == NULL )
{
ret = -1;
printf("Error PHead is NULL %d",ret);
return ret;
}
tmp = PHead->next;
printf("Begin\t");
while(tmp != NULL)
{
printf("%d\t",tmp->node);
tmp = tmp->next;
}
printf("End\n");
return 0;
}
int SList_Insert(SList *PHead,int x,int y)
{
int ret = 0;
SList *pM = NULL,*pCur = NULL,*pPre = NULL;
if( PHead ==NULL )
{
ret = -1;
printf("Error SList_Insert PHead is NULL.");
return ret;
}
pM = (SList *)malloc(sizeof(SList));
if( pM == NULL )
{
return NULL;
}
pM->node = y;
pM->next = NULL;
pPre = PHead;
pCur = PHead->next;
while( pCur )
{
if(pCur->node == x)
{
break;
}
else
{
pPre = pCur;
pCur = pCur->next;
}
}
pM->next = pCur;
pPre->next = pM;
return 0;
}
int SList_Delete(SList *PHead,int x)
{
SList *pCur = NULL,*pPre = NULL;
pPre = PHead;
pCur = PHead->next;
while( pCur )
{
if(pCur->node == x)
{
break;
}
else
{
pPre = pCur;
pCur = pCur->next;
}
}
if(pCur == NULL )
{
printf("没有找到%d\n",x);
return -1;
}
pPre->next = pCur->next;
if(pCur != NULL)
{
free(pCur);
}
return 0;
}
void SList_Destory(SList *PHead)
{
SList *temp = NULL;
temp = PHead;
while(temp)
{
temp = PHead->next;
free(PHead);
PHead = temp;
}
}
int SList_Reverse(SList *PHead)
{
SList *pM = NULL,*pCur = NULL,*pPre = NULL;
int ret = 0;
if( PHead == NULL || PHead->next == NULL || PHead->next->next == NULL )
{
ret = -1;
printf("SList_Reverse()链表为空");
return ret;
}
pPre = PHead->next;
pCur = PHead->next->next;
while( pCur )
{
//逆置
pM = pCur->next;
pCur->next = pPre;
//移动
pPre = pCur;
pCur = pM;
}
PHead->next->next = NULL;
PHead->next = pPre;
return 0;
}
void main()
{
int ret = 0;
SList *list = NULL;
SList *list1 = NULL;
list = SList_Creat();
ret = SList_print(list);
SList_Creat2(&list1);
SList_print(list1);
ret = SList_Insert(list,12,11);
ret = SList_print(list);
ret = SList_Delete(list,10);
ret = SList_print(list);
ret = SList_Reverse(list);
ret = SList_print(list);
SList_Destory(list);
system("pause");
}