UNIT 01
LINEAR
DATA STRUCTURES - LIST
1.1 INTRODUCTION
The term data refers to numerical or other information represented
in a form, suitable for processing by a computer. Structure can be defined
as the way in which parts are arranged or put together to form a whole.
A data structure can be defined as the combination of data and all
the potential operations, which are required for those set of data.
Data Item
‘A data item refers to a single unit of data.
x. Data items that are divided into sub items are called group items.
Example: Emp.Name
4x Data ‘items that are not divided into sub items are called
- Elementary items. Example: Social security number.
Collection of data are frequently organized into a hierarchy of fields,
records and files.
* A field is a single elementary unit of information.
* OA record is the collection of field data.
+ A file is the collection of records.Data Structure and Algorithms
1.2
Each record
certain field may uniquely det
ina file may contain many field items, but the value in g
ermine the record in the file. Such a field ig
called a primary key.
Primary key file
t field or attribute
RNo | Name Dept | Percentage
Le Sasi kumar Record
2. Mani
The major tasks done in any computer system are based on three
activities storing, accessing, manipulations of data. People need to have
an abstract thinking to understand the principles and techniques to solve
the huge collection of unfamiliar problems that arise ina rapidly changing
field. Even a good programmer may unable to write new algorithms for the
given problem if he has no knowledge in data structures.
Definition
Data structure is a collection of data elements organized in a
: specified manner and accessing functions are defined to store and retrieve
individual data elements.
Data structures are widely applied in the following areas:
%* — Compiler design
Operating system
Statistical analysis package
DBMS
Numerical analysis
Simulation
e He He He
Artificial intelligenceLinear Data Structures - List . 13
1.1.1 Basic Operations of Data Structure
All data in the data structures are Processed by some specific
operations. Names of the such operations are listed below:
1. Traversing
Processing each element in the specific data structure.
2. Search
Finding the location of the element with a given value.
3. Insertion
Adding a new element to the list or array.
4. Deletion
Removing an element from the list or array.
5. Sorting
Arranging the elements in some type of order.
6. Merging
Combining two lists into a single list.
1.2 CLASSIFICATION OF DATA STRUCTURES
Data structure can be classified into two categories:
1. Primitive data structure
2. Non-primitive data structureData
Structures
Non-primitive data structure
[Primitive data structure
[>| Integer (ae A
|) Boolean Linear © Non-linear
t—> Float Lyf Array Lp! trees
‘— > Character Li gtaok -» Graphs
> Queue
Lot Tinked list
Fig 1.1: Classification of Data Structures
1.2.1 Primitive Data Structure
These types. of data types are pre-defined and has a fixed set of
rules for declaration. In other words, these data types when belong toa
particular programming language has built in support and hence they are
also called as built-in data types. Basic data types such as integer, real,
character, and Boolean come under primitive data structures.
1.2.2 Non - Primitive Data Structure
Non-primitive data structures
are classified — into two: majot
categories ;
1. Linear data structureLinear Data Structures - List
15
1. Linear data structure
A data structure is said to be linear if its elements form a sequence.
The common examples of linear data structures are:
a. Array
b. Stack
\c. Queue
d. Linked List
a. Array
An array refers to the homogeneous collection of elements stored
contiguously in memory. Arrays follow static allocation of memory (i.e.,)
once the size of the array is declared it cannot be expanded nor contracted,
Example: char Array [5]
b. ‘Linked List !
“Linked list is linear collection of data elements. Each element
called as a node, each and every node consists of two parts are :
1. Data
2. Link to next node.
¢c. Stack
A stack is also an ordered collection of elements like array but it
has a special feature that deletion and insertion of elements can be done
only from one end, called the top of the stack. It is also called as Last In
First Out (LIFO) type of data structure.
Examples :
__..... . Train, stack of trays etc., Radwanane ai nnd. Queues inl
ita structures,
Queues are First In First Out (FIFO) type of dat Na
nd called rear e,
queue new elements are added to the queue from one é1 nd
d the front
and the elements are always removed from other end calle Sront end,
Example :
Queue of railway reservation.
2. Non-Linear data structure
A data structure is said to be non linear if its elements do not form q
sequence,
Non-linear data type are listed below:
i. Trees
ii. Graphs
i. Tree
* — Tree is a non-linear data Structure. A tree consists of a collection
of nodes which are connected by directed arcs. .
* A tree contains a unique first element called as root (i.e.,) node
- at the top of the tree. E
ii. Graph
%* Graphs are non-linear data Structures used to represent network
structures,
* Agraph is defined as Gv; 2), where Vis the set of vertices (nodes)
and E is the set of edges (arcs) Connecting the Vertices,
*
Ant edge is Tepresented as g Pair of vertices (uy, v),Linear Data Structures - List
1.2.3 Difference between Linear and No ear Data ws
S.No Linear Data Structure Non - Linear Data Structure
1. Elements are ordered in a Elements are ordered in a
linear and sequential manner, hierarchy,
24 Only a single level is Present. | Multiple level data structures
are present.
3. Implementation is relatively Implementation is relatively
easier. complicated.
4. Traversed in a single run. Take multiple runs to traverse
the data.
5: Memory utilization is not Memory utilization is efficient.
efficient compared to non-
linear data structures.
6. Examples: array, “queue, Examples: Trees, graphs, etc.
linked list, ete
1.3 ABSTRACT DATA TYPES (ADT)
Abstract-Data Type (ADT) is a specification of a set of data and
the set of operations that can be performed on the data.
It
defines the behavior of the data type
rather than its
implementation details. The ADT encapsulates the data and the operations
that can be performed on that data.
Examples :
Array, Stack, Linked List, Queue, tree, etc.,
1.3.1. ADT Operations
Various Abstract Data Types Operations are,
1. Create : Create the database.Data Structure and Algorithm,
1.8 -
jesired position,
3. Insertion : The element can be inserted at any d
4. Deletion : Any desired element can be deleted.
n ; This operation modifies the desired element's
5. Modificatio
value by any desired new. value.
1.3.2 Advantages of ADT
Some advantages of ADT over the conventional data structures are
ADT is reusable, robust, and is based on principles of Object
Oriented Programming (OOP) and Software Engineering (SE).
An ADT can be re-used at several places and it reduces coding
*
*
efforts. :
%* — Encapsulation ensures that data cannot be corrupted.
Working of various integrated operation cannot be tampered with
by the application program. ;
%* ADT ensures a robust data structure.
1.4 LIST ADT
List is the collection of elements in sequential order. In memory, we
can store the list in two ways.
%* Sequential memory location - Array
* — Pointer or links to associate the elements sequential - Linked list.
List of sequentially stored elements - Using Arrays
GE ee ae
List of elements with associated Pointers - Using Linked List
Fig 1.2 : Array and List representation 02/04/202
ALinear Data Structures - List
- 1.9
The list can be implemented by two ways :
1, Array. based implementation
2. Linked list based implementation
1.5 ARRAY BASED IMPLEMENTATION
%* — The simplest way of Tepresenting a list using array.
% It is an ordered collection of element
stored in continuous
memory locations.
Example:
List A
36 23 49 50 12 55
AO] A[l] A[2],. AB] Al4] ALS]
The basic operations performed on a list of elements are
1. Creation of List.
2. Insertion of data in the List
3. Deletion of data from the List
4. Display all data’s in the List
5. Searching for a data in the list
1.5.1 Advantages of array implementation
1. The elements are faster to access.
2. Searching an element is easier.
3. It allow for constant-time access to elements by their index, making
it efficient to retrieve and update elements at specific positions.
4. It use contiguous memory allocation, which can result in better
cache nerfarmance and reduced memory overhead compared to1,10 Data Structure and Algorith,,
$. Arrays have a straightforward implementation, making them easy ;,
understand and work with,
1.5.2 Limitations of array implementation
1. Size of the array must be declared at the time of programming. Th,
may cause underutilization or overflow,
13
. It requires contiguous memory,
Insert and delete operation are time consuming.
1.8 LINKED LIST
A linked list is a linear collection of data elements called Nodes
where the linear order is given by means of Pointers.
Each node is divided into two parts, the first part contains the
information of the clement and the second part called the link field or next
pointer field, contains the address of next node in the list.
The following figure is a schematic diagram of a linked list with
three nodes.
»
Address of next node
Stores actual value
START (or) HEAD
Information part Ne i
of ™ node ye pst fieldLinear Data Structures - List 1
Each node is divided into two parts,
% — The left part represents the information(Data) part of the node.
%* — The right part represents the next pointer field of the node.
%* — The pointer of the last node contains a special value called the null
pointer, which is any invalid address,
* = The null pointer signals the end of the list.
The linked list contains a list pointer variable called START (or)
HEAD which contains the address of the first node in the list.
* If the value of START is Null then the list is called the Null list
or Empty list.
1.6.1 Advantages of Linked List
1, Insertion and deletion-of elements can be done efficiently.
2. It uses dynamic memory allocation.
3. Memory utilization is efficient compared to arrays.
4. Many complex applications can be easily carried out with
linked list. .
1.6.2 Disadvantages of linked list
1. Linked list does not support random access.
2. Memory is required to store next field.
3. Searching takes time compared to arrays.
1.6.3 Applications of Linked List
1. Linked lists are used in programming requiring dynamic memory
allocation.
2. Advance scientific calculations and numerical analysis using sparse
matrices. The sparse matrix is represented by a linked list.112
Data Structure and Algorithm,
4. It is used in reversing the functionality of the software.
5. Hash tables are created using linked lists. Hash
6.
implemented by a linked list.
table entries ar,
Graphs use linked lists. If we want to represent graphs as aan
adjacency list, we use linked lists.
. Polynomials can be represented with the help of linked lists.
term in a polynomial has coefficient and power. Coefficient and
power are stored as nodes and pointer points to the next node.
1.6.4 Difference between Array and Linked list
I of
elements for insertion and
_| deletion.
needs movements
Array Linked List
Insertions and deletions are | Insertions and deletions can be
difficult, done casily.
It does not need movement
of nodes for
| deletion.
insertion and
In it space is wasted.
It is more expensive.
It requires less space as only
information is stored.
In it space is not wasted.
It is less expensive.
It requires more space as
Pointers are also stored along
L with information.
6. Its size is fixed. Its size is not fixed,
7 It can not be extended | It can be extended or reduced
or reduced according to according to requirements.
requirements,
8. Same amount of time
is
required to
element,
access each
consecutive
memory
locations.
Different amount of time is
Tequired to access each element.
9, Elements are stored in
Elements may or may not be
Stored in Consecutive memoryLinear Data Structures - List 13
1.6.5 Types of Linked List
‘Types of linked lists are,
1. Singly Linked List
2. Doubly Linked List
3. Circularly Linked List
Linked List
| Singly | Doubly Circularly
Linked List Linked List Linked List
Fig: 1.4: Linked list types
1.7 SINGLY LINKED LIST
A Singly Linked List(SLL) is .a sequence of nodes connected to
one another using pointers. Traversing a singly linked list is done in a
Sorward direction. Linked lists do not have a fixed size, and their elements
(nodes) are not stored in contiguous memory locations.
Each node is divided into two fields:
1. Data: This field stores the actual data of the node.
‘2. Next: This field contains a memory address (or pointer) to the
next node in the sequence.
Node
Data | Next Pointerand Algorithms
1.14 Data Structure
Structure of the singly linked list
[Struct node |
{
int data; // data |
struct node * next; // Address Ls
‘ |
med in Singly Linked List
4.7.1 Operations Perfor:
ly linked list (Data, next pointer)
+* Creation — Create a node for sing!
+ Insertion — Adds an element at the beginning, ending and given
position of the list.
+ Deletion — Deletes an element at the beginning, ending and
given position of the list.
4 Search — Searches an element using the given key.
+ - Traverse — Traverse the complete list:
1.7.2 Creation
Creation operation is used to create'a linked-list. Once a linked list
is created with one node, insertion operation can be used to add more
elements in a node. :
1. Algorithm Function CreateNode(data):
newNode = new Node()
newNode.data = data
newNode.next = NULL
return newNode
End Alvsorithm
yawLinear Data Structures - List 115
ae iS
Head
101
Y 101
20 {NULL
Fig: 1.5 : Create a node with DATA(20)
1.7.3 Insertion
There are various positions where a node can be inserted:
(i). Inserting at the beginning (as a first element)
(ii) Inserting at the given position
(ii) Inserting at the end (as a last element)
1. Insertion of node at the Beginning ©
’ The-algorithm is used to insert a node at the beginning of a single
linked list.
Algorithm insertAtBeginning(value):
newNode = CreateNode(value)
If head is NULL:
head = newNode
Else:
newNode.next = head.
head = newNode
End If
End Algorithm
CPI AwAAwWN =
Example:
Step 1: Create a newNode with given data eg. 10Data Structure and. Algorithn,
1.16
ANG at SN eerithns
102 P 103 104
101
40 [ 104 |_| 50 Null
[101 Lf 20 102 »[ 30 | 103 >| aa
105
_newNode
Step 2: if head not null
newNode.next = head
head = newNode
101 102 103 104
101 20 | 1024-5] 30 103 | 40 104] 50. | Nu
newNode ‘i
10 | 1014
105
Fig: 1.6 : Insertion of node at the beginning
2. Insertion of node at the Given Position:
The algorithm is used to insert anode into a sin;
gle linked list at given
Position in the list.
Algorithm Function insertNodeAtPosit
newNode = createNode(data)
currentNode=head
ion(head, data, Position):
while currentpos
[i
previosNode = 102 CurrentNode = 103
Currentpos = 3
Step 2: previousNode.next = currentNode.next
HEAD 101 104
101 20° | 102 50 | Null
previosNode = 102 CurrentNode = 103
Currentpos = 3
Fig: 1.10 : Deletion of node at the given position
3. Deletion of node at the End1.22 Data Structure and Algorithm,
oe eeeeaeaeeea se
1. Algorithm deleteNodeAtEnd()
2. ifhead == NULL
3. Display "List is empty, deletion not possible”
4, Else If head.next = NULL
5. head = NULL
6. Endif
7. currentNode = head
8. while currentNode.next.next # NULL
9. currentNode = currentNode.next
10. currentNode.next = NULL
11. End while
12, End Algorithm
Example: Delete node at end position node 104 will be deleted.
Step 1: If head.next = NULL
head = NULL
HEAD 01 HEAD
7 1
101 Lf 20 Null —_ Null
Step 2: while currentNode.next.next # NULL
currentNode = currentNode.next
aN) 101 102 103 104
—
101 20 | 102 44 30 | 13 | fo | 104 LI 50. | Null
I
CurrentNode = 103
Step 3: currentNode.next =Null
HEAD
+ 101 102 103
— [> fo} fe fa]
ig: 1.11 : Deletion of node at the endLinear Data Structures = List 1.23
1.7.5 Searching of Nodo
Algorithm searchNode(targely:
currentNode @hend
while currentNodel™ NULL:
if currentNode,data == target;
return current
currentNode = currentNode,next
return NULL
End Algorithm
SIAR YD Se
1.7.6 Traversing of Node
In traversing a single linked list, we will visit every node in the list
starting from the first node to the last node.
Algorithm TraverseLinkedList(head):
current_node = head
while current_node is not NULL:
1.
2.
3.
4. ProcessNode(current_node)
5. current_node = current_node.next
6.
End Algorithm
1.7.7 Advantages
1) Singly linked list can store data in non-contiguous locations. Thus
there is no need of compaction of memory, when some large related
data is to be stored into the memory.
2) Insertion and deletion of values is easier as compared to array, as no
shifting of values is involved. ’
1.7.8 Disadvantages
1) Nodes can only be accessed sequentially. That means, we cannot
jump to a particular node directly.1.24 Data Structure and Algorithm,
2) Because of the above disadvantage,
be implemented on the singly linked list.
k from one node to previous one. Only
binary search algorithm canny
3) There is no way to go bacl
forward traversal is possible.
1.7.9 Applications
x Linked Lists can be used to represent Graphs.
3x Used to implement Queue and Stack Data structures.
% Used for performing arithmetic operations on long integers.
*
Used for manipulating polynomials.
1.8 DOUBLY LINKED LIST
A double linked list is a two-way list in which all n
. Itis a bi-directional linked list,
odes will have
two links (previous node and next node).
can traverse it in both directions.
It consists of three field :
1. Data : It contains actual data.
2. Next : It contains the memory address of the next node.
3, Prev: It contains the memory address of the previous node.
Structure of the Doubly linked list
struct node
{
int data;
struct node * next;
struct node * prey;Linear Data Structures - List 125
Linear Data Structures Tag
Points to Next node
Points to Previous node Actual value
pla TAIL
ry
| el 1 Db
L, Next Node
Data
Previous Node
Fig: 1.12 : Doubly linked list
Operations Performed in Doubly Linked List
%* Creation
Insertion
:
* ~~ Deletion
%* ~~ Search
*
Traverse
1.8.1 Creation
1. Algorithm CreateNode(data):
. newNode = new Node
3. newNode.data = data
- newNode.next = NULL
5. newNode.prev = NULL
6. return newNode
7. End Algorithm1.26 Data Structure and Algorithin,
1.8.2. Insertion
1. Insertion of node at the beginning
1. Algorithm InsertAtBeginning(data):
2. newNode = CreateNode(data)
3. if head ='NULL:
4. head = newNode
5. tail = newNode
6. else:
4. newNode.next = head
8 head.prev = newNode
9. head = newNode
10. End Algorithm
HEAD
102
102
Ljnure]20 [104 ke fto2 30 nut]
100
INULL] 10 | NULL
a) Before Insertion
HEAD
102 104
100 | 20 | 104 102 | 30 |NULL
b) After Insertion
Fig: 1.13 : Insertion of node at the beginnigLinear Data Structures - List
1.27
insertion of node at the end
Algorithm insertNodeAtEnd(data):
newNode = CreateNode(data)
if head == NULL:
head = newNode
tail = newNode
else: :
newNode.prev= tail
tail.next = newNode
ewer nAw Pp wn
tail = newNode
End Algorithm
=
HEAD
102
L{wuti] 20 104 [*}102 | 30 | NULL
102 +104
106
[uute] 4o [NULL
a) Before Insertion
HEAD 102 104
b) After Insertion
Fig: 1.14 : Insertion of node at the end1.28 Data Structure and Algorithy,
3. Insertion of node at given Position
1, Algorithm InsertNodeAtPosition(data, position): -
2. newNode = CreateNode(data)
3. if position == 0;
4, newNode.next = head
5. iff head !=Null
6. head.prev =newNode
7. Endif
8. head = newNode
% else
10, currentNode = head
11. current_position = 0
12, while currentNode != NULL and current_position < position:
13, currentNode = currentNode.next
14, current_position = current_position + 1
15. Endif
16. if currentNode == NULL: -
17: Display “Position out of bound”
18. else
19, newNode.next = currentNode.next
20. newNode.prev = currentNode
21. If current.next!=Null:
22, currentNode.prev.next = newNode
23. currentNode.next = newNode
24. End if
25. End Algorithm110
uu 50 JNULL
a) Before Insertion
HEAD 102
feb fade [re]
b) After Insertion
Fig: 1.15 : Insertion of node at the given position
1.8.3 Deletion
1. Deletion of node at the beginning
Algorithm deleteNodeAtBeginning(head):
if head == NULL:
Display "List is empty, deletion not possible"
else
head = head.next
if head != NULL:
head.prev = NULL
End if
End AlgorithmData Structure and Algorithm,
1.30
1300 ata Structure and Algorithns
a) Before Deletion
HEAD
b) After Deletion
Fig: 1.16 : Deletion of node at the beginning
2. Deletion of node at end
Algorithm DeleteNodeAtEnd(head)
if head = NULL
« Display "List is empty"
else
current =head
while currentNode.next |= NULL
currentNode = currentNode.next
if currentNode.prey != NULL
SP NAAR WD
SurrentNode. prev.next=Null
10. end if
ll. End AlgorithmLinear Data Structures - List
HEAD
uy) 102
nut] 10 102 [*}100 | 20 }NULL
1.31
a) Before Deletion
HEAD
INULL
b) After Deletion .
Fig: 1.7: Deletion of node at the end
3. Deletion of node at given position
HEAD
100
100 102 104
L [nun 10 | 102 ee 100 | 20 |104 F202 | 30 [nu]
a) Before Deletion
HEAD .
100 104
pale Fo Ff]
b) After Deletion
Fig: 1.17 ; Deletion of node at the given position1.32
2.
3,
4.
5.
6.
7,
8,
9,
ve
Pp RPraunpw
10.
i.
1.8.4 Searching of node
Algorithm SearchDoublyLL (head, target):
Data Structure and Algorithm,
1.” Algorithm deleteNodeAtPosition(position):
if position = 1:
if head !=Null
» current = head
End if .
head = head.next
ifhead != NULL:
head.prev = NULL |
End if
else . |
current = head |
for i from 1 to position - 1:
Current = current.next
End for
current.prev.1 next=current, next
if current.next != null
Current.next.prev=current, prev
End if
End if
End algorithm
current = head
while current != NULL:
if current.data target;
Teturn current
else:
current = Current.next
return NULL
End algorithmLinear Data Structures - List 1.33
1.8.5 Traversing of node
Algorithm traverseDoublyLL (head):
current = head
while current != NULL:
1
2.
3,
4. display current.data
5. current = current.next
6.
End Algorithm
1.8.6 Advantages
1, We can traverse in both directions i.e. from starting to end and as
well as from end to starting.
2. Itis easy to reverse the linked list.
3. If we are at a node, then we can go to any node. But in linear linked
list, it is not possible to reach the previous node.
1.8.7 Disadvantages
1. ‘It requires more space per space per node because one extra field is
required for pointer to previous node.
2. Insertion and deletion take more time than linear linked list because
more pointer operations are required than linear linked list.
1.8.8 Applications of Doubly Linked List
%* Doubly linked list can be used in navigation systems where both
front and back navigation is required.
* It is used by browsers to implement backward and forward
navigation of visited web pages i.e. back and forward button.134 Data Structure and Algorith,,
% It is also used by various applications to implement Undo 4,
Redo functionality.
% — It can also be used to represent deck of cards in games,
* — Itis also used to represent various states of a game.
1.8.9 Difference between Singly linked list and Doubly linked list
Doubly linked list
The doubly linked list consist
of three parts: data and two
Singly linked list
The Singly linked list
comprises two parts: data
and link.
It allows for one-way
traversal of elements.
Singly linked lists are often
used to implement stacks.
pointers.
It allows for two-way traversal
Doubly linked lists can be usec
to implement binary trees
heaps, and stacks.
Doubly linked lists are preferred
for better implementation during
search operations.
Singly linked lists are
preferred when memory
conservation is important,
and search operations are
not required.
Singly linked lists use
less memory compared to
doubly linked lists.
Doubly linked lists use more
memory compared to singly
linked lists,Linear Data Structures - List 1.35
CIRCULARLY LINKED LIST
Circular linked list is a linked list where the first and last node are
connected to each other which forms a circle. There is no null at the end.
1.9.1
*
*
1.9.2
1.9.3
Advantages of Circular Linked List
No requirement for a NULL assignment.
Circular linked lists are advantageous for end operations since
beginning and end coincide. Algorithms such as the Round Robin
scheduling can neatly eliminate processes which are queued in
a circular fashion without encountering dangling or NULL-
referential pointers.
Circular linked list also performs all regular functions of a singly
linked list. In fact, circular doubly linked lists discussed below
can even eliminate the need for a full-length traversal to locate an
element. That element would at most only be exactly opposite to
the start, completing just half the linked list.
Disadvantages of Circular Linked List
Circular lists are complex as compared to singly linked lists.
Reverse of circular list is a complex as compared to singly or
doubly lists.
If not handled carefully, then the code may go in an infinite loop.
Harder to find the end of the list and loop control.
Inserting at Start, we have to traverse the complete list to find the
last node. (Implementation Perspective).
Applications of Circular Linked List
Useful for implementation of queue.
It is also used by the Operating system for job scheduling (eg.
Round-Robin Algorithm).1.36 Data Structure and Algorithms
%* Multiplayer games use a circular list to. swap between players in
aloop.
%* Helps to implement Advanced data structures like Fibonacci Heap,
There are two types of circular linked lists
1. Singly Circular linked list
2. Doubly Circular linked list
Creation of a node at circular linked list
Algorithm function CreateNode(data):
newNode = new Node()
if head = NULL:
newNode.next = newNode
head = newNode
else:
Current = head
while current.next != head:
current = current.next
newNode.next = head
Current.next = newNode
return newNode
End Algorithmvie
Data Structure. and Algorithm,
1,38
HEAD :
a rol
101 102 103
a) Before Insertion
HEAD
105
b) After Insertion
Fig: 1.19 : Insertion of node at the beginning
1.10.2 Insertion of node at the end
Algorithm InsertAtEndCircularList(data):
newNode = CreateNode(data).
if head = Null:
newNode.next = newNode
head = newNode
else:
current = head
while current.next != head:
current = current.next
SP AA wR wD
10. current.next = newNode
11. newNode.next = head
12. End algorithmLinear Data Structures - List 139
ae
[102 J 30 103 | f 40 —
102
103 104
HEAD
20
101
- a) Before Insertion
101 102 103 104 105
b) After Insertion
Fig: 1.20 : Insertion of node at the end
1.10.3 Insertion of node at the given position
1. Algorithm InsertNodeAtPosition(position, data):
newNode = CreateNode(data)
if position = 1:
; newNode.next = head
head=newNode
else:
current = head
for i from 1 to position-2:
ypwrr ane wn
current=current.next
10. newNode.next=current.next
1. current.next = newNode
12. End Algorithm1.40
101 102 103 104
a) Before Insertion
b) After Insertion
Fig: 1.21 : Insertion of node at the given position
1.10.4 Deletion of node at the beginning
CP ANNA AR wy
5
ll.
Algorithm DeleteFromBeginning(head):
if head—= NULL:
Display "Circular linked list is empty! Deletion not possible”
else:
current = head
while current.next != head
current=current.next
End while
current.next=head,next
head=head.next
End AlgorithmLinear Data Structures - List 1.41
HEAD
| 101 L 20 | 102 4} 30 103 4.[40 [ios df 50 | 101
102
103 104
Current = 104
101-
a) Before Deletion
-————
103 |_| 40 [100 50 101
103 104
b) After Deletion
Fig: 1.22 : Deletion of node at the beginning
1.10.5 Deletion of node at the end
Algorithm deleteNodeAtEnd(head)
current = head
while current.next.next != head
current = current.next
ys YN’
current.next = head
6. End Algorithm
Current = 103
a) Before Deletion
Current = 1041.42
b) After Deletion
Fig: 1.23 :Deletion of node at the end
4.10.6 Deletion of node at the given position
Algorithm deleteNodeAtPosition(head, position):
while count < position:
prev = current
IL
2. ifhead = NULL:
3. Display “List is empty! Deletion not possible”
4. current = head
5. prev=NULL
6. count=1
7.
8.
9.
current = current.next
10. count++
11. prev.next = current.next
12. if current.next=head:
13. tail=prev
14. free(current)
End algorithm
102 103 104
Prev=102 Position =3
t= 103
a) Before Deletion1,43
b) After Deletion
Fig: 1.24 : Deletion of node at the given position
1.10.7 Searching of node
1
2.
3,
4.
5.
6.
7.
8
Algorithm searchNode(key):
if head==null:
Display "List is empty”
current = head // Start from the head node
found = False
repeat:
if current.data = key:
found = True
break
current = current.next // Move to the next node
if current = head:
break
if found:
Display "Node found"
else:
Display "Node
End algorithmData Structure and Algoritin,
1.44
1.10.8 Traversing of node
Algorithm raverseCircularList(head):
if head = NULL:
"Circular list is empty"
Display
current = head
do:
display current
current = current.next
while current != head
Sern aAwawp
End algorithm
1.11 DOUBLY CIRCULAR LINKED LIST
A Doubly Circular Linked List is a type of linked list j in which
each node contains a data element and two pointers, one pointing to the
next node in the sequence, and the other pointing to the previous node.
Prev | Data | Next
Node = /
a doubly circular linked list provides ‘bidirectional’ traversal
allowing movement both forward and backward through the list.
Fig: 1.25 ; Doubly Circular linked listLinear Data Structures - List
Operations :
1.45
1. Insertion
* = Insert at the Beginning: Create a new node, adjust pointers to
include the new node at the beginning.
% Insert at the End: Create a new node, adjust pointers to include
the new node at the end,
%* Insert at the given Position: Create a new node, adjust pointers to
include the new node at the specified position.
2. Deletion
%* ~~ Delete from the Beginning: Adjust pointers to remove the first
node.
%*. Delete from the End: Adjust pointers to remove the last node.
%* Delete from a given Position: Adjust pointers to remove a node
from a specified position.
3. Traversal
Traverse the list both forward and backward using the next and prev
pointers.
4, Searching
Search for a specific data element in the list.
1.11.1 Creation of a node
1. Algorithm CreateDoublyCircularNode(data):
2. newNode = new Node()
3. newNode.data = data
4. newNode.next = null
5. newNode.prev = null
6. End Algorithm146
Data Structure and Algorithn,
4.11.2 Insertion at the beginning
per avayne
BEES
HEAD
‘Algorithm insertAtBeginning(data):
newNode = createNode(data)
If head = null:
newNode.next = newNode
newNode.prev = newNode
head = newNode
Else:
lastNode = head.prev
newNode.next = head
newNode.prev = lastNode
lastNode.next = head.prev = newNode
head = newNode
. End Algorithm
| 101 bh 103] wo | 102 Ie
0)
20 [3 Fo| 102| 30 | 101
101 102 03
a) Before Insertion
b) After Insertion
Fig: 1.26 :
ig: 1.26 : Insertion of node at the beginni;
iningLinear Data Structures - List
1.11.3 Insertion at the end
1.47
1. Algorithm insert_at_end(data):
2. newNode = createNode(data)
3. if head = null: // List is empty
4. newNode.next = newNode
5. newNode.prev = newNode
6.
7.
8.
9.
head = newNode
else:
last_node = head:prev // Last node in the current list
newNode.next = head
10. newNode.prev = last_node
11. last_node-next = newNode
12. head.prev = newNode
End Algorithm
b) After Insertion
Fig: 1.27 : Insertion of node at the endData Structure and Algorithms
1.48
1.11.4 Insertion at the given position
Algorithm insert_at t_position(data, position):
newNode = createNode(data)
If position <= 0:
Raise an error or handle invalid position
1
2.
3
4
5. If list = empty: :
6. newNode.next and newNode.prev = newNode
7.
8.
9.
newNode = head
Else:
current = head
10. count = 1
11S While count < position and current.next != head:
12. current = current.next
13, count++
14. If count 103 | 10 [ 102 L2] 101 | 20 | 103 Ft 102] 30 101 |
101 102 103
a) Before Deletion1.50 Data Structure and Algorithm,
HEAD
b) After Deletion ‘
Fig: 1.29 : Deletion at the beginning
1.11.6 Deletion at the end
1. Algorithm deleteAtEnd():
2. If bead is null:
3. Print “List is empty”
4. If bead.next is head:
a head = null
6. Return
7. current = head
8. while current.next is not head.prev:
9. current = current-next
10. lastNode = current.next
M1. current.next = head
12. head.prev = current
13. Free(lastNode)
End Algorithm
8) Before DeletionLinear Data Structures - List
HEAD [oO
[101 L| 102} 10 102 Ft ioi 20 | 101
101 102 x
b) After Deletion
Fig: 1.30 : Deletion at the end
1.11.7 Deletion at the given Position
Algorithm DeleteAtPosition(head, Position):
if head = null:
return null // List is empty
if position = 1;
if head.next = head:
free head
return null
else:
lastNode = head.prev
head = head.next
lastNode.next = head
head.prev = lastNode
free head
display “updated head”
current = head
count = 1
current = current.next
count = count + 1
if count < position - 1:
print "Invalid position"
return head
nextNode = current.next
prevNode = current.prev
prevNode.next = nextNode
nextNode.prev = prevNode
free current
End Algorithm
while count < position - 1 and current.next != head:Data Structure and Algori,
b) After Deletion
Fig: 1.31 : Deletion at the given position
1.11.8 Searching
DoublyCLLSearch(target):
if head = null:
return "List is empty"
else:
current = head
while True:
if current.data = target:
display "Target found in the list"
current = current.next
if current = head:
break
7. display "Target not found in-the list"
Ave e
1.11.9 Traversing
Algorithm TraverseDoublyCLL (head):
if head != null:
current = head
Tepeat
Display current.data
current = current.next
fy yeLinear Data Structures - List
7. Output
8 endif
9. End Algorithm
"Doubly Circular Linked List is empty"
1.12) APPLICATIONS oF LINKE!
TWO POLYNOMIALS
Polynomial addition is a fundamental operation in computer science
and mathematics,
often encountered in various applications, including
computer graphics, signal Processing, and numerical analysis. A polynomial
is an expression consisting of variables,
D LISTS - ADDITION OF
coefficients, and exponents.
Polynomial addition involves adding two or more polynomials to obtain a
new polynomial.
Polynomial Representation :
A polynomial is typically represented in the form:
P(X) =a, + a,x+ a,x? + eta xe
where ay, a,, a,, a,... are coefficients, x is the variable, and n is the
degree of the polynomial. The terms with non-zero coefficients are called
monomials.
Structure of a node
COEFFICIENT | POWER | NEXT
Polynomial Addition Algorithm :
Given two polynomials A(x) and B(x), their addition
C(x) = A(x)+ B(x) involves combining like terms. The steps for polynomial
addition are as follows:1.54 Data Structure and Algoy Why,
A. Align the Polynomials
Align the polynomials based on their degrees, placing like tein,
(terms with the same exponent) in the same column,
2. Add Coeflicients
Add the coefficients of the like terms, The result becomes th
coefficient of that term in the sum,
3. Write Down the Result
Write down the sum with the corresponding terms and coefficients
4, Combine Like Terms
Combine any like terms in the result to simplify the polynomial
1. Algorithm function addPolynomials(poly1, poly2):
2. result=[]
// Find the maximum degree among the two polynomials
3. maxDeg = max(len(poly1), len(poly2))
// Pad the polynomials with zeros to make them of the same
degree
polyl = pad_Zeros(poly1, maxDeg)
4. poly2= pad_Zeros(poly2, maxDeg)
M Add Corresponding coefficients
5. fori from 0 to maxDeg - 1:
6. sumCoeff = poly1[i] + poly2[i]
7. result.append(sumCoeff)
8. return result
9.
End Algorithm.Linear Data Structures - List
Padding function 1.55
Is Aseria function Pad_Zeros(poly, targetDegree):
// Pad the given polynomial with zeros to make it of the - et des
2. while len(poly) < targetDegree: ° =
3. _ poly.append(0)
4, return poly
5. End Algorithm
Example :
A=3x'4+2x*+1 would be stored as
A
100
100 102 104
3] 14 102 | 2 8 104 Fe} 1 o | |
B=8x!4-3x!+1 0x®
B
110
110 . 112 114
3 fia | uz bY} 3 | na Lol s DX
A(x) = 3x?+, 2x +1
B(x) = 8x2- 3x+ 10
The addition involves aligning the terms and adding the coefficients:
3x2+ 2x +1
+ 8x? -3x+10Structure and Algorithm,
1.56 Data Struct is
PART-A (2 MARKS)
(Apr 2012, Nov 2013, No’
ta elements organized in a
defined to store and retrieve
4. Define Data Structure. v 2014, Apr 2018)
Data structure is a collection of dat
specified manner and accessing functions are
individual data elements.
2. What are the differences between linear and non-linear data
structure. (Nov 2013, Nov 2014)
Linear Data Structure Non - Linear Data Structure
S.No
1. * | Elements are ordered in a | Elements are ordered in a
linear and sequential manner. hierarchy.
2, Only a single level is present. Multiple level data structures
are present.
3. Implementation is relatively | Implementation is relatively
easier. complicated.
Traversed in a single run.
Take multiple runs to traverse
the data.
Memory utilization is not
Memory utilization is efficient.
efficient compared to non- 4
linear data structures.
6. Examples: Examples: Trees, graphs, etc.
linked list, etc
array, queue,
3. Define Array (Apr 2014, Nov 2014, Apr 2017)
ns array refers to the homogeneous collection of elements stored
contiguously in memory. Arrays follow static allocation of memory (i.¢.;)
once the size of the array is declared it cannot be expanded nor contracted.
Example: char Array [5]