Linkedlist

You might also like

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

 

LINKED LIST
 Linked List

 A linked list is a linear data structure that includes a series of


connected nodes. Here, each node stores the data and
the address of the next node.
 A linked list is a sequence of data structures, which are
connected together via links.
 Linked List is a sequence of links which contains items. Each
link contains a connection to another link. Linked list is the
second most-used data structure after array.
 Linked List

 Following are the important terms to understand the


concept of Linked List.
 Link − Each link of a linked list can store a data called an element.
 Next − Each link of a linked list contains a link to the next link called
Next.
 LinkedList − A Linked List contains the connection link to the first
link called First.
 Linked List

 For example
 Linked List

 You have to start somewhere, so we give the address of the


first node a special name called HEAD. Also, the last node in
the linked list can be identified because its next portion points
to NULL.
Representation of Linked List

 Let's see how each node of the linked list is


represented. Each node consists:
 A data item
 An address of another node
Basic 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.
Advantages of Singly linked list
 There are several points about singly linked list that makes it an important data

structure.

 Singly linked list is probably the most easiest data structure to implement.

 Insertion and deletion of element can be done easily.

 Insertion and deletion of elements doesn't requires movement of all elements when

compared to an array.

 Requires less memory when compared to doubly, circular or doubly circular linked list.

 Can allocate or deallocate memory easily when required during its execution.

 It is one of most efficient data structure to implement when traversing in one direction is

required.
Array Linked list

An array is a collection of elements of a similar data A linked list is a collection of objects known as a node
type. where node consists of two parts, i.e., data and address.

Array elements store in a contiguous memory location. Linked list elements can be stored anywhere in the
memory or randomly stored.

Array works with a static memory. Here static memory The Linked list works with dynamic memory. Here,
means that the memory size is fixed and cannot be dynamic memory means that the memory size can be
changed at the run time. changed at the run time according to our requirements.

Array elements are independent of each other. Linked list elements are dependent on each other. As
each node contains the address of the next node so to
access the next node, we need to access its previous
node.

Array takes more time while performing any operation Linked list takes less time while performing any
like insertion, deletion, etc. operation like insertion, deletion, etc.
Accessing any element in an array is faster Accessing an element in a linked list is
as the element in an array can be directly slower as it starts traversing from the first
accessed through the index. element of the linked list.

In the case of an array, memory is allocated In the case of a linked list, memory is
at compile-time. allocated at run time.

Memory utilization is inefficient in the array. Memory utilization is efficient in the case of
For example, if the size of the array is 6, and a linked list as the memory can be allocated
array consists of 3 elements only then the or deallocated at the run time according to
rest of the space will be unused. our requirement.
Types of Linked Lists

 Simple Linked List − Item navigation is forward only.


 Doubly Linked List − Items can be navigated forward and
backward.
 Circular Linked List − Last item contains link of the first
element as next and the first element has a link to the last
element as previous.
What is a Singly Linked List?

 Singly Linked List is a linear and unidirectional data structure,


where data is saved on the nodes, and each node is connected
via a link to its next node. Each node contains a data field and
a link to the next node. Singly Linked Lists can be traversed
in only one direction, whereas Doubly Linked List can be
traversed in both directions.
One way chain or singly linked list

 One way chain or singly linked list can be traversed only in


one direction. In other words, we can say that each node
contains only next pointer, therefore we can not traverse the
list in the reverse direction.
Node is represented as:

struct node

int data;

struct node *next;

}
Example

 Consider an example where the marks obtained by


the student in three subjects are stored in a linked
list as shown in the figure.
Example

 In the above figure, the arrow represents the links. The


data part of every node contains the marks obtained by
the student in the different subject. The last node in the
list is identified by the null pointer which is present in
the address part of the last node. We can have as many
elements we require, in the data part of the list.
Operations of Singly Linked List

 Traversal - access each element of the linked list

 Insertion - adds a new element to the linked list

 Deletion - removes the existing elements Search - find a node

in the linked list

 Sort - sort the nodes of the linked list


A three-member singly linked list can be created as:
Example

 5 Random Names of your Class Fellows


 Salaman , Bilal , Osama , Noman , Navid
 Info[1] , info[2 ] , [3 ] , [4], [5]
 Alphabetic listing of The Names
 Bilal , Navid , Noman , Osama , Slaman
Example

 Start & Link


 Info [2] , Bilal, so assign START= 2
 Info [5] , Navid, so assign Link[2]=5
 Info [4] , Noman, so assign Link[5]= 4
 Info [3] , Osama , so assign Link[4]= 3
 Info [1] , Salman, so assign Link[3]= Null
Start Info Links
2
1 Salaman 0 / Null

2 Bilal 5

3 Osama 1

4 Noman 3

5 Navid 4
Insertion in 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
 struct node *newNode;
 newNode = malloc(sizeof(struct node));
 newNode->data = 4;
 newNode->next = head;
 head = newNode;
Delete from a Linked List
 Delete from beginning
 Point head to the second node
 head = head->next;
 Delete from end
 Traverse to second last element
 Change its next pointer to null
 struct node* temp = head;
 while(temp->next->next!=NULL){
 temp = temp->next;
 }
 temp->next = NULL;
Doubly Linked List

 A Doubly Linked List (DLL) contains an extra pointer,


typically called the previous pointer, together with the next
pointer and data which are there in the singly linked list.
 We add a pointer to the previous node in a doubly-linked list.
Thus, we can go in either direction: forward or backward.
Doubly Linked List
Following are the important terms to understand the
concept of doubly linked list.

 Link − Each link of a linked list can store a data called an element.
 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.
 LinkedList − A Linked List contains the connection link to the first
link called First and to the last link called Last.
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.
Advantages of DLL over the singly linked list:

 A DLL can be traversed in both forward and backward directions. 


 The delete operation in DLL is more efficient if a pointer to the node
to be deleted is given. 
 We can quickly insert a new node before a given node. 
 In a singly linked list, to delete a node, a pointer to the previous node
is needed. To get this previous node, sometimes the list is traversed.
In DLL, we can get the previous node using the previous pointer. 
Disadvantages of DLL over the singly linked list:

 Every node of DLL Requires extra space for a previous pointer.


It is possible to implement DLL with a single pointer though
 All operations require an extra pointer previous to be
maintained. For example, in insertion, we need to modify
previous pointers together with the next pointers. For example
in the following functions for insertions at different positions,
we need 1 or 2 extra steps to set the previous pointer.
A node is represented as

struct node

{ int data;

struct node *next;

struct node *prev;

}
A three-member doubly linked list
Example
Example
Insertion in DLL
Algorithm

 Step 1: IF ptr = NULL


   Write OVERFLOW
 Go to Step 9
 [END OF IF]
 Step 2: SET NEW_NODE = ptr
 Step 3: SET ptr = ptr -> NEXT
 Step 4: SET NEW_NODE -> DATA = VAL
 Step 5: SET NEW_NODE -> PREV = NULL
 Step 6: SET NEW_NODE -> NEXT = START
 Step 7: SET head -> PREV = NEW_NODE
 Step 8: SET head = NEW_NODE
 Step 9: EXIT
Deletion at beginning
 Algorithm
 STEP 1: IF HEAD = NULL
 WRITE UNDERFLOW
 GOTO STEP 6
 STEP 2: SET PTR = HEAD
 STEP 3: SET HEAD = HEAD → NEXT
 STEP 4: SET HEAD → PREV = NULL
 STEP 5: FREE PTR
 STEP 6: EXIT
Circular Linked List

 The circular linked list is a linked list where all nodes are


connected to form a circle. In a circular linked list, the first
node and the last node are connected to each other which
forms a circle. There is no NULL at the end.
The following image shows a circular singly linked
list.
Two types of circular linked lists:

 Circular singly linked list: In a circular Singly linked list, the


last node of the list contains a pointer to the first node of the
list. We traverse the circular singly linked list until we reach
the same node where we started. The circular singly linked list
has no beginning or end. No null value is present in the next
part of any of the nodes.
Circular singly linked list
Circular Doubly linked list

 Circular Doubly Linked List has properties of both


doubly linked list and circular linked list in which two
consecutive elements are linked or connected by the
previous and next pointer and the last node points to
the first node by the next pointer and also the first
node points to the last node by the previous pointer.
Circular Doubly linked list
Circular linked

 Circular linked list are mostly used in task maintenance in


operating systems. There are many examples where circular
linked list are being used in computer science including
browser surfing where a record of pages visited in the past by
the user, is maintained in the form of circular linked lists and
can be accessed again on clicking the previous button.
Single node is represented as

 struct Node
 {
 int data;
 struct Node * next;
 }
 ;
Assignment

 Explain Data Structure, Algorithm, Array & linked


list with the Help of Examples
 Selection, Insertion and Bubble Sort Algorithms
with the Help of Examples
 30th November 2022

You might also like