Data structures 有人能检查一下Insertaden函数发生了什么吗。。它并没有创建主函数中提到的所有块
我在insertatend和InertaFront函数的if-else循环中添加了print语句,这导致每次从main调用这两个函数时都打印printf语句。。。但在显示中,它并不是显示所有值。我想也许在呼叫时节点在某处断开了。。。请参阅代码下面的输出。它只显示三个值,而应该显示我在main()中输入的所有值。 这是我的密码Data structures 有人能检查一下Insertaden函数发生了什么吗。。它并没有创建主函数中提到的所有块,data-structures,linked-list,Data Structures,Linked List,我在insertatend和InertaFront函数的if-else循环中添加了print语句,这导致每次从main调用这两个函数时都打印printf语句。。。但在显示中,它并不是显示所有值。我想也许在呼叫时节点在某处断开了。。。请参阅代码下面的输出。它只显示三个值,而应该显示我在main()中输入的所有值。 这是我的密码 #include <stdio.h> #include <stdlib.h> struct node { int value; s
#include <stdio.h>
#include <stdlib.h>
struct node
{
int value;
struct node* ptr;
};
struct node* insertatend(struct node* h, int value)
{
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->value = value;
newnode->ptr = NULL;
if (h == NULL)
return newnode;
else {
while(h->ptr!=NULL)
h=h->ptr;
h->ptr = newnode;
return h;
}
}
struct node* insertatfront(struct node* h, int value)
{
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
if (h == NULL) {
newnode->value = value;
newnode->ptr = NULL;
return newnode;
}
else
{
newnode->value = value;
newnode->ptr = h;
return newnode;
}
}
void display(struct node* h)
{
while ((h->ptr) != NULL)
{
printf("The value stored in the block is %d\n", h->value);
h = h->ptr;
}
if (h->ptr == NULL)
printf("The value stored in the block is %d\n", h->value);
}
void main()
{
struct node* head;
head = (struct node*)malloc(sizeof(struct node));
head = insertatend(head, 90);
head = insertatend(head, 30);
head = insertatfront(head, 5);
head = insertatend(head, 12);
head = insertatend(head, 1);
head = insertatfront(head, 25);
display(head);
}
/* Output:The value stored in block is 25
* The value stored in block is 5
* The value stored in block is 1
*/
#包括
#包括
结构节点
{
int值;
结构节点*ptr;
};
结构节点*insertaden(结构节点*h,int值)
{
结构节点*newnode;
newnode=(结构节点*)malloc(sizeof(结构节点));
新建节点->值=值;
newnode->ptr=NULL;
if(h==NULL)
返回newnode;
否则{
而(h->ptr!=NULL)
h=h->ptr;
h->ptr=newnode;
返回h;
}
}
结构节点*insertafront(结构节点*h,int值)
{
结构节点*newnode;
newnode=(结构节点*)malloc(sizeof(结构节点));
if(h==NULL){
新建节点->值=值;
newnode->ptr=NULL;
返回newnode;
}
其他的
{
新建节点->值=值;
newnode->ptr=h;
返回newnode;
}
}
无效显示(结构节点*h)
{
而((h->ptr)!=NULL)
{
printf(“存储在块中的值是%d\n”,h->value);
h=h->ptr;
}
如果(h->ptr==NULL)
printf(“存储在块中的值是%d\n”,h->value);
}
void main()
{
结构节点*头部;
head=(结构节点*)malloc(sizeof(结构节点));
头部=插入物(头部,90);
头部=插入物(头部,30);
头部=插入式前部(头部,5);
头=插入器(头,12);
头=插入器(头,1);
头部=插入式前部(头部,25);
显示器(头部);
}
/*输出:存储在块中的值为25
*块中存储的值为5
*块中存储的值为1
*/
您在代码中可能犯的主要错误:
1) 在插入函数中修改头
指针。insertaden
中的此语句:
while(h->ptr!=NULL)
h=h->ptr;
更改列表本身的标题。相反,您应该使用不同的指针来遍历,直到结束。
将插入更改为:
struct node* insertatend(struct node* h, int value)
{
struct node* newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->value = value;
newnode->ptr = NULL;
if (h == NULL){
h=newnode;
}
else {
struct node *temp;
temp=h;
while(temp->ptr!=NULL) temp=temp->ptr;
temp->ptr = newnode;
}
return h;
}
2)
h
为空的情况永远不会发生,因为您已经在main
中对head
进行了malloced。Remove语句head=(结构节点*)malloc(sizeof(结构节点))代码>从main
看起来你的帖子大部分都是代码;请添加更多细节。谢谢!