Understanding C++ Linked List Code: Node Structure and Pointers Explained

  • Comp Sci
  • Thread starter bd411
  • Start date
  • Tags
    C++ List
In summary: Yes, Node is the name of the structure. However, when you use the typedef statement, you are creating a new data type. So NodePtr is not just a name for the structure, it is now a type that is equivalent to "pointer to Node." This allows you to declare variables of type NodePtr, just like you would declare variables of type int or float.In summary, the conversation discusses the use of typedef to assign alternative names to existing types in C and C++. It explains the purpose of typedef and how it is used to create new data types. It also discusses the use of pointers in data structures, particularly in the context of a linked list. The conversation provides examples of both a "C" and "C++
  • #1
bd411
39
0

Homework Statement


Hello, just having a bit of trouble understanding this bit of code:

typedef int Item;

struct Node {
Item data;
Node* next;
};

typedef Node* NodePtr

NodePtr hdlist = NULL;


Homework Equations



None.

The Attempt at a Solution



I understand that typedef is used to assign alternative names to existing types. So Item is now (int). Node is the name of the structure and inside the structure we have Item data = int data basically. What I'm not sure about is Node* next.

As far as I am aware, pointers are declared with the int* ptr method. This is a pointer ptr of type int. However does this mean that next is a pointer of type Node? But Node is simply the name of the structure?
 
Physics news on Phys.org
  • #2
Yes, so every Node has a pointer to a(nother) Node. This will be the next item in the list, like this:
ll2.gif


What you do is you keep a pointer to the first node in the list, which is called the head. The first item in the list is head->data. From there you can skip to head->next, which is the second node in the list, and the ->next of that is the third node, etc. until you eventually reach the end of the list which has it's next pointer pointing to nothing (null).

There are also variations where every Node has two pointers, one to the next item in the list and one to the previous one, which allows you to go through it in two directions; and computer scientists can probably tell you more complicated ones which have their various advantages (for example for easily searching sorted lists, etc).
 
Last edited:
  • #3
Here is a "C" example with the next pointer at the start of the structure. This allows different structures with a next pointer at the beginning to share common code for handling lists of structures. This first example also uses the old "microsoft" "C" style for the typedef syntax. I left out the int item typedef, since I wanted to focus on the structure typedef:

typedef struct _NODE { // underscore used on struct name
struct _NODE* next;
int data;
}NODE, *NODEPTR; // no underscore on typedef names

NODEPTR hdlist = NULL;

Here is a "C++" example that uses inherited class:

class Node{ // base node class
public:
Node * next;
Node(){next = NULL;}
};

class NodeInt : public Node{ // inherited node class with int
public:
int value;
NodeInt(int v) : value(v) {next = NULL;};
};

NodeInt * hdlist = NULL;
 
Last edited:
  • #4
bd411 said:
As far as I am aware, pointers are declared with the int* ptr method.
To declare a pointer you use syntax that looks like this:
Code:
[[I]type[/I]] * ptr;

In the above, type is some data type, such as int, float, char, or some user-defined type, such as Node in your code.
bd411 said:
This is a pointer ptr of type int.
No it's not. The declaration int * ptr; says that ptr is a variable whose type is "pointer to int." The value that is stored in a pointer is the address of the thing it points to.
bd411 said:
However does this mean that next is a pointer of type Node?
No, the type is "pointer to Node."
bd411 said:
But Node is simply the name of the structure?
 
  • #5


First of all, great job on understanding the typedef and the structure in the code! You are correct in understanding that Node is the name of the structure and Item data is an int data. However, you are a bit confused about Node* next.

In C++, pointers are declared using the * symbol after the data type, as you mentioned. In this case, Node* next means that next is a pointer to another Node structure. This creates a "link" between the current node and the next node in the linked list.

So, next is not a pointer of type Node, but rather a pointer to another Node. This is important in linked lists because it allows us to connect multiple nodes together and create a chain of data.

I hope this helps clarify the code a bit more for you. Keep up the good work in understanding C++ linked list code!
 

Related to Understanding C++ Linked List Code: Node Structure and Pointers Explained

What is a linked list in C++?

A linked list is a data structure in C++ that consists of a sequence of nodes. Each node contains data and a pointer to the next node in the list. This allows for dynamic memory allocation and efficient insertion and deletion of elements.

What are the advantages of using a linked list in C++?

One advantage of using a linked list in C++ is that it allows for efficient insertion and deletion of elements, as it does not require shifting of elements like an array would. Additionally, linked lists have a dynamic size, meaning they can grow or shrink as needed. This makes them useful for implementing stacks, queues, and other data structures.

What are the disadvantages of using a linked list in C++?

One disadvantage of using a linked list in C++ is that it is not as efficient for random access as an array. This is because elements in a linked list are not stored contiguously in memory. Additionally, linked lists require extra memory for storing the pointers to the next node, which can be a concern for large lists.

How do you insert an element into a linked list in C++?

To insert an element into a linked list in C++, you must first create a new node with the desired data. Then, you can insert the node at the beginning, end, or in the middle of the list by adjusting the pointers of the adjacent nodes. This allows for efficient insertion without having to shift elements.

How do you remove an element from a linked list in C++?

To remove an element from a linked list in C++, you must first find the node that contains the element you want to remove. Then, you can adjust the pointers of the adjacent nodes to bypass the node and free its memory. This allows for efficient deletion without having to shift elements.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
Back
Top