Search code examples
cnodescs50

What is a node in C?


I'm working on cs50 pset5 speller, and in the lecture they introduce a new thing called nodes. What is a node? I didn't really understand what they said in the video. When I tried googling it, I got some sites that explained what a node is, but I didn't really understand. I'm new to c so I'm not accustomed to what I call 'coding words'. For instance, I found this on a site about nodes: A dynamic array can be extended by doubling the size but there is overhead associated with the operation of copying old data and freeing the memory associated with the old data structure. What is that supposed to mean? Please help me figure out what a node is because they seem important and useful, especially for pset5. My node is defined like this:

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

Here is the link to the walk-through of speller pset5: https://cs50.harvard.edu/x/2020/psets/5/speller/


Solution

  • Node is a common terminology that is used to demonstrate a single block of linked list or tree or related data structures.

    It is a convention to name it node, otherwise you can call it with any name.

    Standard

    C++

    struct node{
    int data;
    int *next;
    };
    

    or in Python

    class Node:
       def __init__(self, data, next= None):
           self.data = data
           self.next = next
    

    But you can call it with anyname

    Not Standard

    C++

    struct my_own_name{
    int data;
    int *nextptr;
    };
    

    or in python

    
    class my_own_name:
        def __init__(self, data, next=None):
            self.data = data
            self.next = next