Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Building a Linked List | Implementing Data Structures
C Structs
course content

Contenido del Curso

C Structs

C Structs

1. Introduction to Structs
2. Pointers and Structs
3. Structs and Memory
4. Advanced Structs Usage
5. Implementing Data Structures

bookBuilding a Linked List

Let's write a simple function to create a node. This function should:

  • As an argument, take a number of type int, which will be stored in the node in the data field;
  • Create a pointer to a dynamically allocated memory area (taking into account the size of the node);
  • Check the success of memory allocation for the node;
  • Put the required value (function argument) into the data field;
  • The next pointer must be NULL, since the function creates only one node;
  • The function must return a pointer to the memory area where value is stored.

The AddNewNode() function takes a pointer to a pointer, because it can change the value of the pointer to the first (head) node of the list, as well as the value that will be stored in the new node:

If the list is empty (*head == NULL), we simply update the pointer to the head of the list (*head) to point to the new node (newNode), in which case newNode will become the first node (head node). In the case of a non-empty list, we use a loop to look for the last node.

The current node plays the role of a buffer node, with its help we can iterate through the list and find a node that points to nowhere (*next == NULL).

Note

The last node in the list will have a next pointer equal to NULL.

After all, you need to make the new node newNode the last node, which means current->next must be equal to newNode. Now newNode is the last node in the list.

As a task, you need to figure out and add a function for adding new nodes to existing nodes.

Tarea

  1. Create a new node newNode, which will store the data value;
  2. Specify the condition under which the list will be considered empty;
  3. Specify what current->next will be equal to after the loop completes.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3
toggle bottom row

bookBuilding a Linked List

Let's write a simple function to create a node. This function should:

  • As an argument, take a number of type int, which will be stored in the node in the data field;
  • Create a pointer to a dynamically allocated memory area (taking into account the size of the node);
  • Check the success of memory allocation for the node;
  • Put the required value (function argument) into the data field;
  • The next pointer must be NULL, since the function creates only one node;
  • The function must return a pointer to the memory area where value is stored.

The AddNewNode() function takes a pointer to a pointer, because it can change the value of the pointer to the first (head) node of the list, as well as the value that will be stored in the new node:

If the list is empty (*head == NULL), we simply update the pointer to the head of the list (*head) to point to the new node (newNode), in which case newNode will become the first node (head node). In the case of a non-empty list, we use a loop to look for the last node.

The current node plays the role of a buffer node, with its help we can iterate through the list and find a node that points to nowhere (*next == NULL).

Note

The last node in the list will have a next pointer equal to NULL.

After all, you need to make the new node newNode the last node, which means current->next must be equal to newNode. Now newNode is the last node in the list.

As a task, you need to figure out and add a function for adding new nodes to existing nodes.

Tarea

  1. Create a new node newNode, which will store the data value;
  2. Specify the condition under which the list will be considered empty;
  3. Specify what current->next will be equal to after the loop completes.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 3
toggle bottom row

bookBuilding a Linked List

Let's write a simple function to create a node. This function should:

  • As an argument, take a number of type int, which will be stored in the node in the data field;
  • Create a pointer to a dynamically allocated memory area (taking into account the size of the node);
  • Check the success of memory allocation for the node;
  • Put the required value (function argument) into the data field;
  • The next pointer must be NULL, since the function creates only one node;
  • The function must return a pointer to the memory area where value is stored.

The AddNewNode() function takes a pointer to a pointer, because it can change the value of the pointer to the first (head) node of the list, as well as the value that will be stored in the new node:

If the list is empty (*head == NULL), we simply update the pointer to the head of the list (*head) to point to the new node (newNode), in which case newNode will become the first node (head node). In the case of a non-empty list, we use a loop to look for the last node.

The current node plays the role of a buffer node, with its help we can iterate through the list and find a node that points to nowhere (*next == NULL).

Note

The last node in the list will have a next pointer equal to NULL.

After all, you need to make the new node newNode the last node, which means current->next must be equal to newNode. Now newNode is the last node in the list.

As a task, you need to figure out and add a function for adding new nodes to existing nodes.

Tarea

  1. Create a new node newNode, which will store the data value;
  2. Specify the condition under which the list will be considered empty;
  3. Specify what current->next will be equal to after the loop completes.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Let's write a simple function to create a node. This function should:

  • As an argument, take a number of type int, which will be stored in the node in the data field;
  • Create a pointer to a dynamically allocated memory area (taking into account the size of the node);
  • Check the success of memory allocation for the node;
  • Put the required value (function argument) into the data field;
  • The next pointer must be NULL, since the function creates only one node;
  • The function must return a pointer to the memory area where value is stored.

The AddNewNode() function takes a pointer to a pointer, because it can change the value of the pointer to the first (head) node of the list, as well as the value that will be stored in the new node:

If the list is empty (*head == NULL), we simply update the pointer to the head of the list (*head) to point to the new node (newNode), in which case newNode will become the first node (head node). In the case of a non-empty list, we use a loop to look for the last node.

The current node plays the role of a buffer node, with its help we can iterate through the list and find a node that points to nowhere (*next == NULL).

Note

The last node in the list will have a next pointer equal to NULL.

After all, you need to make the new node newNode the last node, which means current->next must be equal to newNode. Now newNode is the last node in the list.

As a task, you need to figure out and add a function for adding new nodes to existing nodes.

Tarea

  1. Create a new node newNode, which will store the data value;
  2. Specify the condition under which the list will be considered empty;
  3. Specify what current->next will be equal to after the loop completes.

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 5. Capítulo 3
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
some-alt