#include<stdio.h>
#include<stdlib.h>
struct Student
{
char cName[20];
int iNumber;
struct Student* pNext;
};
int iCount;
struct Student* Create()
{
struct Student* pHead=NULL;
struct Student* pEnd,*pNew;
iCount=0;
pEnd=pNew=(struct Student*)malloc(sizeof(struct Student));
printf("首先输入姓名,然后输入学号\n");
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
while(pNew->iNumber!=0)
{
iCount++;
if(iCount==1)
{
pNew->pNext=pHead;
pEnd=pNew;
pHead=pNew;
}
else
{
pNew->pNext=NULL;
pEnd->pNext=pNew;
pEnd=pNew;
}
pNew=(struct Student*)malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
}
free(pNew);
return pHead;
}
void Print(struct Student* pHead)
{
struct Student* pTemp;
int ilndex=1;
printf("----这个链表中有%d个成员:---\n",iCount);
printf("\n");
pTemp=pHead;
while(pTemp!=NULL)
{
printf("成员%d是:\n",ilndex);
printf("姓名:%s",pTemp->cName);
printf("学号:%d\n",pTemp->iNumber);
printf("\n");
pTemp->pNext;
ilndex++;
}
}
struct Student* lnsert(struct Student* pHead)
{
struct Student* pNew;
printf("---首先插入一个成员---\n");
pNew=(struct Student*)malloc(sizeof(struct Student));
scanf("%s",&pNew->cName);
scanf("%d",&pNew->iNumber);
pNew->pNext=pHead;
pHead=pNew;
iCount++;
return pHead;
}
void Delete(struct Student* pHead,int ilndex)
{
int i;
struct Student* pTemp;
struct Student* pPre;
pTemp=pHead;
pPre=pTemp;
printf("----删除第%d个成员----\n",ilndex);
for(i=1;i<ilndex;i++)
{
pPre=pTemp;
pTemp=pTemp->pNext;
}
pPre->pNext=pTemp->pNext;
free(pTemp);
iCount--;
}
int main()
{
struct Student* pHead;
pHead=Create();
pHead=lnsert(pHead);
Delete(pHead,2);
Print(pHead);
return 0;
}
这是运行结果图,我这个运行输出插入以后回车就进入死循环了 一直重复我输入姓名和学号