SophiaPretty

(5)

$14/per page/Negotiable

About SophiaPretty

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Accounting,Algebra See all
Accounting,Algebra,Applied Sciences,Architecture and Design,Art & Design,Biology,Business & Finance,Calculus,Chemistry,Communications,Computer Science,Economics,Engineering,English,Environmental science,Essay writing Hide all
Teaching Since: Jul 2017
Last Sign in: 304 Weeks Ago, 6 Days Ago
Questions Answered: 15833
Tutorials Posted: 15827

Education

  • MBA,PHD, Juris Doctor
    Strayer,Devery,Harvard University
    Mar-1995 - Mar-2002

Experience

  • Manager Planning
    WalMart
    Mar-2001 - Feb-2009

Category > Computer Science Posted 05 Dec 2017 My Price 10.00

Design linked list class to hold a series of integers.

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;

}

Answers

(5)
Status NEW Posted 05 Dec 2017 05:12 AM My Price 10.00

-----------  ----------- 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

Not Rated(0)