You are on page 1of 33

Linked List

Introduction
• Linked lists are special list of some data elements linked to one
another. The logical ordering is represented by having each element
pointing to the next element.
• Each element is called a node, which has two parts.
• INFO part which stores the information and
• POINTER which points to the next element.
• "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 ofthe element [INFO]
2. The second part called the link field contains the address of the next node
in the list [POINTER]
Linked List
• 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)
• 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 NULLvalue
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.
Normal Way to Draw a Linked List
Advantages
• Linked lists are dynamic data structures. That is, they can grow or
shrink during the execution of a program.
• Effcient memory utilization. Here memory is not pre-allocated.
Memory is allocated whenever it is required. And it is deallocated
(removed) when it is no longer needed.
• Insertion and deletion are easier and efficient. Linked lists provide
flexibility in inserting a data item at a specified posotion and
deletionof a data item from the given position.Many complex
applications can be easily carried out with linked lists.
Disadvantage
• More memory: if the number of fields are more, then more memory
space is needed.
• Searching a particular element in list is difficult and also time
consuming.
The linked list code will depend on the
following functions:
• Malloc() is a system function which allocates a block ot memory in
the "heap" and returns a pointer to the new block.

• Free() is the opposite of malloc() which de-allocates memory. The


argument to free()is a pointer to a block of memory in the heap a
pointer which was obtained by a malloc()function.
• The advantage of free0 is simply memory management when we no longer
need a block.
Types of Linked List
1. A Singly linked list is one in which all nodes are linked together in some sequential
manner. Hence, it is also called Linear linked list . Cleary it has the beginning and the end.
The problem with this list is that we can not access the predecessor of node from the
current node. This can be overcome in doubly linked lists.
2. A Doubly linked list is one in which all nodes are linked together by multiple links which
help in accessing both the successor node (next node) and predecessor node (previous
node) for any arbitrary node within th list. Therefore each node in a doubly linked list
fields (pointers) to the left node (previous) and the right node (next). This helps to
traverse the list in the forword direction and backword direction.
3. A Circular linked list is one which has no begining and no end . A singly linked list can be
made a circular linked list by simply sorting the address of the very first node in the link
field of the last node.
4. A Circular doubly linked list is one which has both the successor pointer and predecessor
pointer in circular manner.
Singly Linked List
Singly Linked List
• Singly linked list is a linked list which is a linear list of some nodes
containing homogeneous elements.
• Each node in a singly linked list consists of two parts, one is data part
and another is address part.
• The data part contains the data or information and except the last node,
• the address part contains the address of the next node in the list.
• The address part of the last node in the list contains NULL.
• The problem with this list is that we cannot access the predecessor
node (previous node) from the current node
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.
Circular Linked List
• Has no beginning and no end
• A singly linked list an be made circular linked list by simply sroing the
adress of the very first node in the link field of the last node
Circular doubly linked list
• A circular doubly linked list is one wich has both the successor pointer
and predecessor pointer in circular manner.
Operation On Linked List
1. Creation: This operation is used to create a linked list.
2. 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 begining of the linked list, At the end of the linked list, At the specified
position in the linked list, At the list itself is empty, then the new node is inserted as a first node.
3. Deletion: This operation is used to delete an item from the linked list. The node may be deleted from the
Beginning of the linked list, End of the linked list, Specifled psition in the list.
4. Traversing: it is the process of going through all the nodes of a linked list from one end to the other end.
If we start traversing from the very first node towards the last node, it is called forword traversing. If the
desired element is found, we signal operation "SUCCESSFULl!'. Otherwise, we signal it as
"UNSUCCESSFULl!’.
5. Concatenation: it is the process of appending the second list to the end of the first list consisting of m
nodes. When we concatenate two lists, the second list has n nodes, then the concatenated list will be
having (m+n) nodes. The last node of the list is modified so that it is now pointing to the first node in the
second list.
6. Display: This operation is used to print each and every node's information. We access each node from
the beginning of the list and output the data housed there.
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
Free Strorage List
• Together with the list in memory, a special list is maintained which
consist of unused memory celsss
• This list is called “list of available space” or “free strorage list” or “the
free pool”
Garbage Collection
• The OS a computer periodically collect all the delete space onto the
free storage list
• Any technique with does this collection is called garbage collection
• It usually takes place in two steps
• Irst computer run throught all the list, tagging those cells which are
currently in use, and then the computer runs throuh 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 strorage list is empty
• This situation is called overflow
• The term underflow refers to the situation where one wants to delete
data from a fata structure that is empty
Create Node
Before writing the code to build the link list, we need to create a node,
used to create and access other nodes in the linked list. The following
structure definition will do.
Operation on Single Linked List
• Insertion
• Inserting a node at the beginning
• Inserting a node at the end
• Inserting a node at intermediate position
• Deletion
• Deleting a node at the beginning
• Deleting a node at the end
• Deleting a node at intermediate position
• Display
Inserting a node at the beginning
Inserting a node at the end
Inserting a node at intermediate position
Deleting at the beginning
Deleting a node at the end
Deleting a node at intermediate position
Linked List Properties
• Analisa Kompleksitas (Running Time)
• insert next, prepend - O(1)
• delete next, delete first - O(1)
• find - O(n)
• retrieve current position - O(1)
• Keuntungan
• Growable (bandingkan dengan array)
• Mudah (quick) dalam read/insert/delete elemen pertama dan terakhir (jika kita juga
menyimpan referensi ke posisi terakhir, tidak hanya posisi head/current)
• Kerugian
• Pemanggilan operator new untuk membuat node baru. (bandingkan dengan array)
• Ada overhead satu reference untuk tiap node

You might also like