请教一个关于链表插入后输出的问题
#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("Please ernter the Name,then Number\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;
}
struct Student *Insert(struct Student *pHead)//链表插入操作//
{
struct Student *pNew;
printf("--Insert member at first--\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 print(struct Student *pHead)//输出链表//
{
struct Student *pTemp;
int iIndex=1;
printf("\n");
printf("--the list has %d member --",iCount);
printf("\n");
pTemp=pHead;
while(pTemp!=NULL)
{
printf("the NO%d member is :\n",iIndex);
printf("the Name is: %s\n",pTemp->cName);
printf("the Menber is: %d\n",pTemp->iNumber);
printf("\n");
pTemp=pTemp->pNext;
iIndex++;
}
}
int main()
{
struct Student *pHead;
pHead=Create();
print(pHead);
pHead=Insert(pHead);
print(pHead);
return 0;
}
请问上面这个Insert哪里出错了呢?
输出的时候插入的第一个的字符可以出现,但数字就只是出现地址
[ 本帖最后由 喜男 于 2011-1-15 11:12 编辑 ]