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
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
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:...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
(C++) Suppose you want to use a linked list where the items stored in the list...
(C++) Suppose you want to use a linked list where the items stored in the list are strings from the standard library string class, how would you change the node1.h header file? Header File: #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <cstdlib> // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node(const value_type& init_data=value_type( ), node* init_link=NULL) { data_field = init_data; link_field = init_link; } // MODIFICATION MEMBER FUNCTIONS node* link( )...
Circle or mark in some way the member functions that should be constant qualified. class intArray...
Circle or mark in some way the member functions that should be constant qualified. class intArray { friend ostream& operator<<(ostream& outStream, intArray& rhs); public:     intArray();     intArray(const intArray&);     ~intArray();     void Print();     int Size();     int operator[](int);     intArray operator=(intArray &);     intArray operator+(intArray); bool operator==(intArray); private:     int size;     int *data; };
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...
#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 {...
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...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) //...
C++ Class involving difference. The goal is to overload the function: void Bag::operator-=(const Bag& a_bag) // The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5. //parameter a_bag to be subtracted from this (the calling) bag //post removes all data from items_ that is also found in a_bag //Since type is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT