02 Linked List
02 Linked List
Definition
A linked list is a data structure that
consists of a sequence of data
records such that each record contains
a reference (i.e., a link) to the next record
in the sequence.
Each data record in a linked list is called a
node
Each node consists of two fields
Data Field
Link Field
Pros and Cons a linked list
Pros
Easy to insert data elements
Easy to delete data elements
Cons
No Random access
Searching is not efficient
Explanation
Think of Linked List like a train.
Always store the address of first node of the
list.
This would be the engine of the train.
The pointer is the connector between cars of
the train.
Every time the train adds a car, it uses the
connectors to add a new car.
This is like a programmer using the keyword
new to create a pointer to a new struct.
Each of the big blocks is a struct that has a
pointer to another one.
Remember that the pointer only stores the
memory location of something.
At the end, there is nothing for the pointer to
point to, so it does not point to anything, it
should be a null pointer or a dummy node to
prevent it from accidentally pointing to a totally
arbitrary and random location in memory.
Code
struct node {
int data;
node *link;
};
int main() {
node *start; // This will be the unchanging first node
start = new node; // Now start points to a node struct
start->link = NULL; // The node stat points to has its next
pointer set equal to a null pointer
start->data = 5; // By using the -> operator, you can
modify the node
}
Structure
A data structure is a group of data elements grouped
together under one name
These data elements, known as members, can have
different types and different lengths. Data structures are
declared in C++ using the following syntax:
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
Example
struct product {
int weight;
float price;
};
product apple;
product banana, melon;
Traversing
Traversing means to visit each element in a data structure
once.
It is necessary to understand how to traverse the linked list
before going further.
Think back to the train.
Lets imagine a conductor
The Conductor can only enter the train through the engine
It can walk through the train down the line as long as the
connector connects to another car.
In a similar pattern the program will traverse the linked
list.
The conductor will be a pointer to node, and it will first
point to start, and then
If the start’s pointer to the next node is
pointing to something, the "conductor" will be
set to point to the link node
(conductor=conductor->link;)
In this fashion, the list can be traversed.
Now, as long as there is a pointer to
something, the traversal will continue.
Once it reaches a null pointer (or dummy
node), meaning there are no more nodes
(train cars) then it will be at the end of the
list, and a new node can subsequently be
added if so desired
Code
struct node {
int data;
node *link;
};
int main() {
node *start; // This won't change, or we would lose the list in memory
node *conductor; // This will point to each node as it traverses the list
start = new node; // Sets it to actually point to something
start->link = NULL; // Otherwise it would not work well
start->data = 12;
conductor = start; // The conductor points to the first node
if ( conductor != 0 ) {
while ( conductor->link != 0)
conductor = conductor->link;
}
node *t;
t = new node; // Creates a node t
t->data = 42;
t->link = NULL; //Prevents it from going any further
conductor->link = t; // adds the node at the end of the inked list
}
Display the linked list
for( q = start ; q != NULL ; q = q->link )
cout<<endl<<q->data;