|
|
5鱼币
编译是能过,也能输入可就是最后有个错误提示- #include<stdio.h>
- #include<stdlib.h>
- #include<malloc.h>
- struct Student
- {
- char cName[20];
- int iNumber;
- struct Student *pNext;//指向下一个结点的指针
- };
- int iCount; //定义全局变量
- struct Student *Create()
- {
- struct Student *pHead = NULL;//初始化链表头指针为空
- struct Student *pEnd;
- struct Student *pNew;
-
- iCount=0; //初始化链表长度
- pEnd = pNew = (struct Student *)malloc(sizeof(struct Student));
- printf("Please first enter 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;
- }
- void Print(struct Student *pHead)
- {
- struct Student *pTemp; //循环所用到的临时指针
- int ilndex=1;
- printf("---the list has %s members:--\n",iCount);
- printf("\n");
- pTemp=pHead; //使指针得到首结点的地址
- while(pTemp != NULL)
- {
- printf("the NO %d menber id:\n",ilndex);
- printf("the name is:%d", pTemp->cName);
- printf("the number is :%d\n", pTemp->iNumber);
- printf("\n");
- //移动临时指针到下一个结点
- pTemp=pTemp->pNext; //
- ilndex++;//进行自加运算
- }
- }
- 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 Delete(struct Student* pHead,int ilndex)
- {
- int i; //控制循环变量
- struct Student *pTemp;//表示临时指针
- struct Student *pPre; //表示要删除节点前的指针
-
- pTemp=pHead; //得到头结点
-
- pPre=pTemp;
-
- printf("---deleteNO %d member---\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=Insert(pHead); //插入节点
- Delete(pHead,2); //删除第 2 个节点的操作
-
- Print(pHead); //输出链表
- return 0;
- }
复制代码
这是结果
|
最佳答案
查看完整内容
第59行格式符%s应为%d
第68行格式符%d应为%s
其中,第59行是关键错误,会引起内存报错。
---------------
自己检查的时候先把各个子函数单独放在主函数里,可找出错误函数,再检查该函数代码。
|