Привет У меня есть ниже код, чтобы создать связанный списокMalloc макет памяти
#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 кода вы печатаете адрес данных2, за которым следует data1.Also в блоке 2 кода вы не печатаете адрес next_node в том случае, если next_node имеет нулевой блок. можете ли вы запустить программу и показать точный результат в виде вывода по программе в двух случаях? – Pradheep
при использовании функции printf(), как правило, лучше всего закончить строку формата с помощью «\ n», поскольку это приведет к потере потока вывода на стандартный вывод. Ведущее «\ n» в строках текущего формата вызывает отображение предыдущей функции printf(), а не текущую printf(). – user3629249
Функция: append() имеет следующую строку: 'printf ("\ n Первый элемент добавлен успешно"); Однако, если malloc терпит неудачу, то этот файл printf() все еще выполняется, но узел не был добавлен – user3629249