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

Chapter III

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

Chapter III

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

Chapter-3

Introduction to Linked Lists


Outlines:
Linked lists.
Nodes in data structure.
Difference between array and linked lists
Types of Linked lists
Operations performed on each linked lists.
Conclusion
• Linked List is a linear data structure which consists of group of nodes
in a sequence.
• Each node holds its own data and the address of the next node hence
forming a chain like structure.
• The elements in a linked list are linked using pointers
• Linked List is a sequence of links which contains items.
What is a Node?
• A Node in a linked list holds the data value and the pointer which
points to the location of the next node in the linked list.
• In the picture above we have a linked list, containing 4 nodes, each
node has some data(A, B, C and D) and a pointer which stores the
location of the next node.
Why we need to store the location of the next node?
• Because the memory locations allocated to these nodes are not
contiguous hence each node should know where the next node is
stored.
Important points of linked lists

Following are the important points to of linked lists:


• Linked List contains head.
• Each link carries a data field(s) and a link field called next.
• Each link is linked with its next link using its next link.
• Last link carries a link as null to mark the end of the list
Difference between Array and Linked List
What is Array?
Do you think any similarity and any difference with linked lists?
If yes explain in what manner /how ?
ARRAY LINKED LIST
Array is a collection of elements of similar data Linked List is an ordered collection of elements of same
type. type, which are connected to each other using pointers.

Array supports Random Access, which means Linked List supports Sequential Access, which means to
elements can be accessed directly using their index, access any element/node in a linked list, we have to
like arr[0] for 1st element, arr[6] for 7th element sequentially traverse the complete linked list, upto that
etc. element.
Hence, accessing elements in an array is fast.

In an array, elements are stored in contiguous In a linked list, new elements can be stored anywhere in
memory location or consecutive manner in the the memory.
memory. Address of the memory location allocated to the new
element is stored in the previous node of linked list.

In array, each element is independent and can be In case of a linked list, each node/element points to the
accessed using it's index value. next, previous, or maybe both nodes.
Array vs Linked list cont.…..

Array can single dimensional, two Linked list can


dimensional or multidimensional be Linear(Singly), Doubly or Circular linke
d list.

Size of the array must be specified at time Size of a Linked list is variable. It grows at
of array decalaration. runtime, as more nodes are added to it.
Example
Advantages of Linked Lists

• Linked lists are used to implement stacks, queues, graphs, etc.


• Insertion and deletion operations can be easily implemented.
• It uses to store data in data structure.
Disadvantages of Linked Lists
Disadvantages of Linked Lists of linked lists are the following:
• It needs additional memory because of pointers require extra memory
for storage.
• No element can be accessed randomly; it has to access each node
sequentially.
• Reverse Traversing is difficult in linked list.
Types of Linked Lists

There are 3 different implementations of Linked List available, they are:


• Singly Linked List
• Doubly Linked List
• Circular Linked List
Singly Linked List

• Singly linked lists contain nodes which have a data part as well as an
address part i.e. next, which points to the next node in the sequence of
nodes.
• Item navigation is forward only.
Ex:
operations
• Following are the basic operations supported by a list.
• 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 Operation
• Adding a new node in linked list is a more than one step activity.
• First, create a node using the same structure and find the location
where it has to be inserted.
Deletion Operation
To delete anode in a linked list:
• First, locate the target node to be removed, by using searching
algorithms.
• The left (previous) node of the target node now should point to the
next node of the target node.
Doubly Linked List
• In a doubly linked list, each node contains a data part and two
addresses, one for the previous node and one for the next node.
• Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either forward and backward easily as
compared to Single Linked List.
• Next − Each link of a linked list contains a link to the next link called
Next.
• Prev − Each link of a linked list contains a link to the previous link
called Prev
• Each link carries a data field(s) and two link fields called next and
prev.
• Each link is linked with its next link using its next link.
• Each link is linked with its previous link using its previous link.
• The last link carries a link as null to mark the end of the list.
Basic Operations

• Insertion − Adds an element at the beginning of the list.


• Deletion − Deletes an element at the beginning of the list.
• Insert Last − Adds an element at the end of the list.
• Delete Last − Deletes an element from the end of the list.
• Insert After − Adds an element after an item of the list.
• Delete − Deletes an element from the list using the key.
• Display forward − Displays the complete list in a forward manner.
• Display backward − Displays the complete list in a backward manner
Delete operation
Methods :
• Checking whether Linked List is empty or not.
• Searching any data element in the Linked List
• Deleting a particular Node(data element) from the List
Circular Linked List
• In circular linked list the last node of the list holds the address of the
first node hence forming a circular chain.
• Circular Linked List is a variation of Linked list in which the first
element points to the last element and the last element points to the
first element.
Circular linked lists
• Both Singly Linked List and Doubly Linked List can be made into a
circular linked list.
Singly Linked List as Circular:
• In singly linked list, the next pointer of the last node points to the first
node.
Doubly Linked List as Circular:
• In doubly linked list, the next pointer of the last node points to the first
node and the previous pointer of the first node points to the last node
making the circular in both directions.
Basic Operations of circular list

Following are the important operations supported by a circular list:


• Insert − Inserts an element at the start of the list.
• Delete − Deletes an element starting from the start of the list.
• Display − Displays the list.
Linked list declaration-sample fragment code
End

You might also like