Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Linked Lists

GROUP 13

Describe how a Linked List


works. Use diagrams. Include
Traversal methods.
Definition

  a linear collection of data elements whose order is not given by their physical
placement in memory. Instead, each element points to the next. It is a data
structure consisting of a collection of nodes which together represent a
sequence.

Purpose
 Linked lists are linear data structures that hold data in individual objects
called nodes. These nodes hold both the data and a reference to the next
node in the list. Linked lists are often used because of their efficient
insertion and deletion.
Example of a linked list
 For instance in the below fig1, we can see that the node with the address of 3600 has
the address pointer of the next node which in this case is 4000.
 The content part stores the data type that has been given.
 The data types can be integers, floats, strings or characters but has to be of the same
type.
 The content/data field holds the actual data associated with list item. Link field
contains address of next data item.
 The pointer field points to the next node in the list.
 The head pointer contains the address of the first node (base address) so it cannot be
named a head node.
 So if you want to access the 5th thing, you can’t do linked list [5], instead you would
have to iterate over the list from the beginning and then stop when the number hits 5
Advantages of a linked list

 They do not waste memory as memory is not pre allocated thus memory is
only allocated when it is needed and is also de-allocated when no longer
needed.
 Linked lists provide flexibility in inserting a data item at a specified position
and deletion of the data item from the given position thus insertion and
deletions are easier and efficient.
 Linked lists are dynamic data structures thus they can grow or shrink during
the execution of a program.
Disadvantages of a linked list

 It takes up more space as every node requires an additional pointer to store


address of the next node.
 It is difficult and time consuming to search a particular element in a linked
list.
Types of linked lists

 Single linked list


 Doubly linked list
 Circular linked list
 Doubly circular linked list
Single linked list

 The singly linked list which can also be called a linked list is a linear data structure in
which each element of the list contains a pointer which points to the next element in
the list.
 Each element in the singly linked list is called a node. Each node has two components:
data and a pointer next which points to the next node in the list.
 The first node of the list is called as head, and the last node of the list is called the
tail.
 The last node of the list contains a pointer to the null.
 Each node in the list can be accessed linearly by traversing through the list from head
to tail. Only forward traverse is possible and not backward traverse or we say forward
sequential movement is possible.
Doubly linked list

 A doubly linked list is another type of the linked list.


 It is called a doubly linked list because it contains two address while a singly
linked list because it contains two address while a singly linked list contains a
single address.
 It is a list that has a total of three parts, one is a data type, and others two
are the pointers, i.e. previous and next.
 The previous pointer holds the address of the next node. Therefore we can
say that list has two references, i.e. forward and backward reference to
traverse in either location.
Circular linked list

 A circular linked list data structure is formed to store the collection of


elements by pointing the start element’s location to the next of the last
element, which is known to form a circular linked list.
 Most operating system operators are required to be performed repeatedly,
thus needing to be maintained in a queue where it can wait if any of the
required resources are currently acquired by other ongoing processes.
 Instead of maintaining first and the last pointer, only one pointer need to be
maintained head to detect the visited node.
Doubly Circular linked list

 A circular double linked list has both successor pointer and predecessor
pointer in circular manner.
 The objective behind considering circular double linked list is to simplify the
insertion and deletion operations performed on double linked list.
 In circular double linked lit the right link of the right most node points back
to the start node and left link of the first node points to the last node.
Operations of a linked list

 In a linked list there are five operations that we can do namely


 Traversal
 Insertion
 Deletion
 Searching an Element
 Sorting a linked
Traversal

 In this process we will be visiting each node of the list once in order to
perform some operation on that and this is done when we may want to print
the list or search for a specific node in the list.
Insertion
 Insertion is a process where we add new data into our list and you can add
elements to either the beginning, middle or end of the linked list.
 Insert at the beginning: Allocate memory for new node, store data, change
next of new node to point to head, change head to point to recently created
node.
 Insert at the End: Allocate memory for new node, store data, traverse to last
node and change next of last node to recently created node.
 Insert at the Middle: Allocate memory and store data for new node, traverse
to node just before the required position of new node, change next pointers
to include new node in between
Insertion at the beginning
Insertion at the middle
Insertion at the end
Deletion

 Deletion is the process where we remove unwanted data from our list
and one can delete either from the beginning, end or from a
particular position.
 Delete from beginning: Point head to the second node
 Delete from end: Traverse to second last element, change its next
pointer to null
 Delete from middle: Traverse to element before the element to be
deleted, change next pointers to exclude the node from the chain
Deletion at the first node
Deletion at the middle
Deletion at the last
Searching an element in a linked list

 This is done in order to find specific items of our data in the list. You can
search an element on a linked list using a loop using the following steps. We
are finding item on a linked list.
 Make head as the current node.
 Run a loop until the current node is null because the last element points to
null.
 In each iteration, check if the key of the node is equal to item. If it the key
matches the item, return true otherwise return false
Sorting elements in a linked list
 This is a process where we set or arrange our data in the data structure in
either ascending or descending order. This is done in order to view or analyze
our data more clearly. We will use a simple sorting algorithm, Bubble Sort, to
sort the elements of a linked list in ascending order below.
1. Make the head as the current node and create another node index for later use
2. If head is null, return.
3. Else, run a loop till the last node (i.e. null).
4. In each iteration, follow the following step 5-6.
5. Store the next node of current in index.
6. Check if the data of the current node is greater than the next node. If it is
greater, swap current and index

You might also like