You are on page 1of 7

DATA STRUCTURES

&
ALGORITHMS
CSL-221

LAB JOURNALS

Name: Obaid Awan

Enrollment No: 01-131182-043

Class: BSE-3A

Lab Instructor: Sir Aleem Ahmad

DEPARTMENT OF SOFTWARE ENGINEERING


BAHRIA UNIVERSITY
ISLAMABAD CAMPUS

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.

Language Used: C++

Tools Used:
 Microsoft Visual Studio 2013

Submission Date: December 18th, 2019

Evaluation: Signature of Lab Engineer:

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;

cout << "\n\t Enter the First Polynomial :- \n";


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 (polynomial1 == NULL)
polynomial1 = newl;
else
end->next = newl;

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;
}

polynomial *p1 = polynomial1, *p2 = polynomial2;


end = NULL;
while (p1 != NULL && p2 != NULL) {
if (p1->power == p2->power) {
newl = new polynomial;
newl->power = p--;
newl->coefficient = p1->coefficient + p2->coefficient;
newl->next = NULL;
if (polynomial3 == NULL)
polynomial3 = newl;
else
end->next = newl;
end = newl;
}
p1 = p1->next;
p2 = p2->next;
}
}

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 First Polynomial :- \n";


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 (polynomial1 == NULL)
polynomial1 = newl;
else
end->next = newl;
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;
}

polynomial *p1 = polynomial1, *p2 = polynomial2;


end = NULL;
while (p1 != NULL && p2 != NULL) {
if (p1->power == p2->power) {
newl = new polynomial;
newl->power = p--;
newl->coefficient = p1->coefficient * p2->coefficient;
newl->next = NULL;
if (polynomial3 == NULL)
polynomial3 = newl;
else
end->next = newl;
end = newl;
}
p1 = p1->next;

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

You might also like