Create a new node (Link List) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Create a new node (Link List)

tokoGtokoG Posts: 209Member
[b][red]This message was edited by tokoG at 2006-6-29 23:22:35[/red][/b][hr]
Hi

I am trying to do a simple thing... create a new node in the function [b]createNode[/b] and make this the end node.

All the values to be assigned get passed from the calling function.

To make the node end, I put NULL on the field of [b]next[/b].
I tried execute and see if it works but it doesnt..I dont think I am doing anything wrong on this code;

[code]
..........
struct itemRecord {
char code[24];
char brand[32];
char remarks[256];
int amount;
struct itemRecord* next;
};

struct itemRecord* createNode(char* code, char* brand, char* remarks, int amount) {

struct itemRecord* temp;

//Assign the input to newly allocated memory block
temp = (struct itemRecord*)malloc(sizeof(struct itemRecord));
strcpy(temp->brand, brand);
strcpy(temp->brand, brand);
strcpy(temp->remarks, remarks);
temp->amount = amount;
temp->next = NULL; //to make this a last node

return temp;

}
[/code]

But then, maybe I did something wrong on the main.
I just created this [b]main[/b] to see if it works.
I wanted to print the result... but it has bugs somewhere... which I can't find. Would anybody advise?

[code]
int main() {
struct itemRecord* itemlist;

char code[] = "A";
char brand[] = "Brand";
char remarks[] = "Remarks";
int amount = 3;


itemlist = itemRecord(code, brand, remarks, amount);

printf("Code: %[^
]", itemlist->code);
printf("Brand: %[^
]", itemlist->brand);
printf("remarks: %[^
]", itemlist->remarks);
printf("Amount: %d", itemlist->amount);

getchar();//To hold the screen
}
[/code]

I didn't write [b]free(temp);[/b] function to free the memory created by malloc. Above function is simple, it's not even linking the list, there is no exixting list. Do I need to [b]free[/b] the memory? If so where would I locate the [b]free[/b] function? If I [b]freed[/b] before printng out with printf, wouldn't the node be gone...?

Comments

  • Gregry2Gregry2 Posts: 607Member
    [b][red]This message was edited by Gregry2 at 2006-6-29 23:50:50[/red][/b][hr]
    : [b][red]This message was edited by tokoG at 2006-6-29 23:22:35[/red][/b][hr]
    : Hi
    :
    : I am trying to do a simple thing... create a new node in the function [b]createNode[/b] and make this the end node.
    :
    : All the values to be assigned get passed from the calling function.
    :
    : To make the node end, I put NULL on the field of [b]next[/b].
    : I tried execute and see if it works but it doesnt..I dont think I am doing anything wrong on this code;
    :
    : [code]
    : ..........
    : struct itemRecord {
    : char code[24];
    : char brand[32];
    : char remarks[256];
    : int amount;
    : struct itemRecord* next;
    : };
    :
    : struct itemRecord* createNode(char* code, char* brand, char* remarks, int amount) {
    :
    : struct itemRecord* temp;
    :
    : //Assign the input to newly allocated memory block
    : temp = (struct itemRecord*)malloc(sizeof(struct itemRecord));
    : strcpy(temp->brand, brand);[red]//you did this twice and forgot the code :)[/red]
    : strcpy(temp->brand, brand);
    : strcpy(temp->remarks, remarks);
    : temp->amount = amount;
    : temp->next = NULL; //to make this a last node
    :
    : return temp;
    :
    : }
    : [/code]
    :
    : But then, maybe I did something wrong on the main.
    : I just created this [b]main[/b] to see if it works.
    : I wanted to print the result... but it has bugs somewhere... which I can't find. Would anybody advise?
    :
    : [code]
    : int main() {
    : struct itemRecord* itemlist;
    :
    : char code[] = "A";
    : char brand[] = "Brand";
    : char remarks[] = "Remarks";
    : int amount = 3;
    :
    :
    : itemlist = itemRecord(code, brand, remarks, amount);
    :
    : printf("Code: %[^
    ]", itemlist->code);
    : printf("Brand: %[^
    ]", itemlist->brand);
    : printf("remarks: %[^
    ]", itemlist->remarks);
    : printf("Amount: %d", itemlist->amount);
    :
    : getchar();//To hold the screen
    : }
    : [/code]
    :
    : I didn't write [b]free(temp);[/b] function to free the memory created by malloc. Above function is simple, it's not even linking the list, there is no exixting list. Do I need to [b]free[/b] the memory? If so where would I locate the [b]free[/b] function? If I [b]freed[/b] before printng out with printf, wouldn't the node be gone...?
    :

    No, you got it right, freeing it in createNode() would end up returning a pointer which points at nothing, and you'd see a bunch of garbage on ur outputs. This is okay, because you still have a pointer pointing to the memory you malloc()'d, you haven't lost it (its returned, and now, in main(), its address is held by itemaddress).

    But, after you've done printing out the values, then you can free it. Since your program ends right away, you don't have to free it, but its a good idea to make it a habit to free it when you lose it.

    Plus...the %[^
    ]...? They're strings, so you just do %s, thats all.
    And the
    for newline
    {2}rIng


Sign In or Register to comment.