#include<stdio.h>
#include<stdlib.h>
struct Student
{
char cName[20];
int iNumber;
struct Student* Pnext;
}student;
int iCount;
struct Student* Creat() {
struct Student* pHead = NULL; //initialize pHead
struct Student* pNew;
struct Student* pEnd;//to move pointer
iCount = 0;
pNew = (struct Student*)malloc(sizeof(struct Student));
pEnd = (struct Student*)malloc(sizeof(struct Student));
printf("Please enter Name firstly,then enter number\n");
scanf_s("%s", &pNew->cName);
scanf_s("%d", &pNew->iNumber);
while (pNew->iNumber != 0)
{
iCount++;//global variable
if (iCount == 0) { //ready to create the first node
pNew->Pnext = NULL;
pEnd = pNew;
pHead = pNew;//
}
else
{
pNew->Pnext = NULL;
pEnd->Pnext = pNew; //move the present pointer to next node
pEnd = pNew; //
}
pNew = (struct Student*)malloc(sizeof(struct Student));
scanf_s("%s", &pNew->cName);
scanf_s("%d", &pNew->iNumber);
}
free(pNew);
return pHead; //can find the any node of LinkedList by pHead
}
void Print(struct Student* pHead) {
struct Student* pTemp; //temporary variable
int iIndex = 1; //using index to seek aim node
printf("The list has %d members\n", iCount);
printf("\n");
pTemp = pHead; //you know the mean
while (pTemp != NULL)
{
printf("the No %d is:\n", iIndex);
printf("Name:%s\n", pTemp->cName);
printf("Number:%d\n", pTemp->iNumber);
printf("\n");
iIndex++;
}
}
int main() {
struct Student* pHead;//define Head node
pHead = Creat(); //create node
Print(pHead); //print Linked List
return 0;
}
/*Hi bro, please copy my code and run it in your IDE, it's not have error in visual studio's complie, but when I run and put some data, I saw big bug in cmd window.
I don't know where I'm wrong, I need your help bro _
沒有編譯錯誤,只是運行不能達到程式撰寫的功能,不知道是不是電腦環境問題
*/
uj5u.com熱心網友回復:
問題不少啊問題1,pEnd = (struct Student*)malloc(sizeof(struct Student)); //這里有記憶體泄漏
問題2,
iCount++;//global variable
if (iCount == 0) { //ready to create the first node //這里的if不滿足,因為上面iCount++導致iCount不為0,所以導致pHead沒有被設定
問題3,
while (pNew->iNumber != 0) //如果輸入iNumber為0,這里已經退出回圈,導致以下不會執行
else
{
pNew->Pnext = NULL;
pEnd->Pnext = pNew; //move the present pointer to next node
pEnd = pNew; //
}
所以導致pEnd沒有指向最后一個節點(即pEnd是倒數第二個節點,不是最后一個節點)
問題4,
while (pTemp != NULL)
{
printf("the No %d is:\n", iIndex);
printf("Name:%s\n", pTemp->cName);
printf("Number:%d\n", pTemp->iNumber);
printf("\n");
iIndex++;
pTemp = pTemp->Pnext; //這里漏了移動鏈表指標了,導致while死回圈
}
問題已經指出,LZ自行修改吧
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
struct Student
{
char cName[20];
int iNumber;
struct Student* Pnext;
}student;
int iCount;
struct Student* Creat()
{
struct Student* pHead = NULL; //initialize pHead
struct Student* pNew;
struct Student* pEnd;//to move pointer
iCount = 0;
pNew = (struct Student*)malloc(sizeof(struct Student));
//pEnd = (struct Student*)malloc(sizeof(struct Student));
printf("Please enter Name firstly,then enter number\n");
scanf_s("%s", pNew->cName, sizeof(pNew->cName);
scanf_s("%d", &pNew->iNumber);
while (pNew->iNumber != 0)
{
iCount++;//global variable
//if (iCount == 0) { //ready to create the first node
if (iCount == 1) { //ready to create the first node
//pNew->Pnext = NULL;
pEnd = pNew;
pHead = pNew;//
}
else
{
//pNew->Pnext = NULL;
pEnd->Pnext = pNew; //move the present pointer to next node
pEnd = pNew; //
}
pNew = (struct Student*)malloc(sizeof(struct Student));
scanf_s("%s", pNew->cName, sizeof(pNew->cName);
scanf_s("%d", &pNew->iNumber);
}
if (iCount)
pEnd->Pnext = NULL;
free(pNew);
return pHead; //can find the any node of LinkedList by pHead
}
void Print(struct Student* pHead)
{
struct Student* pTemp; //temporary variable
int iIndex = 1; //using index to seek aim node
printf("The list has %d members\n", iCount);
printf("\n");
pTemp = pHead; //you know the mean
while (pTemp != NULL)
{
printf("the No %d is:\n", iIndex);
printf("Name:%s\n", pTemp->cName);
printf("Number:%d\n", pTemp->iNumber);
printf("\n");
iIndex++;
pTemp = pTemp->Pnext;
}
}
int main()
{
struct Student* pHead;//define Head node
pHead = Creat(); //create node
Print(pHead); //print Linked List
return 0;
}
供參考~
建議樓主對比代碼找出自己的問題~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/15857.html
標籤:C語言
上一篇:求教大神,為什么我用VS2017撰寫C Primer Plus(第六版)程式清單4.1會出現這種問題?
下一篇:提問:如何強制安裝驅動?
