本帖最后由 小可爱~ 于 2016-9-4 16:41 编辑
仅当参考
[C] 纯文本查看 复制代码 #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _list
{
int Data;
struct _list *Next;
}list;
// - - - - 函数申明 - - -
//链表的初始化
list *creat_list(list *pHead);
//链表的输出
int print_list(list *pHead);
//链表插入
void Insert_list(list *pHead, list *pIn, int num);
//链表删除
void del_list(list *pHead, int num);
//链表的排序
void sort_list(list *pHead);
//链表的逆置
void Slist_Inverse(list *pHead);
//销毁链表
int Destroy_list(list **pHead);
// - - - - - -主函数 - - - - - - -
int main(void)
{
int ret = 0;
list *pHead = NULL;
list *pIn = (list*)malloc(sizeof(list));
if (NULL == pIn)
{
goto END;
}
pIn->Data = 10;
pIn->Next = NULL;
pHead = creat_list(pHead);
if (0 == pHead)
{
goto END;
}
ret = print_list(pHead);
if (-1 == ret)
{
goto END;
}
Insert_list(pHead, pIn, 2);
printf("插入节点之后\n");
ret = print_list(pHead);
if (-1 == ret)
{
goto END;
}
del_list(pHead, 2);
printf("删除节点之后\n");
ret = print_list(pHead);
if (-1 == ret)
{
goto END;
}
sort_list(pHead);
printf("排序节点之后\n");
ret = print_list(pHead);
if (-1 == ret)
{
goto END;
}
Slist_Inverse(pHead);
printf("逆置节点之后\n");
ret = print_list(pHead);
if (-1 == ret)
{
goto END;
}
END:
Destroy_list(&pHead);
system("pause");
return ret;
}
// - - - - 函数定义 - - - - -
//链表的初始化
list *creat_list(list *pHead)
{
list *pCur = NULL; //定义当前节点变量
list *pNext = NULL;//定义后一个节点
int num = 0;
//1.创建头结点
pHead = pCur = pNext = (list *)malloc(sizeof(list));
if (NULL == pHead)
{
printf("func creat_list() err line:%d, file:%s\n", __LINE__, __FILE__);
return NULL;
}
//2.初始化头结点
pHead->Data = 0;
pHead->Next = NULL;
printf("输入数据 -1 结束\n");
while (1)
{
//3.创建新节点
pNext = (list *)malloc(sizeof(list));
if (NULL == pNext)
{
printf("func creat_list() err line:%d, file:%s\n", __LINE__, __FILE__);
return NULL;
}
printf("输入数据 %d :", ++num);
//4.新节点数据初始化
scanf("%d", &pNext->Data);
if (-1 == pNext->Data)
{
break;
}
pNext->Next = NULL;
//5.当前节点连接新节点
pCur->Next = pNext;
//6.当前节点下移到新节点
pCur = pNext;
}
//7.返回头节点
return pHead;
}
//链表的输出
int print_list(list *pHead)
{
int ret = 0;
if (NULL == pHead)
{
ret = -1;
printf("func print_list() err code:%d, line:%d, file:%s\n", ret, __LINE__, __FILE__);
return ret;
}
pHead = pHead->Next;//去掉第一个数值
while (pHead)
{
printf("%d\n", pHead->Data);
pHead = pHead->Next;
}
return 0;
}
//链表插入
void Insert_list(list *pHead, list *pIn, int num)
{
list *pPr = NULL;//前一个节点
list *pCur = NULL;//当前节点
pCur = pPr = pHead;//初始化
pCur = pCur->Next;//当前节点下移 去掉pHead的data为 0 的那一项
//寻找要修改的那一项
while (--num)
{
pCur = pCur->Next;
pPr = pPr->Next;
}
pPr->Next = pIn;//前一个节点的next修改为pIn的地址
pIn->Next = pCur;//插入节点的下一个节点为当前节点
}
//链表删除
void del_list(list *pHead, int num)
{
list *pPr = NULL;//前一个节点
list *pCur = NULL;//当前节点
//初始化节点
pCur = pPr = pHead;
//当前节点介入下一个节点 去掉pHead 的第一个节点的值
pCur = pCur->Next;
while (--num)
{
pCur = pCur->Next;
pPr = pPr->Next;
}
pPr->Next = pCur->Next;
if (NULL != pCur)
{
free(pCur);
}
}
//链表的排序
void sort_list(list *pHead)
{
int Tmp_dat = 0;
list *pNext = NULL;
pNext = pHead;
for (pHead = pHead->Next; pHead != NULL; pHead = pHead->Next)
{
for (pNext = pHead->Next; pNext != NULL; pNext = pNext->Next)
{
if (pHead->Data > pNext->Data)
{
Tmp_dat = pHead->Data;
pHead->Data = pNext->Data;
pNext->Data = Tmp_dat;
}
}
}
}
//链表的逆置
void Slist_Inverse(list *pHead)
{
list *pPr = NULL;//前一个节点
list *pCur = NULL;//当前节点
list *pNext = NULL;//临时节点
pCur = pHead->Next;//初始化
while (pCur)
{
pNext = pCur->Next;//当前节点的next备份
pCur->Next = pPr;//当前节点的next指向前一个节点的地址
pPr = pCur;//前一个节点变成当前节点
pCur = pNext;//当前节点下移
}
pHead->Next = pPr;//这个在循环出来之后是当前节点
}
//销毁链表
int Destroy_list(list **pHead)
{
list *pTmp = NULL;
list *Head = *pHead;
while (Head)
{
pTmp = Head->Next;
if (NULL != Head)
{
free(Head);
*pHead = NULL;//野指针
}
*pHead = Head = pTmp;
}
return 0;
}
|