SlideShare a Scribd company logo
Programming and Data Structures
Lecture #1
Applications of Linked List
Given two polynomial numbers represented by a linked list. Write a function that
add these lists means add the coefficients who have same variable powers
Example:
Input:
1st number = 5x^2 + 4x^1 + 2x^0
2nd number = 5x^1 + 5x^0
Output: 5x^2 + 9x^1 + 7x^0
Input:
1st number = 5x^3 + 4x^2 + 2x^0
2nd number = 5x^1 + 5x^0
Output: 5x^3 + 4x^2 + 5x^1 + 7x^0
2
Adding two polynomials using Linked List
Courtesy: https://www.geeksforgeeks.org/circular-linked-list/
3
// C++ program for addition of two polynomials
// using Linked Lists
#include<bits/stdc++.h>
using namespace std;
// Node structure containing power and coefficient of variable
struct Node
{
int coeff;
int pow;
struct Node *next;
}; 4
// Function to create new node
void create_node(int x, int y, struct Node **temp)
{
struct Node *r, *z;
z = *temp;
if(z == NULL)
{
r =(struct Node*)malloc(sizeof(struct Node));
r->coeff = x;
r->pow = y;
*temp = r;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL; }
else
{
r->coeff = x;
r->pow = y;
r->next = (struct Node*)malloc(sizeof(struct Node));
r = r->next;
r->next = NULL; }
}
5
// Function Adding two polynomial numbers
void polyadd(struct Node *poly1, struct Node *poly2, struct Node *poly)
{
while(poly1->next && poly2->next)
{
// If power of 1st polynomial is greater then 2nd, then store 1st as it is and move its pointer
if(poly1->pow > poly2->pow)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
// If power of 2nd polynomial is greater then 1st, then store 2nd as it is and move its pointer
else if(poly1->pow < poly2->pow)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
6
// If power of both polynomial numbers is same then add their
coefficients
else
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff+poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}
// Dynamically create new node
poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
} 7
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->pow = poly1->pow;
poly->coeff = poly1->coeff;
poly1 = poly1->next;
}
if(poly2->next)
{
poly->pow = poly2->pow;
poly->coeff = poly2->coeff;
poly2 = poly2->next;
}
poly->next = (struct Node *)malloc(sizeof(struct Node));
poly = poly->next;
poly->next = NULL;
}
}
8
// Display Linked list
void show(struct Node *node)
{
while(node->next != NULL)
{
printf("%dx^%d", node->coeff, node->pow);
node = node->next;
if(node->next != NULL)
printf(" + ");
}
}
9
// Driver program
int main()
{ struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;
// Create first list of 5x^2 + 4x^1 + 2x^0
create_node(5,2,&poly1);
create_node(4,1,&poly1);
create_node(2,0,&poly1);
// Create second list of 5x^1 + 5x^0
create_node(5,1,&poly2);
create_node(5,0,&poly2);
printf("1st Number: ");
show(poly1);
printf("n2nd Number: ");
show(poly2);
poly = (struct Node *)malloc(sizeof(struct Node)); 10
// Function add two polynomial numbers
polyadd(poly1, poly2, poly);
// Display resultant List
printf("nAdded polynomial: ");
show(poly);
return 0;
}
Output:
1st Number: 5x^2 + 4x^1 + 2x^0
2nd Number: 5x^1 + 5x^0
Added polynomial: 5x^2 + 9x^1 + 7x^0
Time Complexity: O(m + n) where m and n are number of nodes in first and second lists
respectively. 11
A matrix is a two-dimensional data object made of m rows and n columns, thus
having total m x n values. If most of the elements of the matrix have 0 value, then
it is called a sparse matrix
Why to use Sparse Matrix instead of simple matrix ?
Storage: There are lesser non-zero elements than zeros and thus lesser memory
can be used to store only those elements.
Computing time: Computing time can be saved by logically designing a data
structure traversing only non-zero elements..
12
Sparse Matrix and its representations
using Arrays and Linked List
Example:
Why to use Sparse Matrix instead of simple matrix ?
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as
zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes
with non-zero elements, we only store non-zero elements. This means storing non-
zero elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two common
representations:
Array representation
Linked list representation 13
Example:
Why to use Sparse Matrix instead of simple matrix ?
0 0 3 0 4
0 0 5 7 0
0 0 0 0 0
0 2 6 0 0
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as
zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes
with non-zero elements, we only store non-zero elements. This means storing non-
zero elements with triples- (Row, Column, value).
Sparse Matrix Representations can be done in many ways following are two common
representations:
Array representation
Linked list representation 14
Method 1: Using Arrays
2D array is used to represent a sparse matrix in which there are three rows named
as
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index – (row,column)
15
Method 2: Using Linked Lists
In linked list, each node has four fields. These four fields are defined as:
• Row: Index of row, where non-zero element is located
• Column: Index of column, where non-zero element is located
• Value: Value of the non zero element located at index –
(row,column)
• Next node: Address of the next node
16

More Related Content

Similar to MO 2020 DS Applications of Linked List 1 AB.ppt (20)

linkedlist.pptx
linkedlist.pptx
MeghaKulkarni27
 
Chapter2
Chapter2
Nashra Akhter
 
1.Array and linklst definition
1.Array and linklst definition
balavigneshwari
 
Module 3 Dara structure notes
Module 3 Dara structure notes
Dreamers6
 
Array
Array
Vivian Chia En Chiang
 
Unit - 2.pdf
Unit - 2.pdf
AravindAnand21
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
NathanBOCAbrahamc
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
shashankbhadouria4
 
A polynomial may be represented as a linked list where each node cont.pdf
A polynomial may be represented as a linked list where each node cont.pdf
fashionfolionr
 
Applications of list
Applications of list
Elavarasi K
 
Chapter2
Chapter2
Krishna Kumar
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
Albin562191
 
BCS304 Module 3 Slide 1-22.pptx DSA notes 3rd sem
BCS304 Module 3 Slide 1-22.pptx DSA notes 3rd sem
ticonah393
 
Linked list
Linked list
Trupti Agrawal
 
Sparse Matrix and Polynomial
Sparse Matrix and Polynomial
Aroosa Rajput
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
Arrays and structures
Arrays and structures
Mohd Arif
 
Stack implementation using linked list ppt
Stack implementation using linked list ppt
JayasankarShyam
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
siva009113
 
1.Array and linklst definition
1.Array and linklst definition
balavigneshwari
 
Module 3 Dara structure notes
Module 3 Dara structure notes
Dreamers6
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Albin562191
 
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
8-25 LAB- Simple linked list Given an IntNode struct and the operating.docx
NathanBOCAbrahamc
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
shashankbhadouria4
 
A polynomial may be represented as a linked list where each node cont.pdf
A polynomial may be represented as a linked list where each node cont.pdf
fashionfolionr
 
Applications of list
Applications of list
Elavarasi K
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
Albin562191
 
BCS304 Module 3 Slide 1-22.pptx DSA notes 3rd sem
BCS304 Module 3 Slide 1-22.pptx DSA notes 3rd sem
ticonah393
 
Sparse Matrix and Polynomial
Sparse Matrix and Polynomial
Aroosa Rajput
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
KylaMaeGarcia1
 
Arrays and structures
Arrays and structures
Mohd Arif
 
Stack implementation using linked list ppt
Stack implementation using linked list ppt
JayasankarShyam
 
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
IN C CODEGiven an IntNode struct and the operating functions for a.pdf
siva009113
 

More from shashankbhadouria4 (17)

EC203DSD - Module 5 - 3.ppt
EC203DSD - Module 5 - 3.ppt
shashankbhadouria4
 
Artificial Neural Network.pptx
Artificial Neural Network.pptx
shashankbhadouria4
 
IT201 Basics of Intelligent Systems-1.pptx
IT201 Basics of Intelligent Systems-1.pptx
shashankbhadouria4
 
MO 2020 DS Doubly Linked List 1 AB.ppt
MO 2020 DS Doubly Linked List 1 AB.ppt
shashankbhadouria4
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
shashankbhadouria4
 
A New Multi-Level Inverter Topology With Reduced Switch.pptx
A New Multi-Level Inverter Topology With Reduced Switch.pptx
shashankbhadouria4
 
Birla Institute of Technology Mesra Jaipur.pptx
Birla Institute of Technology Mesra Jaipur.pptx
shashankbhadouria4
 
EE306_EXP1.pptx
EE306_EXP1.pptx
shashankbhadouria4
 
III_Data Structure_Module_1.ppt
III_Data Structure_Module_1.ppt
shashankbhadouria4
 
Chap 6 Graph.ppt
Chap 6 Graph.ppt
shashankbhadouria4
 
Chap 5 Tree.ppt
Chap 5 Tree.ppt
shashankbhadouria4
 
SUmmer Training PPT FINAL.pptx
SUmmer Training PPT FINAL.pptx
shashankbhadouria4
 
RVPN TRAINING PPT.pptx
RVPN TRAINING PPT.pptx
shashankbhadouria4
 
MODULE 1.pptx
MODULE 1.pptx
shashankbhadouria4
 
MODULE 1.pptx
MODULE 1.pptx
shashankbhadouria4
 
MO 2020 DS Stacks 1 AB.ppt
MO 2020 DS Stacks 1 AB.ppt
shashankbhadouria4
 
III_Data Structure_Module_1.pptx
III_Data Structure_Module_1.pptx
shashankbhadouria4
 
Ad

Recently uploaded (20)

Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
COMPOSITE COLUMN IN STEEL CONCRETE COMPOSITES.ppt
ravicivil
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
operationg systemsdocumentmemorymanagement
operationg systemsdocumentmemorymanagement
SNIGDHAAPPANABHOTLA
 
Présentation_gestion[1] [Autosaved].pptx
Présentation_gestion[1] [Autosaved].pptx
KHADIJAESSAKET
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
The first edition of the AIAG-VDA FMEA.pptx
The first edition of the AIAG-VDA FMEA.pptx
Mayank Mathur
 
Understanding Amplitude Modulation : A Guide
Understanding Amplitude Modulation : A Guide
CircuitDigest
 
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
Decoding Kotlin - Your Guide to Solving the Mysterious in Kotlin - Devoxx PL ...
João Esperancinha
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
Universal Human Values and professional ethics Quantum AKTU BVE401
Universal Human Values and professional ethics Quantum AKTU BVE401
Unknown
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Water demand - Types , variations and WDS
Water demand - Types , variations and WDS
dhanashree78
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Ad

MO 2020 DS Applications of Linked List 1 AB.ppt

  • 1. Programming and Data Structures Lecture #1 Applications of Linked List
  • 2. Given two polynomial numbers represented by a linked list. Write a function that add these lists means add the coefficients who have same variable powers Example: Input: 1st number = 5x^2 + 4x^1 + 2x^0 2nd number = 5x^1 + 5x^0 Output: 5x^2 + 9x^1 + 7x^0 Input: 1st number = 5x^3 + 4x^2 + 2x^0 2nd number = 5x^1 + 5x^0 Output: 5x^3 + 4x^2 + 5x^1 + 7x^0 2 Adding two polynomials using Linked List Courtesy: https://www.geeksforgeeks.org/circular-linked-list/
  • 3. 3
  • 4. // C++ program for addition of two polynomials // using Linked Lists #include<bits/stdc++.h> using namespace std; // Node structure containing power and coefficient of variable struct Node { int coeff; int pow; struct Node *next; }; 4
  • 5. // Function to create new node void create_node(int x, int y, struct Node **temp) { struct Node *r, *z; z = *temp; if(z == NULL) { r =(struct Node*)malloc(sizeof(struct Node)); r->coeff = x; r->pow = y; *temp = r; r->next = (struct Node*)malloc(sizeof(struct Node)); r = r->next; r->next = NULL; } else { r->coeff = x; r->pow = y; r->next = (struct Node*)malloc(sizeof(struct Node)); r = r->next; r->next = NULL; } } 5
  • 6. // Function Adding two polynomial numbers void polyadd(struct Node *poly1, struct Node *poly2, struct Node *poly) { while(poly1->next && poly2->next) { // If power of 1st polynomial is greater then 2nd, then store 1st as it is and move its pointer if(poly1->pow > poly2->pow) { poly->pow = poly1->pow; poly->coeff = poly1->coeff; poly1 = poly1->next; } // If power of 2nd polynomial is greater then 1st, then store 2nd as it is and move its pointer else if(poly1->pow < poly2->pow) { poly->pow = poly2->pow; poly->coeff = poly2->coeff; poly2 = poly2->next; } 6
  • 7. // If power of both polynomial numbers is same then add their coefficients else { poly->pow = poly1->pow; poly->coeff = poly1->coeff+poly2->coeff; poly1 = poly1->next; poly2 = poly2->next; } // Dynamically create new node poly->next = (struct Node *)malloc(sizeof(struct Node)); poly = poly->next; poly->next = NULL; } 7
  • 8. while(poly1->next || poly2->next) { if(poly1->next) { poly->pow = poly1->pow; poly->coeff = poly1->coeff; poly1 = poly1->next; } if(poly2->next) { poly->pow = poly2->pow; poly->coeff = poly2->coeff; poly2 = poly2->next; } poly->next = (struct Node *)malloc(sizeof(struct Node)); poly = poly->next; poly->next = NULL; } } 8
  • 9. // Display Linked list void show(struct Node *node) { while(node->next != NULL) { printf("%dx^%d", node->coeff, node->pow); node = node->next; if(node->next != NULL) printf(" + "); } } 9
  • 10. // Driver program int main() { struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL; // Create first list of 5x^2 + 4x^1 + 2x^0 create_node(5,2,&poly1); create_node(4,1,&poly1); create_node(2,0,&poly1); // Create second list of 5x^1 + 5x^0 create_node(5,1,&poly2); create_node(5,0,&poly2); printf("1st Number: "); show(poly1); printf("n2nd Number: "); show(poly2); poly = (struct Node *)malloc(sizeof(struct Node)); 10
  • 11. // Function add two polynomial numbers polyadd(poly1, poly2, poly); // Display resultant List printf("nAdded polynomial: "); show(poly); return 0; } Output: 1st Number: 5x^2 + 4x^1 + 2x^0 2nd Number: 5x^1 + 5x^0 Added polynomial: 5x^2 + 9x^1 + 7x^0 Time Complexity: O(m + n) where m and n are number of nodes in first and second lists respectively. 11
  • 12. A matrix is a two-dimensional data object made of m rows and n columns, thus having total m x n values. If most of the elements of the matrix have 0 value, then it is called a sparse matrix Why to use Sparse Matrix instead of simple matrix ? Storage: There are lesser non-zero elements than zeros and thus lesser memory can be used to store only those elements. Computing time: Computing time can be saved by logically designing a data structure traversing only non-zero elements.. 12 Sparse Matrix and its representations using Arrays and Linked List
  • 13. Example: Why to use Sparse Matrix instead of simple matrix ? 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non- zero elements with triples- (Row, Column, value). Sparse Matrix Representations can be done in many ways following are two common representations: Array representation Linked list representation 13
  • 14. Example: Why to use Sparse Matrix instead of simple matrix ? 0 0 3 0 4 0 0 5 7 0 0 0 0 0 0 0 2 6 0 0 Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the matrix are of no use in most of the cases. So, instead of storing zeroes with non-zero elements, we only store non-zero elements. This means storing non- zero elements with triples- (Row, Column, value). Sparse Matrix Representations can be done in many ways following are two common representations: Array representation Linked list representation 14
  • 15. Method 1: Using Arrays 2D array is used to represent a sparse matrix in which there are three rows named as • Row: Index of row, where non-zero element is located • Column: Index of column, where non-zero element is located • Value: Value of the non zero element located at index – (row,column) 15
  • 16. Method 2: Using Linked Lists In linked list, each node has four fields. These four fields are defined as: • Row: Index of row, where non-zero element is located • Column: Index of column, where non-zero element is located • Value: Value of the non zero element located at index – (row,column) • Next node: Address of the next node 16