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

Algorithm Design Unit 1

Algorithm Design Unit 1.

Uploaded by

komalkhati457
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Algorithm Design Unit 1

Algorithm Design Unit 1.

Uploaded by

komalkhati457
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

ALGORITHM DESIGN

UNIT 1

Linear Data Structure:


Data structure where data elements are arranged sequentially or
linearly where each and every element is attached to its previous
and next adjacent is called a linear data structure. In linear data
structure, single level is involved. Therefore, we can traverse all the
elements in single run only. Linear data structures are easy to
implement because computer memory is arranged in a linear way.
Its examples are array, stack, queue, linked list, etc.

Non-linear Data Structure:


Data structures where data elements are not arranged sequentially
or linearly are called non-linear data structures. In a non-linear
data structure, single level is not involved. Therefore, we can’t
traverse all the elements in single run only. Non-linear data
structures are not easy to implement in comparison to linear data
structure. It utilizes computer memory efficiently in comparison to
a linear data structure. Its examples are trees and graphs.

Abstract Data Types


In this article, we will learn about ADT but before understanding
what ADT is let us consider different in-built data types that are
provided to us. Data types such as int, float, double, long, etc. are
considered to be in-built data types and we can perform basic
operations with them such as addition, subtraction, division,
multiplication, etc. Now there might be a situation when we need
operations for our user-defined data type which have to be defined.
These operations can be defined only as and when we require them.
So, in order to simplify the process of solving problems, we can
create data structures along with their operations, and such data
structures that are not in-built are known as Abstract Data Type
(ADT).
Abstract Data type (ADT) is a type (or class) for objects whose
behavior is defined by a set of values and a set of operations. The
definition of ADT only mentions what operations are to be performed
but not how these operations will be implemented. It does not
specify how data will be organized in memory and what algorithms
will be used for implementing the operations. It is called “abstract”
because it gives an implementation-independent view.
The process of providing only the essentials and hiding the details is
known as abstraction.

The user of data type does not need to know how that data type is
implemented, for example, we have been using Primitive values like
int, float, char data types only with the knowledge that these data
type can operate and be performed on without any idea of how they
are implemented.
So a user only needs to know what a data type can do, but not how
it will be implemented. Think of ADT as a black box which hides the
inner structure and design of the data type. Now we’ll define three
ADTs namely List ADT, Stack ADT, Queue ADT.
1. List ADT

Vies of list

 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.
2. Stack ADT

View of stack

 In Stack ADT Implementation instead of data being stored in


each node, the pointer to data is stored.
 The program allocates memory for the data and address is
passed to the stack ADT.
 The head node and the data nodes are encapsulated in the
ADT. The calling function can only see the pointer to the
stack.
 The stack head structure also contains a pointer
to top and count of number of entries currently in stack.
 push() – Insert an element at one end of the stack called
top.
 pop() – Remove and return the element at the top of the
stack, if it is not empty.
 peek() – Return the element at the top of the stack without
removing it, if the stack is not empty.
 size() – Return the number of elements in the stack.
 isEmpty() – Return true if the stack is empty, otherwise
return false.
 isFull() – Return true if the stack is full, otherwise return
false.
3. Queue ADT

View of Queue

 The queue abstract data type (ADT) follows the basic


design of the stack abstract data type.
 Each node contains a void pointer to the data and the link
pointer to the next element in the queue. The program’s
responsibility is to allocate memory for storing the data.
 enqueue() – Insert an element at the end of the queue.
 dequeue() – Remove and return the first element of the
queue, if the queue is not empty.
 peek() – Return the element of the queue without removing
it, if the queue is not empty.
 size() – Return the number of elements in the queue.
 isEmpty() – Return true if the queue is empty, otherwise
return false.
 isFull() – Return true if the queue is fu

Introduction to Stack – Data Structure and


Algorithm Tutorials
What is Stack?
A stack is a linear data structure in which the insertion of a new
element and removal of an existing element takes place at the
same end represented as the top of the stack.
To implement the stack, it is required to maintain the pointer to
the top of the stack, which is the last element to be inserted
because we can access the elements only on the top of the
stack.
LIFO( Last In First Out ):
This strategy states that the element that is inserted last will come
out first. You can take a pile of plates kept on top of each other as a
real-life example. The plate which we put last is on the top and since
we remove the plate that is at the top, we can say that the plate
that was put last comes out first.
Basic Operations on Stack
In order to make manipulations in a stack, there are certain
operations provided to us.
 push() to insert an element into the stack
 pop() to remove an element from the stack
 top() Returns the top element of the stack.
 isEmpty() returns true if stack is empty else false.
 size() returns the size of stack.

Stack

Push:
Adds an item to the stack. If the stack is full, then it is said to be
an Overflow condition.
Algorithm for push:
begin
if stack is full
return
endif
else
increment top
stack[top] assign value
end else
end procedure
Pop:
Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty, then
it is said to be an Underflow condition.
Algorithm for pop:
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
Top:
Returns the top element of the stack.
Algorithm for Top:
begin
return stack[top]
end procedure
isEmpty:
Returns true if the stack is empty, else false.
Types of Stacks:
 Fixed Size Stack: As the name suggests, a fixed size
stack has a fixed size and cannot grow or shrink
dynamically. If the stack is full and an attempt is made to
add an element to it, an overflow error occurs. If the stack
is empty and an attempt is made to remove an element
from it, an underflow error occurs.
 Dynamic Size Stack: A dynamic size stack can grow or
shrink dynamically. When the stack is full, it automatically
increases its size to accommodate the new element, and
when the stack is empty, it decreases its size. This type of
stack is implemented using a linked list, as it allows for
easy resizing of the stack.
ntroduction to Queue – Data Structure and
Algorithm Tutorials
 Read

 Discuss

 Courses

 Practice

What is Queue?
A queue is a linear data structure that is open at both ends and the
operations are performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are
made at one end, and all deletions from the list are made at the
other end. The element which is first pushed into the order, the
delete operation is first performed on that.
FIFO Principle of Queue:
 A Queue is like a line waiting to purchase tickets, where the
first person in line is the first person served. (i.e. First come
first serve).
 Position of the entry in a queue ready to be served, that is,
the first entry that will be removed from the queue, is called
the front of the queue(sometimes, head of the queue),
similarly, the position of the last entry in the queue, that is,
the one most recently added, is called the rear (or the tail)
of the queue. See the below figure.

FIFO property of queue

Characteristics of Queue:
 Queue can handle multiple data.
 We can access both ends.
 They are fast and flexible.
Queue Representation:

1. Array Representation of Queue:

Like stacks, Queues can also be represented in an array: In this


representation, the Queue is implemented using the array. Variables
used in this case are
Types of Queue:
There are different types of queues:
1. Input Restricted Queue: This is a simple queue. In this type
of queue, the input can be taken from only one end but
deletion can be done from any of the ends.
2. Output Restricted Queue: This is also a simple queue. In this
type of queue, the input can be taken from both ends but
deletion can be done from only one end.
3. Circular Queue: This is a special type of queue where the
last position is connected back to the first position. Here
also the operations are performed in FIFO order. To know
more refer this.
4. Double-Ended Queue (Dequeue) : In a double-ended queue
the insertion and deletion operations, both can be
performed from both ends. To know more refer this.
5. Priority Queue: A priority queue is a special queue where
the elements are accessed based on the priority assigned
to them. To know more refer this.
Basic Operations for Queue in Data Structure:
Some of the basic operations for Queue in Data Structure are:
1. Enqueue() – Adds (or stores) an element to the end of the
queue..
2. Dequeue() – Removal of elements from the queue.
3. Peek() or front()- Acquires the data element available at
the front node of the queue without deleting it.
4. rear() – This operation returns the element at the rear end
without removing it.
5. isFull() – Validates if the queue is full.
6. isNull() – Checks if the queue is empty.

Deque | Set 1 (Introduction and


Applications)
Deque or Double Ended Queue is a generalized version of Queue
data structure that allows insert and delete at both ends.
ther operations performed on deques are explained as follows:
clear(): Remove all the elements from the deque. It leaves the
deque with a size of 0.
erase(): Remove one or more elements from the deque. It takes an
iterator specifying the position of the first element to be removed,
and an optional second iterator specifying the position of the last
element to be removed.
swap(): Swap the contents of one deque with another deque.
emplace_front(): Insert a new element at the front of the deque. It
is similar to the insert operation, but it avoids the copy constructor
of the element being inserted.
emplace_back(): Insert a new element at the back of the deque. It
is similar to the insert operation, but it avoids the copy constructor
of the element being inserted.
resize(): Change the number of elements in the deque to a specific
number. If the new size is larger than the current size, new elements
are appended to the deque. If the new size is smaller than the
current size, elements are removed from the deque.
assign(): Assign new values to the elements in the deque. It
replaces the current contents of the deque with new elements.
reverse(): Reverse the order of the elements in the deque.
sort(): Sort the elements in the deque in ascending order. It uses
the less-than operator to compare the elements.
Applications of Deque: Since Deque supports both stack and
queue operations, it can be used as both. The Deque data structure
supports clockwise and anticlockwise rotations in O(1) time which
can be useful in certain applications. Also, the problems where
elements need to be removed and or added to both ends can be
efficiently solved using Deque. For example see the Maximum of all
subarrays of size k problem., 0-1 BFS, and Find the first circular tour
that visits all petrol pumps. See the wiki page for another example
of the A-Steal job scheduling algorithm where Deque is used as
deletions operation is required at both ends.
Some Practical Applications of Deque:
 Applied as both stack and queue, as it supports both
operations.
 Storing a web browser’s history.
 Storing a software application’s list of undo operations.
 Job scheduling algorithm
Application of Stack Data Structure:
 Function calls and recursion: When a function is called,
the current state of the program is pushed onto the stack.
When the function returns, the state is popped from the
stack to resume the previous function’s execution.
 Undo/Redo operations: The undo-redo feature in various
applications uses stacks to keep track of the previous
actions. Each time an action is performed, it is pushed onto
the stack. To undo the action, the top element of the stack
is popped, and the reverse operation is performed.
 Expression evaluation: Stack data structure is used to
evaluate expressions in infix, postfix, and prefix notations.
Operators and operands are pushed onto the stack, and
operations are performed based on the stack’s top
elements.
 Browser history: Web browsers use stacks to keep track
of the web pages you visit. Each time you visit a new page,
the URL is pushed onto the stack, and when you hit the
back button, the previous URL is popped from the stack.
 Balanced Parentheses: Stack data structure is used to
check if parentheses are balanced or not. An opening
parenthesis is pushed onto the stack, and a closing
parenthesis is popped from the stack. If the stack is empty
at the end of the expression, the parentheses are
balanced.
 Backtracking Algorithms: The backtracking algorithm
uses stacks to keep track of the states of the problem-
solving process. The current state is pushed onto the stack,
and when the algorithm backtracks, the previous state is
popped from the stack.
Application of Stack in real life:
 CD/DVD stand.
 Stack of books in a book shop.
 Call center systems.
 Undo and Redo mechanism in text editors.
 The history of a web browser is stored in the form of a
stack.
 Call logs, E-mails, and Google photos in any gallery are also
stored in form of a stack.
 YouTube downloads and Notifications are also shown in
LIFO format(the latest appears first

Arithmetic Expression Evaluation

 The stack organization is very effective in evaluating


arithmetic expressions. Expressions are usually represented in
what is known as Infix notation, in which each operator is
written between two operands (i.e., A + B). With this notation,
we must distinguish between ( A + B )*C and A + ( B * C ) by
using either parentheses or some operator-precedence
convention. Thus, the order of operators and operands in an
arithmetic expression does not uniquely determine the order in
which the operations are to be performed.
 1. Polish notation (prefix notation) –
It refers to the notation in which the operator is placed before
its two operands. Here no parentheses are required, i.e.,
 +AB
 2. Reverse Polish notation(postfix notation) –
It refers to the analogous notation in which the operator is
placed after its two operands. Again, no parentheses is
required in Reverse Polish notation, i.e.,
 AB+
 Stack-organized computers are better suited for post-fix
notation than the traditional infix notation. Thus, the infix
notation must be converted to the postfix notation. The
conversion from infix notation to postfix notation must take
into consideration the operational hierarchy.
Useful Applications of Queue
 When a resource is shared among multiple consumers.
Examples include CPU scheduling, Disk Scheduling.
 When data is transferred asynchronously (data not
necessarily received at the same rate as sent) between two
processes. Examples include IO Buffers, pipes, etc.
Applications of Queue in Operating systems:
 Semaphores
 FCFS ( first come first serve) scheduling, example: FIFO
queue
 Spooling in printers
 Buffer for devices like keyboard
 CPU Scheduling
 Memory management
Applications of Queue in Networks:
 Queues in routers/ switches
 Mail Queues
 Variations: ( Deque, Priority Queue, Doubly Ended Priority
Queue )
Some other applications of Queue:
 Applied as waiting lists for a single shared resource like
CPU, Disk, and Printer.
 Applied as buffers on MP3 players and portable CD players.
 Applied on Operating system to handle the interruption.
 Applied to add a song at the end or to play from the front.
 Applied on WhatsApp when we send messages to our
friends and they don’t have an internet connection then
these messages are queued on the server of WhatsApp.
 Traffic software ( Each light gets on one by one after every
time of interval of time.)
Issues in applications of Queue :
1. Queue overflow: If a queue has a fixed size, it can
become full, leading to a queue overflow. This can happen
if elements are added to the queue faster than they are
removed. To prevent overflow, some implementations use
dynamic resizing or circular buffers.
2. Queue underflow: If a queue is empty and an attempt is
made to remove an element, this can lead to a queue
underflow. This can happen if elements are removed from
the queue faster than they are added. To prevent
underflow, some implementations use sentinel values or
null pointers to represent an empty queue.
3. Blocking queues: In some applications, a queue may
become blocked if it is full or empty. This can cause delays
in processing or deadlock. To address this, some
implementations use bounded queues or non-blocking
queues.
4. Priority inversion: In some applications, a higher priority
element can get stuck behind a lower priority element in
the queue. This can lead to priority inversion and result in
reduced performance. To prevent this, some
implementations use priority queues or multiple queues
with different priorities.
5. Synchronization issues: In concurrent applications,
multiple threads may access the same queue
simultaneously. This can lead to synchronization issues like
race conditions, deadlocks, and livelocks. To address this,
some implementations use locking mechanisms like
mutexes or semaphores.
6. Memory management: In some implementations, a
queue may allocate and deallocate memory frequently,
leading to memory fragmentation and reduced
performance. To address this, some implementations use
memory pools or pre-allocated buffers.

Linked List Data Structure


A linked list is a linear data structure, in which the elements are not stored
at contiguous memory locations. The elements in a linked list are linked
using pointers as shown in the below image:
In simple words, a linked list consists of nodes where each node contains a
data field and a reference(link) to the next node in the list.
Topics :
 Introduction
 Types of Linked List
 Basic Operations
 Standard problem on Linked List
Introduction:
1. What is Linked List
2. Introduction to Linked List – Data Structure and Algorithm Tutorials
3. Applications, Advantages and Disadvantages of Linked List
4. Linked List vs Array
Types of Linked List:
1. Introduction to Doubly Linked List – Data Structure and Algorithm
Tutorials
2. Introduction to Circular Linked List
3. Circular Singly Linked List
4. Circular Doubly Linked List meaning
5. Types of Linked List
Basic Operations:
1. Linked List Insertion
2. Search an element in a Linked List (Iterative and Recursive)
3. Find Length of a Linked List (Iterative and Recursive)
4. Reverse a linked list
5. Linked List Deletion (Deleting a given key)
6. Linked List Deletion (Deleting a key at given position)
7. Write a function to delete a Linked List
8. Write a function to get Nth node in a Linked List
9. Nth node from the end of a Linked List
Standard problem on Linked List:
 Easy:
1. Print the middle of a given linked list
2. Write a function that counts the number of times a given
int occurs in a Linked List
3. Check if a linked list is Circular Linked List
4. Count nodes in Circular linked list
5. Convert singly linked list into circular linked list
6. Exchange first and last nodes in Circular Linked List
7. Reverse a Doubly Linked List
8. Program to find size of Doubly Linked List
9. An interesting method to print reverse of a linked list
10.Can we reverse a linked list in less than O(n)?
11.Circular Linked List Traversal
12.Delete a node in a Doubly Linked List
Singly linked list or One way chain
Singly linked list can be defined as the collection of ordered set of
elements. The number of elements may vary according to need of the
program. A node in the singly linked list consist of two parts: data part and
link part. Data part of the node stores actual information that is to be
represented by the node while the link part of the node stores the address
of its immediate successor.

Backward Skip 10sPlay VideoForward Skip 10s

One way chain or singly linked list can be traversed only in one direction.
In other words, we can say that each node contains only next pointer,
therefore we can not traverse the list in the reverse direction.

Consider an example where the marks obtained by the student in three


subjects are stored in a linked list as shown in the figure.

In the above figure, the arrow represents the links. The data part of every
node contains the marks obtained by the student in the different subject.
The last node in the list is identified by the null pointer which is present in
the address part of the last node. We can have as many elements we
require, in the data part of the list.

Introduction to Circular Linked List

What is Circular linked list?


The circular linked list is a linked list where all nodes are
connected to form a circle. In a circular linked list, the first node and
the last node are connected to each other which forms a circle.
There is no NULL at the end.

There are generally two types of circular linked lists:


 Circular singly linked list: In a circular Singly linked list,
the last node of the list contains a pointer to the first node
of the list. We traverse the circular singly linked list until we
reach the same node where we started. The circular singly
linked list has no beginning or end. No null value is present
in the next part of any of the nodes.

Representation of Circular singly linked list

 Circular Doubly linked list: Circular Doubly Linked List


has properties of both doubly linked list and circular linked
list in which two consecutive elements are linked or
connected by the previous and next pointer and the last
node points to the first node by the next pointer and also
the first node points to the last node by the previous pointer.

Data Structure Doubly Linked List


Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either forward and backward easily as compared to
Single Linked List. Following are the important terms to understand the
concept of doubly linked list.
 Link − Each link of a linked list can store a data called an
element.
 Next − Each link of a linked list contains a link to the next link
called Next.
 Prev − Each link of a linked list contains a link to the previous
link called Prev.
 Linked List − A Linked List contains the connection link to the
first link called First and to the last link called Last.
Doubly Linked List Representation

As per the above illustration, following are the important points to be


considered.
 Doubly Linked List contains a link element called first and last.
 Each link carries a data field(s) and a link field called next.
 Each link is linked with its next link using its next link.
 Each link is linked with its previous link using its previous link.
 The last link carries a link as null to mark the end of the list.
Basic Operations
Following are the basic operations supported by a list.
 Insertion − Adds an element at the beginning of the list.
 Deletion − Deletes an element at the beginning of the list.
 Insert Last − Adds an element at the end of the list.
 Delete Last − Deletes an element from the end of the list.
 Insert After − Adds an element after an item of the list.
 Delete − Deletes an element from the list using the key.
 Display forward − Displays the complete list in a forward
manner.
 Display backward − Displays the complete list in a backward
manner.
Insertion at the Beginning
In this operation, we create a new node with three compartments, one
containing the data, the others containing the address of its previous and
next nodes in the list. This new node is inserted at the beginning of the
list.

Deletion at the Beginning


This deletion operation deletes the existing first nodes in the doubly linked
list. The head is shifted to the next node and the link is removed

Insertion at the End


In this insertion operation, the new input node is added at the end of the
doubly linked list; if the list is not empty. The head will be pointed to the
new node, if the list is empty.

Applications of linked list data structure


A linked list is a linear data structure, in which the elements are
not stored at contiguous memory locations. The elements in a linked
list are linked using pointers as shown in the below image:

Linked-List

Applications of linked list in computer science:


1. Implementation of stacks and queues
2. Implementation of graphs: Adjacency list representation of
graphs is the most popular which uses a linked list to store
adjacent vertices.
3. Dynamic memory allocation: We use a linked list of free
blocks.
4. Maintaining a directory of names
5. Performing arithmetic operations on long integers
6. Manipulation of polynomials by storing constants in the
node of the linked list
7. Representing sparse matrices
Applications of linked list in the real world:
1. Image viewer – Previous and next images are linked and can
be accessed by the next and previous buttons.
2. Previous and next page in a web browser – We can access
the previous and next URL searched in a web browser by
pressing the back and next buttons since they are linked as
a linked list.
3. Music Player – Songs in the music player are linked to the
previous and next songs. So you can play songs either from
starting or ending of the list.
4. GPS navigation systems- Linked lists can be used to store
and manage a list of locations and routes, allowing users to
easily navigate to their desired destination.
5. Robotics- Linked lists can be used to implement control
systems for robots, allowing them to navigate and interact
with their environment.
6. Task Scheduling- Operating systems use linked lists to
manage task scheduling, where each process waiting to be
executed is represented as a node in the list.
7. Image Processing- Linked lists can be used to represent
images, where each pixel is represented as a node in the
list.
8. File Systems- File systems use linked lists to represent the
hierarchical structure of directories, where each directory or
file is represented as a node in the list.
9. Symbol Table- Compilers use linked lists to build a symbol
table, which is a data structure that stores information
about identifiers used in a program.
10. Undo/Redo Functionality- Many software applications
implement undo/redo functionality using linked lists, where
each action that can be undone is represented as a node in
a doubly linked list.
11. Speech Recognition- Speech recognition software uses
linked lists to represent the possible phonetic pronunciations
of a word, where each possible pronunciation is represented
as a node in the list.
12. Polynomial Representation- Polynomials can be
represented using linked lists, where each term in the
polynomial is represented as a node in the list.
13. Simulation of Physical Systems- Linked lists can be used
to simulate physical systems, where each element in the list
represents a discrete point in time and the state of the
system at that time.
Applications of Circular Linked Lists:
1. Useful for implementation of a queue.
Unlike this implementation, we don’t need to maintain two-
pointers for the front and rear if we use a circular linked list.
We can maintain a pointer to the last inserted node and the
front can always be obtained as next of last.
2. Circular lists are useful in applications to go around the list
repeatedly. For example, when multiple applications are
running on a PC, it is common for the operating system to
put the running applications on a list and then cycle through
them, giving each of them a slice of time to execute, and
then making them wait while the CPU is given to another
application. It is convenient for the operating system to use
a circular list so that when it reaches the end of the list it
can cycle around to the front of the list.
3. Circular Doubly Linked Lists are used for the implementation
of advanced data structures like the Fibonacci Heap.
4. Circular linked lists can be used to implement circular
queues, which are often used in operating systems for
scheduling processes and managing memory allocation.
5. Used in database systems to implement linked data
structures, such as B+ trees, which are used to optimize the
storage and retrieval of data.
6. Circular linked lists can be used in networking. For instance,
to implement circular buffers for streaming data, such as
video and audio, in networking applications.
7. Video games use circular linked lists to manage sprite
animations. Each frame of the animation is represented as a
node in the list, and the last frame is connected to the first
frame to create a loop.
8. Circular linked lists can be used to represent a buffer of
audio or signal data in signal processing applications. The
last node is connected to the first node to create a loop, and
the processing algorithms can efficiently iterate over the
data.
9. Traffic light control systems use circular linked lists to
manage the traffic light cycles. Each phase of the traffic
light cycle is represented as a node in the list, and the last
node is connected to the first node to create a loop.
Application of Doubly Linked Lists:
1. Redo and undo functionality.
2. Use of the Back and forward button in a browser.
3. The most recently used section is represented by the Doubly
Linked list.
4. Other Data structures like Stack, Hash Table, and Binary Tree
can also be applied by Doubly Linked List.
5. Used to implement game objects and their interactions in a
game engine.
6. Used in networking.
7. Used in Graph algorithms.
8. Operating systems use doubly linked lists to manage the
process scheduling. Each process waiting to be executed is
represented as a node in the doubly linked list, and the
operating system can easily traverse the list in both
directions to manage the process queue.
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.

While representing a polynomial using a linked list, each polynomial term


represents a node in the linked list. To get better efficiency in processing,
we assume that the term of every polynomial is stored within the linked
list in the order of decreasing exponents. Also, no two terms have the
same exponent, and no term has a zero coefficient and without
coefficients. The coefficient takes a value of 1.

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.

To generate a new linked list for the resulting polynomials that is formed
on the addition of given polynomials P(x) and Q(x), we perform the
following steps,

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:

3. 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:

4. 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,

You might also like