#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
我調用newNode
函數,它接受一個struct Node*
。現在我把tran
作爲參數我不通過tran
的地址。在這種情況下,函數newNode
只會創建一個tran
的值的副本,並且在函數調用之後,函數中的任何操作都將被撤消?通過鏈接列表的函數指針
我注意到這與一個打印語句至少tran
的值在newNode
函數調用後沒有改變。我想知道的是,我的鏈接列表如何擴展,並且正在跟蹤?在這種情況下,傳遞tran
的值是否會起作用,因爲它最初指向根值的堆內存,然後簡單地遍歷堆中的內存,但實際上並未改變內存的內容?
如果是的話,以更改列表中的一個節點的值,我將不得不通過&trans
作爲參數,但如果我只是遍歷列表添加在最後一個節點,我可以通過tran
作爲論據?
我的另一個問題是我不相信我的freeList(struct Node* a)
功能工作正常。因爲當我釋放root
然後打印它時,它會爲我打印一個垃圾值,而應該打印「列表爲空」或打印垃圾是否導致其訪問內存我不擁有?
最後,這裏有人批評我的代碼是「最終用戶應用程序代碼」。我對編碼還不熟悉,而且我不確定上面的代碼是否格式不正確,或者說是否是最終用戶應用程序代碼。如果有人解釋如何避免編寫「最終用戶應用程序代碼」,我將非常感激。
這是一個很大的問題。 – gsamaras 2015-01-09 23:24:12
我不確定如何在沒有解釋的情況下清楚地問清楚。 – Nightlife 2015-01-09 23:26:15
代碼應該總是檢查malloc(和系列)返回的值以確保操作成功 – user3629249 2015-01-09 23:55:41