Professional Documents
Culture Documents
DSA Fit Fat 8
DSA Fit Fat 8
&
ALGORITHMS
CSL-221
LAB JOURNALS
Class: BSE-3A
1
DSA
LAB JOURNAL # 8
Title
LINKED LIST IMPLEMENTATION
Introduction:
A linked list consists of items called “Nodes” which contain two parts. The first part stores the actual
data and the second part has a pointer that points to the next node. This structure is usually called
“Singly linked list”. A doubly-linked list is a linked data structure that consists of a set of
sequentially linked records called nodes. Each node contains two fields, called links, that are
references to the previous and to the next node in the sequence of nodes. This is a doubly
linked list program in C++.
Objectives:
To make a program that performs the addition of two polynomials using link list.
To make a program that performs the multiplication of the two polynomials using
link list.
Tools Used:
Microsoft Visual Studio 2013
2
LAB TASKS
PROBLEM #1:
Write a program that performs the addition of two polynomials that are inserted in a link list
by the user in a sorted order and output the sorted result after addition.
PROGRAM:
#include <iostream>
#include <iomanip>
#include<conio.h>
using namespace std;
struct polynomial {
int coefficient;
int power;
polynomial* next;
};
class add {
polynomial *polynomial1;
polynomial *polynomial2;
polynomial *polynomial3;
public:
add()
{
polynomial1 = NULL;
polynomial2 = NULL;
polynomial3 = NULL;
}
void addpolynomials();
void display();
};
void add::addpolynomials()
{
int i, p;
polynomial *newl = NULL;
polynomial *end = NULL;
cout << "Enter the highest power for x for the polynomials : ";
cin >> p;
cout << endl;
3
end = newl;
}
cout << "\n\t Enter the Second Polynomial : -\n"; end = NULL;
for (i = p; i >= 0; i--)
{
newl = new polynomial;
newl->power = p;
cout << "Enter Co-efficient for degree" << i << ":: "; cin >> newl-
>coefficient;
newl->next = NULL;
if (polynomial2 == NULL)
polynomial2 = newl;
else
end->next = newl;
end = newl;
}
void add::display()
{
polynomial* t = polynomial3;
cout << "\n\n The Polynomial formed after addition of two polynomials is :-
\n\t\t ";
while (t != NULL)
{
cout.setf(ios::showpos);
cout << t->coefficient;
cout.unsetf(ios::showpos);
cout << "X" << t->power << "\t";
t = t->next;
}
}
int main()
{
cout << "\t NAME : Obaid Awan " << endl;
cout << "\t ENROLLMENT : 01-131182-043" << endl;
cout << endl;
add obj;
obj.addpolynomials();
obj.display();
cout << endl << endl ;
system("pause");
4
}
OUTPUT
The output of the above program is shown below:
PROBLEM #2:
Write a program that performs the multiplication of two polynomials that are inserted in a link
list by the user in a sorted order and output the sorted result after multiplication.
PROGRAM:
#include <iostream>
#include <iomanip>
using namespace std;
struct polynomial {
int coefficient;
int power;
polynomial* next;
};
class add {
polynomial *polynomial1;
polynomial *polynomial2;
polynomial *polynomial3;
public:
add()
{
polynomial1 = NULL;
5
polynomial2 = NULL;
polynomial3 = NULL;
}
void addpolynomials();
void display();
};
void add::addpolynomials()
{
int i, p;
polynomial *newl = NULL;
polynomial *end = NULL;
cout << "Enter the highest power for x for the polynomials : ";
cin >> p;
cout << endl;
cout << "\n\t Enter the Second Polynomial : -\n"; end = NULL;
for (i = p; i >= 0; i--)
{
newl = new polynomial;
newl->power = p;
cout << "Enter Co-efficient for degree" << i << ":: "; cin >> newl-
>coefficient;
newl->next = NULL;
if (polynomial2 == NULL)
polynomial2 = newl;
else
end->next = newl;
end = newl;
}
6
p2 = p2->next;
}
}
void add::display()
{
polynomial* t = polynomial3;
cout << "\n\n The Polynomial formed after multiplication of two polynomials is
:- \n\t\t ";
while (t != NULL)
{
cout.setf(ios::showpos);
cout << t->coefficient;
cout.unsetf(ios::showpos);
cout << "X" << t->power << "\t";
t = t->next;
}
}
int main()
{
cout << "\t MULTIPLICATION OF TWO POLYNOMIALS USING LINKLIST " << endl;
cout << endl;
cout << "\t NAME : Obaid Awan " << endl;
cout << "\t ENROLLMENT : 01-131182-043" << endl;
cout << endl;
add obj;
obj.addpolynomials();
obj.display();
cout << endl << endl;
system("pause");
}
Result
The output of the above program is shown below:
Conclusion
We are able to write the 2 programs given of which we used the link list in them and
performed addition and multiplication of the two polynomials entered by the user and output .
______________________________