Sunteți pe pagina 1din 42

Data Structure & Algorithms

Chapter 4:
Linked List
Imran Ali Memon
IT Department
Contents
4.1 Introduction
4.2 Representation of Linked List in memory
4.3 Traversing Linked List
4.4 Searching Linked List
4.5 Memory Allocation, Garbage Collection
4.6 Overflow & Underflow
4.7 malloc() & free()
4.8 Inserting in Linked List
4.9 Deletion in Linked List
4.1 Introduction
• “A linked list is a linear collection of data elements,
called node pointing to the next nodes by means of
pointers.”
• Each node is divided into two parts.
1. The first part contains the information of the
element.
2. The second part called the link field contains the
address of the next node in the list.
4.1 Introduction
• To see this more clearly lets look at an example:
4.1 Introduction
• The last node contains a null link
• The list may (or may not) have a header
• A node’s successor is the next node in the sequence
– The last node has no successor
• A node’s predecessor is the previous node in the
sequence
– The first node has no predecessor
• A list’s length is the number of elements in it
– A list may be empty (contain no elements)
4.1 Introduction
• The Head is a special pointer variable which
contains the address of the first node of the list.
• If there is no node available in the list then Head
contains NULL value that means, List is empty.
• The left part of the each node represents the
information part of the node
– which may contain an entire record of data (e.g. ID,
name, marks, age etc)
• The right part represents pointer/link to the next
node
• The next pointer of the last node is null pointer signal
the end of the list.
Advantages
• List of data can be stored in arrays but linked
structures (pointers) provide several advantages.
• A linked list is appropriate when the number of
data elements to be represented in data
structure is unpredictable.
• It also appropriate when there are frequently
insertions & deletions occurred in the list.
• Linked lists are dynamic, so the length of a list can
increase or decrease as necessary.
Singly Linked Lists and Arrays

Singly linked list Array


Elements are stored in linear Elements are stored in linear
order, accessible with links. order, accessible with an
index.

Do not have a fixed size. Have a fixed size.

Cannot access the previous Can access the previous


element directly. element easily.

No binary search. Binary search.


Normal Way to Draw a Linked List

c a e d b

first
first
N
U
a b c d e L
L

link field of a node, points to next node

data field of a node


4-9
Representation of Linked List in memory
INFO LINK
START
4 1 A 12
2
3
4 I 10
5
6
7
8 R 1

9
10 M 8
11
12 N 0
TYPES OF LINKED LISTS
1. Singly linked list (Linear linked list / One way list)
2. Doubly linked list (Two way list)
3. Circular linked list
4. Circular doubly linked list
1. Singly linked list

• A singly linked list is one in which all nodes are


linked together in some sequential manner.
• Also called linear linked list.
• It has the beginning and the end.
• The problem with this list is that we cannot access the
predecessor node (previous node) from the current
node.
• That’s why it is called one way list.
• This can be overcome by doubly linked lists.
2. Doubly linked list
• All nodes are linked together by multiple links .
• Which help in accessing both the successor node(next
node) and predecessor node(previous node)
• Therefore, each node in a doubly linked list points
to the left node (previous) and the right node (next).
• This helps to traverse the list in the forward direction
• and backward direction.
3. Circular linked list
• Has no beginning and no end.
• A singly linked list can be made circular linked list
by simply storing the address of the very first node
in the link field of the last node.
Circular List
first

BAT CAT EAT FAT GAT HAT

first
last
last

4-17
4. Circular doubly linked list
• A circular doubly linked list is one which has
both the successor pointer and predecessor
pointer in circular manner.
Operations on Linked List:
• There are several operations associated with linked
list i.e.
1. Traversing a Linked List
2. Searching a Linked List:
3. Insertion into a Linked List:
4. Inserting a new node in list:
5. Delete a node from list:
Traversing a Linked List
• It is a process of going through all the nodes of a
linked list from one end to the other end.

• It we start traversing from the very first node


towards the last node, it is called forward traversing.

• It we start traversing from the very last node


towards the first node, it is called reverse traversing.
Traversing Algorithm
Traversing a SLL (animation)

PTR

START

one two three

22
Searching:
• The process of finding a particular node of an
linked list is called Searching”.
• Two algorithms for searching:
– Unsorted linked list searching algorithm
– Sorted linked list searching algorithm
Sorted Linked List Searching Algorithm
20 16 12 5 3 1 x

< for descending


> For ascending
Free Storage List
• Together with the link list in memory, a special list
is maintained which consist of unused memory
cells.
• This list is called “list of available space” or “free
storage list” or “the free pool” .
Garbage Collection
• The OS of a computer periodically collect all the
deleted space onto the free storage list.
• Any technique with does this collection is called
garbage collection.
• It usually takes place in two steps.
• First computer run through all the lists, tagging
those cells which are currently in use, and then the
computer runs through the memory , collecting all
untagged space onto the free storage list.
Overflow & Underflow
• Sometimes new data are to be inserted into a data
structure but there is no available space i.e the
storage list is empty.
• This situation is called overflow.
• The term underflow refers to the situation where
one wants to delete data from a data structure that
is empty.
Memory Allocation
(a) Static memory allocation
• Compile-time allocation using arrays
• The required amount of memory is allocated to the
program elements at the start of the program.
• Here the memory to be allocated to the variable is fixed
(b) Dynamic / Runtime memory allocation
• Run-time allocation using pointers
• allows us to be able to get the required portion of
memory at runtime (or we say as the need arises)
• best suited type of allocation where we do not know the
memory requirement in advance
New & Delete Operators
• Dynamic memory is allocated using operator new.
• new is followed by a data type specifier.
• If a sequence of more than one element is required,
the number of these within brackets [].
• It returns a pointer to the beginning of the new block
of memory allocated.
• Its syntax is:
pointer = new type  ptr = new int;
pointer = new type [num_of_elem]  ptr = new int[10];
New & Delete Operators
• In most cases, memory allocated dynamically is only
needed during specific periods of time within a
program.
• Once it is no longer needed, it can be freed so that the
memory becomes available again for other requests of
dynamic memory.
• This is the purpose of operator delete, whose syntax is:

1. delete pointer;
2. delete[] pointer;
• Back to Linked List
Insertion:
• This operation is used to insert a new node in the
linked list at the specified position.
A new node may be inserted :
• At the beginning of a linked list
• At the end of a linked list
• At the specified position in a linked list.
• If the list itself is empty, then the new node is
inserted as a first node.
Insertion at the beginning
Inserting at the beginning of the list
Inserting at Given Position
Algorithm of Insertion at Given Position
Insertion at Last of Linked List

Algorithm of insertion at last of linked list is home work for


you.
Deletion:

• This operation is used to delete a node from the


linked list. A node may be deleted from the
• Beginning of a linked list
• End of a linked list.
• Specified position in the linked list.
Deletion at Beginning of Linked List

Algorithm of deletion at beginning of linked list is home


work for you.
Deletion at Given Position
Algorithm of Deletion at Given Position
• DEL(INFO, LINK, START, AVAIL, LOC, LOCP)
• This algorithm deletes the node N with location LOC. LOCP is the location
of the node which leads N or, When N is the first node. LOCP = NULL
1. If LOCP = NULL then:
Set START :=LINK[START]. [Deletes first node].
Else:
Set LINK[LOCP] := LINK[LOC].[Deletes node N]
[End of If structure]
2. [Return deleted node to the AVAIL List]
Set LINK[LOC] := AVAIL.
1. Exit.
Deletion at lastof Linked List

Algorithm of deletion at last of linked list is home work for


you.

S-ar putea să vă placă și