0% found this document useful (0 votes)
4 views

chap2

This chapter covers the concept of linked lists, a linear data structure consisting of nodes that are linked using pointers. It explains the types of linked lists, including singly, doubly, and circular linked lists, as well as their applications and basic operations such as insertion, deletion, and searching. The chapter also discusses the advantages and disadvantages of linked lists compared to arrays.

Uploaded by

raagaqaran1988
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

chap2

This chapter covers the concept of linked lists, a linear data structure consisting of nodes that are linked using pointers. It explains the types of linked lists, including singly, doubly, and circular linked lists, as well as their applications and basic operations such as insertion, deletion, and searching. The chapter also discusses the advantages and disadvantages of linked lists compared to arrays.

Uploaded by

raagaqaran1988
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter 02: Data

Structures &
Algorithms -
Linked List
OBJECTIVES
After going through this chapter, you will be
able to:
– Define linked List, Memory representation of
Linked List
– Types of Linked List-Singly, Doubly, Circular
Linked List
– Implementation of Singly Linked List
– Explain the applications of Linked List
What is linked list Data structure ?
LINKED LIST
• A linked list or one-way list is a linear collection of data
elements, called nodes, where the linear order is given
by means of “pointers”. Each node is divided into two
parts.
• The first part contains the information of the element.
• The second part called the link field contains the address of the
next node in the list.
• To see this more clearly lets look at an example:
CONCEPT ON LINKED LIST
• A linked list is a non-sequential collection of data element
• The data elements in the linked list are not in consecutive
memory locations
• It is a data Structure which consists of group of nodes that
forms a sequence
• It is very common data structure that is used to create tree,
graph and other abstract data types
• Linked list comprise of group or list of nodes in which each
node have link to next node to form a chain
• A linked list is a linear collection of data-elements, called
‘nodes’
Applications of Linked List
• In System Programming
• In operating System
• Used in radix and bubble sorting
• In a FAT file system, the metadata of a large file is
organized as a linked list of FAT entries
• To model the different ADT for other data structure as
like stack, queue, tree & Graph etc
Cont..
• 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.
Types of linked lists
• Singly linked list
– Begins with a pointer to the first node
– Terminates with a null pointer
– Only traversed in one direction
• Circular, singly linked
– Pointer in the last node points back to the first node
• Doubly linked list
– Two “start pointers” – first element and last element
– Each node has a forward pointer and a backward pointer
• Circular, doubly linked list
– Forward pointer of the last node points to the first node and backward
pointer of the first node points to the last node
• Header Linked List
– Linked list contains a header node that contains information regarding
complete linked list.
The Operations on the Linear Lists
Various operations are:
• Search: This operation involves the searching of an element in the linked
list.
• Additional (Inserting): To add new node to data structure.
• Deletion: To delete a node from data structure.
• Merge: To merge two structures or more to constituting one structure. 5-
Split: To divide data structure to two structures or more.
• Counting: counting some of items or nodes in data structure.
• Copying : copy data of data structure to another data structure.
• Sort : sort items or nodes in data structure according to the value of the
field or set of fields.
• Access : To access from node or item to another one may be need some
of purposes to test or change...etc.
Comparison of Linked List and Array
Advantages of Linked List
• Linked list is dynamic in nature which allocates the
memory when required
• Grow and shrink the linked list during run time
• Memory Utilization
• Insertion and Deletion Operations are quite easy
Disadvantages of Linked List
• Memory Usage-The memory is wasted as pointers
require extra memory for storage
• No element can be accessed randomly; it has to
access each node sequentially
• Time Consuming
• Heap Space Restriction
• Reverse Traversing is difficult
• Data accessing is very slow
• No cache Memory Support
Basic Operations in Linked List
• The basic operations in the linked lists are:
insertion, deletion, searching, display, and deleting an
element at a given key.
• These operations are performed on Singly Linked
Lists as given below −
•Insertion − Adds an element at the beginning of the list.
•Deletion − Deletes an element at the beginning of the list.
•Display − Displays the complete list.
•Search − Searches an element using the given key.
•Delete − Deletes an element using the given key.
Insertion into a Linked List:
• If a node N is to be inserted into the list between nodes
A and B in a linked list named LIST. Its
Case 1: Inserting a Node at the Beginning of a Linked List

• Inserting a Node at the Beginning of a Linked List. Consider the linked


list shown in below figure. Suppose we want to add a new node with
data 9 and add it as the first node of the list
Algorithm
Case 2: Inserting a Node at the End of a Linked List
Algorithm
Deleting a Node from a Linked List:
• We will discuss how a node is deleted from an already
existing linked list. We will consider two cases and then
see how deletion is done in each case.
– Case 1: The first node is deleted.
– Case 2: The last node is deleted.
Case 1: Deleting a First Node from a Linked List
Case 2: Deleting the Last Node from a Linked List
Algorithm
Traversing a Linked List
Suppose we want to traverse LIST in order to process each
node exactly once. The traversing algorithm uses a pointer
variable PTR which points to the node that is currently being
processed. Accordingly, PTR->NEXT points to the next node
to be processed so
PTR=HEAD [ Moves the pointer to the first node of the list]
PTR=PTR->NEXT [ Moves the pointer to the next node in the
list.]
Algorithm
Searching a Linked List:
• Searching a linked list means to find a particular
element in the linked list. So searching means finding
whether a given value is present in the information part
of the node or not.
Algorithm
Search Linked List for insertion and deletion of Nodes:

Both insertion and deletion operations need searching


the linked list.
 To add a new node, we must identify the logical
predecessor (address of previous node) where the new
node is to be inserting.
To delete a node, we must identify the location (addresses)
of the node to be deleted and its logical predecessor
(previous node)
Doubly linked list
A doubly linked list or a two-way linked list is a more
complex type of linked list which contains a pointer to the
next as well as the previous node in the sequence.
Therefore, it consists of three parts—data, a pointer to
the next node, and a pointer to the previous node.
Therefore, it consists of three parts—data, a pointer to
the next node, and a pointer to the previous node.
Circular Linked List
A circular linked list is a linked list in which last element
or node of the list points to first node. For non-empty
circular linked list, there are no NULL pointers. The
memory declarations for representing the circular linked
lists are the same as for linear linked lists. All operations
performed on linear linked lists can be easily extended to
circular linked lists with following exceptions: While
inserting new node at the end of the list, its next pointer
field is made to point to the first node.
End

You might also like