LECTURE 2b: THE LIST DATA LECTURE 2b: THE LIST DATA STRUCTURESTRUCTURE
Linked lists◦Abstract data type (ADT)
Basic operations of linked lists◦Insert, find, delete, etc.◦Types; Singly, Doubly & Circularly
DefinitionsDefinitionsList: 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
LISTSLISTSA 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.
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 LISTSThey 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 when Difficulties 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 listsHierarchical listsImplementation of Flat ListsThe most natural way to implement lists is by means of records & pointers.
They can also be implemented by arrays
APPLICATIONS OF LISTAPPLICATIONS OF LISTA 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 ListA 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 listIn 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 listIn 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 listDoubly-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 nodeThis 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 listA 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
2. 2. Doubly-linked listDoubly-linked list
3. Circularly-linked list3. Circularly-linked listIn 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 listsLinked 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 over Advantages 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 over Advantages 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 Linked Advantages 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 LINKED DISADVANTAGES 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 nodeDelete node
LINKED LISTS OPERATIONSLINKED LISTS OPERATIONS
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.