List and Linked List
List and Linked List
TOPIC 2
UNDERSTAND LIST AND LINKED LIST
List
List is a collection of data, element, component or objects with
similar data type.
List always represented as a record. Generally, a collection of
data items that can be selected by indices computed at run-
time, including array data structure, an arrangement of items
at equally spaced addresses in computer memory.
List can be implemented using array that contains sequence
of data/record.
List operations : insertion/add, deletion, retrieval/display,
traversal/search, check list & create list.
Example, if want to create list of 10 number, declared as
2 4 5 8 11 13 16
1 2 4 5 8 11 13 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13
indices
It also means that it is difficult to construct ordered lists. In our implementation, data
must be added to the end. If inserted before the end, all others beneath it must
shuffle down. This is slow and inefficient.
Limitation of list by using array
An array has a limited number of elements
routines inserting a new value have to check that there is room
Can partially solve this problem by reallocating the array
as needed (how much memory to add?)
adding one element at a time could be costly
one approach - double the current size of the array
A better approach: use a Linked List
and dynamically allocate memory
Linked List
A linked list is data structure that consists of a sequence of
data records such that in each record there is a field that
contains a reference (i.e., a link) to the next record in the
sequence.
The principal benefit of a linked list over a conventional
array is that the order of the linked items may be different
from the order that the data items are stored in memory or
on disk. For that reason, linked lists allow insertion and
removal of nodes at any point in the list, with a constant
number of operations.
Diagram of a node
a node
a data a link
Linked List: Basic Ideas
A linked list is an ordered series of connected data /
nodes
Each element of the linked list has
Some data
A link to the next element
The link is used to chain the data
Example : A linked list of integers
The linked list can grow and shrink
Data Link
20 45 75 85
20 45
add(75), add(85)
20 45 75 85
75
Linked List Structure
Before writing the code to build the above list, we need two data types...
Node The type for the nodes which will make up the body of the list. Each
node contains a single client data element and a pointer to the next node
in the list. Type: struct node
struct node
{
int data;
struct node* next;
};
Node Pointer The type for pointers to nodes. This will be the type of the
head pointer and the .next fields inside each node. In C and C++, no
separate type declaration is required since the pointer type is just the
node type followed by a '*'. Type: struct node*
Video 1
Assume, that we have a list with some nodes. Traversal is the very basic
operation, which presents as a part in almost every operation on a singly-
linked list. For instance, algorithm may traverse a singly-linked list to find a
value, find a position for insertion, etc. For a singly-linked list, only forward
direction traversal is possible.
Traversal algorithm
Beginning from the head, check, if the end of a list hasn't been reached
yet;
do some actions with the current node, which is specific for particular
algorithm;
current node becomes previous and next node becomes current. Go to
the step 1.
Example
As for example, let us see an example of summing up values in a singly-
linked list.
Example
As for example, let us see an example of summing up values in a singly-
linked list.
Video 2
Inserting a node
Deleting a node
Exercise
1. State four operations that can performed by list.
2. Draw an empty list constructed using pointer.
3. List two differences between list and linked list.
4. Draw a structure of linked list when the list only consists a
node.
Write sub code for addition and
deletion operations below.
Assume that the node structure is
defined as follows.
20 45
Struct Node
{
add(75), add(85)
int data;
Node *next;
20 45 75 85
} *head, *newNode *trail, *curr;
75