Explain how pointer are used to
implement linked list structure.
A linked
list is a data structure that is built from structures and pointers. It forms a
chain of"nodes" with pointers representing the links of the chain and
holding the entire thing together.
A linked
list can be represented by a diagram like this one: This linked list has four
nodes in it, each with a link to the next node in the series. The last node has
a link to the special value NULL, which any pointer (whatever its type) can
point to, to show that it is the last link in the chain. There is also another
special pointer, called Start (also called head), which points to the first
link in the chain so that we can keep track of it.
Defining
the data structure for a linked list
The key
part of a linked list is a structure, which holds the data for each node (the
name, address, age or whatever for the items in the list), and, most
importantly, a pointer to the next node. Here we have given the structure of a
typical node:
struct
node
{ char
name[20]; // Name of up to 20 letters int age
float
height; // In metres
node
*nxt;// Pointer to next node };
struct
node *start_ptr = NULL;
The
important part of the structure is the line before the closing curly brackets.
This gives a pointer to the next node in the list. This is the only case in C++
where you are allowed to refer to
a data
type (in this case node) before you have even finished defining it!
We have
also declared a pointer called start_ptr that will permanently point to the
start of the list. To start with, there are no nodes in the list, which is why
start_ptr is set to NULL.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.