【发布时间】:2015-06-22 16:51:17
【问题描述】:
您好,我有以下代码来创建链表
#include<stdio.h>
#include<stdlib.h>
struct node{
unsigned int data1;
unsigned int data2;
struct node *ptr;
}obj;
void enterData() // Here the EnterDAta fnnction uses the obj object to enter the data and note that this
{ // obj is used agauin and again in the every node of the list to enter the data
printf("\n Enter the data1 ");
scanf("%u",&obj.data1);
printf("\n Enter the data2 ");
scanf("%u",&obj.data2);
}
void append(struct node **start) // This is used to append the dara un the list or also used to add the first element in the list
{
enterData();
struct node *next_node=*start;
if(next_node==NULL)
{
printf("\nAdding first element in the list ......\n");
next_node=malloc(sizeof(struct node));
printf("\n The memory location of next_node is %p",&next_node);
if(next_node==NULL)
{
printf("\n Out of Memory");
}
else{
next_node->data1=obj.data1;
printf("\n The memory location of next_node->data1 is %p",&next_node->data1);
next_node->data2=obj.data2;
printf("\n The memory location of next_node->data2 is %p",&next_node->data2);
next_node->ptr=NULL;
*start=next_node; //This line of code here is modifying the header pointer see the magic of the pointer :)
}
printf("\n The first element added successfully");
}
else
{
printf("\n Appending the data ......\n");
struct node *temp=next_node;
next_node=malloc(sizeof(struct node));
if(next_node==NULL)
printf("\n Out of Memory");
else
{
next_node->data1=obj.data1;
next_node->data2=obj.data2;
next_node->ptr=NULL;
while(temp->ptr!=NULL)
temp=temp->ptr;
}
temp->ptr=next_node;
temp=NULL;
printf("\n Data appended Successfully!!! ");
}
next_node=NULL;
}
int main()
{
struct node *head=NULL;
append(&head);
return 0;
}
在上面的代码中,如果我得到next_node as 1000 的内存地址,那么我将得到next_node->data1 is 1000 的内存地址和next_node->data2 is 1004 的内存地址
但是如果在上面的附加函数中只是像这样调整代码中的一些变化
void append(struct node **start) // This is used to append the dara un the list or also used to add the first element in the list
{
enterData();
struct node *next_node=*start;
if(next_node==NULL)
{
printf("\nAdding first element in the list ......\n");
next_node=malloc(sizeof(struct node));
if(next_node==NULL)
{
printf("\n Out of Memory");
}
else{
next_node->data2=obj.data2;
printf("\n The memory address of next_node->data2 is %p ",&next_node->data2);
next_node->data1=obj.data1;
printf("\n The memory address of next_node->data1 is %p ",&next_node->data1);
next_node->ptr=NULL;
*start=next_node; //This line of code here is modifying the header pointer see the magic of the pointer :)
}
printf("\n The first element added successfully");
}
else
{
printf("\n Appending the data ......\n");
struct node *temp=next_node;
next_node=malloc(sizeof(struct node));
printf("\n The memory address of next_node is %p ",&next_node);
if(next_node==NULL)
printf("\n Out of Memory");
else
{
next_node->data1=obj.data1;
next_node->data2=obj.data2;
next_node->ptr=NULL;
while(temp->ptr!=NULL)
temp=temp->ptr;
}
temp->ptr=next_node;
temp=NULL;
printf("\n Data appended Successfully!!! ");
}
现在如果next_node is 2000 的地址那么我得到next_node->data1 as 2004 和data2 is 2008 但shouldn't it be the other way as we are first storing the data2 in the memory location using the next_node pointer ? 的内存地址
【问题讨论】:
-
我怀疑它会有所不同。在代码块 2 中,您正在打印 data2 的地址,然后是 data1。同样在代码块 2 中,如果 next_node 为空块,您不会打印 next_node 的地址。你能运行程序并在两种情况下显示程序输出的确切结果吗?
-
在使用 printf() 函数时,(通常)最好以 '\n' 结束格式字符串,因为这会导致输出刷新到标准输出。当前格式字符串中的前导 '\n' 会导致显示先前的 printf(),而不是当前的 printf()。
-
函数:append()有这一行:'printf("\n第一个元素添加成功");'但是,如果 malloc 失败,那么这个 printf() 仍然会被执行,但是节点没有被添加
-
函数:append() 在第一个节点之后添加时,如果malloc失败,则代码继续,就像malloc成功一样。
-
我认为你应该退后一步,考虑一下cmets和答案,而不是试图反驳它们。
标签: c pointers data-structures malloc