1. Create a class Node with
a. attributes data and next pointer
b. accessors and mutators
c. constructor and destructor
d. function called display
e. search node returning true or false
f. overload the function search to include lookAhead method.
1. Create a class polynomial to create a linked list that uses class node with the following functionalities
a. Insertion. This function takes as an input the size of polynomial. E.g if the size is 6, the polynomial equation is of degree 6. Then you are required to create the appropriate list.
6x6+ 12x3+5
b. Deletion. This function deletes the list of the degree of number passed as a parameter. E.g if the number is 7, it is invalid. If it is 2, it deletes the first 2 degrees and the remaining list is 12x3+5
c. Overload the function of deletion. This function deletes the entire list.
d. Traversal
e. Print equation in the following format
6x6+0x5+0x4+ 12x3+0x2+0x1+5.
#include <iostream>
using namespace std;
class Node{
int data, deg;
Node *next;
public:
Node(){
next = NULL;
}
Node(int dat, int deg){
this->data = dat;
this->deg = deg;
next = NULL;
}
~Node(){
if(!next){
delete next;
next = NULL;
}
}
void setData(int dat){
this->data = dat;
}
int getData(){
return data;
}
int getDeg(){
return deg;
}
void setNext(Node *n){
next = n;
}
Node* getNext(){
return next;
}
void display(){
if(data < 0) cout<<" - ";
if(deg == 0){
cout<<data;
}
else{
cout<<data<<"x^"<<deg;
}
}
bool search(Node n){
return n.data == data && n.deg == deg;
}
};
class Polynomial{
Node *head;
public:
Polynomial(){
head = NULL;
}
void insert(int size){
int data;
for(int i = size; i >= 0; i--){
cout<<"Enter coefficient of x^"<<i<<endl;
cin>>data;
if(head == NULL){
head = new Node(data, i);
continue;
}
else{
Node* curr = head;
while(curr->getNext() != NULL){
curr = curr->getNext();
}
curr->setNext(new Node(data, i));
}
}
}
void remove(int par){
if(par > head->getDeg());
else{
Node *curr = head;
int i = 0;
while(curr != NULL && i < par){
head = head->getNext();
i++;
}
}
}
void println(){
Node* curr = head;
curr->display();
while(curr != NULL){
if(curr->getData() >= 0) cout<<" + ";
curr->display();
curr = curr->getNext();
}
cout<<endl;
}
~Polynomial(){
while(head != NULL){
head = head->getNext();
}
}
};
int main(){
Polynomial trial;
trial.insert(6);
trial.println();
return 0;
}
Comments
Leave a comment