Question

IntNode class I am providing the IntNode class you are required to use. Place this class...

IntNode class

I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is.

struct IntNode
{
int data;
IntNode *next;
IntNode( int data ) : data(data), next(0) {}
};

IntList class

Encapsulated (Private) Data Fields

  • head: IntNode *

  • tail: IntNode *

Public Interface (Public Member Functions)

  • IntList()

  • IntList( const IntList &list)

  • ~IntList()

  • void display() const

  • void push_front( int value )

  • void push_back( int value )

  • void pop_front()

  • void select_sort()

  • void insert_sorted( int value )

  • void remove_duplicates()

  • IntListIterator begin()

  • IntListIterator end()

  • int front() const

  • int back() const

  • int length() const;

  • int sum() const;

  • void reverseDisplay() const;

  • IntList & operator=( const IntList &list )

Constructor and Destructor

IntList() - the default constructor

Initialize an empty list.

IntList(const IntList &list) - the overloaded copy constructor

Initialize a new list with the contents of an existing list.

~IntList()

This function should deallocate all remaining dynamically allocated memory (all remaining IntNodes).

Accessors

void display() const

This function displays to a single line all of the int values stored in the list, each separated by a space. It should NOT output a newline or space at the end.

intListIterator begin()

This function returns an iterator at the beginning of the linked list. Returns an iterator pointing to head.

intListIterator end()

This function returns an iterator one element past the last element of the linked list. Returns an iterator pointing to NULL.

int front() const

This function returns the data in the head of the linked list.

int back() const

This function returns the data in the tail of the linked list.

int length() const

This function recursively determines the length of the list.

int sum() const

This function recursively determines the sum of all of the elements in the list.

void reverseDisplay() const

This function recursively displays the contents of the list in reverse order.

Mutators

void push_front( int value )

This function inserts a data value (within a new node) at the front end of the list.

void push_back( int value )

This function inserts a data value (within a new node) at the back end of the list.

void pop_front()

This function removes the value (actually removes the node that contains the value) at the front end of the list. Do nothing if the list is already empty. In other words, do not call the exit function in this function as we did with the IntVector's pop_front.

void select_sort( )

This function sorts the list into ascending order using the selection sort algorithm.

void insert_sorted( int value )

This function assumes the values in the list are in sorted (ascending) order and inserts the data into the appropriate position in the list (so that the values will still be in ascending order after insertion). DO NOT call select_sort within this function.

void remove_duplicates()

This function removes all values (actually removes the nodes that contain the value) that are duplicates of a value that already exists in the list. Always remove the later duplicate, not the first instance of the duplicate. DO NOT call select_sort within this function. This function does NOT assume the data is sorted.


IntList & operator=(const IntList &list)

This function copies over all of the nodes in an existing list to another already existing list.  

IntListIterator class

Encapsulated (Private) Data Fields

  • current: IntNode *

Public Interface (Public Member Functions)

  • IntListIterator()

  • IntListIterator( IntNode *ptr)

  • int operator*()

  • intListIterator operator++()

  • bool operator==(const intListIterator& right) const;

  • bool operator!=(const intListIterator& right) const;


Constructors

IntListIterator() - the default constructor

Initialize the iterator. Basically just need to set the pointer to NULL.

IntListIterator(intNode *ptr) - the overloaded copy constructor

Initialize the iterator with the parameter passed in. Need to set the pointer equal to whatever pointer is passed in.

Accessors

int operator*()

This function overloads the dereferencing operator*. It should return the info contained in the node.  

intListIterator operator++()

This function overloads the pre-increment operator++. It should return an iterator that is pointing to the next node.  

bool operator==(const intListIterator& right) const;

This function overloads the equality operator. Should return true if this iterator is equal to the iterator specified by right, otherwise it returns false.

bool operator!=(const intListIterator& right) const;

This function overloads the not equal to operator. Should return true if this iterator is not equal to the iterator specified by right, otherwise it returns false.

Private Helper Functions

You may define any private helper functions you deem useful, provided they do not affect the efficiency of the problem they are used to solve. Be careful making any function that must traverse the list to get to the node it will be working on. A private helper function that does this will almost always cause the function it is helping to become less efficient. You may lose points for that. For example, DO NOT make a function that returns the size of the list.

You MAY NOT define any other data fields, public or private, for this assignment.

What to Submit

Note: Your files should compile with the command g++ -std=c++11 *.cpp

In Canvas, submit the following files (case sensitive):

  • main.cpp (test harness)

  • intNode.h

  • intList.h

  • intList.cpp

  • intListIterator.h

  • intListIterator.cpp

Programming language: C++

Requirement: please tightly follow up the rules.

Homework Answers

Answer #1
/*IntList.h*/

#ifndef INT_LIST
#define INT_LIST

#include<iostream>

using namespace std;

struct IntNode {
    int data;
    IntNode *prev;
    IntNode *next;
    IntNode(int data) : data(data), prev(0), next(0) {}
};

class IntList
{
    IntNode *dummyHead, *dummyTail;

    public:
        IntList();
      ~IntList();

        void push_front(int value);
        void pop_front();
        void push_back(int value);
        void pop_back();
        bool empty() const;
        friend ostream & operator<<(ostream &out, const IntList &rhs);
        void printReverse() const;
};

#endif


/*End of IntList.h*/

/*IntList.cpp*/

#include"IntList.h"

using namespace std;

//Constructor
IntList::IntList() : dummyHead(0), dummyTail(0){}

//Function to insert one element at the front of the list
void IntList::push_front(int value)
{
    if(dummyHead == 0)//When IntList is empty
    {
        dummyHead = new IntNode(value);//Create new IntNode and pass parameters into the constructor of IntNode
        dummyTail = dummyHead;//Tail and Head both are same
    }
    else
    {
        IntNode *newNode = new IntNode(value);//Create new IntNode and pass parameters into the constructor of IntNode
        dummyHead->prev = newNode;
        newNode->next = dummyHead;
        dummyHead = newNode;//Make newNode as dummyHead
    }
}

//Function to delete one element from the front of the list
void IntList::pop_front()
{
    if(dummyHead == 0)//IntList is empty. So, nothing to delete.
        return;
    if(dummyHead == dummyTail)//If only one item in the list
    {
        delete dummyHead;//Free memory for allocated IntNode
        dummyHead = dummyTail = 0;
        return;
    }
    IntNode *temp = dummyHead;
    dummyHead = dummyHead->next;
    dummyHead->prev = 0;
    delete temp;//Free memory for allocated IntNode
}

//Function to insert one element at the back of the list
void IntList::push_back(int value)
{
    if(dummyHead == 0)//When IntList is empty
    {
        dummyHead = new IntNode(value);//Create new IntNode and pass parameters into the constructor of IntNode
        dummyTail = dummyHead;//Tail and Head both are same
    }
    else
    {
        IntNode *newNode = new IntNode(value);//Create new IntNode and pass parameters into the constructor of IntNode
        dummyTail->next = newNode;
        newNode->prev = dummyTail;
        dummyTail = newNode;//Make newNode as dummyHead
    }
}

//Function to delete one element from the back of the list
void IntList::pop_back()
{
    if(dummyHead == 0)//IntList is empty. So, nothing to delete.
        return;
    if(dummyHead == dummyTail)//If only one item in the list
    {
        delete dummyHead;//Free memory for allocated IntNode
        dummyHead = dummyTail = 0;
        return;
    }
    IntNode *temp = dummyTail;
    dummyTail = dummyTail->prev;
    dummyTail->next = 0;
    delete temp;//Free memory for allocated IntNode
}

//Function to check if list is empty or not
bool IntList::empty() const
{
    if(dummyHead == 0)//If dummyHead points to nowhere, then list is empty
        return true;
    return false;
}

//Overloaded << operator to print list content
ostream & operator<<(ostream &out, const IntList &rhs)
{
    IntNode *ptr = rhs.dummyHead;//ptr points to head of the list
    while(ptr != 0)//Till ptr doesn't point to 0
    {
        out<<ptr->data<<" ";
        ptr = ptr->next;
    }
    return out;
}

//Function to print list in reverse order
void IntList::printReverse() const
{
    IntNode *ptr = dummyTail;
    while(ptr != 0)//Till ptr doesn't become 0
    {
        cout<<ptr->data<<" ";
        ptr = ptr->prev;
    }
}

//Destructor
IntList::~IntList()
{
    while(!empty())//Till list becomes empty
        pop_back();
}

/*End of IntList.cpp*/


/*main.cpp*/

#include <iostream>
#include "IntList.h"

using namespace std;

//main function to test IntList
int main()
{
    IntList l;
    l.push_front(6);
    l.push_front(5);
    l.push_front(4);
    l.push_front(3);
    l.push_back(7);
    l.push_back(8);
    l.push_back(9);
    l.push_back(10);
    l.push_front(2);
    l.push_front(1);
    cout<<"List after pushing some data: "<<l<<endl;
    cout<<"List in reverse order: ";
    l.printReverse();
    cout<<endl;
    l.pop_back();
    l.pop_back();
    l.pop_front();
    l.pop_front();
    cout<<"List after popping some data: "<<l<<endl;
    cout<<"List in reverse order: ";
    l.printReverse();
    cout<<endl;
    return 0;
}

/*End of main.cpp*/

OUTPUT:

/*End of main.cpp*/i

if any doughts please comment me i will solve please don't give me dislike

please like my answer...THANK YOU!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that...
IntList Lab Specifications You are required to come up with a single header file (IntList.h) that declares and implements the IntNode class (just copy it exactly as it is below) as well as declares the IntList Class interface only. You are also required to come up with a separate implementation file (IntList.cpp) that implements the member functions of the IntList class. While developing your IntList class you must write your own test harness (within a file named main.cpp). Never implement...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public:...
#ifndef BAG_H #define BAG_H #include <cstdlib> // Provides size_t using namespace std; class bag { public: // TYPEDEFS and MEMBER CONSTANTS typedef int value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; // CONSTRUCTOR bag() {used = 0;} // MODIFICATION MEMBER FUNCTIONS size_type erase(const value_type& target); bool erase_one(const value_type& target); void insert(const value_type& entry); void operator +=(const bag& addend); void sort(const bag& b); //Sort the array in the bag object // CONSTANT MEMBER FUNCTIONS size_type size( ) const {...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent...
do (iii) and (iv) (i) This is a simple Point class interface file whose objects represent points in the cartesian plane #include <iostream> using namespace std; class Point { public:     Point()      // default constructor         Point(double x, double y); // another constructor         double x() const; // get function, return _x         double y() const; // get function, return _y private:         double _x, _y; }; (ii) Here is a test driver file for the Point class...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop...
STRICT DOWNVOTE IF NOT DONE FULLY, WILL REPORT ALSO IF COPY PASTED OR MODIFIED ANSWER Develop a class, using templates, to provide functionality for a set of recursive functions. The functions specified as recursive must be written recursively (not iterativly). The UML class specifications are provided below. A main will be provided. Additionally, a make file will need to be developed and submitted. ● Recursion Set Class The recursion set template class will implement the template functions. recursionSet -length: int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT