#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
에있는 일반적인 개념적 질문은 구조체 Node*
을 취하는 newNode
함수를 호출하여이 구조체와 함께 struct Node* tran
을 만듭니다. 이제 tran
의 주소를 전달하지 않은 인수로 tran
을 넣었습니다. 어떤 경우에는 함수 newNode
그냥 tran
의 복사본을 만들 수 없으며 함수 호출 후에 함수의 모든 조작을 취소 할 수 있습니까?링크 된 목록의 함수에서 포인터 전달하기
나는 적어도 tran
의 값이 newNode
함수 호출 후에 변경되지 않는다는 것을 print 문에 알았습니다. 내가 얻으려고하는 것은 연결된 목록이 어떻게 확장되고 추적 되는가입니다. 이 경우에 tran
의 값을 인수로 전달하는 것이 루트 값의 힙 메모리를 처음 가리키고 단순히 힙의 메모리를 통과하지만 실제로 메모리 내용을 변경하지 않기 때문에 작동합니까?
그렇다면 목록의 노드 값을 변경하려면 &trans
을 인수로 전달해야합니다. 그러나 노드를 추가하기 위해 목록을 탐색하는 중이라면 tran
을 전달할 수 있습니다. 논의?
내 다른 질문은 내 freeList(struct Node* a)
기능이 올바르게 작동하지 않는다고 생각됩니다. 내가 root
을 풀어서 인쇄 할 때, 대신 "목록이 비어 있습니다"라고 인쇄되거나 쓰레기가 인쇄되어 있기 때문에 소유하고 있지 않은 메모리가 원인 일 때 쓰레기 값을 인쇄합니다.
마지막으로, 여기에있는 누군가가 내 코드를 "최종 사용자 응용 프로그램 코드"로 비판했습니다. 나는 여전히 코딩에 익숙하지 않고 위의 코드가 잘못 포맷되었거나 최종 사용자 응용 프로그램 코드가된다는 것을 확신 할 수 없습니다. 누군가가 "최종 사용자 응용 프로그램 코드"작성을 피할 수있는 방법을 설명하면 많은 도움이 될 것입니다.
큰 질문입니다. – gsamaras
설명하지 않고 명확하게 묻는 방법을 모르겠습니다. – Nightlife
코드는 작업이 성공적으로 수행되었는지 확인하기 위해 malloc (및 패밀리)의 반환 값을 항상 확인해야합니다. – user3629249