发布网友 发布时间:2022-05-21 01:56
共1个回答
热心网友 时间:2023-10-11 08:24
#include <stdio.h>
#include <malloc.h>
typedef struct student
{
char name[20];
long num;
char sex;
struct student *pNext;
}Stu, *pStu;
void creatInfo(pStu *stu);
int deletInfo(pStu stu, long numTemp);
void printInfo(pStu stu);
int main(void)
{
long numTemp = 0;
pStu myStu = NULL;
creatInfo(&myStu);
scanf("%ld", &numTemp);
if(1 == deletInfo(myStu, numTemp))
printInfo(myStu);
return 0;
}
void creatInfo(pStu *stu)
{
int n = 1;
pStu pNew = NULL, pTail = NULL;
*stu = (pStu)malloc(sizeof(Stu));
if(*stu == NULL)
return ;
(*stu)->pNext = NULL;
pTail = *stu;
while(n <= 10)
{
pNew = (pStu)malloc(sizeof(Stu));
if(NULL == pNew)
return ;
pNew->pNext = NULL;
scanf("%s", pNew->name);
if('#' == pNew->name[0])
{
free(pNew);
pNew = NULL;
return ;
}
scanf("%ld %c", &pNew->num, &pNew->sex);
pTail->pNext = pNew;
pTail = pNew;
++n;
}
}
int deletInfo(pStu stu, long numTemp)
{
pStu pTail = NULL, pHead = NULL;
pHead = stu;
pTail = stu->pNext;
while(pTail)
{
if(pTail->num == numTemp)
{
pHead->pNext = pTail->pNext;
free(pTail);
pTail = NULL;
return 1;
}
pTail = pTail->pNext;
pHead = pHead->pNext;
}
printf("链表中唔该学生!\n");
return 0;
}
void printInfo(pStu stu)
{
while(stu = stu->pNext)
printf("%s, %d, %c\n", stu->name, stu->num, stu->sex);
}