为什么书上输入后输出的和我不一样呢,麻烦谁能帮忙也运行下这段代码,看下是不是我哪里错了?
为什么书上输入后输出的和我不一样呢,麻烦谁能帮忙也运行下这段代码,看下是不是我哪里错了?但我是书上抄下来的,真心不知道哪里错了,麻烦高手指点下,谢谢!!截图如下:
程序代码:
/* Program 11.7 Sorting integers using a binary tree */ #include <stdio.h> #include <stdlib.h> #include <ctype.h> /* Function prototypes */ struct Node *createnode(long value); struct Node *addnode(long value, struct Node *pNode); void listnodes(struct Node *pNode); void freenodes(struct Node *pNode); /* Defines a node in a binary tree sotring integers */ struct Node { long item; int count; struct Node *pLeft; struct Node *pRight; }; /* Function main - execution starts here */ int main(void) { long newvalue = 0; struct Node *pRoot = NULL; char answer = 'n'; do { printf("Enter the node value: "); scanf(" %ld", &newvalue); if(pRoot == NULL) pRoot = createnode(newvalue); else addnode(newvalue, pRoot); printf("\nDo you want to enter another (y or n)? "); scanf(" %c", &answer); }while(tolower(answer) == 'y'); printf("The values in ascending sequence are: "); listnodes(pRoot); freenodes(pRoot); return 0; } struct Node *createnode(long value) { struct Node *pNode = (struct Node*)malloc(sizeof(struct Node)); pNode -> item = value; pNode -> count = 1; pNode -> pLeft = pNode -> pRight = NULL; return pNode; }; /* Add a new node to the tree */ struct Node *addnode(long value, struct Node *pNode) { if(pNode == NULL) return createnode(value); if(value == pNode -> item) { /* Value equals current node */ ++pNode -> count; return pNode; } if(value < pNode -> item) { if(pNode -> pLeft = NULL) { pNode -> pLeft = createnode(value); return pNode -> pLeft; } else return addnode(value, pNode -> pLeft); } else { if(pNode -> pRight == NULL) { pNode -> pRight = createnode(value); return pNode -> pRight; } else return addnode(value, pNode -> pRight); } }; /* List the node values in ascending sequence */ void listnodes(struct Node *pNode) { if(pNode -> pLeft != NULL) listnodes(pNode -> pLeft); for(int i = 0; i < pNode -> count ; i++) printf("\n%10ld", pNode -> item); if(pNode -> pRight != NULL) listnodes(pNode -> pRight); } /* Release memory allocated to nodes */ void freenodes(struct Node *pNode) { if(pNode == NULL) return; if(pNode -> pLeft != NULL) freenodes(pNode -> pLeft); if(pNode -> pRight != NULL) freenodes(pNode -> pRight); free(pNode); }
为什么书上输入后输出的和我不一样呢,麻烦谁能帮忙也运行下这段代码,看下是不是我哪里错了?
但我是书上抄下来的,真心不知道哪里错了,麻烦高手指点下,谢谢!!截图如下:[local]1[/local][local]2[/local]