描述:按图实现一个人员管理功能(录入,增加,删除)
思考:是否可以将 动态链表的实现,封装成一套宏接口?(实现后更新)代码实现:
1. 变量声明 和 main函数:
#include#include #include int listCount = 0; typedef struct _Student { char name[20]; int number; struct _Student *pNext; }STUDENT; void Print_Student_Info(STUDENT *pList) { STUDENT *pNewNode = NULL; unsigned int index = 0; if (NULL == pList || NULL == pList->pNext) { printf("n---- student info is not exist ----nn"); return ; } printf("n---- the list has %d members ----nn", listCount); index = 1; pNewNode = pList->pNext; while (NULL != pNewNode) { printf("the NO%02d member is:n", index); printf(" the name is %sn", pNewNode->name); printf(" the number is %dnn", pNewNode->number); index++; pNewNode = pNewNode->pNext; } return ; } int main(int argc, char *argv[]) { STUDENT *pStudentlist = NULL; pStudentlist = Create_Student_Info(); Print_Student_Info(pStudentlist); if (NULL == pStudentlist) { return 0; } Insert_Head_Student_Info(pStudentlist); Print_Student_Info(pStudentlist); Insert_End_Student_Info(pStudentlist); Print_Student_Info(pStudentlist); Delete_Student_Info(pStudentlist); Print_Student_Info(pStudentlist); return 0; }
2. 录入数据功能:
实现:(退出时会创建一个结点,需要free,否则会造成内存泄漏)
STUDENT *Create_Student_Info()
{
STUDENT *pHeadNode = NULL;
STUDENT *pEndNode = NULL;
STUDENT *pNewNode = NULL;
printf("please enter [Name Number]:n");
pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
if (NULL == pNewNode)
{
printf("init head node failed!n");
return NULL;
}
memset(pNewNode, 0, sizeof(STUDENT));
listCount = 0;
pHeadNode = pNewNode;
pEndNode = pNewNode;
do{
pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
if (NULL == pNewNode)
{
printf("create %d-th node failed!n", (listCount+1));
return pHeadNode;
}
scanf("%s", &pNewNode->name[0]);
scanf("%d", &pNewNode->number);
pNewNode->pNext = NULL;
if (0 != pNewNode->number)
{
listCount++;
pEndNode->pNext = pNewNode;
pEndNode = pNewNode;
}
}while (0 != pNewNode->number);
free((void*)pNewNode);
return pHeadNode;
}
3. 插入数据功能
3.1 头插:
实现:
void Insert_Head_Student_Info(STUDENT *pList)
{
STUDENT *pNewNode = NULL;
if (NULL == pList)
{
printf("n---- student is not exist, insert head failed! ----nn");
return ;
}
printf("insert member at head:n");
pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
if (NULL == pNewNode)
{
printf("ncreate insert node at head failed!n");
return ;
}
scanf("%s", &pNewNode->name[0]);
scanf("%d", &pNewNode->number);
pNewNode->pNext = NULL;
listCount++;
pNewNode->pNext = pList->pNext;
pList->pNext = pNewNode;
return ;
}
3.2 尾插:
实现:
void Insert_End_Student_Info(STUDENT *pList)
{
STUDENT *pEndNode = NULL;
STUDENT *pNewNode = NULL;
if (NULL == pList)
{
printf("ninsert member node at end failed!n");
return ;
}
pEndNode = pList;
while(NULL != pEndNode->pNext)
{
pEndNode = pEndNode->pNext;
}
printf("insert member at end:n");
pNewNode = (STUDENT *)malloc(sizeof(STUDENT));
if (NULL == pNewNode)
{
printf("ncreate insert node at end failed!n");
return ;
}
scanf("%s", &pNewNode->name[0]);
scanf("%d", &pNewNode->number);
pNewNode->pNext = NULL;
listCount++;
pEndNode->pNext = pNewNode;
return ;
}
4. 删除数据功能:
实现:
void Delete_Student_Info(STUDENT *pList)
{
STUDENT *pPerNode = NULL;
STUDENT *pCurNode = NULL;
char deleteName[20];
if (NULL == pList ||
NULL == pList->pNext)
{
printf("n---- student info is not exist ----nn");
return ;
}
printf("delete, please input student name:");
scanf("%s", &deleteName[0]);
pPerNode = pList;
pCurNode = pList->pNext;
while (NULL != pCurNode)
{
if (0 == strcmp(pCurNode->name, deleteName))
{
break;
}
pPerNode = pCurNode;
pCurNode = pCurNode->pNext;
}
if (NULL != pCurNode)
{
printf("%s student delete successn", deleteName);
pPerNode->pNext = pCurNode->pNext;
free(pCurNode);
listCount--;
}
else
{
printf("%s student not exist!n", deleteName);
}
return ;
}



