创建一个基于学生信息管理的单链表 (C语言)
答案:1 悬赏:70 手机版
解决时间 2021-11-17 20:01
- 提问者网友:兔牙战士
- 2021-11-17 10:11
创建一个基于学生信息管理的单链表 (C语言)
最佳答案
- 五星知识达人网友:大漠
- 2021-11-17 10:29
#include
#include
#include
#include
struct StuNode
{
char Name[20];
int Age;
StuNode *pNext;
};
bool InputStudentInf(StuNode **pHead)
{
StuNode *pWork = *pHead;
if ( pWork == NULL )
{
*pHead = (StuNode *)malloc(sizeof(StuNode));
memset(*pHead,0x0,sizeof(StuNode));
pWork = *pHead;
}
else
{
while ( pWork->pNext != NULL )
{
pWork = pWork->pNext;
}
pWork->pNext = (StuNode *)malloc(sizeof(StuNode));
memset(pWork->pNext,0x0,sizeof(StuNode));
pWork = pWork->pNext;
}
printf("请输入学生姓名 = ");
fflush(stdin);
scanf("%s",pWork->Name);
printf("请输入学生年龄 = ");
fflush(stdin);
scanf("%d",&(pWork->Age));
printf("继续输入吗?(y继续输入,n结束输入) = ");
char gc = 0;
fflush(stdin);
scanf("%c",&gc);
fflush(stdin);
if ( 'y' == gc )
{
return true;
}
else
{
return false;
}
}
void DestroyStudentInf(StuNode **pHead)
{
StuNode *pWork = NULL;
StuNode *pWorkNext = NULL;
pWork = *pHead;
pWorkNext = pWork->pNext;
while ( NULL != pWork )
{
free(pWork);
pWork = pWorkNext;
if ( NULL == pWork )
{
break;
}
pWorkNext = pWork->pNext;
}
*pHead = NULL;
}
void OutPutStudentInf(StuNode *pHead)
{
StuNode *pWork = pHead;
while ( NULL != pWork )
{
printf("姓名:%s\t年龄:%d\n",pWork->Name,pWork->Age);
pWork = pWork->pNext;
}
}
int FindStudentInf(StuNode *pHead,char *WantName)
{
int pos = 0;
StuNode *pWork = pHead;
while ( NULL != pWork )
{
pos++;
if ( strcmp(WantName,pWork->Name) == 0 )
{
printf("找到姓名为 %s 的学生,年龄为 %d\n",WantName,pWork->Age);
return pos;
}
pWork = pWork->pNext;
}
pos = -1;
printf("无姓名为 %s 的学生\n",WantName);
return pos;
}
void InsertStudentInf(StuNode **pHead)
{
char WantName[20];
memset(WantName,0x0,sizeof(WantName));
printf("在哪个学生信息前插入数据 = ");
scanf("%s",WantName);
int pos = FindStudentInf(*pHead,WantName);
if ( pos < 0 )
{
printf("不存在名字为 %s 的结点\n",WantName);
return;
}
StuNode * pWork = NULL;
StuNode * pSaveNext = NULL;
if ( pos == 1 )
{
pSaveNext = *pHead;
StuNode *pNewNode = (StuNode *)malloc(sizeof(StuNode));
memset(pNewNode,0x0,sizeof(StuNode));
printf("待插入学生的姓名 = ");
scanf("%s",&(pNewNode->Name));
printf("待插入学生的年龄 = ");
scanf("%d",&(pNewNode->Age));
*pHead = pNewNode;
(*pHead)->pNext = pSaveNext;
}
else
{
int thePos = 1;
pWork = *pHead;
pSaveNext = NULL;
while ( thePos < pos - 1 )
{
pWork = pWork->pNext;
thePos++;
}
pSaveNext = pWork->pNext;
StuNode *pNewNode = (StuNode *)malloc(sizeof(StuNode));
memset(pNewNode,0x0,sizeof(StuNode));
printf("待插入学生的姓名 = ");
scanf("%s",&(pNewNode->Name));
printf("待插入学生的年龄 = ");
scanf("%d",&(pNewNode->Age));
pWork->pNext = pNewNode;
pNewNode->pNext = pSaveNext;
}
}
void SwapNode(StuNode *pN1,StuNode *pN2)
{
StuNode tmp;
tmp.Age = pN1->Age;
pN1->Age = pN2->Age;
pN2->Age = tmp.Age;
strcpy(tmp.Name,pN1->Name);
strcpy(pN1->Name,pN2->Name);
strcpy(pN2->Name,tmp.Name);
}
void SourtStudentInf(StuNode **pHead)
{
printf("正在排序...\n");
int length = 0;
StuNode *pWork = *pHead;
while ( NULL != pWork )
{
length++;
pWork = pWork->pNext;
}
pWork = *pHead;
for ( int i = length ; i > 0 ; i-- )
{
for ( int j = 0 ; j < i ; j++ )
{
if ( pWork != NULL && pWork->pNext != NULL )
{
if ( pWork->Age > pWork->pNext->Age )
{
SwapNode(pWork,pWork->pNext);
}
}
pWork = pWork->pNext;
}
pWork = *pHead;
}
}
void main(void)
{
StuNode *pHead = NULL;
while ( InputStudentInf(&pHead) )
{
}
OutPutStudentInf(pHead);
char WantName[20];
memset(WantName,0x0,sizeof(WantName));
printf("输入想查找学生的姓名 = ");
scanf("%s",WantName);
FindStudentInf(pHead,WantName);
InsertStudentInf(&pHead);
OutPutStudentInf(pHead);
SourtStudentInf(&pHead);
OutPutStudentInf(pHead);
DestroyStudentInf(&pHead);
fflush(stdin);
getchar();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯