| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 426 人关注过本帖
标题:请教一个关于链表插入后输出的问题
只看楼主 加入收藏
喜男
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-1-15
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:2 
请教一个关于链表插入后输出的问题
#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 编辑 ]
搜索更多相关主题的帖子: 结构体 
2011-01-15 10:59
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:19 
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;
}

   唯实惟新 至诚致志
2011-01-15 13:02
FMaster
Rank: 2
等 级:论坛游民
帖 子:63
专家分:62
注 册:2010-9-1
收藏
得分:1 
恩,楼上说的在理,pNew->cName本身就是一个地址了,不用再加地址符“&”了。。。。
2011-01-15 15:46
快速回复:请教一个关于链表插入后输出的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015576 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved