C BST,继任者,代码问题
我理解BST中继任者的一般概念。我的代码仍然存在一些问题,我不明白问题出在哪里 编译器运行程序,并在几秒钟后启动和结束。我相信这是一种“分段错误”类型的错误(我使用的是Windows和DevC++)C BST,继任者,代码问题,c,binary-search-tree,C,Binary Search Tree,我理解BST中继任者的一般概念。我的代码仍然存在一些问题,我不明白问题出在哪里 编译器运行程序,并在几秒钟后启动和结束。我相信这是一种“分段错误”类型的错误(我使用的是Windows和DevC++) #包括 #包括 结构节点{ int数据; 结构节点*下一步; }; 结构节点*头部; 结构节点*GetNewNode(int x){ 结构节点*新节点=(结构节点*)malloc(sizeof(结构节点)); newNode->data=x; newNode->next=NULL; 返回newNod
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
结构节点*头部;
结构节点*GetNewNode(int x){
结构节点*新节点=(结构节点*)malloc(sizeof(结构节点));
newNode->data=x;
newNode->next=NULL;
返回newNode;
}
void insertatead(int x){
结构节点*newNode=GetNewNode(x);
if(head==NULL){
头=新节点;
返回;}
新建节点->下一步=头部;
头=新节点;
}
作废打印(){
结构节点*温度=头部;
printf(“转发:”);
while(temp!=NULL){
printf(“%d”,临时->数据);
温度=温度->下一步;
}
printf(“\n”);
}
void ReversePrint(结构节点*p){
if(p==NULL){
返回;
}
反向打印(p->next);
printf(“%d”,p->data);
}
内部主(空){
head=NULL;
insertatead(2);Print();ReversePrint(2);
insertatead(4);Print();ReversePrint(4);
插入日期(6);打印();反向打印(6);
}
您的代码有几个错误需要说明
insertatead()
函数中重新设定head
。您应该保留对列表尾部的引用,并在每次调用insertatead()
时更新下一个节点ReversePrint()
传递一个整数,该整数需要一个struct节点
指针,这将导致分段错误FreeNodes()
函数,它应该很容易实现#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
struct Node* tail;
struct Node* GetNewNode(int x) {
struct Node* newNode = malloc(sizeof(struct Node));
if (newNode == NULL)
return NULL;
newNode->data = x;
newNode->next = NULL;
return newNode;
}
void InsertAtHead(int x) {
struct Node* newNode = GetNewNode(x);
if (head == NULL) {
head = newNode;
tail = head;
return;
}
if (tail != NULL)
tail->next = newNode;
tail = newNode;
}
void Print(){
struct Node* current = head;
printf("forward:\n");
while (current != NULL) {
printf("\t%d\n", current->data);
current = current->next;
}
printf("\n");
}
void ReversePrint(struct Node *node) {
if (node == NULL) {
return;
}
ReversePrint(node->next);
printf("%d\n", node->data);
}
int main(void) {
InsertAtHead(2);
InsertAtHead(4);
InsertAtHead(6);
Print();
ReversePrint(head);
return 0;
}
#包括
#包括
结构节点{
int数据;
结构节点*下一步;
};
结构节点*头部;
结构节点*尾部;
结构节点*GetNewNode(int x){
结构节点*newNode=malloc(sizeof(结构节点));
if(newNode==NULL)
返回NULL;
newNode->data=x;
newNode->next=NULL;
返回newNode;
}
void insertatead(int x){
结构节点*newNode=GetNewNode(x);
if(head==NULL){
头=新节点;
尾=头;
返回;
}
if(tail!=NULL)
tail->next=newNode;
tail=newNode;
}
作废打印(){
结构节点*当前=头部;
printf(“转发:\n”);
while(当前!=NULL){
printf(“\t%d\n”,当前->数据);
当前=当前->下一步;
}
printf(“\n”);
}
void ReversePrint(结构节点*节点){
if(node==NULL){
返回;
}
反向打印(节点->下一步);
printf(“%d\n”,节点->数据);
}
内部主(空){
插入图(2);
插入图(4);
插图(6);
打印();
反向打印(头);
返回0;
}
您不需要显式地将全局变量初始化为
0
或NULL
这是什么ReversePrint(2)
?如果有错误消息,请发布准确的错误消息。您“相信”的内容可能不准确。请逐步完成调试程序。为什么您要为头
使用全局变量?问题是编译器没有错误。它运行代码,然后Windows才写入程序停止工作。这就是我所得到的一切。@ninigi如果这个答案对你有用,你可以接受。
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
struct Node* tail;
struct Node* GetNewNode(int x) {
struct Node* newNode = malloc(sizeof(struct Node));
if (newNode == NULL)
return NULL;
newNode->data = x;
newNode->next = NULL;
return newNode;
}
void InsertAtHead(int x) {
struct Node* newNode = GetNewNode(x);
if (head == NULL) {
head = newNode;
tail = head;
return;
}
if (tail != NULL)
tail->next = newNode;
tail = newNode;
}
void Print(){
struct Node* current = head;
printf("forward:\n");
while (current != NULL) {
printf("\t%d\n", current->data);
current = current->next;
}
printf("\n");
}
void ReversePrint(struct Node *node) {
if (node == NULL) {
return;
}
ReversePrint(node->next);
printf("%d\n", node->data);
}
int main(void) {
InsertAtHead(2);
InsertAtHead(4);
InsertAtHead(6);
Print();
ReversePrint(head);
return 0;
}