You are on page 1of 33

DFC30233

DATA STRUCTURES

CHAPTER 2 :
LIST AND LINKED LIST
LIST

• a list or sequence is an abstract data structure that implements an ordered


collection of values, where the same value may occur more than once.
• value is often called an item, entry or element of the list
• Definition :
• List is a collection of data, element, component or objects with similar data
type
• List is a linear data structure, which contains a sequence of elements.
• The list has the property length (count of elements) and its elements
are arranged consecutively.
LIST

• Using array as a list


• An array is a series of elements of the same type placed in contiguous
memory locations that can be individually referenced by adding an index
to a unique identifier.
• Example: We can store 5 values of type int in an array without having to
declare 5 different variables, each one with a different identifier. Instead
of that, using an array we can store 5 different values of the same type,
int for example, with a unique identifier.
EXAMPLE OF LIST
CHARACTERISTIC OF LIST
LIST OPERATION

• Operations involved in implementing list :


• Insertion - can be made at the beginning of the list, in the middle of the list or
at the end of the list
• Deletion - requires that the list be searched to locate the data being deleted
• Retrieval - requires that data be located in a list and presented to the calling
module without changing the contents of the list
• Traversal - is a special case of retrieval in which all elements are retrieved in
sequence
LIST OPERATION

• Another operation involved in implementing list using array are createList .


• For createList operation, just declare an array.
• Example, if want to create a list of 10 numbers, it can be declared as
• #define maxSize 10 // used to declare maximum size of array
• int listNumber[maxSize]; // declaration of an array
• int NoOfItem; // used to store no. of item in an array
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 to the next record in
the sequence
• 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.
• linked lists allow insertion and removal of nodes at any point in the list
EXAMPLE OF LINKED LIST
BASIC CONCEPT OF LINKED LIST

• 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
DIFFERENCES BETWEEN LIST &
LINKED LIST

List Linked List


 Static size of list.  Dynamic size of list.

 Add and delete item required more  Add & delete item required less
steps. steps.
 Suitable for simple list.  Suitable for larger list.

 Easy programming processes.  Complex programming processes.

 Easy program update.  Complicated program update.

 Low of memory and computerizing  High of memory and computerizing


time. time.
ADVANTAGES OF LINKED LIST

• Linked lists are a dynamic data structure, allocating the needed memory while
the program is running.
• Insertion and deletion node operations are easily implemented in a linked list.
• Does not require any extra space therefore it does not waste extra memory
• They can reduce access time and may expand in real time without memory
overhead.
DISADVANTAGES OF LINKED LIST

• They have a tendency to use more memory due


to pointers requiring extra storage space.
• Nodes in a linked list must be read in order from the
beginning as linked lists are inherently sequential access.
• Nodes are stored incontiguously, greatly increasing the
time required to access individual elements within the list.
• Not suitable for simple list with minimal size
TYPES OF LINKED LIST

• Types of linked list


a) Single linked list
b) Double linked list
c) Circular linked list
d) Circular double linked list
TYPES OF LINKED LIST

• Types of linked list


a) Single linked list
- contain nodes which have a data field as well as 'next' field,
which points to the next node in line of nodes

A single linked list whose nodes contain two fields: an integer


value and a link to the next node
TYPES OF LINKED LIST

• Types of linked list


b) Double linked list
- each node contains, besides the next-node link, a second link
field pointing to the 'previous' node in the sequence. The two
links may be called 'forward('s') and 'backwards', or 'next' and
'prev'('previous').

A double linked list whose nodes contain three fields: an integer value, the link forward to the next
node, and the link backward to the previous node
TYPES OF LINKED LIST

• Types of linked list


c) Circular linked list
- In the last node of a list, the link field often contains a null reference, a
special value used to indicate the lack of further nodes.
- A less common convention is to make it point to the first node of the
list; in that case the list is said to be 'circular' or 'circularly linked‘
TYPES OF LINKED LIST

• Types of linked list


d) Circular double linked list
- Doubly Circular linked list has both the properties of doubly linked list and circular linked list.
- Two consecutive elements are linked by previous and next pointer and the last node points to first
node by next pointer and also the previous pointer of the head node points to the tail node.
OPERATION IN LINKED LIST

• Basic operations of linked list


- Creation
- Insertion
- Deletion
- Traversing
OPERATION IN LINKED LIST

1. Create linked list


- Create one new linked list.
2. Create node
- To create one new source for connecting items in the linked list.
3. Check the linked list
- Check if the linked list is empty.
4. Insert node in the linked list
- Add an item in the linked list. in the beginning
5. Delete node in the linked list
-
in the middle
Delete one item from the linked list.
6. Traversing at the end
- To find a value, find a position for insertion, etc
OPERATION IN LINKED LIST

1. Create linked list


- Create one new linked list.

Structure of a linked list

45 65 38 76
head tail

The address of the first node in the list is stored in a separate location called head / first
OPERATION IN LINKED LIST

2. Create node
- To create one new source for connecting items in the linked list.

Structure of a node

data link

address for the next node


OPERATION IN LINKED LIST

3. Check the linked list


- Check if the linked list is empty.

head tail
OPERATION IN LINKED LIST

4. Insert node in the linked list


- Add an item in the linked list.
head
45 65 38 76
tail

50

Structure of a linked list when inserting 50 (in the beginning)


OPERATION IN LINKED LIST

4. Insert node in the linked list


- Add an item in the linked list.
head
45 65 38 76
tail
80

Structure of a linked list when inserting 80 (in the middle)


OPERATION IN LINKED LIST

4. Insert node in the linked list


- Add an item in the linked list.
head tail
45 65 38 76

102

Structure of a linked list when inserting 102 (at the end)


OPERATION IN LINKED LIST

5. Delete node in the list


head - Delete one item from linked list. tail
45 65 38 76

Structure of a linked list when deleting 65 102

head
45 38 76 102
LINKED LIST STRUCTURE
OPERATION IN LINKED LIST :
DECLARING LINKED LIST
OPERATION IN LINKED LIST: CREATE
HEAD & NODE
OPERATION IN LINKED LIST: INSERT
OPERATION IN LINKED LIST: DELETE

You might also like