#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: %dn",current->data);
current=current->next;
}
printf("last element is: %dn",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: %pn",trans);
newNode(trans,12);
//value does not change
printf("value of trans after function call: %pn",trans);
addingHundred(trans);
//printing the list
printList(root);
int storage;
//removing last node
storage=removeLastNode(trans);
//returns the last nodes value
printf("value removed: %dn",storage);
printList(root);
freeList(root);
printList(root);
return 0;
}
我对上面编写的代码有几个问题。一般的概念问题是main
我用这个结构做一个struct Node* tran
,我称之为newNode
函数,它接受一个结构Node*
。现在我输入tran
作为论据,我不传递tran
的地址。在这种情况下,函数newNode
不会只创建tran
值的副本,并且在函数调用后撤消函数中的任何操作?
我至少在 print 语句中注意到了这一点,tran
的值在newNode
函数调用后不会改变。我想得到的是我的链表如何扩展并被跟踪?在这种情况下,将 tran
的值作为参数传递是否有效,因为它最初指向根值的堆内存,然后简单地遍历堆中的内存,但实际上并没有更改内存的内容?
如果是这样,那么为了更改列表中节点的值,我必须将&trans
作为参数传递,但是如果我只是遍历列表以在最后添加一个节点,我可以传递tran
作为参数?
我的另一个问题是我不相信我的freeList(struct Node* a)
功能正常工作。就像我释放root
然后打印它时一样,它为我打印一个垃圾值,而不是打印"列表为空"还是打印垃圾导致其访问内存我不拥有?
最后,这里有人批评我的代码是"最终用户应用程序代码"。我仍然是编码的新手,我不确定上面的代码格式是否不正确,或者最终用户应用程序代码意味着什么。如果有人解释如何避免编写"最终用户应用程序代码",我将不胜感激。
你不是按值传递trans
,而是传递一个指向 Node
结构的指针,所以永远不会复制。
你说
我不传递 tran 的地址
这绝对是错误的,你传递的实际上正是这样。
该值不会更改,因为您没有修改指针,在每次调用newNode
后,trans
指针指向的地址与调用前完全相同的地址,因此不应观察到值的任何更改。
当您调用newNode
时,新节点将附加到列表的尾部,因此,如果您实际遍历列表,您将看到所有值,以下代码将打印值
struct Node *node;
for (node = root ; node != NULL ; node = node->next)
printf("value %d @ %pn", 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 listn");
for (node = head ; node != NULL ; node = node->next)
printf("list is: %dn", 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: %dn", storage);
printList(root);
freeList(root);
root = NULL;
printList(root);
return 0;
}
我这样做是为了你的最后一条评论,在你的问题正文中。我不明白,但当然你可以改进你的代码格式。不要害怕使用空格字符,编译器无论如何都会忽略它们(当然,字符串文字中除外)。