0% found this document useful (0 votes)
2 views

Unit 1- Datastructures

The document describes abstract data types, specifically focusing on the List ADT and its operations such as get, insert, remove, and replace. It explains data structures like arrays and linked lists, detailing their characteristics, advantages, and disadvantages, as well as various implementations including singly, circular, and doubly linked lists. Additionally, it covers polynomial manipulation using linked lists and provides algorithms for insertion, deletion, and traversal operations in linked lists.

Uploaded by

njkumar
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)
2 views

Unit 1- Datastructures

The document describes abstract data types, specifically focusing on the List ADT and its operations such as get, insert, remove, and replace. It explains data structures like arrays and linked lists, detailing their characteristics, advantages, and disadvantages, as well as various implementations including singly, circular, and doubly linked lists. Additionally, it covers polynomial manipulation using linked lists and provides algorithms for insertion, deletion, and traversal operations in linked lists.

Uploaded by

njkumar
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/ 17

An abstract data type is an abstraction of a data structure that provides only the interface to which the data structure

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 { }.

int Num [5] = { 26, 7, 67, 50, 66 };


This declaration will create an array as shown below:
0 1 2 3 4

Num 26 7 67 50 66

Figure 1.2 Array


In the example of array Paul, we have declared 5 elements and in the list of initial values within
braces { } we have specified 5 values, one for each element. After this declaration, array Paul will
have five integers, as we have provided 5 initialization values.

Arrays can be classified as one-dimensional array, two-dimensional array or multidimensional


array.
One-dimensional Array: It has only one row of elements. It is stored in ascending storage
location.
Two-dimensional Array: It consists of multiple rows and columns of data elements. It is also
called as a matrix.
Multidimensional Array: Multidimensional arrays can be defined as array of arrays.
Multidimensional arrays are not bounded to two indices or two dimensions. They can
include as many indices as required.

II) Linked List

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: A Linked 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.

Advantage: Easier to insert or delete data elements


Disadvantage: Slow search operation and requires more memory space

• Singly Linked List Implementation


A list a1, a2, . . . , an is organized as shown in Figure 2.1

• 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,

– Position 1 is a pointer to the header

Circular Linked List Implementation

• 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

Figure 2.11: Circular linked list implementation


a b

Before Insertion

After broken
a // b
Insertion

p x

temp

Figure 2.2: Insertion in a singly linked list

– End (L) is a pointer to the last cell of list L

• If position of ai is simply a pointer to the cell holding ai, then

– Position 1 will be the address in the header

– end (L) will be a null pointer

• Insert (x, p, L) : See Figure 2.2

• Delete (x, L) : See Figure 2.3

Doubly Linked List Implementation

∗ makes searches twice as efficient


∗ needs as many extra pointers as the number of elements (See Figure 2.4); consequently
insertions and deletions are more expensive in terms of pointer assignments
a b c

header
Figure 2.3: Deletion in a singly linked list
a1 a2 an-1 an

Figure 2.4: A doubly linked list

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.

Each node of a linked list representing polynomial constitute three parts:

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,

P(x) = 3x4 + 2x3 - 4 x2 + 7

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:

Linked List - Insertion Operation

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 −

NewNode.next -> RightNode;

It should look like this −

Now, the next node at the left should point to the new node.

LeftNode.next -> NewNode;

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

In this operation, we are adding an element at the beginning of the list.

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

Linked List - Deletion Operation

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 −

LeftNode.next -> TargetNode.next;

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.

TargetNode.next -> NULL;

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

Deletion at a Given Position


In this deletion operation of the linked, we are deleting an element at any position of the list.

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

Problem Statement Understanding merge two unsorted linked lists

Let’s try to understand the problem statement with the help of examples.

Let’s say the given linked lists are :

List1 = head → 1 → 6 → 10 → NULL ,


List2 = head → 5 → 3 → 9 → NULL

 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.

The linked list will be sorted in ascending order.

Some more Examples

Example 1:

Input:

List1 = head → 4 → 50→ 12 → NULL

List2 = head → 10 →1→ 60 → NULL

Output :
head → 1 → 4 → 10→12 → 50 → 60

Example 2:

Input:

List 1 = head → 10 → 20 → 60 → NULL

List 2 = head → 10 → 50 → 30 → NULL

Output:

head →10 → 10 → 20 → 30 → 50 → 60 → NULL

You might also like