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

2.linked List

The document discusses linked lists, which are data structures used to store collections of data. It describes the properties of linked lists and basic operations like traversing, inserting, and deleting nodes from singly linked and circular linked lists. Time and space complexities of various linked list operations are also provided.

Uploaded by

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

2.linked List

The document discusses linked lists, which are data structures used to store collections of data. It describes the properties of linked lists and basic operations like traversing, inserting, and deleting nodes from singly linked and circular linked lists. Time and space complexities of various linked list operations are also provided.

Uploaded by

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

2.

LINKED LISTS
UPASANA TALUKDAR
CSE
WHAT IS A LINKED LIST?

• A linked list is a data structure used for storing collections of data. A linked list has the
following properties.
• • Successive elements are connected by pointers
• • The last element points to NULL
• • Can grow or shrink in size during execution of a program
• • Can be made just as long as required (until systems memory exhausts)
• • Does not waste memory space (but takes some extra memory for pointers). It allocates
memory as list grows.
LINKED LISTS ADT

• Main Linked Lists Operations


• • Insert: inserts an element into the list
• • Delete: removes and returns the specified position element from the list
• Auxiliary Linked Lists Operations
• • Delete List: removes all elements of the list (disposes the list)
• • Count: returns the number of elements in the list
• • Find nth node from the end of the list
WHY LINKED LISTS?

• The advantage of linked lists is that they can be expanded in constant time. To create an
array, we must allocate memory for a certain number of elements.
• To add more elements to the array when full, we must create a new array and copy the
old array into the new array. This can take a lot of time.
• We can prevent this by allocating lots of space initially but then we might allocate more
than we need and waste memory.
• With a linked list, we can start with space for just one allocated element and add on new
elements easily without the need to do any copying and reallocating.
SINGLY LINKED LISTS

• Generally “linked list” means a singly linked list. This list consists of a number of nodes in
which each node has a next pointer to the following element.
• The link of the last node in the list is NULL, which indicates the end of the list.
BASIC OPERATIONS ON A LIST

• • Traversing the list


• • Inserting an item in the list
• • Deleting an item from the list
TRAVERSING THE LIST

Follow the pointers.


• Display the contents of the nodes (or count) as they are traversed.
• Stop when the next pointer points to NULL.

Time Complexity: O(n), for scanning the list of size n.


Space Complexity: O(1), for creating a temporary variable.
TRAVERSING THE LIST

1. Set PTR:=START [Initializes ptr]


2. Repeat steps 3 and 4 while PTR!=NULL
3. Apply PROCESS to INFO[PTR]
4. Set PTR:=LINK[PTR]
5. Exit
SINGLY LINKED LIST INSERTION

• Insertion into a singly-linked list has three cases:


• • Inserting a new node before the head (at the beginning)
• • Inserting a new node after the tail (at the end of the list)
• • Inserting a new node at the middle of the list (random location)
INSERTING A NODE IN SINGLY LINKED LIST AT
THE BEGINNING

• In this case, a new node is inserted before the current head node. Only one next pointer
needs to be modified (new node’s next pointer) and it can be done in two steps:
• • Update the next pointer of new node, to point to the current head.
• • Update head pointer to point to the new node.
INSERTING A NODE IN SINGLY LINKED LIST AT
THE BEGINNING

• [OVERFLOW?], Write:OVERFLOW and Exit.


• [Remove first node from AVAIL list]
• Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
• Set INFO[NEW]:=ITEM [Copies new data into new node]
• Set LINK[NEW]:=START [Changes START so it points to the new node]
• Set START:=NEW
• EXIT
INSERTING A NODE IN SINGLY LINKED LIST AT
THE ENDING

• In this case, we need to modify two next pointers (last nodes next pointer and new nodes
next pointer).
• • New nodes next pointer points to NULL.
• • Last nodes next pointer points to the new node.
INSERTING A NODE IN SINGLY LINKED LIST AT
THE MIDDLE

• Let us assume that we are given a position where we want to insert the new node. In this
case also, we need to modify two next pointers.
• • If we want to add an element at position 3 then we stop at position 2. That means we
traverse 2 nodes and insert the new node. For simplicity let us assume that the second
node is called position node. The new node points to the next node of the position where
we want to add this node.
• Position node’s next pointer now points to the new node.
INSERTING A NODE IN SINGLY LINKED LIST AT
THE MIDDLE
We can implement the three
variations of the insert operation
separately.
Time Complexity: O(n), since, in
the worst case, we may need to
insert the node at the end of the
list.
Space Complexity: O(1), for
creating one temporary variable.
INSERTING AFTER A GIVEN NODE

• [OVERFLOW?], Write:OVERFLOW and Exit.


• [Remove first node from AVAIL list]
• Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]
• Set INFO[NEW]:=ITEM
• If LOC = NULL, then [Insert as first node]
• Set LINK[NEW]:=START and Set START:=NEW
Else [Insert after node with location LOC]
• Set LINK[NEW]:=LINK[LOC] and LINK[LOC] =NEW
• EXIT
SINGLY LINKED LIST DELETION

• Similar to insertion, here we also have three cases.


• • Deleting the first node
• • Deleting the last node
• • Deleting an intermediate node.
DELETING THE FIRST NODE IN SINGLY LINKED
LIST

• First node (current head node) is removed from the list. It can be done in two steps:
• Create a temporary node which will point to the same node as that of head.
• Now, move the head nodes pointer to the next node and dispose of the temporary
node.
DELETING THE FIRST NODE IN SINGLY LINKED
LIST
DELETING THE LAST NODE IN SINGLY LINKED
LIST

• In this case, the last node is removed from the list. This operation is a bit trickier than
removing the first node, because the algorithm should find a node, which is previous to
the tail. It can be done in three steps:
• • Traverse the list and while traversing maintain the previous node address also. By the
time we reach the end of the list, we will have two pointers, one pointing to the tail node
and the other pointing to the node before the tail node.
• Update previous node’s next pointer with NULL.
• Dispose of the tail node.
DELETING THE LAST NODE IN SINGLY LINKED
LIST
DELETING AN INTERMEDIATE NODE IN SINGLY
LINKED LIST

• In this case, the node to be removed is always located between two nodes. Head and tail
links are not updated in this case. Such a removal can be done in two steps:
• • Similar to the previous case, maintain the previous node while traversing the list.
• Once we find the node to be deleted, change the previous node’s next pointer to the
next pointer of the node to be deleted.
• Dispose of the current node to be deleted.
• Time Complexity: O(n). In the worst case, we may need to delete the node at the end of
the list. Space Complexity: O(1), for one temporary variable.
DELETING AN INTERMEDIATE NODE IN SINGLY
LINKED LIST
DELETION

• [UNDERFLOW?], Write:UNDERFLOW and Exit.


If LOCP=NULL, then
• Set START:=LINK[START] [Delete the first node]
Else
• Set LINK[LOCP]:=LINK[LOC] [Delete the node N]
End
• Set LINK[LOC]:=AVAIL and AVAIL:=LOC [Return deleted node to AVAIL list]
• EXIT
EXAMPLE

• The following list of names is assigned to a linear array INFO:


Mary, June, Barbara, Paula, Diana, Audrey, Karen, Nancy, Ruth, Eileen, Sandra, Helen
That is, INFO[1]=Mary, INFO[2]=June,……,INFO[12]=Helen. Assign values to an array
LINK and a variable START so that INFO, LINK and START form an alphabetical listing of
the names.
CIRCULAR LINKED LISTS

• In singly linked, the end of lists are indicated with NULL value. But circular linked lists do
not have ends.
• While traversing the circular linked lists we should be careful; otherwise we will be
traversing the list infinitely.
• In circular linked lists, each node has a successor. Note that unlike singly linked lists, there
is no node with NULL pointer in a circularly linked list. In some situations, circular linked
lists are useful.
COUNTING NODES IN A CIRCULAR LINKED
LIST
• The circular list is accessible through the node marked head. To count the nodes, the list
has to be traversed from the node marked head, with the help of a dummy node current,
and stop the counting when current reaches the starting node head.

1. Set PTR:=LINK[START]
[Initializes ptr]
2. Repeat steps 3 and 4
while PTR!=START
3. Apply PROCESS to
INFO[PTR]
4. Set PTR:=LINK[PTR]
5. Exit
INSERTING A NODE AT THE END OF A
CIRCULAR LINKED LIST
• Create a new node and initially keep its next pointer pointing to itself.
• Update the next pointer of the new node with the head node and also traverse the list
to the tail. That means in a circular list we should stop at the node whose next node is
head.
• Update the next pointer of the previous node to point to the new node and we get the
list as shown below.
INSERTING A NODE AT THE END OF A
CIRCULAR LINKED LIST
INSERTING A NODE AT THE FRONT OF A
CIRCULAR LINKED LIST
• Create a new node and initially keep its next pointer pointing to itself.
• Update the next pointer of the new node with the head node and also traverse the list
until the tail. That means in a circular list we should stop at the node which is its previous
node in the list.
• Update the previous head node in the list to point to the new node.
• Make the new node as the head.
INSERTING A NODE AT THE FRONT OF A
CIRCULAR LINKED LIST
INSERTING A NODE AT THE FRONT OF A
CIRCULAR LINKED LIST
DELETING THE LAST NODE IN A CIRCULAR
LINKED LIST
• Traverse the list and find the tail node and its previous node.
• Update the next pointer of tail node’s previous node to point to head.
• Dispose of the tail node.
DELETING THE LAST NODE IN A CIRCULAR
LINKED LIST
DELETING THE FIRST NODE IN A CIRCULAR
LIST
• Find the tail node of the linked list by traversing the list. Tail node is the previous node to
the head node which we want to delete.
• Create a temporary node which will point to the head. Also, update the tail nodes next
pointer to point to next node of head.
• Now, move the head pointer to next node. Create a temporary node which will point to
head. Also, update the tail nodes next pointer to point to next node of head
DELETING THE FIRST NODE IN A CIRCULAR
LIST
DOUBLY LINKED LISTS

• The advantage of a doubly linked list (also called two – way linked list) is that given a node
in the list, we can navigate in both directions. A node in a singly linked list cannot be
removed unless we have the pointer to its predecessor. But in a doubly linked list, we can
delete a node even if we don’t have the previous node’s address (since each node has a
left pointer pointing to the previous node and can move backward).
• The primary disadvantages of doubly linked lists are:
• Each node requires an extra pointer, requiring more space.
• The insertion or deletion of a node takes a bit longer (more pointer operations).
INSERTING A NODE IN DOUBLY LINKED LIST AT
THE BEGINNING

• In this case, new node is inserted before the head node. Previous and next pointers need
to be modified and it can be done in two steps:
• Update the right pointer of the new node to point to the current head node (dotted link in
below figure) and also make left pointer of new node as NULL.
• Update head node’s left pointer to point to the new node and make new node as head.
INSERTING A NODE IN DOUBLY LINKED LIST
AT THE ENDING
• In this case, traverse the list till the end and insert the new node.
• New node right pointer points to NULL and left pointer points to the end of the list.
• Update right pointer of last node to point to new node.
INSERTING A NODE IN DOUBLY LINKED LIST
AT THE MIDDLE
• New node right pointer points to the next node of the position node where we want to
insert the new node. Also, new node left pointer points to the position node.
• Position node right pointer points to the new node and the next node of position node
left pointer points to new node.
INSERTING A NODE IN DOUBLY LINKED LIST
AT THE MIDDLE
INSERTION (FORMAL STATEMENT)

• [OVERFLOW?], Write:OVERFLOW and Exit.


• [Remove first node from AVAIL list]
• Set NEW:=AVAIL and AVAIL:=FORW[AVAIL], Set INFO[NEW]:=ITEM
• Set FORW[LOCA]:= NEW, FORW[NEW]:=LOCB, BACK[LOCB]:=NEW,
BACK[NEW]:=LOCA.
• EXIT
DELETING THE FIRST NODE IN DOUBLY
LINKED LIST
• In this case, the first node (current head node) is removed from the list. It can be done in
two steps:
• Create a temporary node which will point to the same node as that of head.
• Now, move the head nodes pointer to the next node and change the heads left pointer to
NULL. Then, dispose of the temporary node.
DELETING THE FIRST NODE IN DOUBLY
LINKED LIST
DELETING THE LAST NODE IN DOUBLY
LINKED LIST
• Traverse the list and while traversing maintain the previous node address also. By the
time we reach the end of the list, we will have two pointers, one pointing to the tail and
the other pointing to the node before the tail.
• Update the next pointer of previous node to the tail node with NULL.
• • Dispose the tail node.
DELETING THE LAST NODE IN DOUBLY
LINKED LIST
DELETING AN INTERMEDIATE NODE IN
DOUBLY LINKED LIST
• Similar to the previous case, maintain the previous node while also traversing the list.
Upon locating the node to be deleted, change the previous node’s next pointer to the
next node of the node to be deleted.
• Dispose of the current node to be deleted.
DELETING AN INTERMEDIATE NODE IN
DOUBLY LINKED LIST
DELETION (FORMAL STATEMENT)

• [UNDERFLOW?], Write:UNDERFLOW and Exit.


• Set FORW[BACK[LOC]]:=FOR[LOC] and BACK[FORW[LOC]]:=BACK[LOC]
• Set FORW[LOC]:=AVAIL and AVAIL:=LOC [Return deleted node to AVAIL list]
• EXIT

You might also like