15,312,009 members
See more:
Problem:

Define a class called RegularPolygon to represent a regular polygon of varying number of sides. Now, in your class,

Declare private fields (member data) to represent the following:

. An integer to represent no of sides

. Another integer to represent the length of the side

Define following public methods (member functions) to:

.set the default value of a polygon object using Constructor

.get values of a polygon (for the client object)

.display perimeter, area & name of the polygon (for the client object)

---------------------------------------------------------------------------------------------------------------------------------------------------------------

Now, define another class to represent a DOUBLY LINKED LIST NODE. In your class,

Declare following fields:

.A RegularPolygon object

.Two self-referential pointers as link to previous and next nodes

Define following methods (NOTE: keep in mind that a node contains a RegularPolygon object):

.Method to insert a node at the beginning of the list

.Method to insert a node at the end of the list

.Method to insert a node as the n-th node of the list (n is user input)

.Method to delete a node from the beginning of the list

.Method to delete a node from the end of the list

.Method to delete the n-th node from the list (n is user input)

.Method to return total areas of ALL of the polygons (from the nodes)

.Method to search polygons having specific area a (a is user input) and display them (if found)

.Method to search polygons having specific perimeter p (p is user input) and display them (if found)

.Method to return average area of the polygons (from the nodes)

.Method to return average perimeter of the polygons (from the nodes)

.Method to SORT the linked list nodes based on areas. If areas are same, then by no of sides

.Method to display the polygons (from the nodes) in forward sequence from the list

.Method to display the polygons (from the nodes) in backward sequence from the list

I stucked on " Method to SORT the linked list nodes based on areas. If areas are same, then by no of sides"

What will be code for this..

here is my code..`

What I have tried:

```#include<iostream>
#include <math.h>
using namespace std;

class RegularPolygon
{

float s,n;

public:
void setRegularPolygonData(){
cout<<"Input the number of sides of the polygon (maximum 12): ";
cin>>n;
cout<<"Input the length of each side of the polygon: ";
cin>> s;

}
void display(){
if(n==3){
cout<<"This is a Triangle"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==4){
cout<<"This is a Square"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==5){
cout<<"This is a Pentagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==6){
cout<<"This is a Hexagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==7){
cout<<"This is a Heptagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==8){
cout<<"This is a Octagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==9){
cout<<"This is a Nonagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==10){
cout<<"This is a Decagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==11){
cout<<"This is a Undecagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
else if(n==12){
cout<<"This is a Dodecagon"<<"\n";
cout<<"The area of the ploygon is: "<<(n * (s * s)) / (4.0 * tan((M_PI / n)))<<"\n";
cout<<"The perimeter of the ploygon is: "<<n*s<<"\n";
}
}
};

class DoublyLLNode{
RegularPolygon c;
DoublyLLNode *prev, *next;
public:
RegularPolygon & RegularPolygonObject(){
return c;
}
DoublyLLNode*& getNext(){
return next;
}
DoublyLLNode*& getPrev(){
return prev;
}
};

if(h==NULL){
h = new DoublyLLNode;
t = h;
h->RegularPolygonObject().setRegularPolygonData();
h->getNext()=NULL;
h->getPrev()=NULL;
}
else {
t->getNext() = new DoublyLLNode;

t->getNext()->getPrev() = t;
t = t->getNext();
t->RegularPolygonObject().setRegularPolygonData();
t->getNext()= NULL;
}
}

if(h==NULL){
h = new DoublyLLNode;
t = h;
h->RegularPolygonObject().setRegularPolygonData();
h->getNext()=NULL;
h->getPrev()=NULL;
}
else {
DoublyLLNode *temp = h;
h = new DoublyLLNode;
h->getNext() = temp;
h->getPrev() = NULL;
temp->getPrev() = h;
h->RegularPolygonObject().setRegularPolygonData();
}
}

void addNodeAtNthPos(DoublyLLNode*& h, DoublyLLNode*& t, int pos){
int noOfNodes=0;
DoublyLLNode *curr=h;
while(curr!=NULL){
noOfNodes++;
curr = curr->getNext();
}

if(pos<0 || noOfNodes < pos-1){
cout<<"Invalid position!"<<endl;
return;
}

if(pos==1)
else if(noOfNodes == pos-1)
else{
int count=1; curr=h;

while(count != pos-1){
curr=curr->getNext();
count++;
}

curr->getNext()->getPrev() = new DoublyLLNode;
curr->getNext()->getPrev()->getPrev()=curr;
curr->getNext()->getPrev()->getNext()=curr->getNext();
curr->getNext() = curr->getNext()->getPrev();

curr->getNext()->RegularPolygonObject().setRegularPolygonData();

}
}

void deleteFirstNode(DoublyLLNode*& h, DoublyLLNode*& t){
if(h==NULL){
cout<<"The list is empty! Nothing to delete."<<endl;
return;
}
if(h==t){
delete h;
h = t = NULL;
}
else {
h = h->getNext();
delete h->getPrev();
h->getPrev() = NULL;
}
}

void deleteLastNode(DoublyLLNode*& h, DoublyLLNode*& t){
if(h==NULL){
cout<<"The list is Empty! Nothing to display..."<<endl;
}
if (h==t){
delete h;
h = t = NULL;
}
else if{
DoublyLLNode *temp = t->getPrev();
delete t;
t=temp;
t->getNext()= NULL;
}
}

void displayInForwardSequence(DoublyLLNode *current){
if(current==NULL)
cout<<"The list is empty! Nothing to display."<<endl;
else{
while(current != NULL)
{
current->RegularPolygonObject().display();
current = current->getNext();
}
}
}

int main(){
int ch, position;
do{
cout<<"Enter [1] to insert a Node at the end."<<endl;
cout<<"Enter [2] to insert a Node at the beginning."<<endl;
cout<<"Enter [3] to insert a Node at n-th position."<<endl;

cout<<"Enter [4] to delete first Node."<<endl;
cout<<"Enter [5] to delete last Node."<<endl;
//cout<<"Enter [6] to delete n-th Node."<<endl;

cout<<"Enter [7]-> to display nodes in forward sequence"<<endl;
//cout<<"Enter [8]-> to display nodes in backward sequence"<<endl;
cout<<"Enter [9]-> to Exit: ";
cin>>ch;
switch(ch)
{
case 3: cout<<"Enter position of new Node: ";
cin>>position;
case 8: //displayInBackwardSequence(tail);
break;
}
}while(ch != 9);
return 0;
}```
Posted
Updated 16-Feb-18 20:06pm
PIEBALDconsult 16-Feb-18 19:11pm

Modularity modularily modularity... separation of concerns.

## Solution 1

Quote:
Method to SORT the linked list nodes based on areas. If areas are same, then by no of sides

Start by defining a comparison function as part of `RegularPolygon` which accepts instance, and returns an integer.
If the current instance is "larger" than Instance X, return 1.
If the current instance is "smaller" than Instance X, return -1.
Otherwise, return 0.
Inside the function, compare the areas, and if they are not the same, return the appropriate value. If they are the same, compare the the number of sides and return that value.