The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 6 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Design your own linked list class to hold a series of integers. The class should have member functions for appending, inserting, and deleting nodes. Don't forget to add a destructor that destroys the list. Verify that all of your class functionality works by calling it in a main()driver program.
Â
List Size
This accessor function returns a count of the number of nodes currently in the list.
Â
List Print
Add a print member function. The function should display all the values in the linked list. Test the class by starting with an empty list, adding some elements, and then printing the resulting list out.
Â
List Copy Constructor
Add a copy constructor. Test your class by making a list, making a copy of the list, and then displaying the values in the copy.
Â
Member Insertion by Position
Add a member function for inserting a new item at a specified position. A position of 0 means that the value will become the first item on the list, a position of 1 means that the value will become the second item on the list, and so on. A position equal to or greater than the length of the list means that the value is placed at the end of the list. What if pos < 0?
Â
Member Append
Add a member function to append a new item to the end of the list.
Â
Member Removal by Position
Add a member function for deleting a node at a specified position. A value of 0 for the position means that the first node in the list (the current head) is deleted. The function does nothing if the specified position is greater than or equal to the length of the list. What if pos < 0 or pos >= size?
Â
List Search
Include a member function named search. The function should search the list for a specified value. If the value is found, it should return a number indicating its position in the list. (The first node is node 1, the second node is node 2, and so forth.) If the value is not found, the function should return 0.
Â
List reverse
This accessor function reverses all elements in the list.
Â
Overloaded [] Operator
Add an overloaded [] operator function. This will give the linked list the ability to access nodes using a subscript, like an array. The subscript 0 will reference the first node in the list, the subscript 1 will reference the second node in the list, and so forth. The subscript of the last node will be the number of nodes minus 1. If an invalid subscript is used, the function should throw an exception.
Â
pop and push Member Functions
The STL list container has member functions named pop_back, pop_front, push_back, and push_front, as described in Table 17-1. Modify the linked list class by adding your own versions of these member functions.
Â
pop_back list.pop_back();
removes the last element of the list. what if empty?
Â
pop_front list.pop_front();
removes the first element of the list. what if empty?
Â
push_back list.push_back(x);
inserts an element with value x at the end of the list.
Â
push_front list.push_front(x);
inserts an element with value x at the beginning of the list.
You must use this interface for the IntList class
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
Â
using namespace std;
Â
Â
class IntList
{
private:
  struct ListNode
  {
     int value;
     struct ListNode *next;
  };
Â
  int size; // current size of the list
Â
  ListNode *head;  // List head pointer
Â
 Â
Â
public:
  // Constructor
  IntList()
     { head = nullptr; size = 0; }
Â
  // Copy constructor
  IntList(const IntList &);
Â
  // Destructor
  virtual ~IntList();
Â
  // List operations
  void appendNode(int val);
  void insertNode(int pos, int val);
  void deleteNode(int pos);
  int searchNode(int val);
  int getSize() const;
  void print() const;
  void reverse();
  int &operator[](const int &);
  int pop_back();
  int pop_front();
  void push_back(int);
  void push_front(int);
};
Â
Â
Â
Â
IntList::~IntList(){
   ListNode *nodePtr; // To traverse the list
   ListNode *nextNode; // To point to the next node
Â
   cout << "Destroying list" << endl;
   // Position nodePtr at the head of the list.
   nodePtr = head;
Â
   // While nodePtr is not at the end of the list...
   while (nodePtr != nullptr){
      // Save a pointer to the next node.
      nextNode = nodePtr->next;
Â
      // Delete the current node.
      delete nodePtr;
Â
      // Position nodePtr at the next node.
      nodePtr = nextNode;
   }
   head = nullptr;
}
Â
// define the remainder of your member functions
Â
Â
Â
int main() {
   IntList intList1;
   IntList emptyIntList;
Â
   int hold, num;
   const int initialSize = 10;
Â
Â
   cout<< "n                  Integer List Node Testn";
   cout<< "============================================================nnn";
Â
   cout<< "Testing print() function with empty list..." << endl;
   cout<< "Printing the list with size: " << intList1.getSize() << "nt";
   intList1.print();
Â
   try
   {
       cout << "nTesting deleteNode() with an empty list at position 0...n";
       intList1.deleteNode(0);
   }
   catch(...)
   {
       cout << "tException: " << endl;
   }
Â
   cout << "nTesting appendNode() with " << initialSize << " values...n";
   for (int i=1; i<= initialSize; i++)
       intList1.appendNode(i);
   cout<< "Printing the list with size: " << intList1.getSize() << "nt";
   intList1.print();
   cout << endl;
Â
   cout << "Testing insertNode() at position 0...n";
   intList1.insertNode(0, rand()%100);
   cout << "Printing the list with size: " << intList1.getSize() << "nt";
   intList1.print();
   num = rand()%intList1.getSize();
   cout << "nTesting insertNode() at in bounds position ";
   cout << num << "...n";
   intList1.insertNode(num, rand()%100);
   cout << "Printing the list with size: " << intList1.getSize() << "nt";
   intList1.print();
Â
   num = rand()%100+20;
   cout << "nTesting insertNode() at out of bounds position ";
   cout << num << "...n";
   intList1.insertNode(num, rand()%100);
   cout << "Printing the list with size: " << intList1.getSize() << "nt";
   intList1.print();
Â
   try
   {
       cout << "nTesting insertNode() at negative positionn";
       intList1.insertNode(-5, 78);
       cout << "Printing the list with size: ";
       cout << intList1.getSize() << endl << "t";
       intList1.print();
   }
   catch (...)
   {
       cout << "tException: "  << endl;
   }
Â
   cout << "Testing Copy Constructor using the following:"
           " IntList intList2 = intList1;n";
   IntList intList2 = intList1;
Â
   cout << "nTesting reverse()..." << endl;
   intList1.reverse();
   cout << "Printing the reversed list with size: ";
   cout << intList1.getSize() << endl << "t";
   intList1.print();
   cout << "nTesting deleteNode() at position 0...n";
   intList1.deleteNode(0);
   cout << "Printing the list with size: ";
   cout << intList1.getSize() << "nt";
   intList1.print();
Â
   num = rand()%intList1.getSize();
   cout << "nTesting deleteNode() at in bounds position ";
   cout << num << "...n";
   intList1.deleteNode(num);
   cout << "Printing the list with size: ";
   cout << intList1.getSize() << endl << "t";
   intList1.print();
Â
   try
   {
       num = rand()%10+intList1.getSize();
       cout << "nTesting deleteNode() at out of bounds position ";
       cout << num << "...n";
       intList1.deleteNode(num);
       cout << "Printing the list with size: ";
       cout << intList1.getSize() << endl << "   ";
       intList1.print();
   }
   catch (...)
   {
       cout << "tException: " << endl;
   }
Â
   try
   {
       num = (rand()%10)*(-1);
       cout << "nTesting deleteNode() at negative position ";
       cout << num << "...n";
       intList1.deleteNode(num);
       cout << "Printing the list with size: ";
       cout << intList1.getSize() << endl << "   ";
       intList1.print();
   }
   catch (...)
   {
       cout << "   Exception: " << endl;
   }
Â
   cout << "nTesting deleteNode() at last position...n";
   intList1.deleteNode(intList1.getSize()-1);
   cout << "Printing the list with size: ";
   cout << intList1.getSize() << endl << "t";
   intList1.print();
Â
   cout << "nTesting searchNode() for value (-1) not in listn";
   hold = intList1.searchNode(-1);
   if (hold ==0)
   {
       cout << "The value -1 was not found in the following list:nt";
       intList1.print();
   }
   else
   {
       cout << "The value -1 was found at position ";
       cout << hold << " in the following listnt";
       intList1.print();
   }
Â
   cout << "nTesting searchNode() w/random values in the following list:nt";
   intList1.print();
   for (int i=0; i< 5; i++)
   {
       num = rand()%10;
       hold = intList1.searchNode(num);
       if (hold ==0)
           cout << "ttThe value " << num << " was not foundn";
       else
           cout << "tThe value " << num << " was found at position "
                << hold << endl;
   }
Â
   try
   {
       num = (rand()%10)*(-1);
       cout << "nTesting the overloaded [] operator at negative position ";
       cout << num << "...n";
       cout << intList1[num] << " was accessed with subscript" << num << endl;
   }
   catch (...)
   {
       cout << "   Exception: " << endl;
   }
Â
   try
   {
       num = (rand()%10)+intList1.getSize()+5;
       cout<<"nTesting the overloaded [] operator at out of bounds position ";
       cout << num << "...n";
       cout << intList1[num] << " was accessed with subscript" << num << endl;
   }
   catch (...)
   {
       cout << "   Exception: " << endl;
   }
Â
   cout << "nTesting the overloaded [] operator at random positions";
   cout << " in the following listnt";
   intList1.print();
   for (int i=0; i< 5; i++)
   {
       try
       {
           num = (rand()%20);
           cout << "tThe value: " << intList1[num];
           cout << " was accessed with subscript: " << num << endl;
       }
       catch (...)
       {
           cout << "tException: " << " attempting to access ";
           cout << "position " << num << endl;
       }
   }
   cout << "nTesting pop_front() function on the following list of size ";
   cout << intList1.getSize() << ":nt";
   intList1.print();
   cout << "tThe value " << intList1.pop_front() << " was removed. ";
   cout << "Now the list (size " << intList1.getSize() << ") reads:nt";
   intList1.print();
Â
   try
   {
       cout << "nTesting pop_front() on empty list";
       emptyIntList.pop_front();
   }
   catch (...)
   {
       cout << "tException: " << endl;
   }
Â
   cout << "nTesting pop_back() function on the following list of size ";
   cout << intList1.getSize() << ":nt";
   intList1.print();
   cout << "tThe value " << intList1.pop_back() << " was removed. ";
   cout << "Now the list (size " << intList1.getSize() << ") reads:nt";
   intList1.print();
Â
   try
   {
       cout << "nTesting pop_back() on empty list:";
       emptyIntList.pop_back();
   }
   catch (...)
   {
       cout << "tException: " << endl;
   }
Â
   num = rand()%100;
   cout << "nTesting push_front() with value "<< num;
   cout << " on the following list of size ";
   cout << intList1.getSize() << ":nt";
   intList1.print();
   intList1.push_front(num);
   cout << "tNow the list (size " << intList1.getSize() << ") reads:nt";
   intList1.print();
Â
   num = rand()%100;
   cout << "nTesting push_front() on empty list...nt";
   emptyIntList.print();
   emptyIntList.push_front(num);
   cout << "tNow the list (size " << emptyIntList.getSize() << ") reads:t";
   emptyIntList.print();
Â
   cout << "nDeleting the value " << emptyIntList.pop_back();
   cout << " from empty list using pop_back()...n";
   cout << "tNow using print() on the list of size " << emptyIntList.getSize() << ":nt";
   emptyIntList.print();
Â
   num = rand()%100;
   cout << "nTesting push_back() with value "<< num;
   cout << " on the following list of size ";
   cout << intList1.getSize() << ":nt";
   intList1.print();
   intList1.push_back(num);
   cout << "tNow the list (size " << intList1.getSize() << ") reads:nt";
   intList1.print();
Â
   num = rand()%100;
   cout << "nTesting push_back() on empty list...nt";
   emptyIntList.print();
   emptyIntList.push_back(num);
   cout << "tNow the list (size " << emptyIntList.getSize() << ") reads:t";
   emptyIntList.print();
Â
   cout << "nDeleting the value " << emptyIntList.pop_front();
   cout << " from empty list using pop_front()...n";
   cout << "tNow using print() on the list of size " << emptyIntList.getSize() << ":nt";
   emptyIntList.print();
Â
   cout << "nDestructor test will occur at the end of the program automatically for ..." << endl;
   cout << "emptyIntList, intList1, and intList2";
Â
   cout << endl;
   return 0;
}
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly