Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Basic Concept and Structure | Implementing Data Structures
C Structs

bookBasic Concept and Structure

A linked list in C is a simple dynamic data structure consisting of elements called nodes. Each node contains data (such as a variable or object) as well as a pointer to the next node in the list.

Here's what a typical node in a singly linked list looks like in C:

If instead of a pointer to the next node you try to simply create an instance of a new node, you will get an error.

The compiler will not be able to allocate memory for such a structure, since it contains itself.

(It's like trying to look at yourself from the outside with your own eyes)

Using a pointer solves this problem because the compiler knows how much memory to allocate for a pointer variable.

Note

The last node pointer will always be NULL.

Opgave

Swipe to start coding

  1. Create a structure called Node;
  2. Create a data field called data;
  3. Create a field for a pointer to the next node.

Løsning

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 2
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

Can you explain how to create a linked list in C?

What does the structure definition for a node look like?

Why does the last node's pointer need to be NULL?

close

Awesome!

Completion rate improved to 4.17

bookBasic Concept and Structure

Stryg for at vise menuen

A linked list in C is a simple dynamic data structure consisting of elements called nodes. Each node contains data (such as a variable or object) as well as a pointer to the next node in the list.

Here's what a typical node in a singly linked list looks like in C:

If instead of a pointer to the next node you try to simply create an instance of a new node, you will get an error.

The compiler will not be able to allocate memory for such a structure, since it contains itself.

(It's like trying to look at yourself from the outside with your own eyes)

Using a pointer solves this problem because the compiler knows how much memory to allocate for a pointer variable.

Note

The last node pointer will always be NULL.

Opgave

Swipe to start coding

  1. Create a structure called Node;
  2. Create a data field called data;
  3. Create a field for a pointer to the next node.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 2
single

single

some-alt