Here is what I have so far. Has to be C++ #include #include #include #include us
ID: 3801591 • Letter: H
Question
Here is what I have so far. Has to be C++
#include
#include
#include
#include
using namespace std;
template
class Node{ //node class holds data value and pointer
public:
Node ();
Node (const Node& orig);
Node* getNext();
Node* getprev();
bool hasNext();
bool hasPrev();
void setNext ( Node* newNext);
void setPrev (Node* newPrev);
int getVal();
void setVal(Node* value);
private:
Node* next;
Node* prev;
int value;
};
template
Node::Node(){ // node constructor
next = NULL;
}
template
Node::Node(const Node& orig){
Node = orig.getNext()
val = orig.getVal()
}
template
bool Node::hasNext(){
if (next != NULL)
return true;
else
return false;
}
template
bool Node::hasPrev(){
if (next == NULL)
prev = orig.val;
}
template
Node* Node::getNext(){
return next;
}
template
Node* Node::getprev(){
return prev;
}
template
Node* Node::getVal(){
return val;
}
template
void Node* Node::setVal(int value){
val = value;
}
template
void Node* Node::setNext(Node* newNext) {
if (newNext = = NULL)
next = NULL;
else
next = newNext -> next;
}
template
void Node* Node::setVal(Node* newPrev){
prev = newPrev -> prev;
}
template
class DLinkedList{
public:
DLinkedList(); //Constructor
head = new Node(0, NULL);
~DLinkedList(); //Destructor
makeEmpty();
const DLinkedList& operator=(const DLinkedList&); // Assignment operator
Node* insert(const TYPE &, Node* ); //
Node* isFirst(); //returns a reference to header node
Node* next(Node* ) const; //reference to next node
Node* precedent(Node* N); //reference to previous node
Node* remove(Node* N); // removes the node to the right of N
bool isEmpty () const; // returns true if list is empty
void display (ostream & ); // write to a file the elements of the linked list
Node* Min(Node* H); // finds the min value in a list headed by H
void sort(); // sorts the list (selection sort)
};
DLinkedList::DLinkedList(){
}
template
const DLinkedList& DLinkedList::operator=(const DLinkedList& Rhs){
if (this == &Rhs)return Rhs;
makeEmpty();
if(Rhs.isEmpty()) return *this;
Node *temp;
temp=Rhs.head;
do{
insertFirst(temp->value);
temp=temp->next;
}while(temp!=NULL);
return *this;
}
template
Node* DLinkedList::insert(const TYPE & e, Node* current){
Node* temp= new Node(e, current -> next);
current -> next = temp;
return temp;
}
DLinkedList::isFirst(){
}
DLinkedList::next(){
}
DLinkedList::precedent(Node* N){
}
template
Node* DLinkedList::remove(Node* N){
Node* temp= N -> next;
N -> next = N -> next -> next;
delete temp;
return N->next;
}
template
bool DLinkedList::isEmpty()const{
return (head->next==NULL);
}
}
void DLinkedList::display(){
return DLinkedList;
}
DLinkedList::Min(Node * H){
}
void DLinkedList::sort(){
}
int main(){
DLinkedList theList; //initialize an empty list
int size; //the size of list to be sorted
int n;
char filename[80];
cout << "Enter an output filename: ";
cin >> filename;
ofstream out;
out.open(filename);
cout << "Enter the size of the list " << endl;
cin >> size;
srand(time(0));
Node *temp =theList.isFirst();
for (int i = 1; i n = rand() % 100;
temp = theList.insert(n, temp);
}
out << "The original list is:";
theList.display(); //displays the generated list
theList.sort(); //sort using recursive selection sort
out << "The sorted list is: ";
theList.display(); //display the sorted list
out.close();
return 0;
}
}
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int x,y,p,loc,temp,min,a[60];
cout<<"Enter the num of elements:"<<endl;
cin>>n;
cout<<"Enter elements"<<endl;
for(x=0;x<p;x++)
{
cin>>a[x];
}
for(x=0;x<p-1;x++)
{
min=a[x];
loc=x;
for(y=x+1;y<p;y++)
{
if(min>a[y])
{
min=a[y];
loc=y;
}
}
temp=a[x];
a[x]=a[loc];
a[loc]=temp;
}
cout<<"Sorted list is as follows"<<endl;
for(x=0;x<p;x++)
{
cout<<a[x]<<" ";
}
return 0;
}