RAA DS Unit 3 Final Print
RAA DS Unit 3 Final Print
It allocates the memory dynamically. All the nodes of the linked list are non-
contiguously stored in the memory and linked together with the help of
pointers.
In linked list, size is no longer a problem since we do not need to define its size at
the time of declaration. List grows as per the program's demand and limited to
the available memory space.
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 1
UNIT – III
Array contains following limitations:
1. The size of array must be known in advance before using it in the program.
2. Increasing size of the array is a time taking process. It is almost impossible to
expand the size of the array at run time.
3. All the elements in the array need to be contiguously stored in the memory.
Inserting any element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array.
Using linked list is useful because,
1. It allocates the memory dynamically. All the nodes of linked list are non-
contiguously stored in the memory and linked together with the help of pointers.
2. Sizing is no longer a problem since we do not need to define its size at the time of
declaration. List grows as per the program's demand and limited to the available
memory space.
DYNAMIC STORAGE MANAGEMENT
When memory is allocated during run/execution time, it is called ‘Dynamic Memory
Management’. This memory is not fixed and is allocated according to our requirements. Thus
in it there is no wastage of memory. So there is no need to know exact memory requirements
in advance. Linked lists use the concept of Dynamic storage management.
LINKED LIST
In this representation, the structure does not necessarily reflect the logical
organization. Items may appear in any order. The logical organization is provided
through pointers. Each item in the list is called as a node that consists of a data portion
containing the item information and a pointer which contains the location of the next item in
the logical sequence which is to be maintained. It is actually possible to have the advantages
of the linked list by using arrays, but in this case two kinds of information must be stored in
arrays, the information contained in the item and the pointer for that item. Suppose that F,
A, C, G are to be stored in an array which can hold a maximum of 5 items. They are to be
stored in the order in which they are read but they should logically be available in
alphabetical order. The array must be set up first. Then some counter to keep track of which
slot is available (i.e. empty) is required. A link field is required for two things. It can specify
the next index which is available after the current available space. It can also specify the
logical order of the items which currently occupy space.
It is also possible to delete and reuse space using this method. However, a
disadvantage is that an array which is large enough to hold the longest list must be declared
which may result in a large waste of memory in many cases.
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 2
UNIT – III
For the implementer this means that the array must be defined and initialized and the
variable indicating the first open position set if it is an array implementation. If dynamic
allocation is used, the node structure must be defined. Obviously the user can request the
create process at any time. Then the old list is lost (or saved if the software allows) and the
initialization done.
Creation is an O(n) process for arrays, where n is the maximum length of the list (i.e.
array size) but an O(1) operation for dynamic allocation. Length is usually implemented by
having a variable which contains the length of the list. Note that this implementation requires
that insert and delete also modify the length of the list.
For array implementations this is usually stored as an integer variable. For dynamic
allocation, it may be stored as an integer variable as well and is stored in the list node for the
list. In order to find a specified item in a list the retrieve process is used. This is essentially a
search process. In a singly-linked list, all items are accessed by beginning at the front or head
of the list and going through until the required position is found.
Retrieve does not need to know how the list is ordered (i.e. ascending, frequency, etc.)
since the structure is built so that each item follows the next according to some ordering
relationship. The implementation may be diverse enough so that we can retrieve an item by
its key or as the nth element. In this case the retrieve has additional parameters. Retrieve is
O(n) in the worst case since the last item may be the one which is required or the item does
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 3
UNIT – III
not exist in the list. n in this case is the number of items in the list, not the maximum possible
number of items. We could make retrieve more efficient at indicating that an item does not
exist, but in doing so we also remove some of the generality of the data type.
Only one procedure should require modification when we change the ordering
relationship (see insertion). If we also incorporate the same code in retrieve so that the search
can stop when the item position is passed, we limit retrieve to that kind of ordered list. Note
also that retrieve from the user point of view is transparent of the data structure. Whether an
array or a series of nodes is accessed is not important to the user. Insert puts a new item in
the list.
Obviously, it is important to know the ordering relationship in order to implement this
process. Looking at the implementation more carefully, there are several steps. We have to
get an open space in the array implementation or create a new node for dynamically allocated
nodes. The information is then placed in the newly acquired position. Finding the correct
position for the new item in the list is dependent on the ordering relationship. Thus if we
change the type of linked list this procedure must be changed, or alternately, we can pass a
flag to the procedure which uses the portion of the code for that ordering relationship.
Having found the position, the insert procedure now sets the links so that the new item
is in the appropriate position with respect to the list ordering. The links determine the
appropriate position not the position physically occupied. Note that insertion at the head of
the list is a special case which must be dealt with. Most often the array implementation has
the link field (see previous) so that data need not be moved each time a new item is inserted.
The insertion itself is O(1) but the process of finding the position may be O(n) where n is the
number of items in the list. This makes the total process O(n) in the worst case, O(1) in the
best case and average of n/2. Delete also requires more than one step. The item must be
found, the links reset so that the integrity of the list is maintained, and the old item
destroyed. Finding the item can be done with the same procedure that finds node position in
insert. Retrieve cannot be used since the preceding node must also be known and this is not
returned with retrieve but must be returned with the find position procedure. Having found
the node, the links are reset to maintain the list, and finally the node destroyed. Once again,
deletion at the head of the list is a special case. In the array implementation, the position
must be placed back as available space, whereas it is returned to the memory pool for
dynamic allocation. Order for deletion is the same as insertion.
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 4
UNIT – III
Step 1: Include all the header files which are used in the program.
Step 2: Declare all the user defined functions.
Step 3: Define a Node structure with two members data and next
Step 4: Define a Node pointer 'head' and set it to NULL.
Step 5: Implement the main method by displaying operations menu and make
suitable function calls in the main method to perform user selected operation.
In a single linked list we perform the following operations...
1. Insertion
2. Deletion
3. Display
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 6
UNIT – III
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 → datais 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'
Algorithm:
void insertItem(List* listPtr, float item, int position)
{
Node* newNodePtr = makeNode(item);
Node* prevPtr = NULL;
if (position == 0)
{
newNodePtr->nextPtr = listPtr->headPtr;
listPtr->headPtr = newNodePtr;
}
else
{
prevPtr = setPosition(listPtr, position-1);
newNodePtr->nextPtr = prevPtr->nextPtr;
prevPtr->nextPtr = newNodePtr;
}
listPtr->count++;
}
1) INSERTING IN A NEW LIST
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 7
UNIT – III
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 8
UNIT – III
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.
Time Complexity:
DOUBLY-LINKED LISTS
One of the major disadvantages of a singly linked list is the inability to move
backward in the list. In double linked list, every node has link to its previous node and next
node. So, we can traverse forward by using next field and can traverse backward by using
previous field. Every node in a double linked list contains three fields and they are shown in
the following figure...
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 10
UNIT – III
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 11
UNIT – III
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 newNode with given value.
Step 2: Check whether list is Empty (head == NULL)
Step 3: If it is Empty then, assign NULL to newNode → previous & newNode →
next and 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 newNode (until temp1 → datais equal to location, here
location is the node value after which we want to insert the newNode).
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...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 12
UNIT – III
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.
Operations
In a circular linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Before we implement actual operations, first we need to setup empty list. First perform the
following steps before implementing actual operations.
Step 1: Include all the header files which are used in the program.
Step 2: Declare all the user defined functions.
Step 3: Define a Node structure with two members data and next
Step 4: Define a Node pointer 'head' and set it to NULL.
Step 4: Implement the main method by displaying operations menu and make
suitable function calls in the main method to perform user selected operation.
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 14
UNIT – III
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as
follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At 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 circular 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 head = newNode and newNode→next = 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 (until
'temp → next == head').
Step 6: Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.
Deletion
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 15
UNIT – III
In a circular linked list, the deletion operation can be performed in three ways those are as
follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
We can use the following steps to delete a node from beginning of the circular 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 both 'temp1' and 'temp2' with head.
Step 4: Check whether list is having only one node (temp1 → next == head)
Step 5: If it is TRUE then set head = NULL and delete temp1 (Setting Empty list
conditions)
Step 6: If it is FALSE move the temp1 until it reaches to the last node. (until temp1
→ next == head )
Step 7: Then set head = temp2 → next, temp1 → next = head and delete temp2.
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 16
UNIT – III
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 set temp2 = head and keep moving temp2 to
its next node until temp2 reaches to the last node. Then set head = head →
next, temp2 → next = head and delete temp1.
Step 10: If temp1 is not first node then check whether it is last node in the list (temp1
→ next == head).
Step 11: If temp1 is last node then set temp2 → next = head 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)).
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 17
UNIT – III
LINKED STACKS AND QUEUES
We have already seen how to represent stacks and queues sequentially. Such a
representation proved efficient if we had only one stack or one queue. However, when several
stacks and queues co-exist, there was no efficient way to represent them sequentially. In this
section we present a good solution to this problem using linked lists. Figure 4.5 shows a
linked stack and a linked queue.
Notice that the direction of links for both the stack and queue are such as to facilitate
easy insertion and deletion of nodes. In the case of figure 4.5(a), one can easily add a node at
the top or delete one from the top. In figure 4.5(b), one can easily add a node at the rear and
both addition and deletion can be performed at the front, though for a queue we normally
would not wish to add nodes at the front. If we wish to represent n stacks and m queues
simultaneously, then the following set of algorithms and initial conditions will serve our
purpose:
T(i) = Top of ith stack 1 ≤ i ≤ n
F(i) = Front of ith queue 1≤ i≤ m
R(i) = Rear of ith queue 1 ≤ i≤ m
Initial conditions:
T(i) = 0 1≤i≤n
F(i) = 0 1 ≤ i≤ m
Boundary conditions:
T(i) = 0 iff stack i empty
F(i) = 0 iff queue i empty
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 18
UNIT – III
procedure DELETES(i, Y) //delete top node from stack i & set Y to be the DATA field of this node//
procedure DELETEQ(i, Y)
//delete the first node in the i’th queue, set Y to its DATA field//
if F(i) = 0 then call QUEUE__EMPTY
else [X F(i); F(i) LINK(X) //set X to front node//
Y DATA(X); call RET(X)] //remove data and return node//
end DELETEQ
The solution presented above to the n-stack, m-queue problem is seen to be both
computationally and conceptually simple.
There is no need to shift stacks or queues around to make space.
Computation can proceed so long as there are free nodes. Though additional space is
needed for the link fields, the cost is no more than a factor of 2.
Sometimes the DATA field does not use the whole word and it is possible to pack the
LINK and DATA fields into the same word.
In such a case the storage requirements for sequential and linked representations would be
the same. For the use of linked lists to make sense, the overhead incurred by the storage
for the links must be overridden by:
i. The virtue of being able to represent complex lists all within the same array,
ii. The computing time for manipulating the lists is less than for sequential
representation.
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 19
UNIT – III
STORAGE ALLOCATION & GARBAGE COLLECTION
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 20
UNIT – III
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 21
UNIT – III
Differences between SLL & DLL
S.Y. B.Tech., CSE (Data Science), SSGBCOET, Bsl, Data Structures, UNIT – III Notes by Prof. R. A. Agrawal. Page 22