KEMBAR78
Implementation of linked list unit 2.pptx
Lists
• defined as a collection of variable number of data items called nodes.
• most commonly used non-primitive data structures.
• Each nodes are divided into two parts:
• first part contains the information of the element.
• second part contains the memory address of the next node in the list.
• Also called Link part.
Lists
Types of linked
lists:
◦ Single linked list
◦ Doubly linked list
◦ Circular linked list
Single linked list
•contains two fields in each node – an information field
and the linked field.
•The information field contains the data of that node.
•The link field contains the memory address of the next
node. There is only one link field in each node, the linked
list is called singly linked list.
Doubly linked
list
It is a linked list in which each node is points both to the
next node and also to the previous node.
In doubly linked list each node contains three parts:
◦ FORW : a pointer field that contains the address of the next node
◦ BACK: a pointer field that contains the address of the previous
node.
◦ INFO: It contains the actual data.
In the first node, if BACK contains NULL, it indicated that it is the
first node in the list.
The node in which FORW contains, NULL indicates that the node is
the last node.
circular linked
list
The link field of the last node contains the memory
address of the first node, such a linked list is called
circular linked list.
·In a circular linked list every node is accessible from a
given node.
Operation on Linked List:
The operation that are performed on linked
lists are:
◦ Creating a linkedlist
◦ Traversing a linkedlist
◦ Inserting an item into a linked list.
◦ Deleting an item from the linked list.
◦ Searching an item in the linked list
• In the Figure, variable START is used to store the address of the
First node.
• Here, in this example, START= 1, so the First data is stored at
address 1, which is H.
• The corresponding NEXT stores the address of the next node,
which is 4.
• So, we will look at address 4 to fetch the next data item.
• The second data element obtained from address 4 is E.
• Again, see the corresponding NEXT to go to the next node.
• From the entry in the NEXT, get the next address, that is 7, and
fetch L as the data.
• repeat this procedure until we reach a position where the NEXT
entry contains –1 or NULL,
Algorithm for traversing a linked list
Traversing a linked list means accessing the nodes of the list in order to
perform some processing on them.
Algorithm to search a linked list
Searching a linked list means to find a particular element in the linked list.
finding whether a given value is present in the information part of the node or
not. If it is present, the algorithm returns the address of the node that contains
the value.
• In Step 1, we initialize the pointer variable PTR with START that
contains the address of the first node.
• In Step 2, a while loop is executed which will compare every
node’s DATA with VAL for which the search is being made.
• If the search is successful, that is, VAL has been found, then the
address of that node is stored in POS and the control jumps to
the last statement of the algorithm.
• However, if the search is unsuccessful, POS is set to NULL which
indicates that VAL is not present in the linked list.
Inserting a Node at the Beginning of a Linked List
• In Step 1, we first check whether memory is
available for the new node.
• If the free memory has exhausted, then an
OVERFLOW message is printed.
• Otherwise, if a free memory cell is available, then
we allocate space for the new node. Set its DATA
part with the given VAL and the NEXT part is
initialized with the address of the first node of the
list, which is stored in START.
• Now, since the new node is added as the first node
of the list, it will now be known as the START node,
that is, the START pointer variable will now hold
the address of the NEW_NODE.
the algorithm to insert a new node at the end of a linked list.
In Step 6, we take a pointer variable PTR and initialize it with START.
That is, PTR now points to the first node of the linked list.
In the while loop, we traverse through the linked list to reach the
last node.
Once we reach the last node, in Step 9, we change the NEXT pointer of the
last node to store the address of the new node.
Remember that the NEXTfield of the new node contains NULL, which
signifies the end of the linked list.
• In Step 5, we take a pointer variable PTR and initialize it with START.
• That is, PTR now points to the first node of the linked list.
• Then we take another pointer variable PREPTR which will be used to store the
address of the node preceding PTR.
• Initially, PREPTR is initialized to PTR. So now, PTR,PREPTR, and START are all
pointing to the first node of the linked list.
• In the while loop, we traverse through the linked list to reach the node that
has its value equal to NUM.
• We need to reach this node because the new node will be inserted after this
node.
• Once we reach this node, in Steps 10 and 11, we change the NEXT pointers in
such a way that new node is inserted after the desired node.
• In Step 5, we take a pointer variable PTR and initialize it with START.
• That is, PTR now points to the first node of the linked list.
• Then, we take another pointer variable PREPTR and initialize it with PTR.
• So now,PTR, PREPTR, and START are all pointing to the first node of the
linked list.
• In the while loop, we traverse through the linked list to reach the node
that has
its value equal to NUM.
• We need to reach this node because the new node will be inserted before
this node.
Once we reach this node, in Steps 10 and 11, we change the NEXT pointers in
such a way that the new node is inserted before the desired node.
Deleting a Node from a Linked List
Deleting the First Node from a Linked List
to delete the first node from a linked list.
• In Step 1, we check if the linked list exists or not.
• If START = NULL, then it signifies that there are no nodes in the list and the
control is transferred to the last statement of the algorithm.
• However, if there are nodes in the linked list, then we use a pointer
variable PTR that is set to point to the first node of the list.
• For this, we initialize PTR with START that stores the address of the first
node of the list.
• In Step 3, START is made to point to the next node in sequence and finally
the memory occupied by the node pointed by PTR (initially the first node
of the list) is freed
and returned to the free pool
Deleting the Last Node from a Linked List
Algorithm to insert a new node at the beginning
Step 1, we first check whether memory is available for the new node.
• If the free memory has exhausted, then an OVERFLOW message is printed.
•Otherwise, if free memory cell is available, then we allocate space for the new
node.
• Set its DATA part with the given VAL and the NEXT part is initialized with the address
of the First node of the list, which is stored in START.
• Now, since the new node is added as the First node of the list, it will now be known
as the START node,that is, the START pointer variable will now hold the address of the
NEW_NODE.
While inserting a node in a circular linked list, we have to use a while loop to traverse
to the last node of the list. Because the last node contains a pointer to START, its NEXT
Field is updated.
• so that after insertion it points to the new node which will be now known as START.
Algorithm to insert a new node at the end of a circular linked list.
In Step 6,we take a pointer variable PTR and initialize it with
START. That is, PTR now points to the First node of the linked list.
In the while loop, we traverse through the linked list to reach the last
node.
Once we reach the last node, in Step 9, we change the NEXT pointer
of the last node to store the address of the new node.
Remember that the NEXT Field of the new node contains the address
of the first node which is denoted by START.
In Step 1 of the algorithm, we check if the linked list exists or not.
If START = NULL, then it signifies that there are no nodes in the list and the
control is transferred to the last statement of the algorithm.
However, if there are nodes in the linked list, then we use a pointer variable
PTR which will be used to traverse the list to ultimately reach the last node.
In Step 5, we change the next pointer of the last node to point to the second
node of the circular linked list.
In Step 6, the memory occupied by the first node is freed.
Finally, in Step 7, the second node now becomes the first
node of the list and its address is stored in the pointer variable START.
Algorithm to delete the last node from a circular linked list.
In Step 2, we take a pointer variable PTR and initialize it with START. That is,
PTR now points to the first node of the linked list. In the while loop, we take
another pointer variable PREPTR such that PREPTR always points to one node before
PTR. Once we reach the last node and the second last node, we set the next pointer of
the second last node to START, so that it now becomes the (new) last node of the
linked list. The memory of the previous last node is freed.
Thank
you

Implementation of linked list unit 2.pptx

  • 1.
    Lists • defined asa collection of variable number of data items called nodes. • most commonly used non-primitive data structures. • Each nodes are divided into two parts: • first part contains the information of the element. • second part contains the memory address of the next node in the list. • Also called Link part.
  • 2.
    Lists Types of linked lists: ◦Single linked list ◦ Doubly linked list ◦ Circular linked list
  • 3.
    Single linked list •containstwo fields in each node – an information field and the linked field. •The information field contains the data of that node. •The link field contains the memory address of the next node. There is only one link field in each node, the linked list is called singly linked list.
  • 4.
    Doubly linked list It isa linked list in which each node is points both to the next node and also to the previous node. In doubly linked list each node contains three parts: ◦ FORW : a pointer field that contains the address of the next node ◦ BACK: a pointer field that contains the address of the previous node. ◦ INFO: It contains the actual data. In the first node, if BACK contains NULL, it indicated that it is the first node in the list. The node in which FORW contains, NULL indicates that the node is the last node.
  • 5.
    circular linked list The linkfield of the last node contains the memory address of the first node, such a linked list is called circular linked list. ·In a circular linked list every node is accessible from a given node.
  • 6.
    Operation on LinkedList: The operation that are performed on linked lists are: ◦ Creating a linkedlist ◦ Traversing a linkedlist ◦ Inserting an item into a linked list. ◦ Deleting an item from the linked list. ◦ Searching an item in the linked list
  • 8.
    • In theFigure, variable START is used to store the address of the First node. • Here, in this example, START= 1, so the First data is stored at address 1, which is H. • The corresponding NEXT stores the address of the next node, which is 4. • So, we will look at address 4 to fetch the next data item. • The second data element obtained from address 4 is E. • Again, see the corresponding NEXT to go to the next node. • From the entry in the NEXT, get the next address, that is 7, and fetch L as the data. • repeat this procedure until we reach a position where the NEXT entry contains –1 or NULL,
  • 9.
    Algorithm for traversinga linked list Traversing a linked list means accessing the nodes of the list in order to perform some processing on them.
  • 10.
    Algorithm to searcha linked list Searching a linked list means to find a particular element in the linked list. finding whether a given value is present in the information part of the node or not. If it is present, the algorithm returns the address of the node that contains the value.
  • 11.
    • In Step1, we initialize the pointer variable PTR with START that contains the address of the first node. • In Step 2, a while loop is executed which will compare every node’s DATA with VAL for which the search is being made. • If the search is successful, that is, VAL has been found, then the address of that node is stored in POS and the control jumps to the last statement of the algorithm. • However, if the search is unsuccessful, POS is set to NULL which indicates that VAL is not present in the linked list.
  • 13.
    Inserting a Nodeat the Beginning of a Linked List
  • 15.
    • In Step1, we first check whether memory is available for the new node. • If the free memory has exhausted, then an OVERFLOW message is printed. • Otherwise, if a free memory cell is available, then we allocate space for the new node. Set its DATA part with the given VAL and the NEXT part is initialized with the address of the first node of the list, which is stored in START. • Now, since the new node is added as the first node of the list, it will now be known as the START node, that is, the START pointer variable will now hold the address of the NEW_NODE.
  • 17.
    the algorithm toinsert a new node at the end of a linked list. In Step 6, we take a pointer variable PTR and initialize it with START. That is, PTR now points to the first node of the linked list. In the while loop, we traverse through the linked list to reach the last node. Once we reach the last node, in Step 9, we change the NEXT pointer of the last node to store the address of the new node. Remember that the NEXTfield of the new node contains NULL, which signifies the end of the linked list.
  • 20.
    • In Step5, we take a pointer variable PTR and initialize it with START. • That is, PTR now points to the first node of the linked list. • Then we take another pointer variable PREPTR which will be used to store the address of the node preceding PTR. • Initially, PREPTR is initialized to PTR. So now, PTR,PREPTR, and START are all pointing to the first node of the linked list. • In the while loop, we traverse through the linked list to reach the node that has its value equal to NUM. • We need to reach this node because the new node will be inserted after this node. • Once we reach this node, in Steps 10 and 11, we change the NEXT pointers in such a way that new node is inserted after the desired node.
  • 23.
    • In Step5, we take a pointer variable PTR and initialize it with START. • That is, PTR now points to the first node of the linked list. • Then, we take another pointer variable PREPTR and initialize it with PTR. • So now,PTR, PREPTR, and START are all pointing to the first node of the linked list. • In the while loop, we traverse through the linked list to reach the node that has its value equal to NUM. • We need to reach this node because the new node will be inserted before this node. Once we reach this node, in Steps 10 and 11, we change the NEXT pointers in such a way that the new node is inserted before the desired node.
  • 25.
    Deleting a Nodefrom a Linked List Deleting the First Node from a Linked List
  • 26.
    to delete thefirst node from a linked list. • In Step 1, we check if the linked list exists or not. • If START = NULL, then it signifies that there are no nodes in the list and the control is transferred to the last statement of the algorithm. • However, if there are nodes in the linked list, then we use a pointer variable PTR that is set to point to the first node of the list. • For this, we initialize PTR with START that stores the address of the first node of the list. • In Step 3, START is made to point to the next node in sequence and finally the memory occupied by the node pointed by PTR (initially the first node of the list) is freed and returned to the free pool
  • 27.
    Deleting the LastNode from a Linked List
  • 29.
    Algorithm to inserta new node at the beginning
  • 30.
    Step 1, wefirst check whether memory is available for the new node. • If the free memory has exhausted, then an OVERFLOW message is printed. •Otherwise, if free memory cell is available, then we allocate space for the new node. • Set its DATA part with the given VAL and the NEXT part is initialized with the address of the First node of the list, which is stored in START. • Now, since the new node is added as the First node of the list, it will now be known as the START node,that is, the START pointer variable will now hold the address of the NEW_NODE. While inserting a node in a circular linked list, we have to use a while loop to traverse to the last node of the list. Because the last node contains a pointer to START, its NEXT Field is updated. • so that after insertion it points to the new node which will be now known as START.
  • 32.
    Algorithm to inserta new node at the end of a circular linked list. In Step 6,we take a pointer variable PTR and initialize it with START. That is, PTR now points to the First node of the linked list. In the while loop, we traverse through the linked list to reach the last node. Once we reach the last node, in Step 9, we change the NEXT pointer of the last node to store the address of the new node. Remember that the NEXT Field of the new node contains the address of the first node which is denoted by START.
  • 34.
    In Step 1of the algorithm, we check if the linked list exists or not. If START = NULL, then it signifies that there are no nodes in the list and the control is transferred to the last statement of the algorithm. However, if there are nodes in the linked list, then we use a pointer variable PTR which will be used to traverse the list to ultimately reach the last node. In Step 5, we change the next pointer of the last node to point to the second node of the circular linked list. In Step 6, the memory occupied by the first node is freed. Finally, in Step 7, the second node now becomes the first node of the list and its address is stored in the pointer variable START.
  • 36.
    Algorithm to deletethe last node from a circular linked list. In Step 2, we take a pointer variable PTR and initialize it with START. That is, PTR now points to the first node of the linked list. In the while loop, we take another pointer variable PREPTR such that PREPTR always points to one node before PTR. Once we reach the last node and the second last node, we set the next pointer of the second last node to START, so that it now becomes the (new) last node of the linked list. The memory of the previous last node is freed.
  • 37.