Вопрос

Here is a linked list I am working on, and trying to figure out exactly what each line does. The way I seem to be learning how to program is painstakingly difficult, and I am getting extremely discouraged. Regardless, I understand how the link list works, but I am not understanding what the code is saying and what it exactly is doing to create the structs. For example: I can't understand why would you be assigning a pointer to node (13 and 14), especially when my understand of pointers is that they are used to store memory locations.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct numnode
{
    int val; 
    struct numnode * next;
};
typedef struct numnode node;

main()
{
    int i;
    node * head;
    node * newnode;
    head = NULL;

    for (i = 1; i <= 10; i++)
    {
        newnode = (node *) malloc(sizeof(node));
        newnode->val = i;
        newnode->next = NULL;

        if (head == NULL)
        {
            head = newnode;
        }
        else
        {
            newnode->next = head;
            head = newnode;
        }
    }
}
Это было полезно?

Решение

Here are some annotations (and minor edits to reduce the amount of code).

/* Linked list node definition */
struct node {
    int val; 
    struct node * next;
};

int main() {
    int i;
    struct node *head, *new_node;
    head = NULL;
    for (i = 1; i <= 10; i++) {
        // Allocate a new node and initialize its components (val and next)
        new_node = (struct node *) malloc(sizeof(node));
        new_node->val = i;
        new_node->next = NULL;

        // The if block is actually not necessary...
        if (head == NULL) {
            // If the linked list is empty, set the head pointer to the initial node
            head = new_node;
        } else {
            // Now that you have your new node, connect it. Start:
            // head->[current linked list]
            // [new_node.next]->NULL

            new_node->next = head;
            // head->[current linked list]->...
            // [new_node.next]->[current linked list]->...

            head = newnode;
            // head->[new_node.next]->[current linked list]->...
        }
    }
}

The key thing is that malloc returns a pointer to memory. Each new node is allocated dynamically and thus is a location in memory (not a basic type).

Другие советы

If you fix the statement pointed out by PakkuDon you will find that the code inserts at the head. It will end up with a list whose values descend from 9 down to 1.

pointer is just the thing to tell you where is the value,like a phone number,you can call anyone no matter who there is,as you konw the number.pointer can point to anything(under your access) as you want,no matter it is a int or a struct.

Here is the summary of this code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct numnode
{
    int val; 
    struct numnode * next;
};
typedef struct numnode node;

main()
{
    int i;
    node * head;
    node * newnode;
    head = NULL;

    for (i = 1; i <= 10; i++)
    {
        newnode = (node *) malloc(sizeof(node));
        newnode->val = i;
        newnode->next = NULL;

        if (head == NULL)  // It'll be NULL first time, as head = NULL.
        {
            // True @ i = 1
            head = newnode;
        }
        else // Afterwards, as head=newnode
        {
            // New node will be created every time. Till i <= 10.
            newnode->next = head;
            head = newnode;
        }
    }
}

It is a simple code though.

PS: It is head == NULL

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top