Académique Documents
Professionnel Documents
Culture Documents
struct node {
int data;
struct node *nextPtr;
};
nextPtr
Points to an object of type node
Referred to as a link
Linked Lists
Types of linked lists:
Singly linked list
Begins with a pointer to the first node
Terminates with a null pointer
Only traversed in one direction
Circular, singly linked
Pointer in the last node points
back to the first node
Doubly linked list
Two “start pointers” – first element and last element
Each node has a forward pointer and a backward pointer
Allows traversals both forwards and backwards
Circular, doubly linked list
Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
Linked Representation of Data
358 797
561
In a linked representation, data is 345
501 724
not stored in a contiguous manner.
Instead, data is stored at random 555 490 701
locations and the current data 513
location provides the information
regarding the location of the next
data. 358 797
561 501
345 724
START Node
Element
Data Pointer (Link) Null Pointer
Field Field
struct node {
int data;
1 300 5
struct node *link;
};
2 500 0 NULL
struct node my_node;
3
3 100 4
Example:
4 200 1
5 400 2
Conventions of Linked List
There are several conventions for the link to indicate the
end of the list.
1. a null link that points to no node (0 or NULL)
2. a dummy node that contains no item
3. a reference back to the first node, making it a
circular list.
Singly Linked List
PTR
32
Grounded Header Link List
A grounded header list is a header list where the last
node contains the null pointer.
The term “grounded” comes from the fact that many
texts use the electrical ground symbol to indicate the
null pointer.
Start
Header Node
Prev Pointer
Next Pointer
Data Field
Insert an Item
Inserting a Node at the Beginning of a Doubly Linked List