Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a simple linked list. (code included) 2. Overload the >> operator as a

ID: 3533005 • Letter: 1

Question

1.     Create a simple linked list. (code included)

2.     Overload the >> operator as a friend function to read a list from the file. (Data in file is not sorted)

3.     Overload the << operator as a friend function to write a list to a file.

4.     Write the original output list to the output file.

5.     Write a member function to sort the list using the algorithm for the Bubble sort. When the sort finishes, the smallest value will be pointed to by head_ptr and the largest at the end of the list.

6.     After sorting the list, write it out with the << operator.

7.     You will have three output files, Bubble.h, Bubble.cpp, and the main program.

Sample Input:

            25 15 45 35 65 55

Sample Output:

            55 65 35 45 15 25   // original data as read in

                                                // adding to head of list

            15 25 35 45 55 65   //after Bubblesort

// Utilities for linked list class

// struct definition

#inclue<iostream>

#include<cstdlib>

using namespace std;

struct nodeType

{

            int info;

            nodeType*link;

};

void list_clear(nodeType*& head_ptr)

            //Library facilities used: cstlib

{ nodeType * removeptr;

            while (head_ptr != NULL)

            {

removeptr = head_ptr;

head_ptr = head_ptr->link;

delete removeptr;

}

}

void list_copy (const nodeType* source_ptr, nodeType* & head_ptr,

            nodeType* & tail_ptr)

{

            nodeType * temp; // to allocate new nodes

            head_ptr = NULL;

            tail_ptr = NULL;

// Handle the case of the empty list.

            If (source_ptr == NULL)

                        return;

//make the head node for the newly created list,

//and put data in it

            head_ptr = new nodeType;

            head_ptr->link = NULL;

            head_ptr->info = source_ptr->info;

            tail_ptr = head_ptr;

//copy the rest of the nodes one at a time, adding at the tail of the new list

           

            source_ptr = source_ptr-> link;

while (source_ptr != NULL)

{

            temp = new nodeType;

            temp->link = NULL;

            temp->info = source_ptr->info;

            tail_ptr->link = temp;

            tail_ptr = tail_ptr->link;

            source_ptr = sources_ptr->link;

} // while

}//list_copy

// Bubble sort for Program 3

#include<iostream>

#include <fstream>

#include <cstdlib>

            using namespace std;

//declare struct for node here

            class Bubble

{

            public:

                        Bubble();

//the big four, constructor, destructor, copy constructor

//and overloaded = operator

                        friend istream & operator >> (istream &infile2, Bubble & mylist);

                        friend ostream & operator <<

                                    (ostream & outfile2, const Bubble & alist);

//yields manynodes

int size() const

void Bubblesort();

            private:

                        nodeType * head_ptr;

                        int manynodes;

};

//prototypes for list clear and copy



Explanation / Answer

#include <iostream>

#include "fraction.h"

using namespace std;

int main()

{

fraction f1, f2; //f1 and f2 are objects: instance of the fraction //class

fraction f3; //f3 stores the answer of f1*f2

cout <<"Enter the first fraction: "<<endl;

f1.readFrac();

cout <<"Enter the second fraction: "<<endl;

f2.readFrac();

f1.displayFrac();

cout<<"*";

f2.displayFrac();

cout<<"=";

f3=f1.multiply(f2);

f3.displayFrac();

cout <<endl;

f1.displayFrac();

cout<<"+";

f2.displayFrac();

cout<<"=";

f3=f1.add(f2);

f3.displayFrac();

cout<<endl;

//******************

//overloads the + operator

f1.displayFrac();

cout<<"+";

f2.displayFrac();

cout<<"=";

//over loads the + operator

f3=f1+f2; //overload the + operator to allow us to add

f3.displayFrac();

cout <<endl;

//***********************

//overload the << (insertion operator)

//display f3:

cout<<f3<<endl;

//****************************

//overloads the >> extraction operator

cout<<"Enter a fraction: ";

cin>>f1;

cout<<f1<<endl;

//************************

//over loads the > operator

cout<<"Enter a fraction: ";

cin>>f2;

cout<<f2<<endl;

if(f1 > f2)

cout<<f1 <<" is greater than " << f2<<endl;

else

cout <<f1 <<" is not greater than "<<f2 <<endl;

//************************

system("PAUSE");

return 0;

}