Question: The class DataList defined in the attached DataList.cpp file gives a simple implementation of linked lists. In this part of the project you will expand

The class DataList defined in the attached DataList.cpp file gives a simple implementation of linked lists. In this part of the project you will expand the class DataList by adding the following two functions to the class. You will also add testing code in the main function of the DataList class to demonstrate the correctness of the functions you add on at least 5 different test cases.

The class DataList defined in the attached DataList.cpp file gives a simple

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Psuedocode:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ListSwap(L, x, y)

  1. if(x=null or y=null or x=y)

  2. Return

    // Saving relevant pointers

  3. beforeX

  4. beforeY

    // Swapping x and y by connecting relevant pointers

    // using the procedure ListConnect described below

  5. ListConnect(L, beforeX, y);

  6. ListConnect(L, y, afterX);

  7. ListConnect(L, beforeY, x);

  8. ListConnect(L, x, afterY);

The following procedure connects a node p to a node q in a given doubly linked non-circular list L, making q the head of L if p is null, or p the tail of L if q is null

ListConnect(L, p, q)

1. if(p=q)

2. return 3. if (p != null)

4. p.next

5. else

6. L.head

7. if (q != null)

8. q.prev

9, else

10. L.tail

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Please attach to this code:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include #include #include

using namespace std;

// Class Node represents a single node in a linked list. class Node{ public: // The following attributes are declared public // for the convenience of accessing them. int val; Node *next; Node *prev; // Constructor Function for creating a node in a linked list // with initial value of aVal Node(int aVal){ val = aVal; next = NULL; prev = NULL; } // Destructor function, optional ~Node(){ //delete next; //delete prev; } };

// Class DataList represents a simple type of doubly-linked list, // where each node has an integer value class DataList{

public: // Default Constructor for // creating an empty linked list DataList(){ // The pointers to the head and tail of the list mHead = NULL; mTail = NULL; } // An alternate constructor function allowing the // initialization of a linked list with a vector of given values. DataList(vector vals){ mHead = NULL; mTail = NULL;

// Populate the linked list with values // in the vector parameter vals for(int i=0; i append(new Node(vals[i])); };

// This function displays all the values stored in the // linked list in the same order as they appeAR in the list. void print(){

Node* p = mHead;

// Traversing the list while(p!=NULL){ cout val next; }

cout next = aNode; aNode->prev = mTail; aNode->next = NULL; mTail = aNode; }

void shuffle(){ // add your code here; return; } private: Node* mHead; Node* mTail; };

int main(){

// Testing code for the function shuffle() of the // class DataList; // Add more cases vector vals{3, 5, 9, 1,-5, 0}; DataList dList(vals);

cout

return 0;

}

The class DataList defined in the attached DataList.cpp file gives a simple implementation of linked lists. In this part of the project you will expand the class DataList by adding the following two functions to the class. You will also add testing code in the main function of the DataList class to demonstrate the correctness of the functions you add on at least 5 different test cases. 1. Write a function swap that Implements the pseudocode you gave as the solution to Problem 1 of Homework 5 and test if it's correct. If not, find and fix all the errors to make sure it works correctly with reasonable error checking such as one or more parameters being null pointers. 2. Write a function shuffle that shuffles a given linked list consisting by swapping every odd-number-th node with its successive node except the last node if the total number of nodes in the list is odd, randomly splitting the list into two non-empty (sub)lists and swapping the two sublists. Take as an exaoutmple a linked list of 10 nodes containing values 0, 1, 2, ..., 9, in that order. Provided below are some examples of possible and impossible output of the shuffle function: Possible outputs: o 4, 7, 6, 9, 8, 1, 0, 3, 2, 5 o 3, 2, 5, 4, 7, 6, 9, 8, 1, 0 6, 9, 8, 1, 0, 3, 2, 4, 5, 7 Impossible outputs: o 4, 7, 1, 9, 8, 1, 6, 3, 2, 5 3, 2, 5, 4, 7, 6, 9, 8, 0, 1 0, 3, 2, 5, 1, 4,7, 6, 9, 8 o O o For this option, you must add your code to the attached DataList.cpp file and submit the modified file (that runs) as your solution. The class DataList defined in the attached DataList.cpp file gives a simple implementation of linked lists. In this part of the project you will expand the class DataList by adding the following two functions to the class. You will also add testing code in the main function of the DataList class to demonstrate the correctness of the functions you add on at least 5 different test cases. 1. Write a function swap that Implements the pseudocode you gave as the solution to Problem 1 of Homework 5 and test if it's correct. If not, find and fix all the errors to make sure it works correctly with reasonable error checking such as one or more parameters being null pointers. 2. Write a function shuffle that shuffles a given linked list consisting by swapping every odd-number-th node with its successive node except the last node if the total number of nodes in the list is odd, randomly splitting the list into two non-empty (sub)lists and swapping the two sublists. Take as an exaoutmple a linked list of 10 nodes containing values 0, 1, 2, ..., 9, in that order. Provided below are some examples of possible and impossible output of the shuffle function: Possible outputs: o 4, 7, 6, 9, 8, 1, 0, 3, 2, 5 o 3, 2, 5, 4, 7, 6, 9, 8, 1, 0 6, 9, 8, 1, 0, 3, 2, 4, 5, 7 Impossible outputs: o 4, 7, 1, 9, 8, 1, 6, 3, 2, 5 3, 2, 5, 4, 7, 6, 9, 8, 0, 1 0, 3, 2, 5, 1, 4,7, 6, 9, 8 o O o For this option, you must add your code to the attached DataList.cpp file and submit the modified file (that runs) as your solution

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!