在链接列表的函数中传递指针
Passing Pointers in Functions for Linked List
#include <stdio.h>
#include <stdlib.h>
//why does this work with pointers thought they made a copy?
//am i freeing memory correctly and well?
//Something wrong with freeing
struct Node{
struct Node* next;
int data;
};
void newNode(struct Node* trans, int val)
{
if(trans!=NULL)
{
while(trans->next!=NULL)
{
trans=trans->next;
}
//next is null create heap memory
trans->next=malloc(sizeof(struct Node));
//checking to see if memory is created
if(trans->next==NULL)
{
printf("This has failed");
}
//put in data
trans->next->data=val;
//next is null
trans->next->next=NULL;
}
}
void printList(struct Node* head)
{
if(head!=NULL)
{
struct Node* current;
current=head;
while(current->next!=NULL)
{
//print that current nodes data
printf("list is: %d\n",current->data);
current=current->next;
}
printf("last element is: %d\n",current->data);
}
else
{
printf("list is empty!");
}
}
int removeLastNode(struct Node* trans)
{
//return -1 if its a empty list
int val=-1;
if(trans!=NULL)
{
/*have to access trans->next->next cause you are freeing trans->next->next and getting its val
then you want to set tran->next to NULL!
*/
while(trans->next->next!=NULL)
{
trans=trans->next;
}
//at end of the list?
val=trans->next->data;
//free the heap
free(trans->next);
//next points to null
trans->next=NULL;
}
return val;
}
//LOOK AT ME!
void freeList(struct Node* root)
{
struct Node* temp;
struct Node* current;
current=root;
while(current->next!=NULL)
{
temp=current;
//going to the next one
current=current->next;
//freeing previous
free(temp);
}
//Am I really freeing the last one?
free(current);
root->next=NULL;
root=NULL;
}
void addingHundred(struct Node* trans)
{
int i;
for(i=0;i<100;i++)
{
newNode(trans,i);
}
}
int main()
{
struct Node* root;
//create heap mem for root
root=malloc(sizeof(struct Node));
root->next=NULL;
root->data=10;
//traversal pointer
struct Node* trans;
//setting to point to root
trans=root;
//adding a new node..
newNode(trans,8);
printf("value of trans after function call: %p\n",trans);
newNode(trans,12);
//value does not change
printf("value of trans after function call: %p\n",trans);
addingHundred(trans);
//printing the list
printList(root);
int storage;
//removing last node
storage=removeLastNode(trans);
//returns the last nodes value
printf("value removed: %d\n",storage);
printList(root);
freeList(root);
printList(root);
return 0;
}
我对上面写的代码有几个问题。 main
中的一般概念性问题我用这个结构创建了一个 struct Node* tran
我调用了接受结构 Node*
的 newNode
函数。现在我输入 tran
作为参数,我不传递 tran
的地址。在这种情况下,函数 newNode
不会只是创建 tran
的值的副本,并且函数中的任何操作都将在函数调用后撤消吗?
我用打印语句注意到这一点,至少 tran
的值在 newNode
函数调用后没有改变。我想要了解的是我的链表是如何扩展和被跟踪的?在这种情况下,将 tran
的值作为参数传递是否有效,因为它最初指向根值的堆内存,然后简单地遍历堆中的内存,但实际上并没有改变内存的内容?
如果是这样,那么为了更改列表中节点的值,我必须将 &trans
作为参数传递,但是如果我只是遍历列表以在末尾添加一个节点,我可以将 tran
作为参数传递吗?
我的另一个问题是我不相信我的 freeList(struct Node* a)
函数工作正常。当我释放 root
然后打印它时,它会为我打印一个垃圾值,而它应该打印 "list is empty" 或者它打印垃圾是因为它正在访问我不拥有的内存?
最后,这里有人批评我的代码 "end user application code." 我对编码还是个新手,我不确定上面的代码是否格式不正确或者最终用户应用程序代码意味着什么。如果有人解释我如何避免写 "end user application code."
,我将不胜感激
您没有按值传递 trans
,您传递的是指向 Node
的结构的指针,因此永远不会创建副本。
你说
I do not pass the address of tran
那是绝对错误的,你传递的实际上就是这样。
值不会改变,因为您没有修改指针,在每次调用 newNode
之后,trans
指针指向与调用前相同的地址,所以应该观察不到值的变化。
当您调用 newNode
时,一个新节点将附加到列表的尾部,因此如果您实际遍历列表,您会看到所有值,以下代码将打印值
struct Node *node;
for (node = root ; node != NULL ; node = node->next)
printf("value %d @ %p\n", node->val, node);
你应该看到每个节点的地址,它的值
free
并不意味着 0
内存,而是你 free
它,对操作系统来说,这就像你放弃了该地址的内存所有权,如果有义务 0
内存,因此会对性能造成巨大影响。
如果你想制作列表,empty
根据你的代码,你应该在调用 freeList()
之后执行此操作
root = NULL;
我修复了你的代码
备注:你的freeList
函数在末尾有一个free(current)
,它双重释放了最后一个节点,所以我删除了它,也修复了一些样式东西,并使 printList()
函数更具可读性
#include <stdio.h>
#include <stdlib.h>
//why does this work with pointers thought they made a copy?
//am i freeing memory correctly and well?
//Something wrong with freeing
struct Node{
struct Node* next;
int data;
};
void newNode(struct Node* trans, int val)
{
if (trans != NULL)
{
while (trans->next != NULL)
trans = trans->next;
/* next is null create heap memory */
trans->next=malloc(sizeof(struct Node));
/* checking to see if memory is created */
if(trans->next == NULL)
printf("This has failed");
/* put in data */
trans->next->data = val;
/* next is null */
trans->next->next = NULL;
}
}
void printList(struct Node* head)
{
struct Node *node;
if (head == NULL)
printf("empty list\n");
for (node = head ; node != NULL ; node = node->next)
printf("list is: %d\n", node->data);
}
int removeLastNode(struct Node* trans)
{
int val = -1;
/* return -1 if its a empty list */
struct Node *node;
struct Node *last;
if (trans == NULL)
return -1;
/*
* have to access trans->next->next cause you are freeing trans->next->next and getting its val
* then you want to set tran->next to NULL!
*/
node = trans;
last = node->next;
while (last->next != NULL)
{
node = node->next;
last = node->next;
}
trans = node;
node = node->next;
/* at end of the list? */
val = node->data;
/* free the heap */
free(node);
/* next points to null */
trans->next = NULL;
return val;
}
//LOOK AT ME!
void freeList(struct Node* root)
{
struct Node* temp;
struct Node* current;
current = root;
while (current != NULL)
{
temp=current;
/* going to the next one */
current=current->next;
/* freeing previous */
free(temp);
}
}
void addingHundred(struct Node* trans)
{
int i;
for (i=0 ; i < 100 ; i++)
newNode(trans, i);
}
int main()
{
struct Node* root;
int storage;
//create heap mem for root
root = malloc(sizeof(struct Node));
root->next=NULL;
root->data=10;
//adding a new node..
newNode(root, 8);
newNode(root, 12);
addingHundred(root);
//printing the list
printList(root);
//removing last node
storage = removeLastNode(root);
//returns the last nodes value
printf("value removed: %d\n", storage);
printList(root);
freeList(root);
root = NULL;
printList(root);
return 0;
}
我在你的问题正文中为你的最后一条评论做了这个。我不明白,但你当然可以改进你的代码格式。不要害怕使用白色 space 字符,编译器无论如何都会忽略它们(当然除了在字符串文字中)。
#include <stdio.h>
#include <stdlib.h>
//why does this work with pointers thought they made a copy?
//am i freeing memory correctly and well?
//Something wrong with freeing
struct Node{
struct Node* next;
int data;
};
void newNode(struct Node* trans, int val)
{
if(trans!=NULL)
{
while(trans->next!=NULL)
{
trans=trans->next;
}
//next is null create heap memory
trans->next=malloc(sizeof(struct Node));
//checking to see if memory is created
if(trans->next==NULL)
{
printf("This has failed");
}
//put in data
trans->next->data=val;
//next is null
trans->next->next=NULL;
}
}
void printList(struct Node* head)
{
if(head!=NULL)
{
struct Node* current;
current=head;
while(current->next!=NULL)
{
//print that current nodes data
printf("list is: %d\n",current->data);
current=current->next;
}
printf("last element is: %d\n",current->data);
}
else
{
printf("list is empty!");
}
}
int removeLastNode(struct Node* trans)
{
//return -1 if its a empty list
int val=-1;
if(trans!=NULL)
{
/*have to access trans->next->next cause you are freeing trans->next->next and getting its val
then you want to set tran->next to NULL!
*/
while(trans->next->next!=NULL)
{
trans=trans->next;
}
//at end of the list?
val=trans->next->data;
//free the heap
free(trans->next);
//next points to null
trans->next=NULL;
}
return val;
}
//LOOK AT ME!
void freeList(struct Node* root)
{
struct Node* temp;
struct Node* current;
current=root;
while(current->next!=NULL)
{
temp=current;
//going to the next one
current=current->next;
//freeing previous
free(temp);
}
//Am I really freeing the last one?
free(current);
root->next=NULL;
root=NULL;
}
void addingHundred(struct Node* trans)
{
int i;
for(i=0;i<100;i++)
{
newNode(trans,i);
}
}
int main()
{
struct Node* root;
//create heap mem for root
root=malloc(sizeof(struct Node));
root->next=NULL;
root->data=10;
//traversal pointer
struct Node* trans;
//setting to point to root
trans=root;
//adding a new node..
newNode(trans,8);
printf("value of trans after function call: %p\n",trans);
newNode(trans,12);
//value does not change
printf("value of trans after function call: %p\n",trans);
addingHundred(trans);
//printing the list
printList(root);
int storage;
//removing last node
storage=removeLastNode(trans);
//returns the last nodes value
printf("value removed: %d\n",storage);
printList(root);
freeList(root);
printList(root);
return 0;
}
我对上面写的代码有几个问题。 main
中的一般概念性问题我用这个结构创建了一个 struct Node* tran
我调用了接受结构 Node*
的 newNode
函数。现在我输入 tran
作为参数,我不传递 tran
的地址。在这种情况下,函数 newNode
不会只是创建 tran
的值的副本,并且函数中的任何操作都将在函数调用后撤消吗?
我用打印语句注意到这一点,至少 tran
的值在 newNode
函数调用后没有改变。我想要了解的是我的链表是如何扩展和被跟踪的?在这种情况下,将 tran
的值作为参数传递是否有效,因为它最初指向根值的堆内存,然后简单地遍历堆中的内存,但实际上并没有改变内存的内容?
如果是这样,那么为了更改列表中节点的值,我必须将 &trans
作为参数传递,但是如果我只是遍历列表以在末尾添加一个节点,我可以将 tran
作为参数传递吗?
我的另一个问题是我不相信我的 freeList(struct Node* a)
函数工作正常。当我释放 root
然后打印它时,它会为我打印一个垃圾值,而它应该打印 "list is empty" 或者它打印垃圾是因为它正在访问我不拥有的内存?
最后,这里有人批评我的代码 "end user application code." 我对编码还是个新手,我不确定上面的代码是否格式不正确或者最终用户应用程序代码意味着什么。如果有人解释我如何避免写 "end user application code."
,我将不胜感激您没有按值传递 trans
,您传递的是指向 Node
的结构的指针,因此永远不会创建副本。
你说
I do not pass the address of tran
那是绝对错误的,你传递的实际上就是这样。
值不会改变,因为您没有修改指针,在每次调用 newNode
之后,trans
指针指向与调用前相同的地址,所以应该观察不到值的变化。
当您调用 newNode
时,一个新节点将附加到列表的尾部,因此如果您实际遍历列表,您会看到所有值,以下代码将打印值
struct Node *node;
for (node = root ; node != NULL ; node = node->next)
printf("value %d @ %p\n", node->val, node);
你应该看到每个节点的地址,它的值
free
并不意味着 0
内存,而是你 free
它,对操作系统来说,这就像你放弃了该地址的内存所有权,如果有义务 0
内存,因此会对性能造成巨大影响。
如果你想制作列表,empty
根据你的代码,你应该在调用 freeList()
root = NULL;
我修复了你的代码
备注:你的freeList
函数在末尾有一个free(current)
,它双重释放了最后一个节点,所以我删除了它,也修复了一些样式东西,并使 printList()
函数更具可读性
#include <stdio.h>
#include <stdlib.h>
//why does this work with pointers thought they made a copy?
//am i freeing memory correctly and well?
//Something wrong with freeing
struct Node{
struct Node* next;
int data;
};
void newNode(struct Node* trans, int val)
{
if (trans != NULL)
{
while (trans->next != NULL)
trans = trans->next;
/* next is null create heap memory */
trans->next=malloc(sizeof(struct Node));
/* checking to see if memory is created */
if(trans->next == NULL)
printf("This has failed");
/* put in data */
trans->next->data = val;
/* next is null */
trans->next->next = NULL;
}
}
void printList(struct Node* head)
{
struct Node *node;
if (head == NULL)
printf("empty list\n");
for (node = head ; node != NULL ; node = node->next)
printf("list is: %d\n", node->data);
}
int removeLastNode(struct Node* trans)
{
int val = -1;
/* return -1 if its a empty list */
struct Node *node;
struct Node *last;
if (trans == NULL)
return -1;
/*
* have to access trans->next->next cause you are freeing trans->next->next and getting its val
* then you want to set tran->next to NULL!
*/
node = trans;
last = node->next;
while (last->next != NULL)
{
node = node->next;
last = node->next;
}
trans = node;
node = node->next;
/* at end of the list? */
val = node->data;
/* free the heap */
free(node);
/* next points to null */
trans->next = NULL;
return val;
}
//LOOK AT ME!
void freeList(struct Node* root)
{
struct Node* temp;
struct Node* current;
current = root;
while (current != NULL)
{
temp=current;
/* going to the next one */
current=current->next;
/* freeing previous */
free(temp);
}
}
void addingHundred(struct Node* trans)
{
int i;
for (i=0 ; i < 100 ; i++)
newNode(trans, i);
}
int main()
{
struct Node* root;
int storage;
//create heap mem for root
root = malloc(sizeof(struct Node));
root->next=NULL;
root->data=10;
//adding a new node..
newNode(root, 8);
newNode(root, 12);
addingHundred(root);
//printing the list
printList(root);
//removing last node
storage = removeLastNode(root);
//returns the last nodes value
printf("value removed: %d\n", storage);
printList(root);
freeList(root);
root = NULL;
printList(root);
return 0;
}
我在你的问题正文中为你的最后一条评论做了这个。我不明白,但你当然可以改进你的代码格式。不要害怕使用白色 space 字符,编译器无论如何都会忽略它们(当然除了在字符串文字中)。