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

Abstract Data Types

ABSTRACT

Uploaded by

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

Abstract Data Types

ABSTRACT

Uploaded by

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

3.

1 ABSTRACT DATA TYPES (ADTS)


 An abstract data type is an abstraction of a data structure that provides only the
interface to which the data structure must adhere. The interface does not give
any specific details about something should be implemented or in what
programming language.
 In other words, we can say that abstract data types are the entities that are
definitions of data and operations but do not have implementation details. In this
case, we know the data that we are storing and the operations that can be
performed on the data, but we don't know about the implementation details.
 The reason for not having implementation details is that every programming
language has a different implementation strategy for example; a C data structure
is implemented using structures while a C++ data structure is implemented using
objects and classes.

3.1.1 Abstract data type model


Abstraction: It is a technique of hiding the internal details from the user and only
showing the necessary details to the user.
Encapsulation: It is a technique of combining the data and the member function in a
single unit is known as encapsulation.
Figure 3.1 shows the ADT model. There are two types of models in the ADT
model, i.e., the public function and the private function. The ADT model also contains
the data structures that we are using in a program. In this model, first encapsulation is
performed, i.e., all the data is wrapped in a single unit, i.e., ADT. Then, the abstraction
is performed means showing the operations that can be performed on the data structure
and what are the data structures that we are using in a program

Fig 3.1 Abstract Data Type Model


Example 3.1
Suppose we have an index array of size 4. We have an index location starting
from 0, 1, 2, 3. Array is a data structure where the elements are stored in a contiguous
location. The memory address of the first element is 1000, second element is 1004, third
element is 1008, and the fourth element is 1012. Since it is of integer type so it will
occupy 4 bytes and the difference between the addresses of each element is 4 bytes. The
values stored in an array are 10, 20, 30 and 40. These values, index positions and the
memory addresses are the implementations.
The abstract or logical view of the integer array can be stated as:
 It stores a set of elements of integer type.
 It reads the elements by position, i.e., index.
 It modifies the elements by index
 It performs sorting
3.2 LIST ADT
 The list can be defined as an abstract data type in which t;he elements are stored
in an ordered manner for easier and efficient retrieval of the elemyents. List Data
Structure allows repetition that means a single piece of data can occur more than
once in a list.
 In the case of multiple entries of the same data, each entry of that repeating data
is considered as a distinct item or entry. It is very much similar to the array but
the major difference between the array and the list data structure is that array
stores only homogenous data in them whereas the list (in some programming
languages) can store heterogeneous data items in its object. List Data Structure is
also known as a sequence.
 The list can be called Dynamic size arrays, which means their size increased as
we go on adding data in them and we need not to pre-define a static size for the
list
For example,
numbers = [ 1, 2, 3, 4, 5]
 In this example, 'numbers' is the name of the List Data Structure object and it has
five items stored in it. In the object named numbers, we have stored all the
elements of numeric type. In the list, the indexing starts from zero, which means
if we want to access or retrieve the first element of this list then we need to use
index zero and similarly whenever we want to access any element from this list
named numbers. In other words, we can say that element 1 is on the index 0 and
element 2 is on index 1 and similarly for further all elements.
Mixed_data = [205, ‘Mathu’, 8.56]
 In this second example, mixed_data is the name of the list object that stores the
data of different types. In the mixed_data list, we have stored data of three types,
first one is the integer type which is id '205', after the integer data we have stored
a string type data having the value ‘Mathu’ stored at index 1 and at last the index
value 2, we have stored a float type data having the value '8.56'.
 To access the elements of the mixed_data list, we need to follow the same
approach as defined in the previous example.
 And we can add more data to these defined List objects and that will get
appended at the last of the list. For example, if we add another data in the
mixed_data list,
it will get appended after the float value object having value '8.56'. And we can
add repeating values to these list-objects.

3.2.1 Operations on the List Data Structure


Add or Insert Operation:
In the Add or Insert operation, a new item (of any data type) is added in the List
Data Structure or Sequence object.
Replace or reassign Operation:
In the Replace or reassign operation, the already existing value in the List object
is changed or modified. In other words, a new value is added at that particular index of
the already existing value.
Delete or remove Operation:
In the Delete or remove operation, the already present element is deleted or
removed from the Dictionary or associative array object.
Find or Lookup or Search Operation:
In the Find or Lookup operation, the element stored in that List Data Structure or
Sequence object is fetched.

3.3 ARRAY-BASED IMPLEMENTATION


 Arrays are defined as the collection of similar types of data items stored at
contiguous memory locations.
 It is one of the simplest data structures where each data element can be randomly
accessed by using its index number.
 They are the derived data types in C programming that can store the primitive
type of data such as int, char, double, float, etc.
 For example, if we want to store the marks of a student in 6 subjects, then we
don't need to define a different variable for the marks in different subjects.
Instead, we can define an array that can store the marks in each subject at the
contiguous memory locations.
3.3.1 Properties of array
 Each element in an array is of the same data type and carries the same size that is
4 bytes.
 Elements in the array are stored at contiguous memory locations from which the
first element is stored at the smallest memory location.
 Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size of
the data element.

3.3.2 Representation of an array


 Array can be represented in various ways in different programming languages.
As an illustration, let's see the declaration of array in C language

Fig. 3.2: Illustration of an Array


As per the above illustration of array, there are some of the following important points -
 Index starts with 0.
 The array's length is 10, which means we can store 10 elements.
 Each element in the array can be accessed via its index.

3.3.3 Memory allocation of an array


 Data elements of an array are stored at contiguous locations in the main
memory. The name of the array represents the base address or the address of the
first element in the main memory. Each element of the array is represented by
proper indexing.
 We can define the indexing of an array in the below ways
 0 (zero-based indexing): The first element of the array will be arr[0].
 1 (one-based indexing): The first element of the array will be arr[1].
 n (n - based indexing): The first element of the array can reside at any
random index number
 Fig 3.3 shows the memory allocation of an array arr of size 5. The array follows
a 0-based indexing approach. The base address of the array is 100 bytes. It is the
address of arr[0]. Here, the size of the data type used is 4 bytes; therefore, each

Fig. 3.3: Memory allocation of an array

3.3.4 Access an element from the array


The information given below are required to access any random element from the array
 Base Address of the array.
 Size of an element in bytes.
 Type of indexing, array follows.
The formula to calculate the address to access an array element
Byte address of element A[i] = base address + size * ( i - first index)
Here, size represents the memory taken by the primitive data types. As an instance, int
takes 2 bytes, float takes 4 bytes of memory space in C programming.
Example 3.2
Suppose an array, A[-10.......+2 ] having Base address (BA) = 999 and size of an
element = 2 bytes, find the location of A[-1].
L(A[-1]) = 999 + 2 x [(-1) - (-10)]
= 999 + 18
= 1017

3.3.5 Basic operations of an Array


Basic operations supported in the array
 Traversal - This operation is used to print the elements of the array.
 Insertion - It is used to add an element at a particular index.
 Deletion - It is used to delete an element from a particular index.
 Update - It updates an element at a particular index.

3.3.6 C
3.3.7
3.3.8 omplexity of Array operations
Time and space complexity of various array operations are described below.
Time Complexity
Operation Average Case Worst Case
Access O(1) O(1)
Search O(n) O(n)
Insertion O(n) O(n)
Deletion O(n) O(n)

Space Complexity
In array, space complexity for worst case is O(n).

3.3.9 Limitations of Array


 The size of the array must be known in advance before using it in the program.
 Increasing the size of the array is a time taking process. It is almost impossible
to expand the size of the array at run time.
 All the elements in the array need to be contiguously stored in the memory.
Inserting an element in the array needs shifting of all its predecessors.

3.3.10 Advantages of Array


 Arrays are good for storing multiple values in a single variable.
 Traversing an array is a very simple process; we just need to increment the base
address of the array in order to visit each element one by one.
 Any element in the array can be directly accessed by using the index.
 Sorting and searching a value in an array is easier.
 Arrays are best to process multiple values quickly and easily.

3.3.11 Disadvantages of Array


 Array is homogenous. It means that the elements with similar data type can be
stored in it.
 There will be wastage of memory if we store less number of elements than the
declared size.

3.4 LINKED LIST


 Linked list is a linear data structure that includes a series of connected nodes.
 Linked list can be defined as the nodes that are randomly stored in the memory.
 A node in the linked list contains two parts, i.e., first is the data part and second
is the address part.
 The last node of the list contains a pointer to the null.
 After array, linked list is the second most used data structure.
 In a linked list, every link contains a connection to another link

3.4.1 Representation of a Linked list


 Linked list can be represented as the connection of nodes in which each node
points to the next node of the list. The representation of the linked list is shown
below.

Fig. 3.4: Representation of Linked List

3.4.2 Why use linked list over array?


 Linked list is a data structure that overcomes the limitations of arrays (Refer 3.3.6)
 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.
3.4.3 Declare a linked list
 It is simple to declare an array, as it is of single type, while the declaration of
linked list is a bit more typical than array. Linked list contains two parts, and
both are of different types, i.e.,
 Simple variable,
 Pointer variable.
We can declare the linked list by using the user-defined data type structure.
The declaration of linked list is given as follows
struct node
{
int data;
struct node *next;
}
In the above declaration, we have defined a structure named as node that
contains two variables, one is data that is of integer type, and another one is next that is
a pointer which contains the address of next node.

3.4.4 Types of Linked list


Linked list is classified into the following types
 Singly-Linked List
 Doubly Linked List
 Circular Singly Linked List
 Circular Doubly Linked List

3.4.4.1 Singly-Linked List


 Singly linked list can be defined as the collection of an ordered set of elements.
 A node in the singly linked list consists of two parts: data part and link part.
 Data part of the node stores actual information that is to be represented by the
node
 Link part of the node stores the address of its immediate successor.
3.4.4.2 Doubly Linked List
 Doubly linked list is a complex type of linked list
 Here, a node contains a pointer to the previous as well as the next node in the
sequence.
 Therefore, in a doubly-linked list, a node consists of three parts:
 Node data,
 Pointer to the next node in sequence (next pointer), and
 Pointer to the previous node (previous pointer).

3.4.4.3 Circular Singly Linked List


 In a circular singly linked list, the last node of the list contains a pointer to the
first node of the list.
 We can have circular singly linked list as well as circular doubly linked list.

3.4.4.4 Circular Doubly Linked List


 Circular doubly linked list is a more complex type of data structure.
 Here a node contains pointers to its previous node as well as the next node.
 Circular doubly linked list doesn't contain NULL in any of the nodes.
 The last node of the list contains the address of the first node of the list.
 The first node of the list also contains the address of the last node in its previous
pointer.

3.4.5 Advantages of Linked list


 Dynamic data structure - The size of the linked list may vary according to the
requirements. Linked list does not have a fixed size.
 Insertion and deletion - Unlike arrays, insertion, and deletion in linked list is
easier. Elements in the linked list are stored at a random location.
 Memory efficient - The size of a linked list can grow or shrink according to the
requirements, so memory consumption in linked list is efficient.

 Implementation - We can implement both stacks and queues using linked list.
3.4.6 Disadvantages of Linked list
 Memory usage - In linked list, node occupies more memory than array. Each
node of the linked list occupies two types of variables, i.e., one is a simple
variable, and another one is the pointer variable.
 Traversal - Traversal is not easy in the linked list. If we have to access an
element in the linked list, we cannot access it randomly.
 Reverse traversing - Backtracking or reverse traversing is difficult in a linked
list. In a doubly-linked list, it is easier but requires more memory to store the
back pointer.

3.4.7 Applications of Linked list


 Using linked list, the polynomials can be represented as well as we can perform
the operations on the polynomial.
 A linked list can be used to represent the sparse matrix.
 The various operations like student's details, employee's details, or product
details can be implemented using the linked list as the linked list uses the
structure data type that can hold different data types.
 Using linked list, we can implement stack, queue, tree, and other various data
structures.
 If we want to represent the graph as an adjacency list, then it can be
implemented as a linked list.
 A linked list can be used to implement dynamic memory allocation. The
dynamic memory allocation is the memory allocation done at the run-time.
3.4.8 Operations performed on Linked list
 Insertion - This operation is performed to add an element into the list.
 Deletion - It is performed to delete an operation from the list.
 Display - It is performed to display the elements of the list.
 Search - It is performed to search an element from the list using the given key.
3.4.9 Complexity of Linked list
1. Time Complexity
Operation Average Case Worst Case
Insertion O(1) O(1)
Deletion O(1) O(1)
Search O(n) O(n)

2. Space Complexity
Operation Space complexity
Insertion O(n)
Deletion O(n)
Search O(n)

3.5 DOUBLY LINKED LIST


 Doubly linked list is a complex type of linked list
 Here, a node contains a pointer to the previous as well as the next node in the
sequence.
 Therefore, in a doubly-linked list, a node consists of three parts:
 Node data,
 Pointer to the next node in sequence (next pointer), and
 Pointer to the previous node (previous pointer).
A sample node in a doubly linked list is shown in Fig. 3.5

A doubly linked list containing three nodes having numbers from 1 to 3 in their
data part, is shown in Fig 3.6

Fig. 3.6: Doubly Linked List


In C, structure of a node in doubly linked list can be given as:
struct node
{
struct node *prev;
int data;
struct node *next;
}
 The prev part of the first node and the next part of the last node will always
contain null indicating end in each direction.
 In a singly linked list, we could traverse only in one direction, because each
node contains address of the next node and it doesn't have any record of its
previous nodes. However, doubly linked list overcome this limitation of singly
linked list.
 Due to the fact that, each node of the list contains the address of its previous
node, we can find all the details about the previous node as well by using the
previous address stored inside the previous part of each node.

3.5.1 Memory Representation of a doubly linked list


 Memory Representation of a doubly linked list is shown in Fig. 3.7. Generally,
doubly linked list consumes more space for every node and therefore, causes
more expansive basic operations such as insertion and deletion. However, we
can easily manipulate the elements of the list since the list maintains pointers in
both the directions (forward and backward).
 In Fig 3.7, the first element of the list that is i.e. 13 stored at address 1. The head
pointer points to the starting address 1. Since this is the first element being added
to the list therefore the prev of the list contains null. The next node of the list
resides at address 4 therefore the first node contains 4 in its next pointer.
 We can traverse the list in this way until we find any node containing null or -1
in its next part.

Fig. 3.7: Memory Representation of a doubly linked list

3.5.2 Operations on doubly linked list


Table 3.1 describes all the operations performed on Doubly Linked List
Table 3.1: Operations on Doubly Linked List
Sl. No. Operation Description
1. Insertion at beginning Adding the node into the linked list at beginning.
2. Insertion at end Adding the node into the linked list to the end.
3. Insertion after Adding the node into the linked list after the
specified node specified node.
4. Deletion at beginning Removing the node from beginning of the list
5. Deletion at the end Removing the node from end of the list.
6. Deletion of the node Removing the node which is present just after the
having given data node containing the given data.
7. Searching Comparing each node data with the item to be
searched and return the location of the item in the
list if the item found else return null
8. Traversing Visiting each node of the list at least once in order
to perform some specific operation like searching,
sorting, display, etc.
3.5.2.1 Insertion at beginning
 There are two scenarios of inserting any element into doubly linked list. Either
the list is empty or it contains at least one element.
The following steps to be performed for insert a node in doubly linked list at beginning.
 Allocate the space for the new node in the memory.
 Check whether the list is empty or not. The list is empty if the condition head ==
NULL holds.
 In that case, the node will be inserted as the only node of the list and therefore
the prev and the next pointer of the node will point to NULL and the head
pointer will point to this node.
 In the second scenario, the condition head == NULL become false and the node
will be inserted in beginning.
 The next pointer of the node will point to the existing head pointer of the node.
 The prev pointer of the existing head will point to the new node being inserted.
 Since, the node being inserted is the first node of the list and therefore it must
contain NULL in its prev pointer.
 Hence assign null to its previous part and make the head point to this node.
Algorithm 3.1
Step 1: IF ptr = NULL
Write OVERFLOW
Go to Step 9
Step 2: SET NEW_NODE = ptr
Step 3: SET ptr = ptr -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> PREV = NULL
Step 6: SET NEW_NODE -> NEXT = START
Step 7: SET head -> PREV = NEW_NODE
Step 8: SET head = NEW_NODE
Step 9: EXIT

Fig. 3.8: Insertion into Doubly Linked List at the beginning

3.5.2.2 Insertion at end


 To insert a node in doubly linked list at the end, make sure whether the list is
empty or it contains any element. Use the following steps in order to insert the
node in doubly linked list at the end.
 Allocate the memory for the new node. Make the pointer ptr point to the new
node being inserted.
 Check whether the list is empty or not. The list is empty if the condition head ==
NULL holds.
 In that case, the node will be inserted as the only node of the list and therefore
the prev and the next pointer of the node will point to NULL and the head
pointer will point to this node.
 In the second scenario, the condition head == NULL become false.
 For this reason, we have to traverse the whole list in order to reach the last node
of the list. Initialize the pointer temp to head and traverse the list by using this
pointer.
 Make the next pointer of temp point to the new node being inserted i.e. ptr.
 Make the previous pointer of the node ptr point to the existing last node of the list
i.e. temp.
 Make the next pointer of the node ptr point to the null as it will be the new last
node of the list.
Algorithm 3.2
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET NEW_NODE -> NEXT = NULL
Step 6: SET TEMP = START
Step 7: Repeat Step 8 while TEMP -> NEXT != NULL
Step 8: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 9: SET TEMP -> NEXT = NEW_NODE
Step 10C: SET NEW_NODE -> PREV = TEMP
Step 11: EXIT

3.5.2.3 Insertion after specified node


To insert a node after the specified node in the list, we need to skip the required
number of nodes in order to reach the mentioned node and then make the pointer
adjustments as required.
The following steps are used for this purpose.
 Allocate the memory for the new node.
 Traverse the list by using the pointer temp to skip the required number of nodes
in order to reach the specified node.
 The temp would point to the specified node at the end of the for loop. The new
node needs to be inserted after this node. Make the next pointer of ptr point to
the next node of temp.
 Make the prev of the new node ptr point to temp.
 Make the next pointer of temp point to the new node ptr.
 Make the previous pointer of the next node of temp point to the new node.
Algorithm 3.3
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 15
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET TEMP = START
Step 6: SET I = 0
Step 7: REPEAT 8 to 10 until I
Step 8: SET TEMP = TEMP -> NEXT
STEP 9: IF TEMP = NULL
STEP 10: WRITE "LESS THAN DESIRED NO. OF ELEMENTS"
GOTO STEP 15
[END OF IF]
[END OF LOOP]
Step 11: SET NEW_NODE -> NEXT = TEMP -> NEXT
Step 12: SET NEW_NODE -> PREV = TEMP
Step 13 : SET TEMP -> NEXT = NEW_NODEStep 14: SET TEMP -> NEXT ->
PREV = NEW_NODE Step 15: EXIT
STEP 1: IF HEAD = NULL

Fig. 3.10: Insertion of Doubly Linked List after specified node

Deletion at beginning
Deletion in douly linked list at the beginning is the simplest operation.
Just need to co the head pointer to pointer ptr and shift the head pointer to its next.
Make the prev of this new head node point to NULL.
Now free the pointer ptr by using the free function.
Algorithm 3.4
WRITE UNDERFLOW
GOTO STEP 6
STEP 2: SET PTR = HEAD
STEP 3: SET HEAD = HEAD → NEXT
STEP 4: SET HEAD → PREV = NULL
STEP 5: FREE PTR

Fig. 3.11: Deletion in Doubly Linked List from beginning

3.5.2.5 Deletion at the end


Deletion of the last node in a doubly linked list needs traversing the list in order
to reach the last node of the list and then make pointer adjustments at that position. To
delete the last node of the list, following steps are performed.
 If the list is already empty then the condition head == NULL will become true
and therefore the operation cannot be carried on.
 If there is only one node in the list then the condition head → next == NULL
become true. In this case, we just need to assign the head of the list to NULL and
free head in order to completely delete the list.
 Otherwise, just traverse the list to reach the last node of the list.
 The ptr would point to the last node of the ist at the end of the for loop. Just
make the next pointer of the previous node of ptr to NULL.
 Free the pointer as this the node which is to be deleted.
Algorithm 3.5:
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET TEMP = HEAD
Step 3: REPEAT STEP 4 WHILE TEMP->NEXT != NULL[END OF LOOP]
Step 5: SET TEMP ->PREV-> NEXT = NULL
Step 6: FREE TEMP
Step 7: EXIT

Fig. 3.12: Deletion in Doubly Linked List at the end

3.5.2.6 Deletion of the node having given data


 Copy the head pointer into a temporary pointer temp.
 Traverse the list until we find the desired data value.
 Check if this is the last node of the list. If it is so then we can't perform deletion.
 Check if the node which is to be deleted, is the last node of the list, if it so then
we have to make the next pointer of this node point to null so that it can be the
new last node of the list.
 Otherwise, make the pointer ptr point to the node which is to be deleted.
 Make the next of temp point to the next of ptr.
 Make the previous of next node of ptr point to temp. free the ptr.
Algorithm 3.6
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET TEMP = HEAD
Step 3: Repeat Step 4 while TEMP -> DATA != ITEM
Step 4: SET TEMP = TEMP -> NEXT
[END OF LOOP]
Step 5: SET PTR = TEMP -> NEXT
Step 6: SET TEMP -> NEXT = PTR -> NEXT
Step 7: SET PTR -> NEXT -> PREV = TEMP
Step 8: FREE PTR
Step 9: EXIT

Fig. 3.13: Deletion of a specified node in Doubly Linked List at the end

3.5.2.7 Searching for a specific node


To search for a specific element in the list, traverse the list in order. The
following operations to be performed in order to search a specific operation.
 Copy head pointer into a temporary pointer variable ptr
 Traverse the list until the pointer ptr becomes null. Keep shifting pointer to its
next and increasing i by +1.
 Compare each element of the list with the item which is to be searched.
 If the item matched with any node value then the location of that value I will be
returned from the function else NULL is returned.
Algorithm 3.7
Step 1: IF HEAD == NULL
WRITE "UNDERFLOW"
GOTO STEP 8
[END OF IF]
Step 2: Set PTR = HEAD
Step 3: Set i = 0
Step 4: Repeat step 5 to 7 while PTR != NULL
Step 5: IF PTR → data = item
return i
[END OF IF]
Step 6: i = i + 1
Step 7: PTR = PTR → next
Step 8: Exit
3.5.2.8 Traversing in doubly linked list
Traversing is the most common operation in case of each data structure. For this purpose,
 Copy the head pointer in any of the temporary pointer ptr.
 Traverse through the list by using while loop.
 Keep shifting value of pointer variable ptr until we find the last node.
 The last node contains null in its next part.
Algorithm 3.8
Step 1: IF HEAD == NULL
WRITE "UNDERFLOW"
GOTO STEP 6
[END OF IF]
Step 2: Set PTR = HEAD
Step 3: Repeat step 4 and 5 while PTR != NULL
Step 4: Write PTR → data
Step 5: PTR = PTR → next
Step 6: Exit

3.5.3 Advantages of Doubly Linked Lists


 Reversing the doubly linked list is very easy.
 It can allocate or reallocate memory easily during its execution.
 As with a singly linked list, it is the easiest data structure to implement.
 The traversal of this doubly linked list is bidirectional which is not possible in a
singly linked list.
 Deletion of nodes is easy as compared to a Singly Linked List.

3.5.4 Disadvantages of Doubly Linked Lists


 It uses extra memory when compared to the array and singly linked list.
 Since elements in memory are stored randomly, therefore the elements are
accessed sequentially no direct access is allowed.

3.6 CIRCULAR LINKED LIST


 In a circular singly linked list, the last node of the list contains a pointer to the
first node of the list.
 We can have circular singly linked list as well as circular doubly linked list.
 We traverse a circular singly linked list until we reach the same node where we
started.
 The circular singly liked list has no beginning and no ending.
 There is no null value present in the next part of any of the nodes.
 Circular linked list are mostly used in task maintenance in operating systems. Fig.
3.14 shows a circular singly linked list.
Fig. 3.14: Circular Singly Linked List.
3.6.1 Memory Representation of circular linked list
 Fig 3.15 shows the memory representation of a circular linked list containing
marks of a student in 4 subjects. However, the image shows a glimpse of how
the circular list is being stored in the memory. The start or head of the list is
pointing
the next part. Which means that it is linked with the node that is being stored at
4th index of the list.
 However, due to the fact that we are considering circular linked list in the
memory therefore the last node of the list contains the address of the first node
of the list.
Fig. 3.15: Memory Representation of Circular Linked List

3.6.2 Operations on Circular Singly linked list


Table 3.2 describes all the operations performed on a Doubly Linked List
Table 3.2: Operations on Doubly Linked List
Sl. No. Operation Description

Insertion at beginning Adding a node into circular singly linked list at


1.
the beginning.

Insertion at end Adding a node into circular singly linked list at


2.
the end.

Deletion at beginning Removing the node from circular singly linked


3.
list at the beginning.

Searching Compare each element of the node with the


4. given item and return the location at which the
item is present in the list otherwise return null.
Traversing Visiting each element of the list at least once in
5.
order to perform some specific operation.
3.6.2.1 Insertion at the beginning
 There are two scenario in which a node can be inserted in circular singly linked
list at beginning. Either the node will be inserted in an empty list or the node is
to be inserted in an already filled list.
 Firstly, allocate the memory space for the new node by using the malloc method
of C language.
 In the first scenario, the condition head == NULL will be true. Since, the list in
which, we are inserting the node is a circular singly linked list, therefore the only
node of the list (which is just inserted into the list) will point to itself only.
 Also need to make the head pointer point to this node.
 In the second scenario, the condition head == NULL will become false which
means that the list contains at least one node.
 In this case, traverse the list in order to reach the last node of the list.
 At the end of the loop, the pointer temp would point to the last node of the list.
 Make the next pointer of the last node point to the head node of the list and the
new node which is being inserted into the list will be the new head node of the
list
 Therefore the next pointer of temp will point to the new node ptr.
Algorithm 3.9
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step 4: SET NEW_NODE -> DATA = VAL
Step 5: SET TEMP = HEAD
Step 6: Repeat Step 8 while TEMP -> NEXT != HEAD
[END OF LOOP]
Step 8: SET NEW_NODE -> NEXT = HEAD
Step 9: SET TEMP → NEXT = NEW_NODE
Step 10: SET HEAD = NEW_NODE
Step 11: EXIT

Fig. 3.16: Insertion into a Circular Linked List at the


beginningInsertion at the end
 In the second scenario, the condition head == NULL will become false which
means that the list contains at least one node.
 In this case, traverse the list in order to reach the last node of the list.
 At the end of the loop, the pointer temp would point to the last node of the list.
 The existing last node i.e. temp must point to the new node ptr
Algorithm 3.10
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 1
[END OF IF]
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR -> NEXT
Step4:7:SET
Step Repeat Step 8 while
NEW_NODE -> TEMP
DATA-> NEXT !=
= VAL
HEAD
Step StepNEW_NODE
5: SET 8: SET TEMP->=NEXT
TEMP=->HEAD
NEXT
[END OF LOOP]
Step 6: SET TEMP = HEAD
Step 9: SET TEMP -> NEXT =
NEW_NODE Step 10: EXIT
Fig. 3.17: Insertion into a Circular Linked List at the end

3.6.2.3 Deletion at the beginning


In order to delete a node in circular singly linked list, we need to make a few
pointer adjustments. There are three scenarios of deleting a node from circular singly
linked list at beginning.
 Scenario 1: (The list is Empty) - If the list is empty then the condition head ==
NULL will become true, in this case, just to print underflow on the screen and
make exit.
 Scenario 2: (The list contains single node) - If the list contains single node then,
the condition head → next == head will become true. In this case, delete the
entire list and make the head pointer free.
 Scenario 3: (The list contains more than one node) - If the list contains more
than one node then, in that case, traverse the list by using the pointer ptr to reach
the last node of the list.
 At the end of the loop, the pointer ptr point to the last node of the list.
 The last node of the list will point to the next of the head node.
 Now, free the head pointer by using the free() method.
 Make the node pointed by the next of the last node, the new head of the list.
Algorithm 3.11
Step 1: IF HEAD = NULL
Write UNDERFLOW
Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Step 4 while PTR → NEXT != HEAD
Step 4: SET PTR = PTR → next
[END OF LOOP]
Step 5: SET PTR → NEXT = HEAD → NEXT
Step 6: FREE HEAD
Step 7: SET HEAD = PTR → NEXT
Step 8: EXIT
3.6.2.4 Deletion at the end
 Scenario 1 (the list is empty) - If the list is empty then the condition head ==
NULL will become true, in this case, just to print underflow on the screen and
make exit.
 Scenario 2(the list contains single element) - If the list contains single node then,
the condition head → next == head will become true. In this case, delete the

a
entire list and make the head pointer free.

a
 Scenario 3(the list contains more than one element) - If the list contains more
than one element, then in order to delete the last element, reach the last node.

in iy
Also keep track of the second last node of the list. For this purpose, the two
pointers ptr and preptr are defined.

n . or
 Make just one more pointer adjustment. We need to make the next pointer of
.p
preptr point to the next of ptr (i.e. head) and then make pointer ptr free.
Algorithm 3.12
w
Step 1: IF HEAD = NULL
w

Write UNDERFLOW
w

Go to Step 8
[END OF IF]
Step 2: SET PTR = HEAD
Step 3: Repeat Steps 4 and 5 while PTR -> NEXT != HEAD
Step 4: SET PREPTR = PTR
Step 5: SET PTR = PTR -> NEXT
[END OF LOOP]
Step 6: SET PREPTR -> NEXT = HEAD
Step 7: FREE PTR
Step 8: EXIT
a a
in iy
Fig. 3.19: Deletion in a Circular Linked List at the end

n . or
3.6.2.5 Searching
 Searching in circular singly linked list needs traversing across the list.
.p
 The item which is to be searched in the list is matched with each node data of the
w
list once.
 If the match found then the location of that item is returned otherwise -1 is
w

returned.
w

Algorithm 3.13
STEP 1: SET PTR = HEAD
STEP 2: Set I = 0
STEP 3: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF
STEP 4: IF HEAD → DATA = ITEM
WRITE i+1 RETURN [END OF IF]
STEP 5: REPEAT STEP 5 TO 7 UNTIL PTR->next != head
STEP 6: if ptr → data = item
write i+1
RETURN
End of IF
STEP 7: I = I + 1
STEP 8: PTR = PTR → NEXT
[END OF LOOP]
STEP 9: EXIT

3.6.2.5 Searching

a
 Traversing in circular singly linked list can be done through a loop.

a
 Initialize the temporary pointer variable temp to head pointer and run the while

in iy
loop until the next pointer of temp becomes head.

n . or
Algorithm 3.14
STEP 1: SET PTR = HEAD .p
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
w
GOTO STEP 8
w

END OF IF
w

STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR → NEXT != HEAD


STEP 5: PRINT PTR → DATA
STEP 6: PTR = PTR → NEXT
[END OF LOOP]
STEP 7: PRINT PTR→ DATA
STEP 8: EXIT

3.6.3 Advantages of Circular Linked Lists


It is possible to traverse from the last node back to the first i.e. the head node.
 The starting node does not matter as we can traverse each and every node
despite whatever node we keep as the starting node.
 The previous node can be easily identified.
 There is no need for a NULL function to code. The circular list never identifies a
NULL identifier unless it is fully assigned.
3.6.4 Disadvantages of Circular Linked Lists
 If the circular linked list is not handled properly then it can lead to an
infinite loop as it is circular in nature.
 In comparison with singly-linked lists, doubly linked lists are more
complex in nature
 Direct accessing of elements is not possible.
 It is generally a complex task to reverse a circular linked list

You might also like