Unit 1- Datastructures
Unit 1- Datastructures
must adhere.
List ADT
The data is generally stored in key sequence in a list which has a head structure consisting
of count, pointers and address of compare function needed to compare the data in the list.
The data node contains the pointer to a data structure and a self-referential pointer which points to the next
node in the list.
The List ADT Functions is given below:
get() – Return an element from the list at any given position.
insert() – Insert an element at any position of the list.
remove() – Remove the first occurrence of any element from a non-empty list.
removeAt() – Remove the element at a specified location from a non-empty list.
replace() – Replace an element at any position by another element.
size() – Return the number of elements in the list.
isEmpty() – Return true if the list is empty, otherwise return false.
isFull() – Return true if the list is full, otherwise return false.
I) Array
Array is a type of data structure that stores data elements in adjacent locations. Array is
considered as linear data structure that stores elements of same data types. Hence, it is also called
as a linear homogenous data structure.
When we declare an array, we can assign initial values to each of its elements by enclosing the
values in braces { }.
Num 26 7 67 50 66
A linked list is a data structure in which each data element contains a pointer or link
to the next element in the list. Through linked list, insertion and deletion of the data
element is possible at all places of a linear list. Each node in the list contains
information field and a pointer field. The information field contains the actual data
and the pointer field contains address of the subsequent nodes in the list.
Figure 1.3 represents a linked list with 4 nodes. Each node has two parts. The left part in the node
represents the information part which contains an entire record of data items and the right part
represents the pointer to the next node. The pointer of the last node contains a null pointer.
• Let us follow a convention that position i is a pointer to the cell holding the pointer to the
cell containing ai, (for i = 1, 2, . . . , n). Thus,
• A linked list in which the node at the tail of the list, instead of having a null pointer,
points back to the node at the head of the list. Thus both ends of a list can be accessed
using a single pointer.
See Figure 2.11.
• If we implement a queue as a circularly linked list, then we need only one pointer namely
tail, to locate both the front and the back.
Tail
Before Insertion
After broken
a // b
Insertion
p x
temp
header
Figure 2.3: Deletion in a singly linked list
a1 a2 an-1 an
Polynomial Manipulation
Polynomial manipulations are one of the most important applications of linked lists. Polynomials are an important
part of mathematics not inherently supported as a data type by most languages. A polynomial is a collection of
different terms, each comprising coefficients, and exponents. It can be represented using a linked list. This
representation makes polynomial manipulation efficient.
o The first part contains the value of the coefficient of the term.
o The second part contains the value of the exponent.
o The third part, LINK points to the next term (next node).
The structure of a node of a linked list that represents a polynomial is shown below:
Consider a polynomial P(x) = 7x 2 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the coefficients, and 4,3,2,0 are the
exponents of the terms in the polynomial. On representing this polynomial using a linked list, we have
Observe that the number of nodes equals the number of terms in the polynomial. So we have 4 nodes. Moreover,
the terms are stored to decrease exponents in the linked list. Such representation of polynomial using linked lists
makes the operations like subtraction, addition, multiplication, etc., on polynomial very easy.
Addition of Polynomials:
To add two polynomials, we traverse the list P and Q. We take corresponding terms of the list P and Q and
compare their exponents. If the two exponents are equal, the coefficients are added to create a new coefficient. If
the new coefficient is equal to 0, then the term is dropped, and if it is not zero, it is inserted at the end of the new
linked list containing the resulting polynomial. If one of the exponents is larger than the other, the corresponding
term is immediately placed into the new linked list, and the term with the smaller exponent is held to be compared
with the next term from the other list. If one list ends before the other, the rest of the terms of the longer list is
inserted at the end of the new linked list containing the resulting polynomial.
Let us consider an example an example to show how the addition of two polynomials is performed,
Q (x) = 5x3 + 4 x2 - 5
These polynomials are represented using a linked list in order of decreasing exponents as follows:
1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two polynomials. The first term of polynomials P
and Q contain exponents 4 and 3, respectively. Since the exponent of the first term of the polynomial P is
greater than the other polynomial Q, the term having a larger exponent is inserted into the new list. The
new list initially looks as shown below:
We then compare the exponent of the next term of the list P with the exponents of the present term of list Q. Since
the two exponents are equal, so their coefficients are added and appended to the new list as follows:
Then we move to the next term of P and Q lists and compare their exponents. Since exponents of both these terms
are equal and after addition of their coefficients, we get 0, so the term is dropped, and no node is appended to the
new list after this,
Then we move to the next term of P and Q lists and compare their exponents. Since exponents of both these terms
are equal and after addition of their coefficients, we get 0, so the term is dropped, and no node is appended to the
new list after this,
Moving to the next term of the two lists, P and Q, we find that the corresponding terms have the same
exponents equal to 0. We add their coefficients and append them to the new list for the resulting polynomial as
shown below:
Adding a new node in linked list is a more than one step activity. We shall learn this with diagrams here. First,
create a node using the same structure and find the location where it has to be inserted.
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point B.next
to C −
Now, the next node at the left should point to the new node.
This will put the new node in the middle of the two. The new list should look like this −
Insertion in linked list can be done in three different ways. They are explained as follows −
Insertion at Beginning
Algorithm
. START
2. Create a node to store the data
3. Check if the list is empty
4. If the list is empty, add the data to the node and
assign the head pointer to it.
5. If the list is not empty, add the data to a node and link to the
current head. Assign the head to the newly added node.
6. END
Insertion at Ending
In this operation, we are adding an element at the ending of the list.
Algorithm
1. START
2. Create a new node and assign the data
3. Find the last node
4. Point the last node to new node
5. END
Insertion at a Given Position
In this operation, we are adding an element at any position within the list.
Algorithm
1. START
2. Create a new node and assign data to it
3. Iterate until the node at position is found
4. Point first to new first node
5. END
Deletion is also a more than one step process. We shall learn with pictorial representation.
First, locate the target node to be removed, by using searching algorithms.
The left (previous) node of the target node now should point to the next node of the target
node −
This will remove the link that was pointing to the target node. Now, using the following code,
we will remove what the target node is pointing at.
We need to use the deleted node. We can keep that in memory otherwise we can simply
deallocate memory and wipe off the target node completely.
Similar steps should be taken if the node is being inserted at the beginning of the list. While
inserting it at the end, the second last node of the list should point to the new node and the
new node will point to NULL.
Deletion in linked lists is also performed in three different ways. They are as follows −
Deletion at Beginning
In this deletion operation of the linked, we are deleting an element from the beginning of the
list. For this, we point the head to the second node.
Algorithm
1. START
2. Assign the head pointer to the next node in the list
3. END
Deletion at Ending
In this deletion operation of the linked, we are deleting an element from the ending of the list.
Algorithm
1. START
2. Iterate until you find the second last element in the list.
3. Assign NULL to the second last element in the list.
4. END
Algorithm
1. START
2. Iterate until find the current node at position in the list.
3. Assign the adjacent node of current node in the list
to its previous node.
4. END
Linked List - Traversal Operation
The traversal operation walks through all the elements of the list in an order and displays the elements in that order.
Algorithm
1. START
2. While the list is not empty and did not reach the end of the list,
print the data in each node
3. END
Let’s try to understand the problem statement with the help of examples.
So now, according to the problem statement, the given linked lists List1 and List2 are
unsorted, and we need to merge them to get a sorted linked list.
After merging the linked lists, we need to return the head of the merged linked list.
After merging the two linked lists in the above example, the sorted linked lists will be
head → 1 → 3 → 5 → 6 → 9 → 10 → NULL.
Example 1:
Input:
Output :
head → 1 → 4 → 10→12 → 50 → 60
Example 2:
Input:
Output: