You are on page 1of 108

Data Structures and Algorithms

Data Structures
Poornima.S

Ver. 1.0 Session 7


Data Structures and Algorithms

Description of data structure


Computer science is a field of study that deals with solving a
variety of problems by using computers.
To solve a given problem by using computers, you need to design an algorithm for it.
Multiple algorithms can be designed to solve a particular
problem.
An algorithm that provides the maximum efficiency should be
used for solving the problem.
The efficiency of an algorithm can be improved by using an appropriate data structure.
Data structures help in creating programs that are simple,
reusable, and easy to maintain.
This module will enable a learner to select and implement an
appropriate data structure and algorithm to solve a given
programming problem.

Ver. 1.0 Session 7


Data Structures and Algorithms
Role of Data Structures

Different algorithms can be used to solve the same problem.


Some algorithms may solve the problem more efficiently
than the others.
An algorithm that provides the maximum efficiency should
be used to solve a problem.
One of the basic techniques for improving the efficiency of
algorithms is to use an appropriate data structure.
Data structure is defined as a way of organizing the various
data elements in memory with respect to each other.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms

Definition:

A data structure is a way of organizing all data items that


considers not only the elements stored but also their
relationship to each other.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Role of Data Structures (Contd.)

Use of an appropriate data structure, helps improve the


efficiency of a program.
The use of appropriate data structures also allows you to
overcome some other programming challenges, such as:
Simplifying complex problems
Creating standard, reusable code components
Creating programs that are easy to understand and
maintain

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms

Classification of DS

Data Structures 2 Types:

1) Primitive DS 2) Non-primitive DS

Integer, Float, Arrays List Files


Character, Pointer
Linear Non-Linear

Stack, Queue Tree,


Graph

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Types of Data Structures

Data can be organized in many different ways. Therefore,


you can create as many data structures as you want.
Some data structures that have proved useful over the
years are:
Arrays
Linked Lists
Stacks
Queues
Trees
Graphs

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Types of Data Structures

Data structures can be classified under the following two


categories:
Static: Example – Array
Dynamic: Example – Linked List

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Abstract Data Type (ADT)
• High-level definition of data types
• An ADT specifies
• A collection of data
• A set of operations on the data or subsets of the data
• ADT does not specify how the operations should be implemented
Examples: vector, list, stack, queue, de-queue, priority queue, table
(map), associative array, set, graph, digraph

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
The List ADT

A sequence of zero or more elements A1, A2, A3, … AN


N: length of the list , If N=0, then empty list
A1: first element
AN: last element
Ai : Element at position i
Linearly ordered
Ai precedes Ai+1
Ai follows Ai-1

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms

List Operations
printList : print the list
makeEmpty: create an empty list
find : locate the position of an object in a list
list : 34,12, 52, 16, 12
find(52) → 3
insert : insert an object to a list
insert(x,3) → 34, 12, 52, x, 16, 12
remove : delete an element from the list
remove(52) → 34, 12, x, 16, 12
findKth : retrieve the element at a certain K position

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Implementation of an ADT

• Choose a data structure to represent the ADT


E.g. arrays, records, etc.
• Each operation associated with the ADT is implemented by one or
more subroutines
• Two standard implementations for the list ADT
• Array-based
• Linked list

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms

Array Implementation

Elements are stored in contiguous array Positions

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Memory Allocation
When you declare an array, a contiguous block of memory is
allocated.
Let us suppose you declare an array of size 10 to store first 10
prime numbers.
If you know the address of the first element in the array you can
calculate the address of any other elements as shown:
Address of the first element + (size of the element × index of the
element)

0 1000
1 1002
2 1004
3 1006
One contiguous block of
4 1008 memory allocated for
5 1010 the array to store 10
6 1012
7 1014
elements.
8 1016
9 1018

Memory representation
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Array Implementation

• Requires an estimate of the maximum size of the list

Operations

insert and delete: O(n)


• e.g. insert at position 0 (making a new element)
- requires first pushing the entire array down one spot to
make room for the new element (shift down)

• e.g. delete at position 0


-requires shifting all the elements in the list up one position to
remove the vacant location (shift up)

• On average, half of the lists needs to be moved for either


operation

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms

Limitations of Arrays
• Size has to be known in advance
• Memory required may be much larger than the number of
elements actually used ->memory wastage
• Inserting or deleting the elements can take time up to O(n)

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Dynamic Memory Allocation (Linked List)

When memory is allocated dynamically, a block of memory


is assigned arbitrarily from any location in the memory.
Therefore, unlike arrays, these blocks may not be
contiguous and may be spread randomly in the memory.

0 1000
1 1002
2 1004
3 1006
One contiguous block of
4 1008 memory allocated for
5 1010 the array to store 10
6 1012
7 1014
elements.
8 1016
9 1018

Memory representation
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Dynamic Memory Allocation (Contd.)
To access any element, you need to know its address.
Now, if you know the address of the first element, you
cannot calculate the address of the rest of the elements.
This is because, all the elements are spread at random
locations in the memory.

1002 2

1008 5

1020 7
1030 11

1036 3

Memory representation

Ver. 1.0 Session 7


Data Structures and Algorithms
Dynamic Memory Allocation (Contd.)
An example of a data structure that implements this concept
is a linked list.
You can declare a variable, START, that stores the address
of the first block.
You can now begin at START and move through the list by
following the links.
START 1002 2 1036

1008 5 1020

1020 7 1030
1030 11

1036 3 1008

Memory representation
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Defining Linked Lists

Linked list:
Is a dynamic data structure.
Allows memory to be allocated as and when it is required.
Consists of a chain of elements, in which each element is
referred to as a node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Defining Linked Lists (Contd.)

A node is the basic building block of a linked list.


A node consists of two parts:
Data: Refers to the information held by the node
Link: Holds the address of the next node in the list

Data Link

Node

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Defining Linked Lists (Contd.)

To keep track of the first node, declare a variable/pointer,


START, which always points to the first node.
When the list is empty, START contains null.
NULL
START

10 3352
Data 10 5689
Data 10 1012
Data 10
Data

2403 3352 5689 1012

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Linked List

Types
i) Singly LL
- contains 2 parts : data & next address
- used for traversing the nodes in forward direction
ii) Doubly LL
- contains 3 parts : previous addr, data & next address
- used for traversing the nodes in both forward &
backward directions

Operations
i) Insertion
ii) Deletion
iii) Traverse
iv) Find
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Representing a Singly-Linked List

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms

Operations of List
IsEmpty(): determine whether or not the list is empty
InsertNode(): insert a new node at a particular position
FindNode(): find a node with a given value
DeleteNode(): delete a node with a given value
DisplayList(): print all the nodes in the list

head / start: a pointer to the first node in the list.

•Since the list is empty initially, head is set to NULL

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Class SinglyLL
{
public:
singlyLL( )
{
start=NULL;
}
void IsEmpty();
void create();
void InsertNode();
void FindNode();
void DeleteNode();
void DisplayList();
};
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Node Creation
Start

Temp =new (struct Node);


10
10 NULL Temp->data=num; //10
Temp->next=NULL;
Temp

Start =Temp; // (10)

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Insertion at Beginning
Start

10 10
20 NULL 10 NULL

Temp

Temp =new (Node);


Temp->data=num2; //20
Temp->next=NULL;

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Insertion at Beginning (contd..)
Start

20 10 Addr 10
10 NULL
(head)

Temp

Temp = new Node();


Temp->data=num2; // 20
Temp->next=Start;

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Insertion at Beginning (contd..)
Start

20 10 Addr 10
10 NULL
(head)

Temp = new Node();


Temp->data=num2; //20
Temp->next=Start;

Start =Temp; // (20)

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node in a Singly-Linked List

Now, let us solve the given problem using a linked list.


Suppose the linked list currently contains the following
elements.

START

10 15 17 20

The linked list needs to be created in the ascending order of


values.
Therefore, the position of a new node in the list will be
determined by the value contained in the new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node in a Singly-Linked List (Contd.)

Once the position of the new node has been determined,


the new node can be inserted in the linked list.
A new node can be inserted at any of the following positions
in the list:
Beginning of the list
End of the list
Between two nodes in the list

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node in a Singly-Linked List (Contd.)
1. Allocate memory for the new
node.
Insert 7
2. Assign value to the data field of
the new node.
Temp
3. Make the next field of the new
node point to the first node in
the list.
7 START 4. Make START, point to the new
node.

10
10 15 17 20

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node in a Singly-Linked List (Contd.)
1. Allocate memory for the new
node.

2. Assign value to the data field of


the new node.
Temp
3. Make the next field of the new
node point to the first node in
the list.
7 START 4. Make START, point to the new
node.

10
10 15 17 20

Temp-> next = START

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node in a Singly-Linked List (Contd.)
1. Allocate memory for the new
node.

2. Assign value to the data field of


the new node.
Temp
3. Make the next field of the new
node point to the first node in
the list.
7 START 4. Make START, point to the new
node.

10
10 15 17 20

Insertion complete

Temp-> next = START


START = Temp

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
1. Identify the nodes between which
Inserting a Node in middle the new node is to be inserted.
Mark them as previous and current.
To locate previous and current,
execute the following steps:
Insert 16 a. Make current point to the first
node.
b. Make previous point to NULL.
c. Repeat step d and step e until
START current.info becomes greater
than Temp.info or current
becomes equal to NULL.
d. Make previous point to
10
10 15 17 20 current.
e. Make current point to the next
node in sequence.
current
2. Allocate memory for the new node.
previous = NULL
3. Assign value to the data field of the
new node.

4. Make the next field of the new node


point to current.

5. Make the next field of previous


point to the new node.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
1. Identify the nodes between which
Data Structures and Algorithms the new node is to be inserted.
Mark them as previous and current.
Inserting a Node in middle To locate previous and current,
execute the following steps:
a. Make current point to the
first node.
b. Make previous point to NULL.
Insert 16 c. Repeat step d and step e
until current.info becomes
greater than Temp.info or
START current becomes equal to
NULL.
d. Make previous point to
current.
10
10 15 17 20 e. Make current point to the
next node in sequence.

previous current 2. Allocate memory for the new node.

3. Assign value to the data field of


Nodes located the new node.

4. Make the next field of the new


node point to current.

5. Make the next field of previous


point to the new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
1. Identify the nodes between which the
Inserting a Node in middle new node is to be inserted. Mark
them as previous and current. To
locate previous and current, execute
Temp the following steps:
a. Make current point to the first
node.
b. Make previous point to NULL.
16
c. Repeat step d and step e until
START current.info becomes greater
than Temp.info or current
becomes equal to NULL.
10
10 15 17 20 d. Make previous point to current.
e. Make current point to the next
node in sequence.
previous current
2. Allocate memory for the new node.

3. Assign value to the data field of the


new node.

4. Make the next field of the new node


point to current.

5. Make the next field of previous point


to the new node.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
1. Identify the nodes between which the
Inserting a Node in middle new node is to be inserted. Mark them
as previous and current. To locate
previous and current, execute the
Temp following steps:
a. Make current point to the first
node.
b. Make previous point to NULL.
16 c. Repeat step d and step e until
START current.info becomes greater than
Temp.info or current becomes
equal to NULL.
10 15 20 d. Make previous point to current.
10 17
e. Make current point to the next
node in sequence.
previous current 2. Allocate memory for the new node.

3. Assign value to the data field of the


new node.

Temp->next = current 4. Make the next field of the new node


point to current.
Previous->next = Temp
5. Make the next field of previous point to
Insertion complete the new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Insertion in front, middle & end of list)
Data Structures and Algorithms

Ver. 1.0 Session 7


Data Structures and Algorithms
Traversing a Singly-Linked List (Contd.)
1. Make currentNode point to
the first node in the list.

2. Repeat step 3 and 4 until


currentNode becomes
NULL.

3. Display the information


START contained in the node
marked as currentNode.
2
10 3
10 5
10 7
10
4. Make currentNode point to
the next node in sequence.
currentNode

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Traversing a Singly-Linked List (Contd.)
1. Make currentNode point
to the first node in the list.

2. Repeat step 3 and 4 until


currentNode becomes
NULL.

3. Display the information


START contained in the node
marked as currentNode.
2
10 3
10 5
10 7
10
4. Make currentNode point
to the next node in
sequence.
currentNode

2 3

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Traversing a Singly-Linked List (Contd.)
1. Make currentNode point
to the first node in the
list.

2. Repeat step 3 and 4


until currentNode
becomes NULL.
START 3. Display the information
contained in the node
2 3 5 7 marked as currentNode.
10 10 10 10
4. Make currentNode point
to the next node in
currentNode
sequence.
2 3 5

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Traversing a Singly-Linked List (Contd.)
1. Make currentNode point to
the first node in the list.

2. Repeat step 3 and 4 until


currentNode becomes
NULL.

3. Display the information


START contained in the node
marked as currentNode.
2
10 3
10 5
10 7
10
4. Make currentNode point to
the next node in sequence.
currentNode

2 3 5 7

Traversal complete

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Display the elements of List

Void singlyLL :: display ( )


{
curr=start;

cout<<“List elements are\n”;

while(curr!=NULL)
{
”;
cout<<curr->data<<“
curr=curr->next;

}
}

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node from a Singly-Linked List

Delete operation in a linked list refers to the process of


removing a specified node from the list.
You can delete a node from the following places in a linked
list:
Beginning of the list
Between two nodes in the list
End of the list

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node From the Beginning of the List (Contd.)

1. Mark the first node in the


Algorithm to delete a node from the list as current.
beginning of a linked list. 2. Make START point to the
next node in its sequence.
START
3. Release the memory for
the node marked as
current.
10 15 17 20

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node From the Beginning of a Linked List (Contd.)

1. Mark the first node in the


list as current.

2. Make START point to the


next node in its sequence.
START
3. Release the memory for
the node marked as
current.
10
10 15 17 20

current

current = START

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node From the Beginning of a Linked List (Contd.)

1. Mark the first node in the


list as current.

2. Make START point to the


next node in its sequence.
START
3. Release the memory for
the node marked as
current.
10 15 17 20

current
Memory released

current = START
START = START -> next

Delete operation complete


III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node Between two Nodes in the List (Contd.)

Delete 17 1. Locate the node to be deleted.


Mark the node to be deleted as
current and its predecessor as
previous. To locate current and
previous, execute the following
steps:
START a. Set previous = START
b. Set current = START
c. Repeat step d and e
until either the node
10 15 17 20 is found or current
becomes NULL.
d. Make previous point
to current .
e. Make current point to
the next node in
sequence.

2. Make the next field of previous


point to the successor of current.

3. Release the memory for the node


marked as current.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node Between two Nodes in the List (Contd.)
1. Locate the node to be deleted.
Mark the node to be deleted as
Delete 17 current and its predecessor as
previous. To locate current and
previous, execute the following
steps:
a. Set previous = START
START
b. Set current = START
c. Repeat step d and e
until either the node
10
10 15 17 20 is found or current
becomes NULL.
d. Make previous point
to current.
previous current e. Make current point to
the next node in
sequence.

2. Make the next field of previous


point to the successor of current.

3. Release the memory for the node


marked as current.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node Between two Nodes in the List (Contd.)

1. Locate the node to be deleted.


Delete 17 Mark the node to be deleted as
current and its predecessor as
previous. To locate current and
previous, execute the following
steps:
START a. Set previous = START
b. Set current = START
c. Repeat step d and e
until either the node
10
10 15 17 20 is found or current
becomes NULL.
d. Make previous point
previous current to current.
e. Make current point to
the next node in
sequence.

2. Make the next field of previous


point to the successor of current.

3. Release the memory for the node


marked as current.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node Between two Nodes in the List (Contd.)
1. Locate the node to be deleted.
Mark the node to be deleted as
Delete 17 current and its predecessor as
previous. To locate current and
previous, execute the following
steps:
a. Set previous = START
START
b. Set current = START
c. Repeat step d and e
until either the node
10
10 15 17 20 is found or current
becomes NULL.
d. Make previous point
to current.
previous current
e. Make current point to
the next node in
sequence.

2. Make the next field of previous


point to the successor of current.

3. Release the memory for the node


marked as current.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node Between two Nodes in the List (Contd.)

1. Locate the node to be deleted.


Delete 17 Mark the node to be deleted as
current and its predecessor as
previous. To locate current and
previous, execute the following
steps:
START a. Set previous = START
b. Set current = START
c. Repeat step d and e
until either the node
10
10 15 17 20 is found or current
becomes NULL.
d. Make previous point
previous current to current.
e. Make current point to
the next node in
sequence.

Previous->next = current->next 2. Make the next field of previous


point to the successor of current.

3. Release the memory for the node


marked as current.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Deletion in front, middle & end of List

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Same way,

Write functions for find() & findkth() to search the element in the list.

find()
{

get the input “element to be found “


traverse until element is found starting from the 1st node (head/start ptr);
print the position of element if found else print as “not found”

findkth()
{

get the input “position of element to be found “


traverse until the given position starting from the 1st node (head/start ptr);
print the element value if found else print as “invalid position”

} III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
int
Datamain( )
Structures and Algorithms
{
singlyLL s;
int x, pos, ch;
start=NULL;
curr =NULL;
prev= NULL;
do
{
cout<<“1.IsEmpty\n2.Insert\n3.Delete\n4.Find\n5.Findkth\n6.Display\n”;
cout<<“\nEnter the choice : “;
cin>>ch;
switch(ch)
{
case 1: s.isempty();
break;
case 2: cout<<“Enter the element & position to be inserted”;
cin>>x>>pos;
s.insert(x,pos);
break;
case 3: cout<<“Enter the element to be deleted”;
cin>>x;
s.delete(x);
Ver. 1.0 break; Session 7
Data Structures and Algorithms
case 4: cout<<“Enter the element to be found”;
cin>>x;
s.find(x);
break;
case 5: cout<<“Enter the position of element to be found”;
cin>>pos;
s.findkth(pos);
break;
case 6: s.display();
}
}while(ch<7);
}

Ver. 1.0 Session 7


Data Structures and Algorithms
Advantages and disadvantages of linked lists.

Linked lists are more complex to code and manage than arrays, but they have
some distinct advantages.

• Dynamic: a linked list can easily grow and shrink in size.


• We don’t need to know how many nodes will be in the list. They are created
in memory as needed.
• In contrast, the size of a C++ array is fixed at compilation time.
• Easy and fast insertions and deletions
•To insert or delete an element in an array, we need to copy to temporary
variables to make room for new elements or close the gap caused by deleted
elements.
• With a linked list, no need to move other nodes. Only need to reset some
pointers.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Doubly-Linked List

Introduce an additional field in each node of a singly-


linked list, which would hold the address of its previous
node.
Such a type of list is known as a doubly-linked list.

START

10 15 19 21 23 25 32

Structure of a doubly-linked list

Prev Data Next


addr addr

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Representing a Doubly-Linked List

In a doubly-linked list, each node needs to store:

•The information
•The address of the next node in sequence
•The address of the previous node

struct Node
{

int data;
struct Node *prevaddr,
struct Node *nextaddr;

} *Start, *Temp, *previous, *current;

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting Nodes in a Doubly-Linked List

A node can be inserted at any of the following positions


in a doubly-linked list:
Beginning of the list
Between two nodes in the list
End of the list

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the
new node.
Algorithm to insert a node in the
beginning of a doubly-linked list. 2. Assign value to the data
field of the new node.

3. Make the nextaddr field of


the new node point to the
first node in the list.

4. Make the prev field of


START point to the new
node.
START
5. Make the prevaddr field of
the new node point to
NULL.
10 15 17 20
6. Make START, point to the
new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the
new node.

2. Assign value to the data


field of the new node.

3. Make the nextaddr field of


the new node point to the
first node in the list.

4. Make the prevaddr field of


Temp START point to the new
node.
START
7 5. Make the prevaddr field of
the new node point to
NULL.
10
10 15 17 20
6. Make START, point to the
new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the
Temp->nextaddr = START new node.

START->prevaddr = Temp 2. Assign value to the data


field of the new node.
Temp->prevaddr = NULL
3. Make the nextaddr field of
START = Temp the new node point to the
first node in the list.

4. Make the prevaddr field of


Temp START point to the new
node.
START
7 5. Make the prevaddr field of
the new node point to
NULL.
10
10 15 17 20
6. Make START, point to the
new node.

Insertion complete

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List
1. Identify the nodes between which
the new node is to be inserted. Mark
them as previous and current
Insert 16 respectively. To locate previous and
current, execute the following steps:
a. Make current point to the first
START node.
b. Make previous point to NULL.
c. Repeat steps d and e until
current.info > Temp.info or
10
10 15 17 20 current = NULL.
d. Make previous point to current.
e. Make current point to the next
node in sequence.
current 2. Allocate memory for the new node.
previous = NULL 3. Assign value to the data field of the
new node.
4. Make the nextaddr field of the new
node point to current.
5. Make the prevaddr field of the new
node point to previous.
6. Make the prevaddr field of current
point to the new node.
7. Make the nextaddr field of previous
point to the new node.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List
1. Identify the nodes between which
the new node is to be inserted. Mark
them as previous and current
Insert 16 respectively. To locate previous and
current, execute the following steps:
a. Make current point to the first
START node.
b. Make previous point to NULL.
c. Repeat steps d and e until
current.info > Temp.info or
10
10 15 17 20 current = NULL.
d. Make previous point to current.
e. Make current point to the next
node in sequence.
previous current 2. Allocate memory for the new node.
3. Assign value to the data field of the
new node.
4. Make the nextaddr field of the new
node point to current.
5. Make the prevaddr field of the new
node point to previous.
6. Make the prevaddr field of current
point to the new node.
7. Make the nextaddr field of previous
point to the new node.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List
1. Identify the nodes between which the
new node is to be inserted. Mark
them as previous and current
Insert 16 respectively. To locate previous and
current, execute the following steps:
a. Make current point to the first
START node.
b. Make previous point to NULL.
c. Repeat steps d and e until
current.info > Temp.info or
10
10 15 17 20 current = NULL.
d. Make previous point to current.
e. Make current point to the next
node in sequence.
previous current 2. Allocate memory for the new node.
3. Assign value to the data field of the
new node.
4. Make the nextaddr field of the new
node point to current.
5. Make the prevaddr field of the new
node point to previous.
6. Make the prevaddr field of current
point to the new node.
7. Make the nextaddr field of previous
point to the new node.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List
1. Identify the nodes between which
the new node is to be inserted. Mark
Temp them as previous and current
Insert 16 respectively. To locate previous and
current, execute the following steps:
16 a. Make current point to the first
START node.
b. Make previous point to NULL.
c. Repeat steps d and e until
current.info > Temp.info or
10
10 15 17 20 current = NULL.
d. Make previous point to current.
e. Make current point to the next
node in sequence.
previous current 2. Allocate memory for the new node.
3. Assign value to the data field of the
new node.
4. Make the nextaddr field of the new
node point to current.
5. Make the prevaddr field of the new
node point to prevaddrious.
6. Make the prevaddr field of current
point to the new node.
7. Make the nextaddr field of previous
point to the new node.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List
1. Identify the nodes between which
the new node is to be inserted. Mark
Temp them as previous and current
respectively. To locate previous and
current, execute the following steps:
16 a. Make current point to the first
START node.
b. Make previous point to NULL.
c. Repeat steps d and e until
current.info > Temp.info or
10
10 15 17 20 current = NULL.
d. Make previous point to current.
e. Make current point to the next
node in sequence.
previous current 2. Allocate memory for the new node.
3. Assign value to the data field of the
Temp->nextaddr = current new node.
4. Make the nextaddr field of the new
Temp->prevaddr = previous node point to current.
current->prevaddr = Temp 5. Make the prevaddr field of the new
node point to prevaddrious.
previous->nextaddr = Temp
6. Make the prevaddr field of current
point to the new node.
7. Make the nextaddr field of previous
point to the new node.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting Nodes from a Doubly-Linked List

You can delete a node from one of the following places


in a doubly-linked list:
Beginning of the list
Between two nodes in the list
End of the list

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node From the Beginning of the List (Contd.)

1. Mark the first node in the


Algorithm to delete a node from list as current.

the beginning of a doubly-linked 2. Make START point to the


list. next node in sequence.

3. If START is not NULL: /* If


the node to be deleted is
START not the only node in the
list */

10 15 17 20 a. Assign NULL to the


prevaddr field of the
node marked as
START.
current
4. Release the memory of
the node marked as
current.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node From the Beginning of the List

1. Mark the first node in the


Delete operation complete list as current.

2. Make START point to the


next node in sequence.

3. If START is not NULL: /* If


the node to be deleted is
START not the only node in the
list */

10 15 17 20 a. Assign NULL to the


prevaddr field of the
node marked as
START.
current
Memory released 4. Release the memory of
the node marked as
START = START->nextaddr current.
START-> prevaddr = NULL
free(curr);

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
1. Mark the node to be deleted as current
Deleting a Node Between Two Nodes and its predecessor as previous. To
locate previous and current, execute the
following steps:
Algorithm to delete a node
a. Make previous point to NULL. //
between two nodes in a doubly- Set previous = NULL
linked list.
b. Make current point to the first
Delete 17 node in the linked list. // Set
START
current = START

c. Repeat steps d and e until either


10 15 17 20 the node is found or current
becomes NULL.

d. Make previous point to current.

e. Make current point to the next


node in sequence.

2. Make the nextaddr field of previous


point to the successor of current.

3. Make the prevaddr field of the


successor of current point to previous.

4. Release the memory of the node marked


III Sem ECE-B, Poornima.S
Ver. 1.0 as current. Session 7
Data Structures and Algorithms
1. Mark the node to be deleted as current
Deleting a Node Between Two Nodes and its predecessor as previous. To
locate previous and current, execute the
following steps:

a. Make previous point to NULL. //


Set previous = NULL

b. Make current point to the first node


in the linked list. // Set current =
START START

c. Repeat steps d and e until either the


10
10 15 17 20 node is found or current becomes
NULL.

d. Make previous point to current.


current
e. Make current point to the next node
previous = NULL in sequence.

2. Make the nextaddr field of previous point


to the successor of current.

3. Make the prevaddr field of the successor


of current point to prevaddrious.

4. Release the memory of the node marked


III Sem ECE-B, Poornima.S
Ver. 1.0 as current. Session 7
Data Structures and Algorithms
1. Mark the node to be deleted as current and
Deleting a Node Between Two Nodes its predecessor as previous. To locate
previous and current, execute the
following steps:

a. Make previous point to NULL. // Set


previous = NULL

b. Make current point to the first node in


START the linked list. // Set current = START

c. Repeat steps d and e until either the


node is found or current becomes
10
10 15 17 20 NULL.

d. Make previous point to current.


current
e. Make current point to the nextaddr
previous node in sequence.

2. Make the nextaddr field of previous point


to the successor of current.

3. Make the prevaddr field of the successor


of current point to previous.

4. Release the memory of the node marked


as current.
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
1. Mark the node to be deleted as current
Deleting a Node Between Two Nodes and its predecessor as previous. To
locate previous and current, execute the
following steps:

a. Make previous point to NULL. //


Set previous = NULL

b. Make current point to the first node


in the linked list. // Set current =
START
START

c. Repeat steps d and e until either the


10
10 15 17 20 node is found or current becomes
NULL.

d. Make previous point to current.


current
previous e. Make current point to the next node
in sequence.

2. Make the nextaddr field of previous point


to the successor of current.

3. Make the prevaddr field of the successor


of current point to previous.

4. Release the memory of the node marked


III Sem ECE-B, Poornima.S
Ver. 1.0 as current. Session 7
Data Structures and Algorithms
1. Mark the node to be deleted as current
Deleting a Node Between Two Nodes and its predecessor as previous. To
locate previous and current, execute the
following steps:

a. Make previous point to NULL. //


Deletion complete Set previous = NULL

b. Make current point to the first node


in the linked list. // Set current =
START START

c. Repeat steps d and e until either


10
10 15 17 20 the node is found or current
becomes NULL.

d. Make previous point to current.


previous current
e. Make current point to the next node
in sequence.
previous->nextaddr = current->nextaddr 2. Make the next field of previous point to
current->nextaddr->prevaddr = previous the successor of current.

3. Make the prevaddr field of the successor


of current point to previous.

4. Release the memory of the node marked


III Sem ECE-B, Poornima.S
Ver. 1.0 as current. Session 7
Data Structures and Algorithms
Circular Linked List

Let us suppose you are developing an action game in


which a player is given a set of n weapons.
Each weapon appears on the screen after a specific
time period.
The player is required to select the weapon within 10
seconds or else the weapon becomes unusable.
Once the nth weapon is displayed, the weapon that
came first is displayed again and the process continues.

Which data structure will you use to store the list of


weapons in this case?

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
You can use a singly-linked list.
However, the weapons need to be displayed in a sequence
repeatedly in a round robin manner.
Therefore, once all the weapons have been displayed, the pointer
must start again with the first weapon in the list.
This requires the pointer to be reinitialized each time the end of
the list is reached.
In this situation, it would be good if after traversing the node
corresponding to the last weapon, the pointer could
automatically move to the first weapon in the list.
This can be implemented by using a circular linked list.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Implementing a Circular Linked List (Contd.)

You can implement a circular linked list by linking the


last node back to the first node in the list.

START

10 15 9 11 23 5 12

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Implementing a Circular Linked List (Contd.)

In a circular linked list, the last node holds the address


of the first node.

LAST

10 15 9 11 23 5 12

In a circular linked list, you need to maintain a


variable/pointer, LAST, which always point to the last
node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node in a Circular Linked List

In a circular linked list, you can insert a node at any of


the following positions:
Beginning of the list
End of the list
Between two node in the list

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the
new node.
Algorithm to insert a node in the 2. Assign value to the data
beginning of a circular linked list. field of the new node.

3. Make the next field of the


new node point to the
successor of LAST.
LAST
4. Make the nextaddr field of
LAST point to the new
node.
10
10 15 17 20

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the
new node.

2. Assign value to the data


Temp field of the new node.

3. Make the nextaddr field


7 LAST of the new node point to
the successor of LAST.

4. Make the nextaddr field


10
10 15 17 20 of LAST point to the
new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the Beginning of the List (Contd.)
1. Allocate memory for the
new node.

2. Assign value to the data


Temp field of the new node.
Insertion complete
3. Make the nextaddr field
7 LAST of the new node point to
the successor of LAST.

4. Make the nextaddr field


10
10 15 17 20 of LAST point to the new
node.

Temp-> nextaddr = LAST->nextaddr


LAST-> nextaddr =Temp

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List (Contd.)
Insert 16 1. Make current point to the first
node.
Algorithm to locate the nodes
2. Make previous point to NULL.
between which the new node
is to be inserted. 3. Repeat steps 4 and 5 until
current.info > Temp.info or
previous = LAST.

4. Make previous point to


current.
LAST
5. Make current point to the next
node in sequence.
10 15 17 20

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List (Contd.)
1. Make current point to the
first node.

2. Make previous point to NULL.

3. Repeat steps 4 and 5 until


current.info > Temp.info or
previous = LAST.

4. Make previous point to


current.
LAST
5. Make current point to the
next node in sequence.
10
10 15 17 20

current

previous = NULL

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List (Contd.)
1. Make current point to the first
node.

2. Make previous point to NULL.

3. Repeat steps 4 and 5 until


current.info > Temp.info or
previous = LAST.

4. Make previous point to


current.
LAST
5. Make current point to the next
node in sequence.
10
10 15 17 20

current

previous = NULL

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node Between Two Nodes in the List (Contd.)
1. Make current point to the
first node.

2. Make previous point to NULL.

3. Repeat steps 4 and 5 until


current.info > Temp.info or
previous = LAST.

Nodes located 4. Make previous point to


current.
LAST
5. Make current point to the
next node in sequence.
10
10 15 17 20

previous current

Now insert new node

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the End of the List (Contd.)
1. Allocate memory for the
Let us insert a node after node 20 new node.

Algorithm to insert a node at 2. Assign value to the data


field of the new node.
the end of the list
3. Make the nextaddr field of
LAST the new node point to the
successor of LAST.

10 15 17 20 4. Make the nextaddr field of


LAST point to the new
node.

5. Mark LAST point to the


new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the End of the List (Contd.)
1. Allocate memory for
the new node.

2. Assign value to the


data field of the new
Temp node.

LAST 3. Make the nextaddr field


24 of the new node point
to the successor of
10
10 15 17 20 LAST.

4. Make the nextaddr field


of LAST point to the
new node.

5. Mark LAST point to the


new node.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Inserting a Node at the End of the List (Contd.)
1. Allocate memory for
the new node.
Insertion complete
2. Assign value to the
data field of the new
Temp node.

LAST 3. Make the nextaddr field


24 of the new node point
to the successor of
10
10 15 17 20 LAST.

4. Make the nextaddr field


of LAST point to the
new node.

5. Mark LAST point to the


new node.

Temp->nextaddr = LAST-> nextaddr


LAST-> nextaddr = Temp
LAST = Temp

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Deleting a Node from a Circular Linked List

You can delete a node from any of the following places


in a circular linked list:
Beginning of the list
End of the list
Between two nodes in the list

Change the pointer positions accordingly to create links correctly.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Applications of Linked Lists

Linked lists provide an efficient mechanism for storing


data.
They are used to solve many programming problems
easily.
They form the basic foundation for implementing
various other data structures such as stacks, queues,
and binary trees.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Linked lists offer various applications.
For example:
They form the basis of various other data structures
such as stacks and queues, and binary trees.
They are used in various gaming applications.
They are used to implement internals of file system
internals in various operating systems.
They offer a simple and convenient mechanism to
perform various arithmetic operations on polynomial
expressions.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Gaming Applications

Linked lists are implemented in various gaming


applications.
Consider a game in which the player protects himself from the
enemy by firing bullets.
Whenever a bullet is fired, its details (size, color, and
coordinates) need to be stored somewhere.
The details of the bullets are stored in a linked list, because the
total number of bullets to be fired are not known in advance.
The coordinates of the bullets stored in a node are updated on a
regular basis to indicate that the bullet is moving forward.
The same is then rendered on the screen.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
File System in Operating System

Linked lists are used to implement the internals of a file


system.
A file is divided into blocks, which are scattered
randomly on the disk.
When a new file is created, a new block of memory is
allocated to it.
The new block may not be contiguous to the block that
was earlier allocated.
Therefore, each block also contains the address of the
next allocated block, forming a linked list.

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists

Linked lists can be used to implement various


arithmetic operations on polynomial expressions.
In the polynomial expression, 4x5 + 5x4 + 2x3 + 3x2 + 7x,
each occurrence of variable x is attached to two values:
Coefficient
Exponent

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Each node holds three pieces of information:


Coefficient
Exponent
Address of the next node in sequence

910 6

coefficient

exponent
address of the next node

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Let us now perform addition of two polynomial


expressions.

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the first node of Polynomial 1 and the first node of


Polynomial 2.
Compare the exponent values of the two nodes.
The node read from Polynomial 2 has a greater
exponent value (6>5).
Add the node with exponent value 6 to the list for
Polynomial 3.
9 106

Polynomial 3: 9x6
III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from Polynomial 2.


Compare the exponent value of current node of Polynomial 1
with that of Polynomial 2.
The node read from Polynomial 1 has a greater exponent
value (5>4). Therefore, add the node with the exponent value 5
to the list for Polynomial 3.

9 106 4 105

Polynomial 3: 9x6 + 4x5


III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from Polynomial 1.


Compare the exponent value of the current node of
Polynomial 1 with that of Polynomial 2. Both the exponents
are equal, that is, 4.
Add the coefficients of both the nodes. The resultant node
now has the coefficient value 11 (6+5) and same exponent 4.
Add the resultant node to the list for Polynomial 3.

9 106 4 105 11 104

Polynomial 3: 9x6 + 4x5 + 11x4


III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from both the lists.


Compare the exponent value of the current node of
Polynomial 1 with that of Polynomial 2.
The node read from Polynomial 1 has a greater exponent
value (3>2).
Add the node with the exponent value 3 to the list for
Polynomial 3.

9 106 4 105 11 104 2 103

Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3


III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Read the next node from Polynomial 1.


Compare the exponent value of the current node of
Polynomial 1 with that of Polynomial 2. Both the exponents
are equal, that is, 2.
Add the coefficients of both the nodes. The resultant node
now has the coefficient value 6 (3+3).
Add the resultant node to the list for Polynomial 3.

9 106 4 105 11 104 2 103 6 102

Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 + 6x2


III Sem ECE-B, Poornima.S
Ver. 1.0 Session 7
Data Structures and Algorithms
Adding Polynomials Using Linked Lists (Contd.)

Polynomial 1: 4x5 + 5x4 + 2x3 + 3x2 + 7x

Polynomial 2: 9x6 + 6x4 + 3x2

Now there are no more nodes in Polynomial 2.


Therefore, add the remaining nodes of Polynomial 1 to the
resultant list.

9 106 4 105 11 104 2 103 6 102 7 101

Polynomial 3: 9x6 + 4x5 + 11x4 + 2x3 + 6x2 + 7x

III Sem ECE-B, Poornima.S


Ver. 1.0 Session 7

You might also like