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.
/*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!
Get Answers For Free
Most questions answered within 1 hours.