0% found this document useful (0 votes)
6 views

Assignment 2

The document discusses implementing linked lists and sparse matrices in C++. It provides code to create linked lists with insert and delete functions. It also provides code for a sparse polynomial class that implements polynomials as linked lists with addition and multiplication functions. The code also shows how to implement a sparse matrix using an array of linked lists.
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)
6 views

Assignment 2

The document discusses implementing linked lists and sparse matrices in C++. It provides code to create linked lists with insert and delete functions. It also provides code for a sparse polynomial class that implements polynomials as linked lists with addition and multiplication functions. The code also shows how to implement a sparse matrix using an array of linked lists.
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/ 14

ASSIGNMENT 2:

PROBLEM 1 : LINKED LIST IMPLEMENTATION

CODE:

#include<iostream>

using namespace std;

class Node

public:

int data;

Node* next;

};

class List

private:

Node* head;

public:

void Insert(int data,int pos);

void Delete(int position);

void Create(int *A,int size);

void Display();

};

void List::Insert(int data,int pos)

Node* t= new Node;

t->data=data;

if(pos==0)

t->next=head;

head=t;

else
{

Node* curr=head;

for(int i=0;i<pos-1&&curr;i++)

curr=curr->next;

if(!curr)

cout<<"Invalid position!!\n";

delete t;

return;

t->next=curr->next;

curr->next=t;

curr=t;

void List::Delete(int position)

if (!head)

cout << "List is empty\n";

return;

if (position == 0)

Node* temp = head;

head = head->next;

delete temp;

else {

Node* curr = head;


for (int i = 0; curr && i < position - 1; ++i) {

curr = curr->next;

if (!curr || !curr->next) {

cout << "Invalid position\n";

return;

Node* toDelete = curr->next;

curr->next = toDelete->next;

delete toDelete;

void List::Create(int *A, int size)

head = nullptr;

Node* curr;

for (int i = 0; i < size; ++i) {

Node* newNode = new Node;

newNode->data=A[i];

if (!head)

head = newNode;

curr=head;

else

curr->next = newNode;

curr = newNode;

}
void List::Display()

Node* curr=head;

while(curr)

cout<<curr->data<<" ";

curr=curr->next;

cout<<endl;

int main() {

List myList;

int arr[] = {1, 2, 3, 4, 5};

int size = sizeof(arr) / sizeof(arr[0]);

myList.Create(arr, size);

cout << "Initial linked list: ";

myList.Display();

myList.Insert(10, 4);

cout << "After insertion: ";

myList.Display();

myList.Delete(4);

std::cout << "After deletion: ";

myList.Display();

return 0;

} // OUTPUT:
PROBLEM 2: POLYNOMIAL IMPLEMENTATION AS LINKED LIST

CODE:

#include<iostream>

using namespace std;

class Node

public:

int coefficient;

int exponent;

Node* next;

Node(int coef,int exp)

this->exponent=exp;

this->coefficient=coef;

this->next=NULL;

};

class SparsePolynomial

private:

Node* head;

public:

SparsePolynomial()

head=NULL;

void Insert_term(int coefficient,int exponent)

Node* newNode=new Node(coefficient,exponent);

if (!head || exponent>head->exponent)

{
newNode->next=head;

head=newNode;

else

Node* curr=head;

Node* prev=NULL;

while(curr&&(exponent<(curr->exponent)))

prev=curr;

curr=curr->next; // checking for the correct position to link/insert

newNode->next=curr; // inserting the data to list

prev->next=newNode;

Node* findterm(int exponent)const // finding the existing exponent

Node* curr=head; //if yes for combining into single term

while(curr)

if(curr->exponent==exponent)

return curr;

curr=curr->next;

return NULL;

void display()

{
Node* curr=head;

while(curr)

{ if(curr->exponent!=0)

cout<<curr->coefficient<<"x^"<<curr->exponent;

else

cout<<curr->coefficient;

curr=curr->next;

if(curr)

if(curr->coefficient>0)

cout<<" + ";

else

cout<<" ";

cout<<endl;

SparsePolynomial add(const SparsePolynomial& other)const

SparsePolynomial result;

Node* term1=head;

Node* term2=other.head;

while(term1&&term2)

if(term1->exponent>term2->exponent)

result.Insert_term(term1->coefficient,term1->exponent);

term1=term1->next;

else if (term1->exponent<term2->exponent)

{
result.Insert_term(term2->coefficient,term2->exponent);

term2=term2->next;

else

int sum=term1->coefficient+term2->coefficient;

result.Insert_term(sum,term1->exponent);

term1=term1->next;

term2=term2->next;

while(term1)

result.Insert_term(term1->coefficient,term1->exponent);

term1=term1->next;

while(term2)

result.Insert_term(term2->coefficient,term2->exponent);

term2=term2->next;

return result;

SparsePolynomial multiply(const SparsePolynomial& other) const {

SparsePolynomial result;

Node* term1 = head;

while (term1) {

Node* term2 = other.head;

while (term2) {
int product_exponent = term1->exponent + term2->exponent;

int product_coefficient = term1->coefficient * term2->coefficient;

// Update existing term or insert a new term

Node* existingTerm = result.findterm(product_exponent); // if exponent already exists

if (existingTerm) { // then combine it to single term of same exponent

existingTerm->coefficient += product_coefficient;

} else {

result.Insert_term(product_coefficient, product_exponent);

term2 = term2->next;

term1 = term1->next;

return result;

};

int main()

SparsePolynomial poly1,poly2;

poly1.Insert_term(9,5);

poly1.Insert_term(7,3);

poly1.Insert_term(8,1);

cout<<"Polynomial 1 : ";

poly1.display();

poly2.Insert_term(7,5);

poly2.Insert_term(9,4);

poly2.Insert_term(6,3);
poly2.Insert_term(3,2);

poly2.Insert_term(14,0);

cout<<"Polynomial 2 : ";

poly2.display();

SparsePolynomial addresult=poly1.add(poly2);

SparsePolynomial multiresult=poly1.multiply(poly2);

cout<<" Addition : ";

addresult.display();

cout<<"Multiplication : ";

multiresult.display();

return 0;

OUTPUT:

PROBLEM 3: SPARSE MATRIX AS ARRAY IMPLEMENTATION

CODE:

#include <iostream>

using namespace std;

// Node class to represent a non-zero element in the sparse matrix

class Node {

public:

int row, col, value;

Node* next;

Node(int r, int c, int v) : row(r), col(c), value(v), next(NULL) {}


};

class Sparse {

private:

int rows, cols;

Node** matrix; // Array of Node pointers

public:

// Constructor

Sparse(int r, int c) : rows(r), cols(c), matrix(new Node*[r]) {

for (int i = 0; i < r; ++i) {

matrix[i] = NULL; // Initialize each row with NULL

// Destructor to free allocated memory

~Sparse() {

for (int i = 0; i < rows; ++i) {

Node* current = matrix[i];

Node* nextNode;

// Free memory for each node in the row

while (current != NULL) {

nextNode = current->next;

delete current;

current = nextNode;

delete[] matrix; // Free memory for the array of Node pointers

}
// Function to insert a non-zero element into the sparse matrix

void insert(int row, int col, int value) {

Node* newNode = new Node(row, col, value);

newNode->next = matrix[row];

matrix[row] = newNode;

// Function to transpose the sparse matrix

Sparse transpose() const {

Sparse result(cols, rows);

for (int i = 0; i < rows; ++i) {

Node* current = matrix[i];

while (current != NULL) {

result.insert(current->col, current->row, current->value);

current = current->next;

return result;

// Function to display the sparse matrix in matrix form

void displayMatrixForm() const {

for (int i = 0; i < rows; ++i) {

Node* current = matrix[i];

for (int j = 0; j < cols; ++j) {

if (current != NULL && current->col == j) {


cout << current->value << " ";

current = current->next;

} else {

cout << "0 ";

cout << endl;

};

int main()

Sparse matrix(3, 4);

matrix.insert(0, 1, 5);

matrix.insert(1, 2, 8);

matrix.insert(2, 0, 9);

Sparse transposedMatrix = matrix.transpose();

cout << "\nMatrix Form of Original Sparse Matrix:" << endl;

matrix.displayMatrixForm();

cout << "\nMatrix Form of Transposed Sparse Matrix:" << endl;

transposedMatrix.displayMatrixForm();

return 0;

OUTPUT:

You might also like