Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ question, only need code zDepthList.cpp Sample P3main.cpp : using namespace

ID: 3605552 • Letter: C

Question

C++ question, only need code zDepthList.cpp

Sample P3main.cpp :

using namespace std;

#include <iostream>

#include "zDepthList.cpp"

int main() {

int l[30];

for (int i=0;i<30;i++) l[i]=i;

zDepthList z(l,30);

z.out('f');

z.move(10,2,'f');

z.out();

z.move(14,3,'r');

z.out('r');

z.movefront(4);

z.moveback(13);

z.out();

cout << endl << endl;

for (int i=0;i<30;i+=2) l[i]=28

-i;

zDepthList x(l,30), x1;

x.out('f');

x.move(27,2,'f');

x.out();

x.move(3,8,'r');

x.out('r');

x.movefront(4);

x.moveback(13);

x.out();

x.del(3);

x.out();

x.addback();

x.out();

x.split(12,x1);

x.out();

x1.out();

x1.combine(x);

x1.out();

#define bigsize 1000000

int big[bigsize];

for (int i=0;i<bigsize;i++) big[i]=i;

zDepthList y(big,bigsize);

for (int i=0;i<bigsize;i+=2) y.moveback(i);

for (int i=0;i<bigsize;i+=3) y.movefront(i);

for (int i=1;i<bigsize;i+=3) y.move(i,4);

for (int i=0;i<bigsize;i+=2)

y.move(i,2,'r');

for (int i=0;i<5; i++) cout << y.at(i) << " ";

for (int i=0;i<5; i++) cout << y.at(bigsize-

1-i) << " ";

cout << endl;

}

Suppose that our T/A is creating a video game. In this game there are many "targets" that need to be displayed on the screen, and modern graphics engines need to know the distance from the viewer to quickly process the scene. So, for this project you Will implement a class to help the T/A keep track of an ordered list of the targets in increasing distance from the viewer. Every item in the zDepthList will be given a unique integer "name" that is in the range 0..n. This name can be used as the index of an array to provide fast access. The class will be called zDepthList. The public methods of your class should be the following: zDepthList() zDepthList(int[], int) Constructor where the first parameter is an array and the second Default constructor. Should create an empty list. parameter is the length of the array. Should create a zDepthList containing all the items in the array. The items in the array are given in increasing distance to the source. The entry in the array is the index of that item in the zDepthList. Output method. Traverses the zDepthList in order of depth. The parameter indicates the direction. The default is 'f' indicating close to far, or forward. If the parameter is 'r', then the traversal should be far to near, or reverse. void out(char) void move(int, int, char) This method reorders thezDepthList. The first parameter is the index of the item to be moved. The second parameter is the number of steps, and the last parameter is the direction, where 'f' is the default and means to move the item "deeper" irn the list and 'r' means to move the item closer in the list. void movefront(int) Moves the item at the given index to the front of the zDepthList void moveback(int) Moves the item at the given index to the back of the zDepthList int at(int) returns the index of the item at position I in the zDepthList starting at 0 for the front of the list.

Explanation / Answer

#include <iostream>

using namespace std;

class Node {
public:
    Node *prev;
    Node *next;
    int depth;

    Node(int depthInput) {
        depth = depthInput;
        prev = next = NULL;
    }
};

class zDepthList {
public:
    Node *head;
    Node *tail;
    Node **zList;
    int zSize;

    zDepthList() {
        zList = new Node *[0];
        head = tail = NULL;
    }

    zDepthList(int array[], int length) {
        head = tail = NULL;
        zSize = length;
        zList = new Node *[zSize];
        for(int i = 0; i < zSize; i++) {
            push_back(array[i], array[i]);
        }
    }

    void out(const char direction = ' ') {
        if(direction == 'f' || direction == ' ') {
            Node *curr = head;
            while(curr != NULL) {
                cout << curr->depth << " ";
                curr = curr->next;
            }
        }
        else {
            Node *curr = tail;
            while(curr != NULL) {
                cout << curr->depth << " ";
                curr = curr->prev;
            }
        }
        cout << endl;
    }

    void move(int indexValue, int steps, char traversalDirection = ' ') {
        if(traversalDirection == 'f' || traversalDirection == ' ') {
            for(int i = steps; i > 0; i--) {
                if(zList[indexValue]->next != NULL) {
                    swap(zList[indexValue], zList[indexValue]->next);
                }
            }
        }
        else {
            for(int i = steps; i > 0; i--) {
                if(zList[indexValue]->prev != NULL) {
                    swap(zList[indexValue]->prev, zList[indexValue]);
                }
            }
        }
    }

    void movefront(int indexToMove) {
        Node *current = zList[indexToMove];
        Node *tempX = current->prev;
        Node *tempY = current->next;

        if(current != head){

            current->next = head;
            head->prev = current;
            current->prev = NULL;

            head = current;

            if(tempY != NULL) {
                tempX->next = tempY;
                tempY->prev = tempX;
            }
            else {
                tail = tempX;
                tempX->next = NULL;
            }
        }
    }

    void moveback(int indexToMove) {
        Node *current = zList[indexToMove];
        Node *tempX = current->prev;
        Node *tempY = current->next;

        if(current != tail) {
            current->next = NULL;
            current->prev = tail;
            tail->next = current;

            tail = current;

            if(tempX != NULL) {
                tempX->next = tempY;
                tempY->prev = tempX;
            }
            else {
                head = tempY;
                head->prev = NULL;
            }
        }
    }

    int at(int indexToRetrieve) {
        int dIndex = 0;
        Node *walk = head;
        while(walk) {
            Node *node = walk;
            if(dIndex == indexToRetrieve) {
                return walk->depth;
            }
            walk = walk->next;
            dIndex++;
        }
        return -1;
    }

private:
    void push_back(int addedNum, int index) {
        Node *node = new Node(addedNum);

        if(head == NULL) {
            head = tail = node;
        }
        else {
            tail->next = node;
            node->prev = tail;
            tail = node;
        }
        zList[index] = node;

    }

    void swap(Node *A, Node *B) {
        Node *tempX = A->prev;
        Node *tempY = B->next;
        if(tempX != NULL) {
            tempX->next = B;
        }
        else {
            head = B;
        }
        A->prev = B;
        if(tempY != NULL) {
            tempY->prev = A;
        }
        else {
            tail = A;
        }
        B->next = A;
        A->next = tempY;
        B->prev = tempX;
    }
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote