Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

SRI BALAMURUGAN ARTS & SCIENCE COLLEGE

DEPARTMENT OF COMPUTER SCIENCE


STUDY MATERIAL

PAPER NAME: DATA STRUCTURE AND ALGORITHMS


CLASS: I B.Sc (CS)

SEMESTER - II

SUBJECT INCHARGE HOD PRINCIPAL


SYLLABUS
DATA STRUCTURE AND ALGORITHMS

UNIT I
Abstract Data Types (ADTs)- List ADT-array-based implementation-linked list
implementationsingly linked lists-circular linked lists-doubly-linked lists-applications of lists-
Polynomial Manipulation- All operations-Insertion-Deletion-Merge-Traversal
UNIT II
Stack ADT-Operations- Applications- Evaluating arithmetic expressions – Conversion of infix
topostfix expression-Queue ADT-Operations-Circular Queue- Priority Queue-
deQueueapplications of queues.
UNIT III
Tree ADT-tree traversals-Binary Tree ADT-expression trees-applications of trees-binary search
tree ADT- Threaded Binary Trees-AVL Trees- B-Tree- B+ Tree – Heap-Applications of heap.
UNIT IV
Definition- Representation of Graph- Types of graph-Breadth first traversal – Depth first
traversal-Topological sort- Bi-connectivity – Cut vertex- Euler circuits-Applications of graphs.
UNIT V
Searching- Linear search-Binary search-Sorting-Bubble sort-Selection sort-Insertion sort-Shell
sort-Radix sort-Hashing-Hash functions-Separate chaining- Open Addressing-
RehashingExtendible Hashing

TEXT BOOK
1. Mark Allen Weiss, ―Data Structures and Algorithm Analysis in C++‖, Pearson Education 2014,
4th Edition.
2.ReemaThareja, ―Data Structures Using C‖, Oxford Universities Press 2014, 2nd Edition

REFERENCE BOOKS
1.Thomas H.Cormen,ChalesE.Leiserson,RonaldL.Rivest, Clifford Stein, ―Introduction to
Algorithms‖, McGraw Hill 2009, 3rd Edition.
2.Aho, Hopcroft and Ullman, ―Data Structures and Algorithms‖, Pearson Education 2003.
UNIT I
DATA:
A collection of facts, concepts, figures, observations, occurrences or instructions in a
formalized manner.
INFORMATION:
The meaning that is currently assigned to data by means of the conventions applied to
those data(i.e. processed data)
RECORD:
Collection of related fields.
DATA TYPE:
Set of elements that share common set of properties used to solve a program.
DATA STRUCTURES:
Data Structure is the way of organizing, storing, and retrieving data and their relationship
with each other.
Characteristics of data structures:
1. It depicts the logical representation of data in computer memory.
2. It represents the logical relationship between the various data elements.
3. It helps in efficient manipulation of stored data elements.
4. It allows the programs to process the data in an efficient manner.
Operations on Data Structures:
1. Traversal
2.Search
3.Insertion
4.Deletion
Application of data structures:
Data structures are widely applied in the following areas:
Compiler design
Operating system
Statistical analysis package
DBMS
Numerical analysis
Simulation
Artificial intelligence
Graphics
ABSTRACT DATA TYPES (ADTS):
An abstract Data type (ADT) is defined as a mathematical model with a collection of operations
defined on that model. Set of integers, together with the operations of union, intersection and set
difference form a example of an ADT. An ADT consists of data together with functions that
operate on that data.
Advantages/Benefits of ADT:
1. Modularity
2.Reuse
3. code is easier to understand
4. Implementation of ADTs can be changed without requiring changes to the program that uses
the ADTs.

THE LIST ADT:


List is an ordered set of elements.
The general form of the list is A1 ,A2 , ……,AN
A1 - First element of the list
A2- 1st element of the list
N –Size of the list
If the element at position i is Ai, then its successor is Ai+1 and its predecessor is Ai-1
Various operations performed on List
1. Insert (X, 5)- Insert the element X after the position 5.
2. Delete (X) - The element X is deleted
3. Find (X) - Returns the position of X.
4. Next (i) - Returns the position of its successor element i+1.
5. Previous (i) Returns the position of its predecessor i-1.
6. Print list - Contents of the list is displayed.
7. Makeempty- Makes the list empty.
Implementation of list ADT:
1. Array based Implementation
2. Linked List based implementation
Array Implementation of list:
Array is a collection of specific number of same type of data stored in consecutive memory
locations. Array is a static data structure i.e., the memory should be allocated in advance and the
size is fixed. This will waste the memory space when used space is less than the allocated space.
Insertion and Deletion operation are expensive as it requires more data movements
Find and Print list operations takes constant time.

The basic operations performed on a list of elements are


a. Creation of List.
b. Insertion of data in the List
c. Deletion of data from the List
d. Display all data‟s in the List
e. Searching for a data in the list

Declaration of Array:

#define maxsize 10
int list[maxsize], n ;
Create Operation:

Create operation is used to create the list with „ n „ number of elements .If „ n „ exceeds the
array‟s maxsize, then elements cannot be inserted into the list. Otherwise the array elements are
stored in the consecutive array locations (i.e.) list [0], list [1] and so on.
void Create ( )
{
int i;
printf("\nEnter the number of elements to be added in the list:\t");
scanf("%d",&n);
printf("\nEnter the array elements:\t");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
}

If n=6, the output of creation is as follows:


list[6]

Insert Operation:
Insert operation is used to insert an element at particular position in the existing list. Inserting the
element in the last position of an array is easy. But inserting the element at a particular position
in an array is quite difficult since it involves all the subsequent elements to be shifted one
position to the right.

Routine to insert an element in the array:


void Insert( )
{

int i,data,pos;
printf("\nEnter the data to be inserted:\t");
scanf("%d",&data);
printf("\nEnter the position at which element to be inserted:\t");
scanf("%d",&pos);
if (pos==n)
printf (“Array overflow”);
for(i = n-1 ; i >= pos-1 ; i--)
list[i+1] = list[i];
list[pos-1] = data;
n=n+1;
Display();}
Consider an array with 5 elements [ max elements = 10 ]

10 20 30 40 50

If data 15 is to be inserted in the 2nd position then 50 has to be moved to next index position, 40
has to be moved to 50 position, 30 has to be moved to 40 position and 20 has to be moved to
30 position.

10 20 30 40 50

10 20 30 40 50

After this four data movement, 15 is inserted in the 2nd position of the array.

10 15 20 30 40 50
Deletion Operation:
Deletion is the process of removing an element from the array at any position.
Deleting an element from the end is easy. If an element is to be deleted from any particular
position ,it requires all subsequent element from that position is shifted one position towards
left.
Routine to delete an element in the array:
void Delete( )
{

int i, pos ;
printf("\nEnter the position of the data to be deleted:\t");
scanf("%d",&pos);
printf("\nThe data deleted is:\t %d", list[pos-1]);
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1];
n=n-1;
Display();
}

Consider an array with 5 elements [ max elements = 10 ]

10 20 30 40 50

If data 20 is to be deleted from the array, then 30 has to be moved to data 20 position, 40 has to
be moved to data 30 position and 50 has to be moved to data 40 position.

10 20 30 40 50
After this 3 data movements, data 20 is deleted from the 2nd position of the array.

10 30 40 50

Display Operation/Traversing a list


Traversal is the process of visiting the elements in a array.
Display( ) operation is used to display all the elements stored in the list. The elements are stored
from the index 0 to n - 1. Using a for loop, the elements in the list are viewed
Routine to traverse/display elements of the array:
void display( )
{

int i;
printf("\n**********Elements in the array**********\n");
for(i=0;i<n;i++)
printf("%d\t",list[i]);
}

Search Operation:
Search( ) operation is used to determine whether a particular element is present in the list or not.
Input the search element to be checked in the list.
Routine to search an element in the array:
void Search( )
{

int search,i,count = 0;
printf("\nEnter the element to be searched:\t");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(search == list[i])
count++;
}

if(count==0)
printf("\nElement not present in the list");
else
printf("\nElement present in the list");
}

Linked list based implementation:

Linked Lists:
A Linked list is an ordered collection of elements. Each element in the list is referred as a node.
Each node contains two fields namely,
Data field-The data field contains the actual data of the elements to be stored in the list
Next field- The next field contains the address of the next node in the list

DATA NEXT

A Linked list node

Null 10 40 20 30

L
Advantages of Linked list:
1. Insertion and deletion of elements can be done efficiently
2. It uses dynamic memory allocation
3. Memory utilization is efficient compared to arrays
Disadvantages of linked list:
1. Linked list does not support random access
2. Memory is required to store next field
3.Searching takes time compared to arrays
Types of Linked List
1. Singly Linked List or One Way List
2. Doubly Linked List or Two-Way Linked List
3. Circular Linked List
Dynamic allocation

The process of allocating memory to the variables during execution of the program or at run time
is known as dynamic memory allocation. C language has four library routines which allow this
function.
Dynamic memory allocation gives best performance in situations in which we do not know
memory requirements in advance. C provides four library routines to automatically allocate
memory at the run time.

To use dynamic memory allocation functions, you must include the header file stdlib.h.
malloc()
The malloc function reserves a block of memory of specified size and returns a pointer of
type void. This means that we can assign it to any type of pointer.
The general syntax of malloc()
is ptr =(cast-type*)malloc(byte-
size);
where ptr is a pointer of type cast-type. malloc() returns a pointer (of cast type) to an area of
memory with size byte-size.
calloc():
calloc() function is another function that reserves memory at the run time. It is normally used
to request multiple blocks of storage each of the same size and then sets all bytes to zero.
calloc() stands for contiguous memory allocation and is primarily used to allocate memory
for arrays. The syntax of calloc() can be given as:
ptr=(cast-type*) calloc(n,elem-size);
The above statement allocates contiguous space for n blocks each of size elem-size bytes.
The only difference between malloc() and calloc() is that when we use calloc(), all bytes
are initialized to zero. calloc() returns a pointer to the first byte of the allocated region.
free():
The free() is used to release the block of memory.
Syntax:
The general syntax of the free()function
is, free(ptr);
where ptr is a pointer that has been created by using malloc() or calloc(). When memory is
deallocated using free(), it is returned back to the free list within the heap.
realloc():
At times the memory allocated by using calloc() or malloc() might be insufficient or in
excess. In both the situations we can always use realloc() to change the memory size
already allocated by calloc() and malloc(). This process is called reallocation of memory.
The general syntax for realloc() can be given as,
ptr = realloc(ptr,newsize);
variable ptr. It returns a pointer to the first byte of the memory block. The allocated new block may
be or may not be at the same region. Thus, we see that realloc() takes two arguments. The first is the
pointer referencing the memory and the second is the total number of bytes you want to reallocate.
Differences between Array based and Linked based implementation

Array Linked List

Linked list is an ordered


Array is a collection of elements
collection of elements which
Definition having same data type with common
are connected by
name
links/pointers

Elements can be accessed using


Access Sequential access
index/subscript, random access

Elements are stored in contiguous Elements are stored at


Memory structure
memory locations available memory space

Insertion and deletion takes more time Insertion and deletion are fast
Insertion & Deletion
in array and easy

Memory is allocated at run


Memory is allocated at compile time
Memory Allocation time i.e dynamic memory
i.e static memory allocation
allocation

Types 1D,2D,multi-dimensional SLL, DLL circular linked list

Each node is dependent on


each other as address part
Dependency Each elements is independent
contains address of next
node in the list
SINGLY LINKED LIST
A singly linked list is a linked list in which each node contains only one link field
pointing to the next node in the list

SLL

SLL with a Header


Basic operations on a singly-linked list are:
1. Insert – Inserts a new node in the list.
2. Delete – Deletes any node from the list.
3. Find – Finds the position( address ) of any node in the list.
4. FindPrevious - Finds the position( address ) of the previous node in the list.
5. FindNext- Finds the position( address ) of the next node in the list.
6. Display-display the date in the list
7. Search-find whether a element is present in the list or not

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

Operations on Singly Linked List


There are various operations which can be performed on singly linked list. A list of all such
operations is given below.

Node Creation
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));
Insertion
The insertion into a singly linked list can be performed at different positions. Based on the
position of the new node being inserted, the insertion is categorized into the following categories.
SN Operation Description

1 Insertion at It involves inserting any element at the front of the list. We just
beginning need to a few link adjustments to make the new node as the
head of the list.

2 Insertion at end of It involves insertion at the last of the linked list. The new node
the list can be inserted as the only node in the list or it can be inserted
as the last one. Different logics are implemented in each
scenario.

3 Insertion after It involves insertion after the specified node of the linked list.
specified node We need to skip the desired number of nodes in order to reach
the node after which the new node will be inserted. .

Deletion and Traversing


The Deletion of a node from a singly linked list can be performed at different positions. Based on
the position of the node being deleted, the operation is categorized into the following categories.

SN Operation Description

1 Deletion at It involves deletion of a node from the beginning of the list. This
beginning is the simplest operation among all. It just need a few adjustments
in the node pointers.

2 Deletion at the It involves deleting the last node of the list. The list can either be
end of the list empty or full. Different logic is implemented for the different
scenarios.

3 Deletion after It involves deleting the node after the specified node in the list. we
specified node need to skip the desired number of nodes to reach the node after
which the node will be deleted. This requires traversing through
the list.

4 Traversing In traversing, we simply visit each node of the list at least once in
order to perform some specific operation on it, for example,
printing data part of each node present in the list.

5 Searching In searching, we match each element of the list with the given
element. If the element is found on any of the location then
location of that element is returned otherwise null is returned. .

Advantages of SLL
1. The elements can be accessed using the next link
2. Occupies less memory than DLL as it has only one next field.
Disadvantages of SLL
1. Traversal in the backwards is not possible
2. Less efficient to for insertion and deletion.

DOUBLY-LINKED LIST
A doubly linked list is a linked list in which each node has three fields namely Data,
Next, Prev. Data-This field stores the value of the element
Next-This field points to the successor node
in the list Prev-This field points to the
predecessor node in the list
PREV DATA NEXT

DLL NODE

DOUBLY LINKED LIST

Basic operations of a doubly -linked list are:


1. Insert – Inserts a new element at the end of the list.
2. Delete – Deletes any node from the list.
3. Find – Finds any node in the list.
4. Print – Prints the 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) , pointer to
the previous node (previous pointer). A sample node in a doubly linked list is shown in the
figure.

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 :

1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. }
The prev part of the first node and the next part of the last node will always contain null
indicating end in each direction.

In a singly linked list, we could traverse only in one direction, because each node contains
address of the next node and it doesn't have any record of its previous nodes. However, doubly
linked list overcome this limitation of singly linked list. Due to the fact that, each node of the list
contains the address of its previous node, we can find all the details about the previous node as
well by using the previous address stored inside the previous part of each node.

Memory Representation of a doubly linked list


Memory Representation of a doubly linked list is shown in the following image. Generally,
doubly linked list consumes more space for every node and therefore, causes more expansive
basic operations such as insertion and deletion. However, we can easily manipulate the elements
of the list since the list maintains pointers in both the directions (forward and backward).

In the following image, the first element of the list that is i.e. 13 stored at address 1. The head
pointer points to the starting address 1. Since this is the first element being added to the list
therefore the prev of the list contains null. The next node of the list resides at address 4
therefore the first node contains 4 in its next pointer.
We can traverse the list in this way until we find any node containing null or -1 in its next part.

Operations on doubly linked list


Node Creation

1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. };
7. struct node *head;
All the remaining operations regarding doubly linked list are described in the following table.

SN Operation Description

1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.

3 Insertion after specified Adding the node into the linked list after the specified
node node.

4 Deletion at beginning Removing the node from beginning of the list

5 Deletion at the end Removing the node from end of the list.

6 Deletion of the node Removing the node which is present just after the node
having given data containing the given data.

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

8 Traversing Visiting each node of the list at least once in order to


perform some specific operation like searching, sorting,
display, etc.

Advantages of DLL:
The DLL has two pointer fields. One field is prev link field and another is next link
field. Because of these two pointer fields we can access any node efficiently whereas in
SLL only one link field is there which stores next node which makes accessing of any
node difficult.
Disadvantages of DLL:
The DLL has two pointer fields. One field is prev link field and another is next link
field. Because of these two pointer fields, more memory space is used by DLL
compared to SLL
CIRCULAR LINKED LIST:
Circular Linked list is a linked list in which the pointer of the last node points to the first node.

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 can have circular singly linked list as well as circular doubly linked list.

We traverse a circular singly linked list until we reach the same node where we started. The
circular singly liked list has no beginning and no ending. There is no null value present in the
next part of any of the nodes.

The following image shows a circular singly linked list.

Circular linked list are mostly used in task maintenance in operating systems. There are many
examples where circular linked list are being used in computer science including browser surfing
where a record of pages visited in the past by the user, is maintained in the form of circular
linked lists and can be accessed again on clicking the previous button.

Memory Representation of circular linked list:


In the following image, memory representation of a circular linked list containing marks of a
student in 4 subjects. However, the image shows a glimpse of how the circular list is being stored
in the memory. The start or head of the list is pointing to the element with the index 1 and
containing 13 marks in the data part and 4 in the next part. Which means that it is linked with the
node that is being stored at 4th index of the list.

However, due to the fact that we are considering circular linked list in the memory therefore the
last node of the list contains the address of the first node of the list.

We can also have more than one number of linked list in the memory with the different start
pointers pointing to the different start nodes in the list. The last node is identified by its next part
which contains the address of the start node of the list. We must be able to identify the last node
of any linked list so that we can find out the number of iterations which need to be performed
while traversing the list.

Operations on Circular Singly linked list:


Insertion

SN Operation Description

1 Insertion at beginning Adding a node into circular singly linked list at the
beginning.

2 Insertion at the end Adding a node into circular singly linked list at the end.
Deletion & Traversing

SN Operation Description

1 Deletion at Removing the node from circular singly linked list at the beginning.
beginning

2 Deletion at the Removing the node from circular singly linked list at the end.
end

3 Searching Compare each element of the node with the given item and return
the location at which the item is present in the list otherwise return
null.

4 Traversing Visiting each element of the list at least once in order to perform
some specific operation.

CIRCULAR DOUBLY LINKED LIST


Circular doubly linked list is a more complexed type of data structure in which a node contain
pointers to its previous node as well as the next node. Circular doubly linked list doesn't contain
NULL in any of the node. The last node of the list contains the address of the first node of the
list. The first node of the list also contain address of the last node in its previous pointer.

A circular doubly linked list is shown in the following figure.


Due to the fact that a circular doubly linked list contains three parts in its structure therefore, it
demands more space per node and more expensive basic operations. However, a circular doubly
linked list provides easy manipulation of the pointers and the searching becomes twice as
efficient.

Memory Management of Circular Doubly linked list


The following figure shows the way in which the memory is allocated for a circular doubly
linked list. The variable head contains the address of the first element of the list i.e. 1 hence the
starting node of the list contains data A is stored at address 1. Since, each node of the list is
supposed to have three parts therefore, the starting node of the list contains address of the last
node i.e. 8 and the next node i.e. 4. The last node of the list that is stored at address 8 and
containing data as 6, contains address of the first node of the list as shown in the image i.e. 1. In
circular doubly linked list, the last node is identified by the address of the first node which is
stored in the next part of the last node therefore the node which contains the address of the first
node, is actually the last node of the list.

Operations on circular doubly linked list :


There are various operations which can be performed on circular doubly linked list. The node
structure of a circular doubly linked list is similar to doubly linked list. However, the
operations on circular doubly linked list is described in the following table.
SN Operation Description

1 Insertion at Adding a node in circular doubly


beginning linked list at the beginning.

2 Insertion at end Adding a node in circular doubly


linked list at the end.

3 Deletion at Removing a node in circular


beginning doubly linked list from beginning.

4 Deletion at end Removing a node in circular


doubly linked list at the end.

Traversing and searching in circular doubly linked list is similar to that in the circular singly
linked list.

Polynomial Manipulation

Polynomial manipulations such as addition, subtraction & differentiation etc.. can


be performed using linked list

Declaration for Linked list implementation of Polynomial ADT

struct poly
{
int coeff;
int power;
struct poly *next;
}*list1,*list2,*list3;
Creation of the Polynomial

poly create(poly*head1,poly*newnode1)
{

poly *ptr;
if(head1==NULL)
{

head1=newnode1;
} return (head1);

else
{
ptr=head1;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=newnode1;

return(head1);
}

Addition of two polynomials


void add()
{

poly *ptr1, *ptr2, *newnode ;


ptr1= list1;
ptr2 = list2;
while( ptr1 != NULL && ptr2 != NULL )
{

newnode = (struct poly*)malloc( sizeof ( struct poly ));


if( ptr1 -> power = = ptr2 -> power )
{

newnode -> coeff = ptr1 -> coeff + ptr2 -> coeff;

newnode -> power = ptr1 -> power ;


newnode -> next = NULL;
list3 = create( list3, newnode );
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}

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


{

newnode -> coeff = ptr1 -> coeff;


newnode -> power = ptr1 -> power;
newnode -> next = NULL;
list3 = create( list3, newnode );
ptr1 = ptr1 -> next;

else
newnode -> coeff = ptr2 -> coeff;
{
newnode -> power = ptr2 -> power;
newnode -> next = NULL;
list3 = create( list3, newnode );

ptr2 = ptr2 -> next;


}}}
Subtraction of two polynomial
{

void sub()poly *ptr1, *ptr2, *newnode;


ptr1 = list1;
ptr2 = list2;
while( ptr1 != NULL && ptr2 != NULL )
{

newnode = (struct poly*)malloc( sizeof (struct poly)) ;


if(ptr1->power==ptr2->power)
{

newnode->coeff=(ptr1-coeff)-(ptr2->coeff);
newnode->power=ptr1->power;
newnode->next=NULL;
list3=create(list3,newnode);
ptr1=ptr1->next;

ptr2=ptr2->next;
}

else
{

if(ptr1-power>ptr2-power)
{

else
{

}
n eff=ptr1->coeff; newnode-
e >power=ptr1->power; newnode-
w >next=NULL;
n list3=create(list3,newnode);
o ptr1=ptr1->next;
d
e
-
>
c
o
newnode->coeff=-(ptr2->coeff);
newnode->power=ptr2->power;
newnode->next=NULL;
list3=create(list3,newnode);
ptr2=ptr2->next;
}

Polynomial Differentiation:
void diff()
{

poly *ptr1, *newnode;


ptr1 = list1;
while( ptr1 != NULL)
{

newnode = (struct poly*)malloc( sizeof (struct poly));


newnode->coeff=(ptr1-coeff)*(ptr1->power);
newnode->power=ptr1->power-1;

newnode->next=NULL;
list3=create(list3,newnode);
ptr1=ptr1->next;
}

Polynomial Multiplication
void mul()
{

poly *ptr1, *ptr2, *newnode ;


ptr1= list1;
ptr2 = list2;
while( ptr1 != NULL && ptr2 != NULL )
{

newnode = (struct poly*)malloc( sizeof ( struct poly ));


if( ptr1 -> power = = ptr2 -> power )
{

newnode -> coeff = ptr1 -> coeff * ptr2 -> coeff;


newnode -> power = ptr1 -> power+ptr2->power; ;
newnode -> next = NULL;
list3 = create( list3, newnode );
ptr1 = ptr1 -> next;
ptr2 = ptr2 -> next;
}}

}
Basic Operations in the Linked Lists
There are several operations which were performed on the Linked Lists
1. Traversal - To traverse throughout the linked list.
2. Insertion - Insertion of a node at any position.
3. Deletion - Deletion of a node from any position.
4. Updation - Updation of data of a node.
5. Sort - Sorting the node members
We will cover each one of these operations on linked lists in C/C++ one by one in detail. So, fasten
your seat belts.
1. Traversal Operation
Traversal is one of the basic operations on linked lists in C/C++ in which we traverse throughout the
linked list from beginning to the end or from head to tail.
Traversal can be used to print the nodes of the linked list, to reach out for a required node in a list.
Let us take the example of printing the nodes of the linked list through traversal operation.

Algorithm :
o First, initialize a Node variable, say ‘temp’ pointing to the head of the linked list.
o Iterate till the ‘temp’ will not become NULL.
o Print the node’s data.
o Increment ‘temp’ to temp’s next.

void traversal(Node* head) {


Node* temp = head;
while(temp != NULL)
{
cout<<(temp->data);
temp = temp->next;
}
}
2. Insertion Operation
Of the operations on linked lists in C/C++, insertion is used to insert a node at a particular position.
The positions can be:
o At the beginning of the linked list.

o At the end of the linked list.

o After after a particular node.

Insertion at the Beginning - In this case we need not to traverse the linked list. We will check if the
list is empty then we will make the new node as head. Otherwise, we have to connect the head of the
list with the new node by making the next pointer of the node pointing to the head and then making
the new node as head of the list.

Node* insertAtBegin(Node* head, int x)


{
// creation of a new node of linked list.
Node* newNode = new Node(x)

// checking if the linked list is empty.


if(head == NULL)
return newNode;

// insertion of the node at the beginning.


else
{
newNode->next = head;
return newNode;
}
}

Insertion at the end - In this case we have to traverse throughout the linked list until we will find
the last node and then we will insert the required node in the list.
For this, we have to consider some special cases. For eg. if the list is empty then we will just create
the new node and make it as head. In this case the new node will be the first and the last node of the
list.

Node* insertAtEnd(Node* head, int x)


{

// If the list is empty.


if( head == NULL )
{
Node* newNode = new Node(x);
head = newNode;
return head;
}
Node* temp = head;

// Traversing the list till the last node


while(temp->next != NULL)
{
temp = temp->next;
}
Node* newNode = new Node(x);
temp->next = newNode;
return head;
}

Insertion after a particular node -


In this case a reference to a particular node will be given. Then a new node will be inserted after that.
If the reference of the node is not given instead data of the node is given, then we have to traverse to
the given node matching the given data and then we will insert the new node after that node.

void insertAfterNode(Node* givenNode, int x)


{
Node* newNode = new Node(x);

newNode->next = givenNode->next;
givenNode->next = newNode;
}
3. Deletion Operation
This is one type of the operations on linked lists in C/C++, in which we just need to follow the
following steps:
o Traversing to the previous node of the node to be deleted.
o Changing the next pointer of that previous node to point to the address of the next node of the
node to be deleted.

o Freeing up the memory which is occupied by the node to be deleted.

Node deleteNode(Node* head, Node* toBeDeleted)


{

// If the node to be deleted is the head node.


if(head == toBeDeleted)
{
return head.next;
}

// temp is the traversing node.


Node* temp = head;

while( temp->next != NULL )


{

// Searching for the previous node of the node to be deleted.


if(temp->next == toBeDeleted)
{
temp->next = temp->next->next;

// Freeing up the memory of the node to be deleted.


return head;
}
temp = temp->next;
}

// If no node matches in the Linked List.


return head;
}
4. Updation Operation
This kind is one of the operations on linked lists in C/C++, in which we have to replace the data part
of the required node with the given value.
For this, we will be traversing throughout the linked list until we find a node that will be matching to
the required node to be updated.

void updateNode(Node* head, int value, int newValue)


{
// temp is the traversing node
Node* temp = head;
while(temp != NULL)
{
// If the node matches the given node to be updated.
if( temp->data == val)
{
// Updating the data part of the node with the new value.
temp->data = newVal;
return;
}
temp = temp->next;
}
}

5. Sort Operation
This operation sorts the nodes present in a linked list. You can use various sorting algorithms for
linked lists but merge sort is one of the most performant. Let's see the implementation of Merge sort
for singly linked lists in C++:-

ListNode* mergeLists(ListNode* Left, ListNode* Right){


ListNode *head = new ListNode(-1), *curr = head;

while(Left && Right){


if(Left->val < Right->val){
curr->next = Left;
Left = Left->next;
}else{
curr->next = Right;
Right = Right->next;
}
curr = curr->next;
}

curr->next = (Left)?Left:Right;

return head->next;
}
ListNode* sortList(ListNode* head) {
if(!head || !head->next) return head;

ListNode *slow = head, *fast = head->next;

while(fast && fast->next){


slow = slow->next;
fast = fast->next->next;
}

auto Right = sortList(slow->next);


slow->next = NULL;
auto Left = sortList(head);
return mergeLists(Left, Right);
}

The sortList() function accepts the Linked List head pointer and returns the head pointer after sorting
it recursively. At each step, the middle node is found, and the left and right halves are sorted
independently, and then they are merged using the mergeLists() helper function.

You might also like