LECTURE 2b: THE LIST DATALECTURE 2b: THE LIST DATA
STRUCTURESTRUCTURE
Linked lists
◦ Abstract data type (ADT)
Basic operations of linked lists
◦ Insert, find, delete, etc.
◦ Types; Singly, Doubly & Circularly
DefinitionsDefinitions
List: Is a finite sequence of elements
with basic operators that vary from one
application to another.
Linked list: A linked list is a self-
referential data type that contains a
pointer or link to data of the same type.
Linked ListsLinked Lists
A B C
CONTDCONTD
A
data pointer
node
LISTSLISTS
A pointer is a data field added to a segment by
the DBMS.
When pointers connect several related
segments in succession, the segments form a
chain.
The segments connected by one or more
chains form a list.
The list is a flexible ADT.
LISTSLISTS
It is particularly useful when the number of
elements to be stored is not known before
running a program
It is well suited to sequential processing of
elements because the next element of a list is
readily accessible from the current element.
It is less suitable for random access to
elements as this requires a slow linear
search.
CHARACTERICS OF LISTSCHARACTERICS OF LISTS
They are ADT, i.e. the details as to how they
are organized & arranged is hidden to the user
or operator
The user can access only one element at a
time by simply asking for it
It consists of two parts i.e. head & tail & at
times it can be empty.
CHARACTERICS OF LISTSCHARACTERICS OF LISTS
Access of an item is possible without
necessary disturbing other elements because
each item has its own address.
It’s a simple collection of elements though it
facilitates complex algorithms to be written
because of its flexible & dynamic nature thus
allowing better memory management.
Difficulties experienced whenDifficulties experienced when
using lists:using lists:
When deleting & inserting they can
take time
Unless length is fixed, it is difficult to
know how much space is needed thus
over or under estimation.
There are two forms of lists:There are two forms of lists:
Flat lists
Hierarchical lists
Implementation of Flat Lists
The most natural way to implement lists is by
means of records & pointers.
They can also be implemented by arrays
APPLICATIONS OF LISTAPPLICATIONS OF LIST
A dynamic list may allow items to be inserted,
replaced, or deleted during the list's existence.
As the name implies, lists can be used to store a list
of records. The items in a list can be sorted for the
purpose of fast search - Applicable in the sorting
Used to allocate storage in the computer memory
in a lexicographic order
Used to keep track of multi-lists of example two-
dimensional array
Location of elements within a program
Linked ListLinked List
A linked list is a self-referential data type
because it contains a pointer or link to another
data of the same type
Linked lists permit insertion and removal of
nodes at any point in the list in constant time,
but do not allow random access
It consists of a sequence of nodes, each
containing arbitrary data fields and one or two
references ("links") pointing to the next and/or
previous nodes.
Linked ListLinked List
1 Bernard 4 Celine 3 Jonny 2 Terry 0
3 2Start 1 4
Linked listLinked list
Linked listLinked list
In linked lists, each item contains information
about items before & after it.
A linked list is similar to an array in that both
can be used to store data that is best
represented as a list of elements.
In linked lists elements needs not be
sequentially adjacent in memory rather
contains a pointer to the next element.
A linked list is a collection of nodes where
each node is one unit of data.
Linked listLinked list
In linked lists, we connect these nodes
together using pointers.
Each node contains a pointer that points to the
next node in the list.
Like an array, a linked list has a beginning & an
end.
The beginning is accessed using a head
pointer, the final node in the list points to the
null value
Different types of linked listDifferent types of linked list
Linearly-linked List- Singly-linked list
Doubly-linked lists,
Circularly-linked lists.
1. Linearly-linked List/ Singly-1. Linearly-linked List/ Singly-
linked listlinked list
 A singly linked list is a concrete data structure
consisting of a sequence of nodes
Each node stores
◦ Element
◦ Link to the next node
It has one link per node
This link points to the next node in the list, or
to a null value or empty list if it is the final node
Singly-linked listSingly-linked list
A B C D
∅
2.2. Doubly-linked listDoubly-linked list
Also known as two-way linked list.
Each node has two links: one points to the
previous node, or points to a null value or
empty list if it is the first node; & one points to
the next, or points to a null value or empty list
if it is the final node
Doubly-linked listDoubly-linked list
A doubly linked list is often more
convenient!
Nodes store:
◦ Element
◦ Link to the previous node
◦ Link to the next node
Special trailer and header nodes
Doubly-linked listDoubly-linked list
previous next
element node
2.2. Doubly-linked listDoubly-linked list
3. Circularly-linked list3. Circularly-linked list
In a circularly-linked list, the first & final nodes
are linked together.
This can be done for both singly & doubly
linked lists.
To traverse a circular linked list, you begin at
any node & follow the list in either direction
until you return to the original node.
3. Circularly-linked list3. Circularly-linked list
 Circularly-linked lists can also be seen
as having no beginning or end.
This type of list is most useful for
managing buffers for data ingest & in
cases where you have one object in a list
& wish to see all other objects in the list.
The pointer pointing to the whole list is
usually called the end pointer.
Applications of linked listsApplications of linked lists
Linked lists are used as a building block for
many other data structures, such as stacks,
queues & their variations.
Linked lists are used to implement associative
arrays & are in this context called association
lists.
Advantages of linked lists overAdvantages of linked lists over
arraysarrays
1. Elements can be inserted into linked lists
indefinitely, while an array will eventually either
fill up or need to be resized, an expensive
operation that may not even be possible if
memory is fragmented.
..............................................
2. Memory savings can be achieved, in certain
cases, by sharing the same "tail" of elements
among two or more lists — that is, the lists end in
the same sequence of elements.
In this way, one can add new elements to the
front of the list while keeping a reference to both
the new & the old versions — a simple example of
a persistent data structure.
Advantages of Arrays overAdvantages of Arrays over
Linked listsLinked lists
 1. Arrays allow random access, while linked
lists allow only sequential access to elements.
Singly-linked lists, in fact, can only be
traversed in one direction.
 This makes linked lists unsuitable for
applications where it's useful to look up an
element by its index quickly.
 Sequential access on arrays is also faster than
on linked lists on many machines due to
locality of reference and data caches.
Advantages of Arrays over LinkedAdvantages of Arrays over Linked
listslists
2. Another disadvantage of linked lists is the extra
storage needed for references, which often makes
them impractical for lists of small data items such
as characters or Boolean values.
It can also be slow & with a naïve allocator,
wasteful, to allocate memory separately for each
new element, a problem generally solved using
memory pools.
DISADVANTAGES OF LINKEDDISADVANTAGES OF LINKED
LISTSLISTS
Deleting & inserting is time consuming &
space consuming.
Unless length is fixed its difficult to estimate
how much space is required, this could result
in inefficiency.
Doubly linked list takes more time & space
because of extra operations.
Linked list operationsLinked list operations
Add new node
Delete node
LINKED LISTS OPERATIONSLINKED LISTS OPERATIONS
CONCLUSIONCONCLUSION
Linked list is similar to an array that it
contains data that is best organised in
a list fashion.
Its dynamic structure make it
expandable or shrinkable execution.
This dynamic quality make it
appealing to use in certain situations
where the static nature of arrays will
be wasteful.
ENDEND
QUESTIONS????

Lecture 2b lists

  • 1.
    LECTURE 2b: THELIST DATALECTURE 2b: THE LIST DATA STRUCTURESTRUCTURE Linked lists ◦ Abstract data type (ADT) Basic operations of linked lists ◦ Insert, find, delete, etc. ◦ Types; Singly, Doubly & Circularly
  • 2.
    DefinitionsDefinitions List: Is afinite sequence of elements with basic operators that vary from one application to another. Linked list: A linked list is a self- referential data type that contains a pointer or link to data of the same type.
  • 3.
  • 4.
  • 5.
    LISTSLISTS A pointer isa data field added to a segment by the DBMS. When pointers connect several related segments in succession, the segments form a chain. The segments connected by one or more chains form a list. The list is a flexible ADT.
  • 6.
    LISTSLISTS It is particularlyuseful when the number of elements to be stored is not known before running a program It is well suited to sequential processing of elements because the next element of a list is readily accessible from the current element. It is less suitable for random access to elements as this requires a slow linear search.
  • 7.
    CHARACTERICS OF LISTSCHARACTERICSOF LISTS They are ADT, i.e. the details as to how they are organized & arranged is hidden to the user or operator The user can access only one element at a time by simply asking for it It consists of two parts i.e. head & tail & at times it can be empty.
  • 8.
    CHARACTERICS OF LISTSCHARACTERICSOF LISTS Access of an item is possible without necessary disturbing other elements because each item has its own address. It’s a simple collection of elements though it facilitates complex algorithms to be written because of its flexible & dynamic nature thus allowing better memory management.
  • 9.
    Difficulties experienced whenDifficultiesexperienced when using lists:using lists: When deleting & inserting they can take time Unless length is fixed, it is difficult to know how much space is needed thus over or under estimation.
  • 10.
    There are twoforms of lists:There are two forms of lists: Flat lists Hierarchical lists Implementation of Flat Lists The most natural way to implement lists is by means of records & pointers. They can also be implemented by arrays
  • 11.
    APPLICATIONS OF LISTAPPLICATIONSOF LIST A dynamic list may allow items to be inserted, replaced, or deleted during the list's existence. As the name implies, lists can be used to store a list of records. The items in a list can be sorted for the purpose of fast search - Applicable in the sorting Used to allocate storage in the computer memory in a lexicographic order Used to keep track of multi-lists of example two- dimensional array Location of elements within a program
  • 12.
    Linked ListLinked List Alinked list is a self-referential data type because it contains a pointer or link to another data of the same type Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes.
  • 13.
    Linked ListLinked List 1Bernard 4 Celine 3 Jonny 2 Terry 0 3 2Start 1 4
  • 14.
  • 15.
    Linked listLinked list Inlinked lists, each item contains information about items before & after it. A linked list is similar to an array in that both can be used to store data that is best represented as a list of elements. In linked lists elements needs not be sequentially adjacent in memory rather contains a pointer to the next element. A linked list is a collection of nodes where each node is one unit of data.
  • 16.
    Linked listLinked list Inlinked lists, we connect these nodes together using pointers. Each node contains a pointer that points to the next node in the list. Like an array, a linked list has a beginning & an end. The beginning is accessed using a head pointer, the final node in the list points to the null value
  • 17.
    Different types oflinked listDifferent types of linked list Linearly-linked List- Singly-linked list Doubly-linked lists, Circularly-linked lists.
  • 18.
    1. Linearly-linked List/Singly-1. Linearly-linked List/ Singly- linked listlinked list  A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores ◦ Element ◦ Link to the next node It has one link per node This link points to the next node in the list, or to a null value or empty list if it is the final node
  • 19.
  • 20.
    2.2. Doubly-linked listDoubly-linkedlist Also known as two-way linked list. Each node has two links: one points to the previous node, or points to a null value or empty list if it is the first node; & one points to the next, or points to a null value or empty list if it is the final node
  • 21.
    Doubly-linked listDoubly-linked list Adoubly linked list is often more convenient! Nodes store: ◦ Element ◦ Link to the previous node ◦ Link to the next node Special trailer and header nodes
  • 22.
  • 23.
  • 24.
    3. Circularly-linked list3.Circularly-linked list In a circularly-linked list, the first & final nodes are linked together. This can be done for both singly & doubly linked lists. To traverse a circular linked list, you begin at any node & follow the list in either direction until you return to the original node.
  • 25.
    3. Circularly-linked list3.Circularly-linked list  Circularly-linked lists can also be seen as having no beginning or end. This type of list is most useful for managing buffers for data ingest & in cases where you have one object in a list & wish to see all other objects in the list. The pointer pointing to the whole list is usually called the end pointer.
  • 26.
    Applications of linkedlistsApplications of linked lists Linked lists are used as a building block for many other data structures, such as stacks, queues & their variations. Linked lists are used to implement associative arrays & are in this context called association lists.
  • 27.
    Advantages of linkedlists overAdvantages of linked lists over arraysarrays 1. Elements can be inserted into linked lists indefinitely, while an array will eventually either fill up or need to be resized, an expensive operation that may not even be possible if memory is fragmented.
  • 28.
    .............................................. 2. Memory savingscan be achieved, in certain cases, by sharing the same "tail" of elements among two or more lists — that is, the lists end in the same sequence of elements. In this way, one can add new elements to the front of the list while keeping a reference to both the new & the old versions — a simple example of a persistent data structure.
  • 29.
    Advantages of ArraysoverAdvantages of Arrays over Linked listsLinked lists  1. Arrays allow random access, while linked lists allow only sequential access to elements. Singly-linked lists, in fact, can only be traversed in one direction.  This makes linked lists unsuitable for applications where it's useful to look up an element by its index quickly.  Sequential access on arrays is also faster than on linked lists on many machines due to locality of reference and data caches.
  • 30.
    Advantages of Arraysover LinkedAdvantages of Arrays over Linked listslists 2. Another disadvantage of linked lists is the extra storage needed for references, which often makes them impractical for lists of small data items such as characters or Boolean values. It can also be slow & with a naïve allocator, wasteful, to allocate memory separately for each new element, a problem generally solved using memory pools.
  • 31.
    DISADVANTAGES OF LINKEDDISADVANTAGESOF LINKED LISTSLISTS Deleting & inserting is time consuming & space consuming. Unless length is fixed its difficult to estimate how much space is required, this could result in inefficiency. Doubly linked list takes more time & space because of extra operations.
  • 32.
    Linked list operationsLinkedlist operations Add new node Delete node
  • 33.
  • 34.
    CONCLUSIONCONCLUSION Linked list issimilar to an array that it contains data that is best organised in a list fashion. Its dynamic structure make it expandable or shrinkable execution. This dynamic quality make it appealing to use in certain situations where the static nature of arrays will be wasteful.
  • 35.