0% found this document useful (0 votes)
19 views30 pages

Dsa Unit - 1

A data structure is an organized way of storing data for efficient operations, with common types including arrays, linked lists, and trees. An algorithm is a set of instructions for problem-solving, while an abstract data type (ADT) defines a set of objects and operations without specifying implementation details. Linked lists, including singly, doubly, and circular types, allow for efficient insertion and deletion operations compared to arrays, with specific algorithms for various operations detailed in the document.

Uploaded by

geniusambur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views30 pages

Dsa Unit - 1

A data structure is an organized way of storing data for efficient operations, with common types including arrays, linked lists, and trees. An algorithm is a set of instructions for problem-solving, while an abstract data type (ADT) defines a set of objects and operations without specifying implementation details. Linked lists, including singly, doubly, and circular types, allow for efficient insertion and deletion operations compared to arrays, with specific algorithms for various operations detailed in the document.

Uploaded by

geniusambur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

What is Data Structure?

A data structure is a way of organizing and storing data to perform operations efficiently. It
defines a set of rules for organizing and manipulating data, allowing for easy access,
modification, and retrieval of information.
Common type of Data structures are arrays, linked lists, stacks, queues, trees, graphs, hash
table, heaps.

What is an Algorithm?
A set of finite rules or instructions to be followed in calculations or other
problem-solving operations
What is an abstract data type (ADT)?
An abstract data type (ADT) is a set of objects together with a set of operations. Abstract data
types are mathematical abstractions. Objects such as lists, stack, queue and graphs, along
with their operations, can be viewed as abstract data types, just as integers, reals, and
Booleans are data types.
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 List ADT


A general list of the form A0, A1, A2,. ......., AN-1. We say that the size of this list is N. We
will call the special list of size 0 an empty list.
For any list except the empty list, we say that Ai, follows (or succeeds) Ai-1 (I < N) and that
Ai-1 precedes Ai, (i> 0). The first element of the list is A0, and the last element is AN-1. We
will not define the predecessor of A0 or the successor of AN-1. The position of element Ai, in
a list is i.
Some popular operations are printList and makeEmpty, which do the obvious things, find,
which returns the position of the first occurrence of an item; insert and remove, which
generally insert and remove some element from some position in the list, and findkth, which
returns the element in some position.
If the list is 34, 12, 52, 16, 12, then find (52) might return 2; insert(x,2) might make the list
into 34, 12, x, 52, 16, 12 (if we insert into the position given); and remove (52) might turn
that list into 34, 12, x, 16, 12,

Simple Array Implementation of Lists


All of these instructions can be implemented just by using an array. Although arrays are
created with a fixed capacity, the vector class, which internally stores an array, allows the
array to grow by doubling its capacity when needed. This solves the most serious problem
with using an array, namely that historically, to use an array; an estimate of the maximum size
of the list was required.
An array implementation allows printList to be carried out in linear time, and the findKth
operation takes constant time, which is as good as, can be expected.
However, insertion and deletion are potentially expensive, depending on where the insertions
and deletions occur. In the worst case, inserting into position 0 pushing the entire array down
one spot to make room, and deleting the first element requires shifting all the elements in the
list up one spot, so the worst case for these operations is O(N).
On average, half of the list needs to be moved for either operation, so linear time is still
required. On the other hand, if all the operations occur at the high end of the list, then no
elements need to be shifted, and then adding and deleting take O(1) time
However, if insertions and deletions occur throughout the list, and in particular, at the front of
the list, then the array is not a good option. The next section deals with the alternative: the
linked list

Simple Linked Lists


In order to avoid the linear cost of insertion and deletion, we need to ensure that the list is not
stored contiguously, since otherwise entire parts of the list will need to be moved Figure
shows the general idea of a linked list.

Each node contains the element and a link to a node containing its successor. We call this the
next link. The last cell's next link points to NULL.
The findKth operation is no longer quite as efficient as an array implementation; findkth (i)
takes O(i) time and works by traversing down the list in the obvious manner.
The remove method can be executed in one next pointer change. Figure shows the result of
deleting the second element in the original list.
The insert method requires obtaining a new node from the system by using a new call and
then executing two next pointer. The general idea is shown in Figure 3.3. The dashed line
represents the old pointer.

As we can see, in principle, if we know where a change is to be made, inserting or removing


an item from a linked list does not require moving lots of items, and instead involves only a
constant number of changes to node links.

The special case of adding to the front or removing the first item is thus a constant-time
operation, presuming of course that a link to the front of the linked list is maintained.
The special case of adding at the end (i.e., making the new item as the last item) can be
constant- time, as long as we maintain a link to the last node. Thus, a typical linked list keeps
links to both ends of the list. Removing the last item is trickier, because we have to find the
next- to-last item, change its next link to NULL, and then update the link that maintains the
last node.
We have every node maintain a link to its previous node in the list. This is shown in Figure
and is known as a doubly linked list.

Linked list Data Structure


A linked list is a linear data structure that includes a series of connected
nodes. Here, each node stores the data and the address of the next node.
For example,
Linked list Data Structure

You have to start somewhere, so we give the address of the first node a
special name called HEAD. Also, the last node in the linked list can be
identified because its next portion points to NULL.
Representation of Linked List
Let's see how each node of the linked list is represented. Each node
consists:

● A data item

● An address of another node

class Node
{
int data;
Node* next;
};

Types of linked lists:


There are mainly three types of linked lists:
1. Singly-linked
linked list
2. Doubly linked list
3. Circular linked list

Singly-linked list
● Each node points to the next node in the sequence.
● Simplest form of a linked list.
Doubly Linked List:
● Each node has pointers to both the next and the previous nodes.
● Allows for easy traversal in both directions.
Circular Linked List:
● Last node in the list points back to the first node, forming a circle.
● Useful for certain algorithms and applications.
1. Singly-linked list
Traversal of items can be done in the forward direction only due to the linking
of every node to its next node.

Representation of Single linked list:


A Node Creation:
ClassNode {
public:
intdata;
Node* next;
};

Operations on Singly Linked List:


The following operations are performed on a Single Linked List

Traversal - access each element of the linked list


Insertion - adds a new element to the linked list
Deletion - removes the existing elements
Search - find a node in the linked list
Sort - sort the nodes of the linked list

Insertion: The insertion operation can be performed in three ways. They are
as follows
● At the front of the linked list
● At the arbitrary position.
● At the end of the linked list.
To insert a node at the start/beginning/front of a Linked List, we need to:

● Make the first node of Linked List linked to the new node
● Remove the head from the original first node of Linked List
● Make the new node as the Head of the Linked List.
Algorithm

void insertAtHead(Node* &head, int val)

node* newnode=new Node(val);

newnode->next=head;

head=newnode;

Complexity Analysis:
● Time Complexity: O(1)
● Auxiliary Space: O(1)

To insert a node at an arbitrary position in a Linked List, we need to:

● Check if the given node exists or not.


● If it do not exists,
● terminate the process.
● If the given node exists,
● Make the element to be inserted as a new node
● Change the next pointer of given node to the new
node
● Now shift the original next pointer of given node
to the next pointer of new node
Algorithm:

void insertAtPosition(Node* &head, int val, int pos)

if(pos==0)

insertAtHead(head,val);

return;

Node* newnode=new Node(val);

Node* temp=head;

Int currentpos=0;

while(currentpos!=pos-1)

temp=temp->next;

currentpos++;

newnode->next=temp->next;

temp->next=newnode;

Complexity Analysis:
● Time complexity: O(1)
● Auxiliary Space: O(1)
To insert a node at the end of a Linked List, we need to:

● Go to the last node of the Linked List


● Change the next pointer of last node from NULL to the new node
● Make the next pointer of new node as NULL to show the end of
Linked List

Algorithm:
void insertAtTail(Node* head, int val)
{
Node* newnode=new Node(val);
Node*temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}

temp->next=newnode;
newnode=NULL;
}

Complexity Analysis:
● Time complexity: O(N)
● Auxiliary Space: O(1)

Deletion: The deletion operation can be performed in three ways as


follows
Delete a node from a Linked List:-
Deletion in a linked list involves removing a node. You can
delete an element in a list from:

● Delete a node at the front


● Delete a node at the end
● .Delete a node at an arbitrary position

Delete a node at the front

● If the singly linked list is empty, indicate that deletion is not possible.
● Save the reference to the current head node in a temporary variable.
● Update the head to point to the next node in the list.
● Delete the node that was originally at the beginning.
Algorithm:

void deleteAtHead(Node* &head)

Node* temp=head;

head=head->next;

delete(temp);

Time Complexity: O(1)


Auxiliary Space: O(1)

Delete a node at the end


● If the singly linked list is empty, indicate that deletion is not possible
● If there is only one node in the list, delete it and set the head to nullptr.
● Traverse the list to the second-last node (the one before the last node).
● Delete the last node.
● Set the next pointer of the second-last node to nullptr.
Algorithm:

void deleteAtTail(Node* &head)

Node* secondlast=head;

while(secondlast->next->next!=NULL)

secondlast->next=NULL;

delete(temp);

Time Complexity: O(n)


Auxiliary Space: O(1)

Delete a node at and arbitrary position

● If the singly linked list is empty, indicate that deletion is not possible.
● If the position is 1, delete the node at the beginning.
● Traverse the list to the node just before the specified position.
● Ensure that the specified position is valid for deletion.
● Update pointers to skip the node to be deleted.
● Delete the node.

Algorithm:

void deleteAtPositionl(Node* &head, int pos)

if(pos==0)

deleteAtHead(head);
return;

Int curpos=0;

Node* prev=head;

while(currpos!=pos-1)

prev=prev->next;

currpos++;

Node* temp=prev->next;

prev->next=prev->next->next;

delete(temp);

Time Complexity: O(n)


Auxiliary Space: O(1)

Doubly linked list


Doubly linked list is a complex type of linked list in which a node contains a pointer to
the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a
node consists of three parts: node data, pointer to the next node in sequence (next pointer)
and pointer to the previous node (previous pointer). A sample node in a doubly linked list
is shown below.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part,
is shown in the following image.

In C++, structure of a node in doubly linked list can be given as :


class node
{
class node* prev;
int data;
class node* next;
}

The prev part of the first node and the next part of the last node will always contain null
indicating end in each direction.

Difference between Singly linked list and doubly linked list

Singly Linked List Vs Doubly Linked List


Singly Linked List Doubly Linked List

Each node consists of a data value Each node consists of a data value, a pointer to the
and a pointer to the next node. next node, and a pointer to the previous node.

Traversal can occur in one way only


Traversal can occur in both ways.
(forward direction).

It requires less space. It requires more space because of an extra pointer.

It has multiple usages. It can be implemented on the


It can be implemented on the stack.
stack, heap, and binary tree.
Operations on doubly linked list

SN Operation Description

1 Insertion Adding the node into the Doubly linked list.

2 Deletion Removing the node from the Doubly linked list.

3 Searching Comparing each node data with the item to be searched and return
the location of the item in the list if the item found else return
null.

4 Traversing Visiting each node of the list at least once in order to perform
some specific operation like searching, sorting, display, etc.

Insertion in a Doubly Linked List


Inserting a new node in a doubly linked list is very similar to inserting new node in
linked list. There is a little extra work required to maintain the link of the previous
node. A node can be inserted in a Doubly Linked List in three ways:
● At the front of the DLL.
● At the end of the DLL.
● At the arbitrary position of DLL.
Add a node at the front in a Doubly Linked List:
The new node is always added before the head of the given Linked List. The task
can be performed by using the following steps:
1. Firstly, allocate a new node (say newnode).
2. Now put the required data in the new node.
3. Make the next of newnode point to the current head of the doubly linked list.
4. Make the previous of the current head point to newnode.
5. Lastly, point head to newnode.
Illustration:
See the below illustration where E is being inserted at the beginning of the doubly
linked list.
Algorithm:
void insertAtstart(int val)
{
Node* newnode=new Node(val);
if(head==NULL)
{
head=newnode;
tail=newnode;
return;
}
newnode->next=head;
head->prev=newnode;
head=newnode;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Add a node at the end in a Doubly Linked List:
The new node is always added after the last node of the given Linked List.
This can be done using the following steps:

1. Create a new node (say newnode).


2. Put the value in the new node.
3. Make the next pointer of newnode as null.
4. If the list is empty, make newnode as the tail.
5. Now make the next pointer of tail point to newnode.
6. Change the previous pointer of newnode to the tail node of the list.

Illustration:
See the below illustration where ‘D‘ is inserted at the end of the linked list.

Algorithm:
void insertAtEnd(int val)
{
node* newnode=new Node(val);
if(tail==NULL)
{
head=newnode;
tail=newnode;
return;
}
tail->next=newnode;
newnode->prev=tail;
tail=newnode;
return;
Time Complexity: O(n)
Auxiliary Space: O(1)
Add a node after a given node in a Doubly Linked List:
We are given a pointer to a node as prevnode,, and the new node is inserted after
the given node. This can be done using the following steps:
1. Firstly create a new node (say newnode).
2. Now insert the data in the new node.
3. Point the next of newnode to the next of prevnode.
4. Point the next of prevnode to newnode.
5. Point the previous of newnode to prevnode.
6. Change the pointer of the new node’s previous pointer to newnode.
Illustration:
See the below illustration where ‘E‘
‘ is being inserted after ‘B‘.

Algorithm:
void insertAtPosition(int val, int k)
{
Node* temp=head;
int count=0;
While(count<(k-1));
{
temp=temp->next;
count++;
}
Node* newnode=new Node(val);
newnode->next=temp->next;
>next;
temp->next=newnode;
newnode->prev=temp;
newnode->next->prev=newnode;
>prev=newnode;
return;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Deletion in Doubly Linked List
Deletion from Beginning

In the process of deletion from beginning we make the second node of the list as new
head and free the previous head of the list. This can be done using the following
steps:

● If the list is empty (head is null), there is nothing to delete, so return.


● Create a temporary pointer (temp) and assign it to the current head.
● Move the head pointer to the next node.
● update its prev pointer to nullptr.
● Delete the node that was originally at the front of the list, using the temporary
pointer (temp).

Algorithm:
void deleteAtStart()
{
if(head==NULL)
{
return;
}
Node* temp=head;
head=head->next;
if(head==NULL)
{
tail==NULL;
}
else{
head->prev=NULL;
}
delete(temp);
Time Complexity: O(1)
Auxiliary Space: O(1)

Deletion from end

In the process of deletion from the end of the doubly linked list we point the next pointer
of last second node to NULL and then we will free the last node. This can be done
using the following steps:

● If the list is empty (head is null), there is nothing to delete, so return.


● Create a temporary pointer (temp) and assign it to the tail which is the last
node.
● Set the tail as tail prev pointer and set tail next to null, indicating that it is
now the last node.
● Delete the node that was originally at the end of the list.

Algorithm:
void deleteAtStart()
{
if(head==NULL)
{
return;
}
Node* temp=tail;
tail=tail->prev;
if(tail==NULL)
{
head==NULL;
else
{
tail->next=NULL;
}
delete(tail);
Time Complexity: O(1)
Auxiliary Space: O(1)

Deletion from specific position

In the process of deletion from a specific position in a doubly linked list we delete any
node that the user wants to delete from the given linked list. This can be done using
the following steps:

● If the list is empty (head is nullptr), there is nothing to delete, so return.


● Start from the head and traverse the list to find the node with the specified
value or position.
● Set the next pointer of the previous node to the next pointer of the node to be
deleted.
● Set the prev pointer of the next node to the prev pointer of the node to be
deleted.
● Delete the node that was at the arbitrary position.

Algorithm:
void deleteAtPosition(int k)
{
Node* temp=head;
int count=1;
while(count<k)
{
temp=temp->next;
count++;
}
temp->prev->next=temp->next;
temp->next->prev=temp=temp->prev;
delete(temp);
}
Time Complexity: O(n)
Auxiliary Space: O(1)

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
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 as 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
thee last node by the previous pointer.
Representation of circular doubly linked list

Representation of circular linked list:


class Node {
int data;
class Node *next;
};

Operation:
Insertion at the beginning
Unsurprisingly, because of the circular property, entering data at the beginning is slightly
more complicated than entering data at the end. For entering data at the beginning of the
linked list, we have to create a new node and assign the data to it. Once done, we have to
traverse to the end of the linked list using a pointer.

The task can be performed by using the following steps:


● Allocate memory for a new node.
● Set the data of the new node.
● If the circular linked list is empty, make the new node point to itself.
● Traverse the list to find the last node (the one pointing back to the head).
● Make the new node point to the current head.
● Update the last node to point to the new node.
● Update the head to the new node.

Algorithm:
void insertend(int val)
{
Node* newnode= new Node(val);
if(head==NULL)
{
head=newnode;
Newnode->next=head;
return;
}
Node* tail=head;
while(tail->next!=head)
{
tail=tail->next;
}
tail->next=newnode;
newnode->next=head;
head=newnode;
}

Time Complexity: O(N)

Auxiliary Space: O(1)

Insertion at the end


We need to create a new node and assign the data to it. Following that, we need to
traverse to the last node of the linked list.
The task can be performed by using the following steps:
● Allocate memory for a new node.
● Set the data of the new node.
● If the circular linked list is empty, make the new node point to itself.
● Traverse the list to find the last node (the one pointing back to the head).
● Update the next pointer of the last node to point to the new node.
● Update the next pointer of the new node to point back to the head.

Algorithm
void insertStart(Node* &head, int val)
{
Node* newnode= new Node(val);
if(head==NULL)
{
head=newnode;
Newnode->next=head;
return;
}
Node* tail=head;
while(tail->next!=head)
{
tail=tail->next;
}
tail->next=newnode;
newnode->next=head;
}

Time Complexity: O(N)

Auxiliary Space: O(1)

Insertion at the Specified Position(Same as Singly Linked List)


Traverse the linked list till before the point where the new node has to be inserted.

The task can be performed by using the following steps:

● Allocate memory for a new node.


● Set the data of the new node.
● If the circular linked list is empty or the position is 1, insert at the beginning.
● Update the last node to maintain circular linkage.
● Traverse the list to the node just before the specified position.
● Update pointers to include the new node.
Algorithm
void insertAtPosition(Node* &head, int val, int pos)
{
if(pos==0)
{
insertAtHead(head,val);
return;
}
Node* newnode=new Node(val);
Node* te
mp=head;
Int currentpos=0;
while(currentpos!=pos-1)
{
temp=temp->next;
currentpos++;
}
newnode->next=temp->next;
temp->next=newnode;
}

Time Complexity: O(N)

Auxiliary Space: O(1)

Deletion of Element at Start


The process for deletion works almost in a similar manner as for insertion, with only one
fundamental difference – deletion requires two pointers, one for pointing towards the
node before, and one for pointing towards the node after. In a similar fashion as insertion,
traverse till the last node of the linked list with one pointer and store the head of the
linked list in another pointer.

The task can be performed by using the following steps:

● If the circular linked list is empty, indicate that deletion is not possible.
● If there is only one node in the list, delete it and set the head to nullptr.
● Traverse the list to find the last node (the one pointing back to the head).
● Update the head to the next node.
● Update the next pointer of the last node to point to the new head.
● Delete the node at the beginning.

Algorithm
void deleteAtStart()
{
if(head==NULL)
{
return;
}
Node* temp=head;
Node* tail=head;
while(tail->next!=head)
{
tail=tail->next;
{
head=head->next;
tail->next=head;
delete(temp);
}

Time Complexity: O(N)

Auxiliary Space: O(1)

Deletion of Element at the End


Deletion from the end is considerably easier. All you need to do is traverse till the end of
the linked list with one pointer and till the node before the last node with another pointer.

The task can be performed by using the following steps:

● If the circular linked list is empty, indicate that deletion is not possible.
● If there is only one node in the list, delete it and set the head to nullptr.
● Traverse the list to find the last and second-last nodes (the one pointing back to the
head).
● Update the next pointer of the second-last node to point to the head.
● Delete the last node.
Algorithm:
void deleteAtEnd()
{
if(head==NULL)
{
return;
}
Node* temp=head;
while(tail->next->next!=head)
{
tail=tail->next
}
tail=tail->next;
Node* temp=tail->next;
tail->next=head;
delete(temp);
}

Time Complexity: O(N)

Auxiliary Space: O(1)

Deletion of Element at the Middle(Same as Singly Linked List)


A generalized algorithm for deletion can be designed similarly to insertion. All one needs
to do is make one pointer traverse to a node before the required node in the list and one
pointer traverse to the required node itself.

The task can be performed by using the following steps:

● If the circular linked list is empty, indicate that deletion is not possible.
● If there is only one node in the list, delete it and set the head to nullptr.
● If the position is 1, update the last node's next pointer and delete the first node.
● Traverse the list to the node just before the specified position.
● Ensure that the specified position is valid for deletion.
● Update pointers to skip the node to be deleted.
● Delete the node.
Algorithm:
void deleteAtPositionl(Node* &head, int pos)
{
if(pos==0)
{
deleteAtHead(head);
return;
}
Int curpos=0;
Node* prev=head;
while(currpos!=pos-1)
{
prev=prev->next;
currpos++;
}
Node* temp=prev->next;
prev->next=prev->next->next;
delete(temp);
}

Time Complexity: O(N)

Auxiliary Space: O(1)

Application
Application of Linked list:

Dynamic Memory Allocation: Linked lists allow for dynamic memory allocation, as they
can easily grow or shrink in size without the need for contiguous memory blocks.
Implementation of Data Structures: Linked lists are fundamental for implementing
other data structures like stacks, queues, and symbolic expressions
File Systems: Linked lists are used in the implementation of file systems to manage the
allocation of disk space.
Graph Algorithms: In graph algorithms, linked lists are used to represent adjacency
lists.
Hash Tables: Separate chaining, a technique used in hash tables to handle collisions,
involves using linked lists.
Polynomial Representation: Linked lists can be used to represent polynomials
efficiently
Navigation Systems: Linked lists are used in navigation systems to represent routes.

Application of Doubly Linked List


1. Undo Functionality in Text Editors: Text editors often use doubly linked lists to
implement undo functionality.
2. Browser History: Each page in the history is a node, and users can navigate both
forward and nd backward through their browsing history.
3. Cache Implementation: Doubly linked lists are used in cache implementation,
particularly in the LRU (Least Recently Used) algorithm.
4. Database Management Systems: Doubly linked lists can be used in database systems
to represent relationships between records or rows. The bidirectional links enable
efficient traversal in both directions.
5. Image Viewer with Zoom and Pan: In applications like image viewers, doubly linkelinked
lists can be utilized to manage a sequence of images. Bidirectional links allow users to
zoom in, pan, and navigate both forward and backward through the images.

Application of Circular Linked List

● Round-Robin
Robin Scheduling: In a round-robin scheduling scheme,
eme, each process is
assigned a fixed time slot, and the scheduler moves to the next process in a
circular manner.
● Buffer Management: Data is continually processed in a loop. When the end of
the list is reached, the processing continues from the beginning.
● Game Development: A list of players taking turns in a game or a circular path in
a game world.
● Resource Allocation: Circular linked lists can be used in resource allocation
scenarios where resources are allocated in a cyclical manner.
● File System Management:
ent: Circular linked lists can be used in file systems to
manage free disk space.

Polynomial Addition
Let say we have the following two polynomials
3x^2+2x+1
5x^2+x+2
Addition of polynomials involves combining like terms present in the two
polynomials.It means adding terms having same variables and some exponents.
In these two polynomials 3x^6 and 5x^2 are like terms. Similarly, 2x and x are like terms
3x^2+2x+1
5x^2+x+2
8x^2+x+3
Addition of two polynomials becomes easier if the terms are arranged in descending
order of their exponents.
Let's say we have the following two polynomials and our job is to add them.
5x^6+6x^4+2x^3
8X^6=3x^2+4X+5
Terms are arranged in descending order of their exponents.
Adding two polynomials means adding their like terms.
The only thing we have to do is to compare their exponents.
5x^6+6x^4+2x^3 poly1
8x^6+3x^2+4x+5 poly2
13x^6+6x^4+2x^3+3x^24x+5
In a linked list representation,
on, the addition operation is easier to perform because the
two polynomials represented by the linked lists are arranged in descending order.

Algorithm

void PolyAdd(Node* head1, Node* head2)

Node* ptr1=head1;

Node* ptr2=head2;

Node* head3=NULL;

while(ptr1!=NULL&&ptr2!=NULL)

if(ptr1->expo==ptr2->expo)

head3=insert(head3,ptr1->coeff+ptr2
>coeff+ptr2->coeff,ptr1->expo);
ptr1=ptr1->link;

ptr2=ptr2->link;

else if(ptr1->expo>ptr2->expo)

head3=insert(head3,ptr1->coeff,ptr1->expo);

ptr1=ptr1->link;

else if(ptr1->expo<ptr2->expo)

head3=insert(head3,ptr2->coeff,ptr2->expo);

ptr1=ptr1->link;

While(ptr1!=NULL)

head3=insert(head3,ptr1->coeff,ptr1->expo);

ptr2=ptr2->link;

while(ptr2!=NULL)

head3=insert(head3,ptr2->coeff,ptr2->expo);

ptr1=ptr1->link;

You might also like