0% found this document useful (0 votes)
13 views72 pages

UNIT I Material

Uploaded by

brawltalk2006
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)
13 views72 pages

UNIT I Material

Uploaded by

brawltalk2006
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/ 72

1

UNIT-I- LISTS
Syllabus
Abstract Data Types (ADTs) – List ADT – Array-based implementation – Linked list
implementation – Singly linked lists – Circularly linked lists – Doubly-linked lists –
Applications of lists – Polynomial ADT – Radix Sort – Multilists.
Why Data Structures?
• Data Structure is the orderly arrangement of data in computers to use it in a
more efficient way.
• It is the collection of data objects that allows sorting, organizing and retrieving
of data on computers.
• It is the key component of computer science and largely used in the areas of
Artificial Intelligence, Operating systems, Graphics and etc.
• Real life Example: Searching cloth (data) in a messy wardrobe.
• Program= Data Structures + Algorithms
Data Structures- Definition
Data Structure is defined as the way of organizing data not only tell how data
is stored but also explains the relationships among them.
Ex: List, Stack, Queue, Tree, Graph and etc.
Data Structures- Classification
Linear Data Structures- In Linear Data Structures, the elements in the data structures
are accessed sequentially.
Ex: List, Stack, Queue
Non-Linear Data Structures- In Non-linear Data Structures, the elements in the data
structures are not accessed sequentially.
Ex: Tree, Graph
Abstract Data Type (ADT)
• The abstract datatype is a special kind of datatype, whose behavior is defined
by set of values and set of operations.
• The ADT is made of primitive datatypes, but operation logics are hidden from
the user.
• Ex: List, Stack, Queue, Tree, Graph and etc.
List ADT
• List is the group of elements and its general form is A1, A2, A3,…….A N
• The size of the list is N.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


2

• The size of empty list is ‘0’.


• The element Ai+1 follows Ai and the element Ai-1 precedes Ai.
• The position of the element is i.
• The following operations such as insertion, deletion, find, print list can be
performed over the list.
• Implementation: Array implementation, Linked list implementation
Array Implementation of the List
• Array stores fixed number of items of same type.
• Most of the data structures make use of arrays to implement their algorithms.
• Following are the important terms to understand the concept of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index,
which is used to identify the element.
Storage of Elements in array

• Arrays are declared in ‘C’ language as follows.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


3

Operations in List:
• Printlist − print all the array elements one by one.
• Insertion − Adds an element at the given index.
• Deletion − Deletes an element at the given index.
• Search − Searches an element using the given index or by the value.
• Update − Updates an element at the given index.
Printlist Operation:
It is used to print the elements of the list one by one starting from the element
at index ‘0’ to index ‘n-1’.
Assume the array ‘a’ has ‘n’ elements. The print operation will print the
elements a[0], a[1], a[2], a[3],…..a[n-1]
void printlist(int a[], int n)
{
int i;
for (i=0; i<=n-1;i++)
{
printf(“%d”, a[i])
}
}
Search Operation:
We can perform two kinds of search.
1. Search by value and return the index of the element if it is present in the list
otherwise -1 is returned.
2. Search by position will return the element at the given position.
Search by value Operation:
The list and the element to be searched is given. If the element is present
in the list, the position (index) of the element is returned otherwise the value -1 is
returned.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


4

Input: Search(A, 33); Output: 1


Input :Search(A,26); Output:8
Input : Search(A,100); Output:-1
Assume the array is ‘a’ and its size is ‘n’
int search(int a[], int n, int x)
{
int i;
for (i=0; i<=n-1;i++)
{
if (x==a[i])
return i;
}
return -1;
}
Search by Position Operation:
For a given list and a position, search by position operation will return the
element at the position.

Input: Search(A, 1); Output: 33


Input :Search(A,8); Output:26
Input : Search(A,10); Output:-1

Assume the array is ‘a’ and its size is ‘n’


int searchpos(int a[], int n, int i)
{
if (i<=n-1)
return (a[i]);
else

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


5

return -1;
}
Insertion Operation:
Insertion operation inserts a new element to the list. For a given array with ‘n’
elements, the insertion operation inserts a new element in the specified location. If
there is an element at the specified location, then the elements in the list from the
specified location up to the end are moved backward one by one starting from the last
element. This makes the empty place at the specified location. The new element is
now inserted into the specified location. The size of the list is also increased into ‘n+1’
after the insertion of the element.
Example:
List a, n=6
Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 26 78 63 49 - - - -
Insert(18,3)

Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 26 78 63 49 - - - -
a[6]=a[5]

0 1 2 3 4 5 6 7 8 9
Element 55 35 26 78 63 - 49 - - -
a[5]=a[4]

Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 26 78 - 63 49 - - -
a[4]=a[3]

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


6

Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 26 - 78 63 49 - - -

a[3]=new
Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 26 18 78 63 49 - - -
List a, n=7
void insert(int a[],int n,int x,int pos)
Note: X-New value to be inserted
{
int i;
if (n==size(a))
{
printf(“list is full and cannot insert”);
return;
}
else
{
i=n-1;
while(i>=pos)
{
a[i+1]=a[i];
i=i-1;
}
a[pos]=x;
n=n+1;
}
}
Deletion:
Deletion operation deletes an element from the list. For a given array with ‘n’
elements, the deletion operation deletes an element from the specified location. This
makes empty position at the specified location. After deletion, the elements from the
next of deleted position up to the last position are moved forward to enforce the

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


7

continuous storage of elements. The size of the list is decreased into ‘n-1’ after the
deletion of the element.

Example:
List a, n=6
Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 26 78 63 49 - - - -
Delete(2)

Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 - 78 63 49 - - - -
a[2]=a[3]

Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 78 - 63 49 - - - -
a[3]=a[4]

Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 78 63 - 49 - - -
a[4]=a[5]

Index 0 1 2 3 4 5 6 7 8 9
Element 55 35 78 63 49 - - - -
List a, n=5
int delete(int a[],int n, int pos)
{
int i, del;
if (n==0)
{
printf(“list is empty and cannot delete”);

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


8

return -1;
}
else
{
del=a[pos];
i=pos;
while(i<=n-1)
{
a[i]=a[i+1];
i=i+1;
}
n=n-1;
return del;
}
}
Note : If the list is empty, then the deletion operation will return the value -1.
Update Operation:
• Update operation updates the value of the element at the specified
location (i).

• Update (a, 2, 100)

Assume the array is ‘a’ and its size is ‘n’


void update(int a[], int n,int i,int u)
{
a[i]=u;
}

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


9

Advantages of Array Implementation of the List


• Accessing the element from the list is easy.
Disadvantages of Array Implementation of the List
• Size of the list cannot be increased or decreased since the array size is
defined statically.
• Insertion and deletion is not easy as they involve lot of data movement
to ensure that the list is stored continuously in memory.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


10

Linked List Implementation


• To overcome the sequential access to the array, the idea is to use the Linked
list. In a Linked list, the elements are not stored in contiguous memory
locations.
• The linked list consists of series of structures (Nodes) which are not stored
continuously in memory. Each structure contains one element of the list and
address to the next structure in the list. Last Structure address will be stored as
‘NULL’.
Diagrammatic Representation:

The list contains three elements such as 5,9,and 6

Types of Linked List:


• Singly Linked List
• Doubly Linked List
• Circularly Linked List
Singly Linked List Implementation
• Singly linked list can be defined as the collection of ordered set of elements.
Each element is represented by the node.
• A node in the singly linked list consists of two parts: data part and link part. Data
part of the node stores actual information of the element and the link part of the
node stores the address of its immediate successor.
• Singly linked list can be traversed in one direction only as each node contains
only next pointer, therefore we cannot traverse the list in the reverse direction.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


11

Diagrammatic Representation:

• In the above figure, the arrow represents the links. The data part of every node
contains the value of the elements. The address part of every node contains
the address of the next node in the list. The address of the last node is made
as null pointer. We can have as many elements as we require, in the linked list.
• Head is the special node whose address part is pointing the first data node of
the list.
Defining Node datatype of the singly linked list node:
struct snode
{
int data;
struct snode *next;
};
Declaration of Node:
struct snode *head, *p;
Allocating Space for Node:
p = (struct snode *)malloc(sizeof(struct snode));
Operations on Singly Linked List:
• Insertion
• Deletion
• Find
• Find previous
• Traverse
Print Operation:
• Each node of the list is visited at least once in order to perform printing and it
prints the data part of each node present in the list.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


12

• The singly linked list (head address) is given as the input for print operation.
One pointer has to declare and it initially points the first element. After visiting
the first element, the pointer moves and points the second element and it is
moving continuously till the last node is reached.
Print Implementation:
void print(struct snode *head)
{
struct snode *p;
p=head->next;
while(p!=NULL)
{
printf(%d”,p->data);
p=p->next;
}
}
Find Operation:
• Find operation gets the singly linked list and the element to be searched as the
input. Each element of the list is matched with the given element. If the element
is found on any of the location, then location (address) of that element is
returned otherwise null is returned.

Input: find(17)=> output: 4000


Input: find(100)=>output:NULL
Find Implementation:
struct snode * find(struct snode *head, int x) x is the element
{ to be searched
struct snode *p;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


13

p=head->next;
while(p!=null)
{
if(x==p->data)
return(p);
else
p=p->next;
}
return null;
}
Find previous Operation:
• Find previous operation gets the singly linked list and the element to be
searched as the input. Each element of the list is matched with the given
element. If the element is found on any of the location, then location (address)
of the previous element is returned otherwise null is returned.

Input: findprevious(17)=> output: 3600


Input: findprevious(100)=>output:NULL
Find previous Implementation:
struct snode * findprevious(struct snode *head, int x)
x is the element
{
to be searched
struct snode *p,*pre;
pre=head;
p=head->next;
while(p!=null)
{
if(x==p->data)

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


14

return(pre);
else
{
pre=p;
p=p->next;
}
}
return null;
}
Insertion Operation:
• Insertion at the beginning- It involves inserting any element at the front of the
singly linked list.
• Insertion at the end-It involves insertion at the end of the singly linked list. It is
necessary to traverse the singly linked list to reach the last node after which the
new node will be inserted.
• Insertion after the specified node-It involves insertion after the specified node
of the linked list. It is necessary to traverse the singly linked list to reach the
specified node after which the new node will be inserted.
Insertion at the beginning Operation:
• The list and the element to be inserted is given. The new element is inserted
at the front of the list.
• Initially first element is pointed by header node.
• After the insertion of new element, the new element will point the first element
and header node will point the new element.
• The new element becomes the first element in the list.
Before Insertion:

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


15

Insert(45) at the beginning of the list


• Create a node for 45;

3100 (newnode)
• Insert the data 45 into the data part of the node

45
3100 (newnode)
• This new node should be inserted at the front of the list.
During Insertion:

Changes:
1. newnode->next=2000 (address of 10) (identified in the name of head->next)
2. head->next=3100 (address of 45) (identified in the name of newnode)
After Insertion:

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


16

Implementation:

void insertbegin(struct snode *head, int x)


{
struct snode *newnode;
newnode=(struct snode *)malloc(sizeof(struct snode));
newnode->data=x;
newnode->next=head->next;
head->next=newnode;
}
Insertion at the end Operation:
• The list and the element to be inserted is given. The new element is inserted
after the last node of the linked list. The last node is identified by traversing the
list.
• After insertion, the last node will point the new element. The new element will
point NULL address. The new element becomes the last element in the list.
Before Insertion:

• Insert(45) at the end of the list


• Create a node for 45;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


17

• Insert the data 45 into the data part of the node and the next of newnode is set
as NULL.

 This new node should be inserted at the last of the list.

During Insertion:

Changes:
1. (identified by traversing the list) (address of last node)
4300->next =3100 (address of 45) (identified in the name of newnode)
After Insertion:

Implementation:

void insertend(struct snode *head, int x)


{

struct snode *newnode,*p;


newnode=(struct snode *)malloc(sizeof(struct snode));
newnode->data=x;
newnode->next=null;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


18

p=head;
while(p->next!=null)
{
p=p->next;
}
p->next=newnode;
}
Insertion after the specified node Operation:
• The list and the element to be inserted is given. The new element is inserted
after the specified node of the linked list. It is necessary to traverse the node to
reach the specified node after which the new node will be inserted.
• Assume the specified node as ‘p’.
• After the insertion of new element, the new element will point the node which
was pointed by node ‘p’ and node ‘p’ will point the new element.
Before Insertion:

• Insert(45) after (20)


• It is necessary to identify the address of 20 and call it as ‘p’
• Create a node for 45;

• Insert the data 45 into the data part of the node

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


19

During Insertion:

Changes:
1. newnode->next=4300 (address of 30) (identified in the name of
p->next)
2. p->next=3100 (address of 45) (identified in the name of newnode)

After Insertion:

Implementation:
Assume the following

• x is the element to be inserted;


• m is the element after which new node is inserted
Prepared• By p is the node address
Dr.C.Callins holds the address
Christiyana Professorof mHead, CSE, SRM Madurai
and
20

void insertspecify(struct snode *head, int x, int m)


{
structs node *newnode,*p;
newnode=(struct snode *)malloc(sizeof(struct snode));
newnode->data=x;
p=head->next;
while(p!=null)
{
if(m==p->data)
{
newnode->next=p->next;
p->next=newnode;
return;
}
else
p=p->next;
}
}
Deletion Operation:
• Deletion at the beginning- It involves deletion of a node from the beginning of
the singly linked list.
• Deletion at the end-It involves deleting last node of the singly linked list. It is
necessary to traverse the singly linked list to reach the last node.
• Deletion of the specified node-It involves deleting the specified node of the
linked list. It is necessary to traverse the singly linked list to reach the specified
node.
• It is necessary to check whether the singly linked list is empty or not before
doing deletion
Configuration of Empty singly linked list

header head->next=NULL

head
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
21

Deletion at the beginning Operation:


• The singly linked list is given. This operation deletes the first element of the
list.
• Initially first element is pointed by header node.
• After deleting the first node, the header will point the node which was pointed
by the first element.
Before Deletion:

Delete the first element => which means delete (10)


During Deletion:

Changes:
1. p=head->next=2000 (address of 10)
(identified in the name of head->next and called as ‘p’)
2. head->next=p->next=1500 (address of 20)
After Deletion:

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


22

Implementation:
Assume the following

• p is the address stores the address of the first element

void deletebegin(struct snode *head)


{
struct snode *p;
if(head->next!=NULL)
{
p=head->next;
head->next=p->next;
free(p);
}
}
Deletion at the end Operation:
The list is given. This operation deletes the last node from the list. The last node
is identified by traversing the list. After the deletion of the last node, the address of the
previous node of last node is set as NULL.
Before Deletion:

• Delete the last element => which means delete (30)


During Deletion:

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


23

Changes:
1. (identified by traversing the list) (address of last node) 4300
2. (identified by traversing the list) (address of previous to last node) 1500
3. 1500->next=NULL(4300->next)  pre->next=p->next
After Deletion:

Implementation:
Assume the following

• p is the address stores the address of the last element


• Pre is the address stores the previous address of the last element

void deleteend(struct snode *head)


{
struct snode *pre,*p;
if(head->next!=NULL)
{
pre=head;
p=head->link;
while(p->next!=NULL)
{
pre=p;
p=p->next;
}
pre->next=NULL;
free(p);
}
}

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


24

Deletion of the specified node Operation:


• The list and the element to be deleted is given. It involves deleting the specified
node of the linked list. It is necessary to traverse the linked list to reach the
specified node.
• Assume the specified node as ‘p’. It is necessary to identify the previous
address of ‘p’ and the address of the previous is stored in ‘pre’.
• After the deletion of the node ‘p’, the next of ‘pre’ will point next of ‘p’.
Before Deletion:

• Delete(20)
• It is necessary to identify the address of 20 and call it as ‘p’ and the address of
the previous node of 20 that is address of 10 and call it as ‘pre’.
During Deletion:

Changes:
1. (identified by traversing the list) (address of node to be deleted) 1500
2. (identified by traversing the list) (address of previous of the node to be
deleted) 2000
3. 2000->next=4300  pre->next=p->next

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


25

After Deletion:

Implementation:
Assume the following

• x is the element to be deleted;


• p is the node address of the element x;
• pre is the node address of the previous node of x;

void deletespecify(struct snode *head, int x)


{
struct snode *pre,*p;
if (head->next!=NULL)
{
pre=head;
p=head->next;
while(p!=NULL)
{
if(x==p->data)
{
pre->next=p->next;
free(p);
return;
}
else
{

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


26

pre=p;
p=p->next;
}
}
}
}
Advantages of Singly Linked List
1. Insertion and deletion operations are very easy in the singly linked list.
There is no need to move the elements forward and backward after the insertion
or deletion of an element, only the address present in the next pointer needs to
be updated.
2. In the singly linked list, efficient memory utilization can be achieved since
the size of the singly linked list can be increased or decreased at run time so
there is no memory wastage and there is no need to pre-allocate the memory.
Disadvantages of Singly Linked List

1. More memory is required in the singly linked list as compared to an array.


Because in a linked list, a pointer is also required to store the address of the
next element.
2. Direct access to an element is not possible in a singly linked list. For
example, for accessing a node at position n, one has to traverse all the nodes
before it.
3. In a singly linked list reverse traversing is not possible.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


27

Doubly Linked List Implementation


• Doubly linked list can be defined as the collection of ordered set of elements.
Each element is represented by the node.
• A node in the doubly linked list consist of three parts: one data part and two link
parts. Data part of the node stores actual information of the element and the
one link part stores the address of its immediate predecessor and another link
part of the node stores the address of its immediate successor.
• Doubly linked list can be traversed in both directions as each node contains
prev and next pointers.
Diagrammatic Representation:

The list contains three elements such as 10,20 and 30

• In the above figure, the forward arrow represents the next address. The
backward arrow represents the prev address. The data part of every node
contains the value of the elements. The next address of the last node is set as
NULL. We can have as many elements as we require, in the doubly linked list.
Header is the special node (at head address) whose next address is pointing
the first node of the list. The prev address of it points NULL.
Defining Node datatype of Doubly Linked List:
struct dnode
{
int data;
struct dnode *prev;
struct dnode *next;
};
Declaration of Node:
struct dnode *head, *p;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


28

Allocating Space for Node:


p = (struct dnode *)malloc(sizeof(struct dnode));
Operations on Doubly Linked List:
• Insertion
• Deletion
• Find
• Traverse
Print Operation:
• Each node of the doubly linked list is visited at least once in order to perform
printing and it prints the data part of each node present in the list.
• The doubly linked list (head address) is given as the input for print operation.
One pointer has to be declared and it initially points the next of head address
which is nothing but the address of the first element. After visiting the first
element, the pointer moves and points the second element and it is moving
continuously till the last node is reached.
Print Implementation:
void print(struct dnode *head)
{
struct dnode *p;
p=head->next;
while(p!=NULL)
{
printf(%d”,p->data);
p=p->next;
}
}
Find Operation:
• Find operation gets the doubly linked list and the element to be searched as the
input. Each element of the list is matched with the given element. If the element
is found on any of the location then location (address) of that element is
returned otherwise NULL is returned.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


29

Input: find(20)=> output: 1000


Input: find(100)=>output:NULL
Find Implementation:
struct dnode * find(struct dnode *head, int x) x is the element to
{ be searched
struct dnode *p;
p=head->next;
while(p!=NULL)
{
if(x==p->data)
return(p);
else
p=p->next;
}
return NULL;
}
Insertion Operation:
• Insertion at the beginning- It involves inserting any element at the front of the
doubly linked list.
• Insertion at the end-It involves insertion at the end of the doubly linked list.
• Insertion after the specified node-It involves insertion after the specified node
of the doubly linked list. It is necessary to traverse the doubly linked list to reach
the specified node after which the new node will be inserted.
Insertion at the beginning Operation:
• The doubly linked list and the element to be inserted is given. The new element
is inserted at the front of the list.
• Initially first element is pointed by header node.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


30

• After the insertion of new element, the new element next address will point the
first element and its prev address will point the header node. The header node
next address is changed and it will points the new element and the prev address
of the first element is changed and points the new element. The new element
becomes the first element of the list.
Before Insertion:

Insert (45) at the beginning of the list


• Create a node for 45;

• Insert the data 45 into the data part of the node

• This new node should be inserted at the front of the list.


During Insertion:

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


31

Changes:
1. newnode->next=1500 (address of 10) (identified in the name of
head->next as p)
2. newnode->prev=head
3. p->prev=newnode (identified in the name of newnode)
4. head->next=newnode
After Insertion:

x is the element to
Implementation:
be inserted
void insertbegin(struct dnode *head, int x)
{ p is the address to hold
the address of the first
struct dnode *newnode,*p;
element
newnode=(struct dnode *)malloc(sizeof(struct dnode));
p=head->next;
newnode->data=x;
newnode->next=p;
newnode->prev=head;
p->prev=newnode;
head->next=newnode;
}
Insertion at the end Operation:
• The doubly linked list and the element to be inserted is given. The new element
is inserted at the end of the doubly linked list. The last node is identified by
traversing the list.
• The last node will point the new element after insertion. The new element will
point NULL address. The new element becomes the last element of the list.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


32

Before Insertion:

• Insert(45) at the end of the list


• Create a node for 45;

• Insert the data 45 into the data part of the node and make the next address of
this newnode as NULL

 This new node should be inserted at the end of the list.

During Insertion:

Changes:
1. (identified by traversing the list) (address of last node)
1700->next=2900 (address of 45) (identified in the name of newnode)
2. 2900->prev=1700

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


33

After Insertion:

Implementation:
x is the element to
void insertend(struct dnode *head, int x) be inserted
{
p is the address to hold
struct dnode *newnode,*p; the address of the first
newnode=(struct dnode *)malloc(sizeof(struct dnode)); element
p=head;
newnode->data=x;
newnode->next=NULL;
while(p->next!=NULL)
{
p=p->next;
}
newnode->prev=p;
p->next=newnode;
}
Insertion after the specified node Operation:
• The doubly linked list and the element to be inserted is given. The new element
is inserted after the specified node of the doubly linked list. It is necessary to
traverse the doubly linked list to reach the specified node after which the new
node will be inserted.
• Assume the specified node as ‘p’.
• After the insertion of new element, the new element will point the node which
was pointed by node ‘p’ and node ‘p’ will point the new element.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


34

Before Insertion:

• Insert (45) after the node (20)


• It is necessary to traverse the doubly linked list to identify the address of 20
and call it as ‘p’
• Create a node for 45;

• Insert the data 45 into the data part of the node

• This new node should be inserted after the node (20).


During Insertion:

Changes:
1. 45->next=30 (p->next)
2. 30->prev=45
3. p->next=newnode
4.
Prepared By 45->prev=20(p)
Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
35

After Insertion:

Implementation:
Assume the following
• x is the element to be inserted;
• m is the element after which new node is inserted
• p is the node address holds the address of m
• q is the node address holds the address of next of p.

void insertspecify(struct dnode *head, int x,int m )


{
struct dnode *newnode,*p,*q;
newnode=(struct dnode *)malloc(sizeof(struct dnode));
p=head->next;
newnode->data=x;
while(p!=NULL)
{
if(p->data==m)
{
q=p->next;
newnode->next=p->next;
q->prev=newnode;
p->next=newnode;
newnode->prev=p;
}
else
p=p->next;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


36

}
}
Deletion Operation:
• Deletion at the beginning- It involves deletion of a node from the beginning of
the doubly linked list.
• Deletion at the end-It involves deleting the last node of the doubly linked list. It
is necessary to traverse the doubly linked list to reach the last node.
• Deletion of the specified node-It involves deleting the specified node of the
doubly linked list. It is necessary to traverse the doubly linked list to reach the
specified node.
• It is necessary to check whether the doubly linked list is empty or not before
doing deletion
Configuration of Empty doubly linked list

header head->next=NULL

head

Deletion at the beginning Operation:


• The doubly linked list is given. This operation deletes the first element of the
list.
• Initially first element is pointed by the header node.
• After deleting the first node, the header will point the node which was pointed
by the first element.
Before Deletion:

Delete the first element => which means delete (10)

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


37

During Deletion:

Changes:
1. p=head->next=2000 (address of 10)
2. q=p->next(address of 20)
3. head->next=q (head->next=node 20)
4. q->prev=head (node 20->prev=head)
After Deletion:

Implementation:
Assume the following

• p is the address stores the address of the first element


• q is the address which stores the address of the second element
of the list

void deletebegin(struct dnode *head)
{
struct dnode *p,*q;
if(head->next!=NULL)
{
p=head->next;
q=p->next;
head->next=q;
q->prev=head;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


38

free(p);
}
}
Deletion at the end Operation:
The doubly linked list is given. This operation deletes the last node from the
doubly linked list. The last node is identified by traversing the list. After the deletion of
the lastnode, the address of the previous node of last node is set as NULL.
Before Deletion:

• Delete the last element => which means delete (30)


During Deletion:

Changes:
1. p=lastnode address
2. q=p->prev
3. q->next=null
After Deletion:

Implementation:
Assume the following

• p is the address used to hold the address of the last node.


• q is the address used to hold the address of the previous node of last node.
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
39

void deleteend(struct dnode *head)


{
struct dnode *p,*q;
if(head->next !=NULL)
{
p=head->link;
while(p->next!=NULL)
{
p=p->next;
}
q=p->prev;
q->next=NULL;
free(p);
}
}
Deletion of the specified node Operation:
• The doubly linked list and the element to be deleted is given. It involves deleting
the specified node of the doubly linked list. It is necessary to traverse the doubly
linked list to reach the specified node.
• Assume the specified node as ‘p’. It is necessary to address the previous node
of node ‘p’ and next node of ‘p’.
• After the deletion of the node ‘p’, the next of previous node of ‘p’ and prev of
next node of ‘p’ are changed.
Before Deletion:

• Delete(20)
• It is necessary to identify the address of 20 and call it as ‘p’

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


40

During Deletion:

Changes:
1. (identified by traversing the list) (address of node to be deleted) 1000 as
‘p’
2. pr=p->prev
3. ne=p->next
4. pr->next=ne
5. ne->prev=pr
After Deletion:

Implementation:
Assume the following

• x is the element to be deleted;


• p is the node address of the element x;
• pr is the node address of the previous node of x;
• ne is the node address of the next node of x;
void deletespecify(struct dnode *head, int x)
{
struct dnode *p,*pr,*ne;
if(head->next!=NULL)
{
p=head->next;
while(p!=NULL)

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


41

{
if(x==p->data)
{
pr=p->prev;
ne=p->next;
pr->next=ne;
ne->prev=pr;
free(p);
return;
}
else
{
p=p->next;
}
}
}
}
Advantages of Doubly Linked List
1. Reversing the doubly linked list is very easy because doubly linked list is
bidirectional.
2. Deletion of nodes is easy as compared to a Singly Linked List. A singly
linked list deletion requires a pointer to the node and previous node to be
deleted but in the doubly linked list, it only required the pointer which is to be
deleted.
3. Insertion and deletion operations are very easy in the doubly linked list.
There is no need to move the elements forward and backward after the insertion
or deletion of an element, only the address present in the next pointer or prev
pointer needs to be updated.
4. In the doubly linked list, efficient memory utilization can be achieved since
the size of the singly linked list can be increased or decreased at run time so
there is no memory wastage and there is no need to pre-allocate the memory.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


42

Disadvantages of Doubly Linked List

1. It uses extra memory when compared to the array and singly linked list.
2. Since elements in memory are stored randomly, direct access of elements is
not allowed.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


43

Circularly Linked List


 Circularly linked list is a linked list where all nodes are connected to form a
circle. There is no NULL at the end. We traverse a circularly linked list until
we reach the same node where we started. Any node can be a starting node.
We can traverse the whole list by starting from any node.
• A circularly linked list can be a single circularly linked list or double circularly
linked list.
Applications of Circularly Linked List
 Circularly linked lists are useful in applications where the process has to
repeatedly go around the list.
 Circularly linked list are mostly used in task maintenance in operating
systems.
 Circularly linked list are being used in browser surfing where a record of pages
visited in the past by the user, is maintained in the form of circularly linked
lists.
Types of Circularly Linked List:
• Single Circularly Linked List
• Double Circularly Linked List
Single Circularly Linked List Implementation
• In a single circularly linked list, the last node next address points the first node
of the list.
• The single circularly linked list has no beginning and no ending. There is no
NULL value present in the next part of any of the nodes.
Diagrammatic Representation:

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


44

Defining Node datatype:


struct csnode
{
int data;
struct csnode *next;
};
Declaration of Node:
struct csnode *head, *p;
Allocating Space for Node:
p = (struct csnode *)malloc(sizeof(struct csnode));
Operations on Single Circularly Linked List:
• Insertion
• Deletion
• Find
• Find previous
• Traverse
Print Operation:
• Each node of the list is visited at least once in order to perform printing and it
prints the data part of each node present in the list.
• The single circularly linked list (head address) is given as the input for print
operation. One pointer has to be declared and it initially points the next of head
address which is nothing but the address of the first element. After visiting the
first element, the pointer moves and points the second element and it is moving
continuously till the header node is reached.
Print Implementation:
void print(struct csnode *head)
{
struct csnode *p;
p=head->next;
while(p!=head)
{
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
45

printf(%d”,p->data);
p=p->next;
}
}
Find Operation:
• Find operation gets the single circularly linked list and the element to be
searched as the input. Each element of the list is matched with the given
element. If the element is found on any of the location, then location (address)
of that element is returned otherwise null is returned.

Input: find(20)=> output: 1500


Input: find(100)=>output:NULL
Find Implementation:
struct csnode * find(struct csnode *head, int x) x is the element to
{ be searched
struct csnode *p;
p=head->next;
while(p!=head)
{
if(x==p->data)
return(p);
else
p=p->next;
}
return NULL;
}

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


46

Find previous Operation:


• Find previous operation gets the single circularly linked list and the element to
be searched as the input. Each element of the list is matched with the given
element. If the element is found on any of the location, then location (address)
of the previous element is returned otherwise NULL is returned.

Input: findprevious(20)=> output: 2000


Input: findprevious(100)=>output:NULL
Findprevious Implementation:
struct csnode * findprevious(struct csnode *head, int x) x is the element to
{ be searched
struct csnode *p,*pre;
pre=head;
p=head->next;
while(p!=head)
{
if(x==p->data)
return(pre);
else
{
pre=p;
p=p->next;
}
}
return NULL;
}
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
47

Insertion Operation:
• The single circularly linked list (head address), the element to be inserted (value
x) and the value of the node (value m) after which the new element is inserted
are given. In order to insert the new element at the beginning of the list, the
value of ‘m’ is given as ‘-1’.
• Initially first element is pointed by header node.
• It is necessary to traverse the single circularly linked list to reach the specified
node after which the new node will be inserted.
• Assume the specified node as ‘p’. After the insertion of new element, the new
element will point the node which was pointed by node ‘p’ and node ‘p’ will point
the new element.
Before Insertion:

• Insert(45) after (20)


• It is necessary to identify the address of 20 and call it as ‘p’
• Create a node for 45;

• Insert the data 45 into the data part of the node

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


48

During Insertion:

Changes:
1. newnode->next=4300 (address of 30) (identified in the name of
p->next)
2. p->next=3100 (address of 45) (identified in the name of newnode)

After Insertion:

Implementation:
Assume the following

• x is the element to be inserted;


• m is the element after which new node is inserted
• p is the node address holds the address of m

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


49

void insertsinglecircularly(struct csnode *head, int x, int m)


{
struct csnode *newnode,*p;
newnode=(struct csnode *)malloc(sizeof(struct csnode));
newnode->data=x;
if (m==-1) // Insertion at beginning
{
newnode->next=head->next;
head->next=newnode;
}
else // Insertion anywhere
{
p=head->next;
while(p!=head)
{
if(m==p->data)
{
newnode->next=p->next;
p->next=newnode;
return;
}
else
p=p->next;
}
}
}
Deletion Operation:
• The single circularly linked list (head address), the element to be deleted (value
x) are given. It involves deleting the specified node of the linked list.
• It is necessary to traverse the single circularly linked list to reach the specified
node. Initially first element is pointed by header node.
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
50

• Assume the specified node as ‘p’. It is necessary to identify the previous


address of ‘p’ as ‘pre’.
• After the deletion of the node ‘p’, the next of ‘pre’ will point next of ‘p’.
• It is necessary to check whether the single circularly linked list is empty or not
before doing deletion
Configuration of Empty single circularly linked list

head->next=head
header

head

Before Deletion:

• Delete(20)
• It is necessary to identify the address of 20 and call it as ‘p’ and the address of
the previous node of 20 that is address of 10 and call it as ‘pre’.
During Deletion:

Changes:
1. (identified by traversing the list) (address of node to be deleted) 1500
2. (identified by traversing the list) (address of previous of the node to be
deleted) 2000

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


51

3. 2000->next=4300  (pre->next=p->next)
After Deletion:

Implementation:
Assume the following

• x is the element to be deleted;


• p is the node address of the element x;
• pre is the node address of the previous node of x;

void deletesinglecircularly(struct csnode *head, int x)


{
struct csnode *pre,*p;
if(head->next!=head)
{
pre=head;
p=head->next;
while(p!=head)
{
if(x==p->data)
{
pre->next=p->next;
free(p);
return;
}
else
{
pre=p;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


52

p=p->next;
}
}
}
}
Double Circularly Linked List Implementation
• In a double circularly linked list, the last node next address points the first node
of the list. The header node prev address points the last node.
• The double circularly linked list has no beginning and no ending. There is no
null value present in the next part and prev part of any of the nodes.
Diagrammatic Representation:

• In the above figure, the forward arrow represents the next address. The
backward arrow represents the prev address. The data part of every node
contains the value of the elements. The next address of the last node points to
header node and header node prev address points the last node. We can have
as many elements we require, in the double circular linked list.
Defining Node datatype of Double Circulary Linked List:
struct dcnode
{
int data;
struct dcnode *prev;
struct dcnode *next;
};
Declaration of Node:
struct dcnode *head, *p;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


53

Allocating Space for Node:


p = (struct dcnode *)malloc(sizeof(struct dcnode));
Operations on Doubly Linked List:
• Insertion
• Deletion
• Find
• Traverse
Print Operation:
• Each node of the double circularly linked list is visited at least once in order to
perform printing and it prints the data part of each node present in the list.
• The double circularly linked list (head address) is given as the input for print
operation. One pointer has to be declared and it initially points the next of head
address which is nothing but the address of the first element. After visiting the
first element, the pointer moves and points the second element and it is moving
continuously till the header node is reached.
Print Implementation:
void print(struct dcnode *head)
{
struct dcnode *p;
p=head->next;
while(p!=head)
{
printf(%d”,p->data);
p=p->next;
}
}

Find Operation:
• Find operation gets the double circularly linked list (head address) and the
element to be searched as the input. Each element of the list is matched with
the given element. If the element is found on any of the location, then location
(address) of that element is returned otherwise NULL is returned.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


54

Input: find(20)=> output: 1000


Input: find(100)=>output:NULL
Find Implementation:
struct dcnode * find(struct dcnode *head, int x) x is the element to
{ be searched
struct dcnode *p;
p=head->next;
while(p!=head)
{
if(x==p->data)
return(p);
else
p=p->next;
}
return NULL;
}
Insertion Operation:
• The double circularly linked list (head address), the element to be inserted
(value x) is given and the value of the node (value m) after which the new
element is inserted are given. In order to insert the new element at the
beginning of the list, the value of ‘m’ is given as ‘-1’.
• Initially first element is pointed by header node.
• It is necessary to traverse the double circularly linked list to reach the specified
node after which the new node will be inserted.
• Assume the specified node as ‘p’. After the insertion of new element, the next
of new element will point the node which was pointed by node ‘p’ and the prev

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


55

of next of ‘p’ will point the new element. The prev of new element will point node
‘p’ and next of node ‘p’ will point new element.
Before Insertion:

• Insert(45) after the node (20)


• It is necessary to traverse the double circularly linked list to identify the
address of 20 and call it as ‘p’
• Create a node for 45;

• Insert the data 45 into the data part of the node

• This new node should be inserted after the node (20).


During Insertion:

Changes:
1. 45->next=30 (p->next)
2. 30->prev=45
3. 20->next=45(newnode)
Prepared By
4. 45->prev=20(p)
Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
56

After Insertion:

Implementation:
Assume the following

• x is the element to be inserted;


• m is the element after which new node is inserted
• p is the node address holds the address of m
• q is the node address holds the address of next of p.
void insertdoublecircularly(struct dcnode *head, int x, int m )
{
struct dcnode *newnode,*p,*q;
newnode=(struct dcnode *)malloc(sizeof(struct dcnode));
newnode->data=x;
if(m==-1) // Insert in the beginning
{
q=head->next;
newnode->next=q;
q->prev=newnode;
head->next=newnode;
newnode->prev=head;
}
else // Insert anywhere
{
p=head->next;
while(p!=head)
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
57

{
if(m==p->data)
{
q=p->next;
newnode->next=q;
q->prev=newnode;
p->next=newnode;
newnode->prev=p;
}
else
p=p->next;
}
}
}
Deletion Operation:
• The double circularly linked list and the element to be deleted are given. It
involves deleting the specified node of the double circularly linked list. It is
necessary to traverse the double circularly linked list to reach the specified
node.
• Assume the specified node as ‘p’. It is necessary to address the previous node
of node ‘p’ and next node of ‘p’.
• After the deletion of the node ‘p’, the next of previous node of ‘p’ and prev of
next node of ‘p’ are changed.
• It is necessary to check whether the double circularly linked list is empty or not
before doing deletion

Configuration of empty double circularly linked list

head->next=head
header

head

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


58

Before Deletion:

• Delete(20)
• It is necessary to identify the address of 20 and call it as ‘p’ as well as to mark
the addresses of 10 and 30 as pr and ne respectively.
During Deletion:

Changes:
1. (identified by traversing the list) (address of node to be deleted) 1000 as
‘p’
2. pr=p->prev
3. ne=p->next
4. pr->next=ne
5. ne->prev=pr

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


59

After Deletion:

Implementation:
Assume the following

• x is the element to be deleted;


• p is the node address of the element x;
• pr is the node address of the previous node of x;
• ne is the node address of the next node of x;

void deletedoublecircularly(struct dcnode *head, int x)


{
struct dcnode *p,*pr,*ne;
if(head->next!=head)
{
p=head->next;
while(p!=head)
{
if(x==p->data)
{
pr=p->prev;
ne=p->next;
pr->next=ne;
ne->prev=pr;
free(p);
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
60

return;
}
else
{
p=p->next;
}
}
}
}
Advantages of circularly Linked List

 Any node can be a starting point.


 Useful for implementation of queue.
 Circular lists are useful in applications which should be executed repeatedly go
around the list.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


61

Applications of Linked List:


• Linked list is used for single variable polynomial manipulation.
• Linked list is used in Radix sort.
• Multi linked list structure is used for course registration in university.
What is Polynomial?:
• A polynomial p(x) is the expression in variable x which is in the form
(axn + bxn-1 + …. + jx+ k), where a, b, c …., k fall in the category of real numbers
and 'n' is non negative integer, which is called the degree of polynomial.
• Polynomial expression consists of two parts:
• one is the coefficient
• other is the exponent
• Example: 10x2 + 26x, here 10 and 26 are coefficients and 2, 1 is its
exponential value.
Representation of Polynomial using Linked List:
• The singly linked list is used to represent the polynomial equation. Each term of
polynomial equation is represented by a single node. The node of polynomial
linked list consists of three parts with two data parts and one address part
• Two data parts store coefficient and exponent respectively. Address part stores
the address of the next node in the linked list.
Node Structure:

Defining data type for the node of Polynomial Linked List:


struct pnode
{
int coeff;
int exp;
struct pnode *next;
};

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


62

Representation of Polynomial using Linked List:


The polynomial equation 4x7 + 12x2 +45 is represented as follows:

Manipulation in polynomial Equation


1. Polynomial Addition
2. Polynomial Multiplication
Adding two polynomials:
Input :
Assume P1 and P2 are two polynomial equations.
p1= 13x8 + 7x5 + 32x2 + 54
p2= 3x12 + 17x5 + 3x3 + 98
Output : 3x12 + 13x8 + 24x5 + 3x3 + 32x2 + 152
Adding two polynomials using polynomial Linked List-Procedure:
• Assume there are two polynomial linked list to represent two polynomial equations
with two head addresses head1 and head2 respectively. The result of polynomial
addition is stored into new polynomial linked list. The resultant polynomial linked
list head address is addressed as head3.
• we first create two pointers, p1 and p2, to represent the current nodes of two input
polynomial linked lists. Then, we generate the new polynomial nodes based on the
exponent values of these two pointers. There are three cases:
• Case 1: p1‘s power is greater than p2‘s power: In this case, we create a new
node in the resultant polynomial with p1‘s coefficient and p1‘s exponent. After that
p1 is moved into the next node.
• Case 2: p2‘s power is greater than p1‘s power: In this case, we create a new
node in the resultant polynomial with p2‘s coefficient and p2‘s exponent. After that
p2 is moved into the next node.
• Case 3: p1 and p2 have the same power: In this case, the new coefficient is the
total of p1‘s coefficient and p2‘s coefficient. If the new coefficient is not 0, we create
a new node with the same exponent and the new coefficient. After that, both p1
and p2 are moved to the next nodes.
Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai
63

• The procedure is continued until either one of the linked list is exhausted. Then
the remaining nodes of other linked list is directly appended to the resultant
polynomial linked list.
Poly1:

Poly2:

ResulatantPoly2:

Implementation:
void polyadd(struct pnode *head1,struct pnode *head2)
{
struct pnode *p1,*p2,*head3,*newnode,*p3;
head3=(struct pnode *)malloc(sizeof(struct pnode));
head3->coeff=-1;
head3->exp=-1;
head3->next=NULL;
p3=head3;
p1=head1->next;
p2=head2->next;
while(p1!=NULL && p2!=NULL)
{
if (p1->exp > p2->exp)
{

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


64

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


newnode->coeff=p1->coeff;
newnode->exp=p1->exp;
newnode->next= NULL;
p3->next=newnode;
p3=newnode;
p1=p1->next;
}
elseif(p2->exp > p1->exp)
{
newnode=(struct pnode *)malloc(sizeof(struct pnode));
newnode->coeff=p2->coeff;
newnode->exp=p2->exp;
newnode->next= NULL;
p3->next=newnode;
p3=newnode;
p2=p2->next;
}
else
{
newnode=(struct pnode *)malloc(sizeof(struct pnode));
newnode->coeff=p1->coeff + p2->coeff;
newnode->exp=p1->exp;
newnode->next= NULL;
p3->next=newnode;
p3=newnode;
p1=p1->next;
p2=p2->next;
}
}

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


65

if(p1== NULL)
{
while(p2!=NULL)
{
newnode=(struct pnode *)malloc(sizeof(struct pnode));
newnode->coeff=p2->coeff;
newnode->exp=p2->exp;
newnode->next= NULL;
p3->next=newnode;
p3=newnode;
p2=p2->next;
}
}
if(p2== NULL)
{
while(p1!=NULL)
{
newnode=(struct pnode *)malloc(sizeof(struct pnode));
newnode->coeff=p1->coeff;
newnode->exp=p1->exp;
newnode->next= NULL;
p3->next=newnode;
p3=newnode;
p1=p1->next;
}
}
}
Multiplying two polynomials:
Input: Poly1: 3x2 + 5x1 + 6,
Poly2: 6x1 + 8

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


66

On multiplying each element of 1st polynomial with elements of 2nd polynomial,


we get
18x3 + 24x2 + 30x2 + 40x1 + 36x1 + 48
On adding values with same power of x,
18x3 + 54x2 + 76x1 + 48
Output: 18x3 + 54x2 + 76x1 + 48
Multiplying two polynomials using polynomial Linked List-Procedure:
• To multiply two polynomial equations, we have to multiply the 2nd polynomial with
each term of 1st polynomial.
• Store the multiplied value in a new linked list.
• Then the coefficients of elements having the same power in resultant polynomial
are added.
Poly1:

Poly2:

Resultant Poly

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


67

Merged Poly

Polynomial Multiplication Implementation:


void polymul(struct pnode *head1,struct pnode *head2)
{
struct pnode *p1,*p2,*head3,*newnode,*p3,*p4,*head5, *p5;
head3=(struct pnode *)malloc(sizeof(struct pnode));
head3->coeff=-1;
head3->exp=-1;
head3->next=NULL;
p3=head;
head5=(struct pnode *)malloc(sizeof(struct pnode));
head5->coeff=-1;
head5->exp=-1;
head5->next=NULL;
p5=head5;
p1=head1->next;
while(p1!=NULL)
{
p2=head2->next;
while(p2!=NULL)
{
newnode=(struct pnode *)malloc(sizeof(struct pnode));
newnode->coeff=p1->coeff*p2->coeff;
newnode->exp=p1->exp+p2->exp;
newnode->next= null;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


68

p3->next=newnode;
p3=newnode;
p2=p2->next;
}
p1=p1->next;
}
//merging
p3=head3->next;
while(p3!=NULL)
{
p4=p3->next;
newnode=(struct pnode *)malloc(sizeof(struct pnode));
newnode->coeff=p3->coeff;
newnode->exp=p3->exp;
newnode->next=null;
while(p4!=NULL)
{
if(p3->exp==p4->exp)
{
newnode->coeff= newnode->coeff +p4->coeff;
p4=p4->next;
}
else
{
p4=p4->next;
}
}
p3=p3->next;
p5->next=newnode;
p5=newnode;

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


69

}
}
Radix Sort:
Radix sort is sorting algorithm for integers. In Radix sort, there is digit by digit
sorting is performed starting from the least significant digit to the most significant digit.
Algorithmic Steps:
1. Find the maximum element in the given list.
2. Find the number of digits (d) in the maximum element.
3. Create ‘d’ buckets of size 0 to 9.
4. For i= 1 to d do
i. Sort the array elements using digits at the i th place
Example:
Sort the following array elements using radix sort

The maximum element in the array is ‘736’ and the number of digits in
the maximum element is ‘3’. Hence the algorithm will work for 3 passes.
Pass 1:
In the first pass, the list is sorted on the basis of the digits at 1's place.

After the first pass, the array elements are -

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


70

Pass2:
In the Second pass, the list is sorted on the basis of the digits at 10 th place.

After the second pass, the array elements are -

Pass3:
In the third pass, the list is sorted on the basis of the digits at 100th place.

After the third pass, the array elements are completely sorted

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


71

Multi Linked List


Multi Linked List is a 2D data structure that comprises several linked lists and
each node in a multilevel linked list has a next and child pointer. All the elements
are linked using pointers. Representation: A multilevel linked list is represented by a
pointer to the first node of the linked lists.
Use cases of Multi-Linked Lists:
Some use cases of a multi-linked list are:
 Multiple orders of one set of elements
 Representation of a sparse matrix
 List of List
Sparse Matrix use case
Multi Linked Lists are used to store sparse matrices. A sparse matrix is such a matrix
that has few non-zero values. If we use a normal array to store such a matrix, it will end
up wasting lots of space.

Spare Matrix
The sparse matrix can be represented by using a linked list for every row and column.
 A node in a multi-linked list has four parts:
 The first part stores the data.
 The second stores the pointer to the next row.
 Third for the pointer to the next column and
 Fourth for storing the coordinate number of the cell in the matrix.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai


72

Representation of sparse matrix


Advantages of Multi-Linked List:
The advantages of a multi-linked list are:
 Sets of same data can be processed into multiple sequences.
 Data are not duplicated anywhere.
 Data of one kind exist only once in the list.

Prepared By Dr.C.Callins Christiyana Professor and Head, CSE, SRM Madurai

You might also like