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

DSA-Ch4Linked-Lists

Uploaded by

reemjawedunar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

DSA-Ch4Linked-Lists

Uploaded by

reemjawedunar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Data Structure & Algorithms

Chapter 4:
Linked List
Contents
4.1 Introduction
4.2 Representation of Linked List in memory
4.3 Traversing Linked List
4.4 Searching Linked List
4.5 Memory Allocation, Garbage Collection
4.6 Overflow & Underflow
4.7 malloc() & free()
4.8 Inserting in Linked List
4.9 Deletion in Linked List
4.1 Introduction
• “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 of the
element.
2. The second part called the link field contains the
address of the next node in the list.
4.1 Introduction
• To see this more clearly lets look at an example:
4.1 Introduction
• 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)
4.1 Introduction
• 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 NULL value 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.
Advantages
• List of data can be stored in arrays but linked
structures (pointers) provide several advantages.
• A linked list is appropriate when the number of
data elements to be represented in data
structure is unpredictable.
• It also appropriate when there are frequently
insertions & deletions occurred in the list.
• Linked lists are dynamic, so the length of a list can
increase or decrease as necessary.
Singly Linked Lists and Arrays

Singly linked list Array


Elements are stored in linear Elements are stored in linear
order, accessible with links. order, accessible with an
index.

Do not have a fixed size. Have a fixed size.

Cannot access the previous Can access the previous


element directly. element easily.

No binary search. Binary search.


Normal Way to Draw a Linked List

c a e d b

first
first
N
U
a b c d e L
L

link field of a node, points to next node

data field of a node


4-9
Representation of Linked List in memory
INFO LINK
START
4 1 D 12
2
3
4 A 10
5
6
7
8 C 1

9
10 B 8
11
12 E 0
Representation of Linked List in memory
INFO LINK
START
4 1 D 12
2
3
4 A 10
5
6
7
8 C 1

9
10 B 8
11
12 E 0
TYPES OF LINKED LISTS
1. Singly linked list (Linear linked list / One way list)
2. Doubly linked list (Two way list)
3. Circular linked list
4. Circular doubly linked list
1. Singly linked list

• A singly linked list is one in which all nodes are


linked together in some sequential manner.
• Also called linear linked list.
• It has the beginning and the end.
• The problem with this list is that we cannot access
the predecessor node (previous node) from the
current node.
• That’s why it is called one way list.
• This can be overcome by doubly linked lists.
2. 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.
3. Circular linked list
• Has no beginning and no end.
• A singly linked list can be made circular linked list
by simply storing the address of the very first node
in the link field of the last node.
Circular List
first

BAT CAT EAT FAT GAT HAT

first
last
last

4-18
4. Circular doubly linked list
• A circular doubly linked list is one which has
both the successor pointer and predecessor
pointer in circular manner.
LL Implementation
Struct Node 200 100 300
200
{
int data; data 1 100 2 300 3 0
node* next;
}; next

// insert node n
n n n n
node* n;
node* t; t
t tt t t
node* h;
h
h

n=new node; N=new node; 1 2 3


n=->data=3; 4
n->data=1;
T->next=n;
t=n; next next next
T=t->next; next
h=n;
N=new node;
N=new node;
n=->data=4;
n=->data=2;
T->next=n;
T->next=n;
n->next=0;
T=t->next;
Operations on Linked List:
• There are several operations associated with linked
list i.e.
1. Traversing a Linked List
2. Searching a Linked List:
3. Insertion into a Linked List:
4. Inserting a new node in list:
5. Delete a node from list:
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.

• Forward Traversing:
– traversing from the very first node towards the last node.
• Reverse Traversing:
– traversing from the very last node towards the first node.
Traversing Algorithm
Traversing a SLL (animation)

PTR

START

one two three

24
Searching:
• The process of finding a particular node of an
linked list is called Searching”.
• Two algorithms for searching:
– Unsorted linked list searching algorithm
– Sorted linked list searching algorithm
Sorted Linked List Searching Algorithm
20 16 12 5 3 1 x

< for descending


> For ascending
1.void search()
2.{ struct node *ptr;
3. int item,i=0,flag; ptr = head;
4. if(ptr == NULL)
5. { printf("\nEmpty List\n");
6. }
7. else
8. { printf("\nEnter item which you want to search?\n");
9. scanf("%d",&item);
10. while (ptr!=NULL)
11. { if(ptr->data == item)
12. {
13. printf("item found at location %d ",i+1); flag=0;
14. }
15. else
16. { flag=1; }
17. i++;
18. ptr = ptr -> next;
19. }
20. if(flag==1)
21. { printf("Item not found\n");
22. } }}

Fore more details visit: https://www.javatpoint.com/searching-in-singly-linked-list


Garbage Collection: Free Storage List
• Providing unused memory blocks for new nodes.

• Some mechanism is required where memory of deleted


nodes becomes available for future use.

• Together with the linked list in memory, a special list is


maintained which consist of unused memory cells.

• This list is called “list of available space” or “free storage list”


or “the free pool” .
Garbage Collection
• The OS of a computer periodically collect all the
deleted space onto the free storage list.
• Any technique with does this collection is called
garbage collection.
• It usually takes place in two steps.
• First computer run through all the lists, tagging
those cells which are currently in use, and then the
computer runs through 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
storage list is empty.
• This situation is called overflow.
• The term underflow refers to the situation where
one wants to delete data from a data structure that
is empty.
Memory Allocation
(a) Static memory allocation
• Compile-time allocation using arrays
• The required amount of memory is allocated to the program
elements at the start of the program.
• Here the memory to be allocated to the variable is fixed
(b) Dynamic / Runtime memory allocation
• Run-time allocation using pointers
• allows us to be able to get the required portion of memory
at runtime (or we say as the need arises)
• best suited type of allocation where we do not know the
memory requirement in advance
New & Delete Operators
• Dynamic memory is allocated using operator new.
• new is followed by a data type specifier.
• If a sequence of more than one element is required,
the number of these within brackets [].
• It returns a pointer to the beginning of the new block
of memory allocated.
• Its syntax is:
pointer = new type  ptr = new int;
pointer = new type [num_of_elem]  ptr = new int[10];
New & Delete Operators
• In most cases, memory allocated dynamically is only
needed during specific periods of time within a program.
• Once it is no longer needed, it can be freed so that the
memory becomes available again for other requests of
dynamic memory.
• This is the purpose of operator delete, whose syntax is:

1. delete pointer;
2. delete[] pointer;
• Back to Linked List
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 beginning of a linked list
• At the end of a linked list
• At the specified position in a linked list.
• If the list itself is empty, then the new node is
inserted as a first node.
Insertion at the beginning
Inserting at the beginning of the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the
single linked list...
•Step 1: Create a newNode with given value.
•Step 2: Check whether list is Empty (head == NULL)
•Step 3: If it is Empty then,
set newNode→next = NULL and head = newNode.
•Step 4: If it is Not Empty then,
set newNode→next = head and head = newNode.
Inserting at Given Position
Algorithm of Insertion at Given Position
Inserting At Specific location in the list (After a Node)
We can use the following steps to insert a new node after a node in the single
linked list...
•Step 1: Create a newNode with given value.
•Step 2: Check whether list is Empty (head == NULL)
•Step 3: If it is Empty then, set newNode →
next = NULL and head = newNode.
•Step 4: If it is Not Empty then, define a node pointer temp and initialize
with head.
•Step 5: Keep moving the temp to its next node until it reaches to the node after
which we want to insert the newNode (until temp1 → data is equal to location,
here location is the node value after which we want to insert the newNode).
•Step 6: Every time check whether temp is reached to last node or not. If it is
reached to last node then display 'Given node is not found in the list!!!
Insertion not possible!!!' and terminate the function. Otherwise move
the temp to next node.
•Step 7: Finally, Set 'newNode → next = temp → next' and 'temp →
next = newNode'
Insertion at Last of Linked List

Algorithm of insertion at last of linked list is home work for


you.
Inserting At End of the list
We can use the following steps to insert a new node at end of the single linked
list...
•Step 1: Create a newNode with given value and newNode → next as NULL.
•Step 2: Check whether list is Empty (head == NULL).
•Step 3: If it is Empty then, set head = newNode.
•Step 4: If it is Not Empty then, define a node pointer temp and initialize
with head.
•Step 5: Keep moving the temp to its next node until it reaches to the last node in
the list (until temp → next is equal to NULL).
•Step 6: Set temp → next = newNode.
Deletion:

• This operation is used to delete a node from the


linked list. A node may be deleted from the
• Beginning of a linked list
• End of a linked list.
• Specified position in the linked list.
• Delete first node Algorithm
Let the node to be deleted is del.

• Step 1: IF HEAD = NULL


• Write UNDERFLOW
Go to Step 5
[END OF IF]
• Step 2: SET PTR = HEAD
• Step 3: SET HEAD = HEAD -> NEXT
• Step 4: FREE PTR
• Step 5: EXIT
Deletion at Beginning of Linked List

Algorithm of deletion at beginning of linked list is home


work for you.
Deletion at Given Position
Algorithm of Deletion at Given Position
• DEL(INFO, LINK, START, AVAIL, LOC, LOCP)
• This algorithm deletes the node N with location LOC. LOCP is the location of
the node which leads N or, When N is the first node. LOCP = NULL
1. If LOCP = NULL then:
Set START :=LINK[START]. [Deletes first node].
Else:
Set LINK[LOCP] := LINK[LOC].[Deletes node N]
[End of If structure]
2. [Return deleted node to the AVAIL List]
Set LINK[LOC] := AVAIL.
3. Exit.
Deletion at lastof Linked List

Algorithm of deletion at last of linked list is home work for


you.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy