0% found this document useful (0 votes)
21 views13 pages

Linked Representation and Chains PDF

Uploaded by

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

Linked Representation and Chains PDF

Uploaded by

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

Linked Representation And Chains

In a linked list representation each element of an instance of a data object is represented in


a cell or node. The nodes however need not be component of an array and no formula is
used to locate individual elements. Instead of each node keeps explicit information about
the location of other relevant nodes. This explicit information about the location of another
node is called Link or Pointer.
Let L=(e1, e2, e3…en) be a linear List. In one possible linked representation for this list, each
element ei is represented in a separate node. Each node has exactly one link field that is
used to locate the next element in the linear list. So the node for e i links to that for ei+1,
0<=i<n-1. The node for en-1 has no need to link to and so its link field is NULL. The pointer
variables first locate the first node in the representation. The below figure shows the linked
representation of a List=(e1, e2, e3…en).

Since each node in the Linked representation of the above figure has exartly one link, the
structure of this figure is called a ‘Single Linked List’.the nodes are ordered from left to right
with each node (other than last one) linking to the next,and the last node has a NULL
link,the structure is also called a chain.
Insertion and Deletion of a Single Linked List:
Insertion Let the list be a Linked list with succesive nodes A and B as shown in below
figure.suppose a node N id to be inserted into the list between the node A and B.
In the New list the Node A points to the new Node N and the new node N points to the node
B to which Node A previously pointed.
Deletion:
Let list be a Linked list with node N between Nodes A and B is as shown in the following
figure.
In the new list the node N is to be deleted from the Linked List. The deletion occurs as the
link field in the Node A is made to point node B this excluding node N from its path.

DOUBLE LINKED LIST (Or) TWO WAY LINKED LIST


In certain applications it is very desirable that list be traversed in either forward direction or
Back word direction. The property of Double Linked List implies that each node must contain
two link fields instead of one. The links are used to denote the preceding and succeeding of
the node. The link denoting the preceding of a node is called Left Link. The link denoting
succeeding of a node is called Right Link. The list contain this type of node is called a
“Double Linked List” or “Two Way List”. The Node structure in the Double Linked List is as
follows:

Lptr contains the address of the before node. Rptr contains the address of next node. Data
Contains the Linked List is as follows.

In the above diagram Last and Start are pointer variables which contains the address of last
node and starting node respectively.
Insertion in to the Double Linked List:Let list be a double linked list with successive modes A
and B as shown in the following diagram. Suppose a node N is to be inserted into the list
between the node s A and B this is shown in the following diagram.
As in the new list the right pointer of node A points to the new node N ,the Lptr of the node
‘N’ points to the node A and Rptr of node ‘N’ points to the node ‘B’ and Lpts of node B
points the new node ‘N’
Deletion Of Double Linked List :- Let list be a linked list contains node N between the nodes
A and B as shown in the following diagram.

Support node N is to be deleted from the list diagram will appear as the above mention
double linked list. The deletion occurs as soon as the right pointer field of node A charged,
so that it points to node B and the lift point field of node B is changed. So that it pointes to
node A.
Circular Linked List:- Circular Linked List is a special type of linked list in which all the nodes
are linked in continuous circle. Circular list can be singly or doubly linked list. Note that,
there are no Nulls in Circular Linked Lists. In these types of lists, elements can be added to
the back of the list and removed from the front in constant time.
Both types of circularly-linked lists benefit from the ability to traverse the full list beginning
at any given node. This avoids the necessity of storing first Node and last node, but we need
a special representation for the empty list, such as a last node variable which points to some
node in the list or is null if it's empty. This representation significantly simplifies adding and
removing nodes with a non-empty list, but empty lists are then a special case. Circular
linked lists are most useful for describing naturally circular structures, and have the
advantage of being able to traverse the list starting at any point. They also allow quick
access to the first and last records through a single pointer (the address of the last element)
Circular single linked list:

Circular linked list are one they of liner linked list. In which the link fields of last node of the
list contains the address of the first node of the list instead of contains a null pointer.
Advantages:- Circular list are frequency used instead of ordinary linked list because in
circular list all nodes contain a valid address. The important feature of circular list is as
follows.
(1) In a circular list every node is accessible from a given node.
(2) Certain operations like concatenation and splitting becomes more efficient in circular
list.
Disadvantages: Without some conditions in processing it is possible to get into an infinite
Loop.
Circular Double Linked List :- These are one type of double linked list. In which the rpt field
of the last node of the list contain the address of the first node ad the left points of the first
node contains the address of the last node of the list instead of containing null pointer.

Advantages:- circular list are frequently used instead of ordinary linked list because in
circular list all nodes contained a valid address. The important feature of circular list is as
follows.
(1) In a circular list every node is accessible from a given node.
(2) Certain operations like concatenation and splitting becomes more efficient
in circular list.
Disadvantage:-Without some conditions in processes it is possible to get in to an infant glad.

Difference between single linked list and double linked list?

Single linked list(SLL) Double linked list(DLL)


1. In Single Linked List the list will be 1. In Double Linked List the list will be
traversed in only one way ie; in forward. traversed in two way ie; either forward
2. In Single Linked List the node contains and backward
one link field only. 2. In Double Linked List the node contains
3. Every node contains the address of next two link fields.
node. 3. Every node contains the address of
4. The node structure in Single linked list is next node as well as preceding node.
as follows: 4.the node structure in double linked list is as
follows:
5. the conceptual view of DLL is as follows:
5. The conceptual view of SLL is as follows:

6. DLL is maintained in memory by using


three arrays.
6. SLL are maintained in memory by using
two arrays.

1. Difference between sequential allocation and linked allocation?


OR
Difference between Linear List and Linked List?
OR
Difference between Arrays and Linked List?
Arrays Linked List
1. Arrays are used in the predictable storage 1. Linked Lists are used in unpredictable
requirement ie; exert amount of data storage requirements ie; exert amount of
storage required by the program can be data storage required by the program can’t
determined. be determined.

2. In arrays the operations such as 2. In Linked List the operations such as


insertion and deletion are done in an insertion and deletion are done more
inefficient manner. efficient manner ie; only by changing the
pointer.

3. The insertion and deletion are done by 3. The insertion and deletion are done by
moving the elements either up or down. only changing the pointers.

4. Successive elements occupy 4. Successive elements need not


adjacent space on memory. occupy adjacent space.

5. In arrays each location contains DATA only 5. In the linked list each location contains
6. The linear relationship between the data data and a pointer to denote whether the
elements of an array is reflected by the next element is present in the memory.
physical relationship of data in the memory.
6. The linear relationship between the data
7. In array declaration a block of elements of a Linked List is reflected by the
memory space is required. Linked field of the node.

8.There is no need of storage of pointer 7. In Linked list there is no need of


or lines such thing.
8. In Linked list a pointer is stored along
into the element.
9. The Conceptual view of an Array is 9. The Conceptual view of Linked list is as
as follows: follows:

10. In array there is no need for an


element to specify whether the next is
stored 10. There is need for an element (node) to
specify whether the next node is formed.
Single Linked List Algorithm’s: Insertion
 In a single linked list, the insertion operation can be performed in three
ways. They are as follows...
 Inserting At Beginning of the list
 Inserting At End of the list
 Inserting At Specific location in the list
Inserting At Beginning of the list
Step 1 - Create a new Node with given value.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, set new Node→next = NULL and head = new
Node.
Step 4 - If it is Not Empty then, set new Node→next = head and head = new
Node.
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 new Node with given value and new Node → next as NULL.
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = new Node.
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 = new Node.
Deletion
In a single linked list, the deletion operation can be performed in three ways.
They are as follows...
Deleting from Beginning of the list
Deleting from End of the list
Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the single
linked list...
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3 - If it is Not Empty then, define a Node pointer 'temp' and initialize
with head.
Step 4 - Check whether list is having only one node (temp → next == NULL)
Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list
conditions)
Step 6 - If it is FALSE then set head = temp → next, and delete temp.
Deleting from End of the list:
We can use the following steps to delete a node from end of the single linked
list...
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminate the function.
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and
'temp2' and initialize 'temp1' with head.
Step 4 - Check whether list has only one Node (temp1 → next == NULL)
Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate
the function. (Setting Empty list condition)
Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next
node. Repeat the same until it reaches to the last node in the list. (until temp1
→ next == NULL)
Step 7 - Finally, Set temp2 → next = NULL and delete temp1

Deleting a Specific Node from the list :


 We can use the following steps to delete a specific node from the single
linked list...
 Step 1 - Check whether list is Empty (head == NULL)
 Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not
possible' and terminates the function.
 Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and
'temp2' and initialize 'temp1' with head.
 Step 4 - Keep moving the temp1 until it reaches to the exact node to be
deleted or to the last node. And every time set 'temp2 = temp1' before
moving the 'temp1' to its next node.
 Step 5 - If it is reached to the last node then display 'Given node not
found in the list! Deletion not possible!!!'. And terminate the function.
 Step 6 - If it is reached to the exact node which we want to delete, then
check whether list is having only one node or not
 Step 7 - If list has only one node and that is the node to be deleted, then
set head = NULL and delete temp1 (free(temp1)).
 Step 8 - If list contains multiple nodes, then check whether temp1 is the
first node in the list (temp1 == head).
 Step 9 - If temp1 is the first node then move the head to the next node
(head = head → next) and delete temp1.
 Step 10 - If temp1 is not first node then check whether it is last node in
the list (temp1 → next == NULL).
 Step 11 - If temp1 is last node then set temp2 → next = NULL and
delete temp1 (free(temp1)).
 Step 12 - If temp1 is not first node and not last node then set temp2 →
next = temp1 → next and delete temp1 (free(temp1)).
Operations on Double Linked List
In a double linked list, we perform the following operations...

Insertion
Deletion
Display
Insertion
In a double-linked list, the insertion operation can be performed in three ways
as follows...

Inserting At Beginning of the list


Inserting at the end of the list
Inserting a specific location in the list
Inserting At Beginning of the list
We can use the following steps to insert a new node at beginning of the double
linked list...

Step 1 - Create a new Node with a given value and newNode → previous as
NULL.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to
head.
Step 4 - If it is not Empty then, assign head to newNode → next and newNode
to head.
Inserting At End of the list
We can use the following steps to insert a new node at the end of the double-
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 assign NULL to newNode → previous and newNode
to head.
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 - Assign newNode to temp → next and temp to newNode → previous.

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 double
linked list...

Step 1 - Create a new Node with the given value.


Step 2 - Check whether the list is Empty (head == NULL)
Step 3 - If it is Empty then, assign NULL to both newNode → previous &
newNode → next and set newNode to head.
Step 4 - If it is not Empty then, define two node pointers temp1 & temp2 and
initialize temp1 with head.
Step 5 - Keep moving the temp1 to its next node until it reaches to the node
after which we want to insert the new node (until temp1 → data is equal to
location, here location is the node value after which we want to insert the new
node).
Step 6 - Every time check whether temp1 is reached to the last node. If it is
reached to the last node then display 'Given node is not found in the list!!!
Insertion not possible!!!' and terminate the function. Otherwise move the
temp1 to next node.
Step 7 - Assign temp1 → next to temp2, newNode to temp1 → next, temp1 to
newNode → previous, temp2 to newNode → next and newNode to temp2 →
previous.
Deletion
In a double linked list, the deletion operation can be performed in three ways
as follows...

Deleting from Beginning of the list


Deleting from End of the list
Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from the beginning of the
double-linked list...

Step 1 - Check whether the list is Empty (head == NULL)


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible and
terminates the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with
head.
Step 4 - Check whether list is having only one node (temp → previous is equal
to temp → next)
Step 5 - If it is TRUE, then set head to NULL and delete temp (Setting Empty list
conditions)
Step 6 - If it is FALSE, then assign temp → next to head, NULL to head →
previous and delete temp.
Deleting from End of the list
We can use the following steps to delete a node from end of the double linked
list...

Step 1 - Check whether list is Empty (head == NULL)


Step 2 - If it is Empty, then display 'List is Empty!!! Deletion is not possible' and
terminate the function.
Step 3 - If it is not Empty then, define a Node pointer 'temp' and initialize with
head.
Step 4 - Check whether list has only one Node (temp → previous and temp →
next both are NULL)
Step 5 - If it is TRUE, then assign NULL to head and delete temp. And terminate
from the function. (Setting Empty list condition)
Step 6 - If it is FALSE, then keep moving temp until it reaches to the last node in
the list. (until temp → next is equal to NULL)
Step 7 - Assign NULL to temp → previous → next and delete temp.
Deleting a Specific Node from the list
We can use the following steps to delete a specific node from the double-
linked list...

Step 1 - Check whether the list is Empty (head == NULL)


Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible and
terminates the function.
Step 3 - If it is not Empty, then define a Node pointer 'temp' and initialize with
head.
Step 4 - Keep moving the temp until it reaches the exact node to be deleted or
to the last node.
Step 5 - If it is reached to the last node, then display 'Given node not found in
the list! Deletion not possible!!!' and terminate the fuction.
Step 6 - If it is reached to the exact node which we want to delete, then check
whether list is having only one node or not
Step 7 - If list has only one node and that is the node which is to be deleted
then set head to NULL and delete temp (free(temp)).
Step 8 - If list contains multiple nodes, then check whether temp is the first
node in the list (temp == head).
Step 9 - If temp is the first node, then move the head to the next node (head =
head → next), set head of previous to NULL (head → previous = NULL) and
delete temp.
Step 10 - If temp is not the first node, then check whether it is the last node in
the list (temp → next == NULL).
Step 11 - If temp is the last node then set temp of previous of next to NULL
(temp → previous → next = NULL) and delete temp (free(temp)).
Step 12 - If temp is not the first node and not the last node, then set temp of
previous of next to temp of next (temp → previous → next = temp → next),
temp of next of previous to temp of previous (temp → next → previous = temp
→ previous) and delete temp (free(temp)).

You might also like