C语言单向链表练习
功能:实现链表数据的增删改查
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
在makefile中添加编译时间,可以在c代码中以宏的形式调用
CT = $(shell date +"%Y-%m-%d %H:%M:%S")
CTFLAG = -D _COMPILE_TIME="\"$(CT)"\"
$(CC) $(CTFLAG) -o test test.c
2021年2月4日
lisklist test
实现单向链表的增删改查
*/
#define NAMELEN 32
#define FILMMAX 3
#define METHOD_TWO
struct film
{
char Name[NAMELEN];
int Score;
struct film *pNext;
};
typedef struct film TFilm;
/*
获取子函数malloc的内存指针
法1:将malloc得到的内存首地址通过二级指针返回到主函数
法2:将malloc得到的内存首地址通过函数的返回值返回到主函数
*/
#ifdef METHOD_TWO
/* *****************************************************************************
* Name : CreateFilmList
* Description :
* Input :
* Output :
* Return : 返回链表头指针
* Note : 创建链表头节点
* *****************************************************************************/
TFilm * CreateFilmList(void)
{
TFilm *pHead = NULL;
pHead = malloc(sizeof(TFilm));
if (NULL == pHead)
{
printf("malloc err\n");
return NULL;
}
memset(pHead->Name, 0, sizeof(pHead->Name));
pHead->Score = 0;
pHead->pNext = NULL;
return pHead;
}
#else
/* *****************************************************************************
* Name : CreateFilmList
* Description :
* Input :
* Output :
* Return : 通过二级指针返回链表头指针
* Note : 创建链表头节点
* *****************************************************************************/
void CreateFilmList(TFilm **ppFilm)
{
TFilm *pHead = NULL;
pHead = malloc(sizeof(TFilm));
if (NULL == pHead)
{
printf("malloc err\n");
return;
}
memset(pHead->Name, 0, sizeof(pHead->Name));
pHead->Score = 0;
pHead->pNext = NULL;
*ppFilm = pHead;
}
#endif
/* *****************************************************************************
* Name : AddFilm
* Description :
* Input :
* Output :
* Return :
* Note : 尾部增加链表节点
* *****************************************************************************/
void AddFilm(TFilm *pFilm, char *pName, int nScore)
{
TFilm *pCur = NULL;
TFilm *pAddFilm = NULL;
if (NULL == pFilm)
{
printf("linklist err\n");
return;
}
pCur = pFilm;
//移动到末尾节点
while(NULL != pCur->pNext)
{
pCur = pCur->pNext;
}
pAddFilm = malloc(sizeof(TFilm));
if (NULL == pAddFilm)
{
printf("malloc err\n");
return;
}
//末尾节点的next指向新的节点
pCur->pNext = pAddFilm;
strcpy(pAddFilm->Name, pName);
pAddFilm->Score = nScore;
pAddFilm->pNext = NULL;
printf("add film[%s] socre[%d]\n", pAddFilm->Name, pAddFilm->Score);
}
/* *****************************************************************************
* Name : InsertFilm
* Description :
* Input : 在第n个节点处插入(计算不包括头节点)
* Output :
* Return :
* Note : 插入链表节点
* *****************************************************************************/
void InsertFilm(TFilm *pFilm, char *pName, int nScore, int n)
{
int i = 1;
TFilm *pCur = NULL;
TFilm *pInsertFilm = NULL;
if (NULL == pFilm)
{
printf("linklist err\n");
return;
}
//跳过头节点
pCur = pFilm->pNext;
//移动到第n-1节点
while((i < n-1) && (NULL != pCur->pNext))
{
pCur = pCur->pNext;
i++;
}
pInsertFilm = malloc(sizeof(TFilm));
if (NULL == pInsertFilm)
{
printf("malloc err\n");
return;
}
strcpy(pInsertFilm->Name, pName);
pInsertFilm->Score = nScore;
pInsertFilm->pNext = pCur->pNext;
printf("insert film[%s] socre[%d] index[%d]\n", pInsertFilm->Name, pInsertFilm->Score, n);
//第n-1节点的next指向新加节点
pCur->pNext = pInsertFilm;
}
/* *****************************************************************************
* Name : ChangeFilmScore
* Description : 根据name找到节点
* Input :
* Output :
* Return :
* Note : 修改链表节点的数据
* *****************************************************************************/
void ChangeFilmScore(TFilm *pFilm, char *pName, int nScore)
{
TFilm *pCur = NULL;
if (NULL == pFilm)
{
printf("linklist err\n");
return;
}
pCur = pFilm;
//查找对应节点
while(NULL != pCur)
{
if (0 == strcmp(pCur->Name, pName))
{
printf("previous film[%s] socre[%d]\n", pCur->Name, pCur->Score);
pCur->Score = nScore;
printf("current film[%s] socre[%d]\n", pCur->Name, pCur->Score);
break;
}
else
{
pCur = pCur->pNext;
}
}
}
/* *****************************************************************************
* Name : DelFilm
* Description :
* Input :
* Output :
* Return :
* Note : 删除链表节点
* *****************************************************************************/
void DelFilm(TFilm *pFilm, char *pName)
{
TFilm *pCur = NULL;
TFilm *pDelFilm = NULL;
if (NULL == pFilm)
{
printf("linklist err\n");
return;
}
pCur = pFilm;
//先找到指定节点的前一节点,使其next指向指定节点的next所指向的地址
while(NULL != pCur->pNext)
{
if (0 == strcmp(pCur->pNext->Name, pName))
{
pDelFilm = pCur->pNext;
printf("delet film[%s] socre[%d]\n", pDelFilm->Name, pDelFilm->Score);
pCur->pNext = pDelFilm->pNext;
free(pDelFilm);
pDelFilm = NULL;
}
else
{
pCur = pCur->pNext;
}
}
}
/* *****************************************************************************
* Name : DestoryFilmList
* Description :
* Input :
* Output :
* Return :
* Note : 删除整个链表
* *****************************************************************************/
void DestoryFilmList(TFilm *pFilm)
{
int i = 0;
TFilm *pTmp;
TFilm *pCur = pFilm;
if (NULL == pCur)
{
printf("linklist err\n");
return;
}
//循环处理,从头开始释放
while(NULL != pCur)
{
pTmp = pCur->pNext;
printf("node[%d] free[%#x]\n", i, pCur);
free(pCur);
i++;
pCur = pTmp;
}
}
/* *****************************************************************************
* Name : ShowFilmList
* Description :
* Input :
* Output :
* Return :
* Note : 显示链表数据
* *****************************************************************************/
void ShowFilmList(TFilm *pFilm)
{
int i = 1;
TFilm *pHead = pFilm;
TFilm *pCur = NULL;
if (NULL == pHead)
{
printf("linklist err\n");
return;
}
//跳过头节点
pCur = pHead->pNext;
//循环打印
while(NULL != pCur)
{
printf("node[%d]: film[%s] score[%d]\n", i, pCur->Name, pCur->Score);
i++;
pCur = pCur->pNext;
}
}
int main(int argc, char *argv[])
{
printf("*******************************************************************************\n");
printf("linklist test\n");
printf("compile date:%s\n",_COMPILE_TIME);
printf("*******************************************************************************\n");
int nIndex;
int i;
int cmd;
char Name[NAMELEN];
int Score;
TFilm *pFilmList = NULL;
//创建头节点
#ifdef METHOD_TWO
pFilmList = CreateFilmList();
#else
CreateFilmList(&pFilmList);
#endif
if (NULL == pFilmList)
{
printf("Creat FilmList Err\n");
}
//手动增加三个节点
for (i = 0; i < FILMMAX; i++)
{
printf("input film name:");
scanf("%s", Name);
printf("input film score:");
scanf("%d", &Score);
AddFilm(pFilmList, Name, Score);
}
while(1)
{
printf("input cmd(1:add 2:insert 3:del 4:change 5:destory 6:show other:quit)\ncmd:");
scanf("%d", &cmd);
switch(cmd)
{
case 1:
printf("input film name:");
scanf("%s", Name);
printf("input film score:");
scanf("%d", &Score);
AddFilm(pFilmList, Name, Score);
break;
case 2:
printf("input film index:");
scanf("%d", &nIndex);
printf("input film name:");
scanf("%s", Name);
printf("input film score:");
scanf("%d", &Score);
InsertFilm(pFilmList, Name, Score, nIndex);
break;
case 3:
printf("input film name:");
scanf("%s", Name);
DelFilm(pFilmList, Name);
break;
case 4:
printf("input film name:");
scanf("%s", Name);
printf("input film score:");
scanf("%d", &Score);
ChangeFilmScore(pFilmList, Name, Score);
break;
case 5:
printf("destory filmlist\n");
DestoryFilmList(pFilmList);
return 0;
break;
case 6:
printf("show filmlist\n");
ShowFilmList(pFilmList);
break;
default:
printf("input err bye\n");
DestoryFilmList(pFilmList);
return 0;
break;
}
sleep(1);
}
return 0;
}