This is a C++ problem,P3main.cpp has been given, I need a help for zDepthList.cp
ID: 3606640 • Letter: T
Question
This is a C++ problem,P3main.cpp has been given, I need a help for zDepthList.cpp, thank you
Thank you so much!!!
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 is the default and means to move the item "deeper" in 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 O for the front of the list.Explanation / Answer
using namespace std;
struct Node{
int data;
Node *next;
Node *prev;
};
class zDepthList{
private:
Node *head;
Node *tail;
Node **arrayOfPointers;
public:
zDepthList(){
head = new Node;
tail = new Node;
head = NULL;
tail = NULL;
}
zDepthList(int array[], int size){
int i = 0;
head = new Node;
tail = new Node;
arrayOfPointers = new Node*[size];
head = NULL;
for(i = size-1; i>=0; i--){
Node* newElement = new Node;
newElement->data = array[i];
newElement->prev = NULL;
if(head==NULL){
newElement->next = NULL;
}
else{
newElement->next = head;
head->prev = newElement;
}
head = newElement;
arrayOfPointers[newElement->data] = newElement;
}
tail = arrayOfPointers[array[size-1]];
}
//Destructor
~zDepthList(){
delete head;
delete tail;
delete arrayOfPointers;
}
void out(const char direction = 'f') {
Node *forward = new Node;
forward = head;
Node *backward = new Node;
backward = tail;
//outputs list from back of list to front of list
if (direction == 'r'){
while(backward!=NULL){
cout << backward->data << " ";
backward = backward->prev;
}
}
//outputs list from front of list to back of list
else{
while(forward!= NULL){
cout << forward->data << " ";
forward = forward->next;
}
}
cout << endl;
}
void move(int startPosition, int moveNumSpaces, const char direction = 'f'){
Node* moveNode = new Node;
Node* newPosition = new Node;
int numSpacesMoved = 0;
moveNode = arrayOfPointers[startPosition];
newPosition = moveNode;
//Moves Node closer
if(direction == 'r'){
if(moveNode==head){
return;
}
else if(moveNode==tail){
(moveNode->prev)->next = NULL;
tail = moveNode->prev;
}
else{
(moveNode->next)->prev = moveNode->prev;
(moveNode->prev)->next = moveNode->next;
}
while(numSpacesMoved <= moveNumSpaces){
newPosition = newPosition->prev;
if(newPosition->prev == NULL){
movefront(startPosition);
return;
}
numSpacesMoved++;
}
}
//Moves Node deeper
else{
if(moveNode == tail){
return;
}
else if(moveNode == head){
(moveNode->next)->prev = NULL;
head = moveNode->next;
}
else{
(moveNode->next)->prev = moveNode->prev;
(moveNode->prev)->next = moveNode->next;
}
while(numSpacesMoved < moveNumSpaces){
newPosition = newPosition->next;
if(newPosition->next == NULL){
moveback(startPosition);
return;
}
numSpacesMoved++;
}
}
moveNode->prev = newPosition;
moveNode->next = newPosition->next;
(newPosition->next)->prev = moveNode;
newPosition->next = moveNode;
}
void movefront(int nodeData){
Node *moveNode = arrayOfPointers[nodeData];
Node *front = new Node;
front = head;
//Node is already in the front of the list
if(moveNode == head){
return;
}
//Node is in the very back of the list
else if(moveNode == tail){
(moveNode->prev)->next = NULL;
tail = moveNode->prev;
}
else{
(moveNode->next)->prev = moveNode->prev;
(moveNode->prev)->next = moveNode->next;
}
moveNode->next = front;
front->prev = moveNode;
moveNode->prev = NULL;
head = moveNode;
}
void moveback(int nodeData){
Node *moveNode = arrayOfPointers[nodeData];
Node *back = new Node;
back = tail;
//Node is already in the back of the list
if(moveNode == tail){
return;
}
//Node is in the very front of the list
else if(moveNode == head){
(moveNode->next)->prev = NULL;
head = moveNode->next;
}
else{
(moveNode->next)->prev = moveNode->prev;
(moveNode->prev)->next = moveNode->next;
}
back->next = moveNode;
moveNode->prev = back;
moveNode->next = NULL;
tail = moveNode;
}
int at(int moveNumSpaces){
int numSpacesMoved = 0;
Node *newPosition = new Node;
newPosition = head;
for(numSpacesMoved = 0; numSpacesMoved < moveNumSpaces; numSpacesMoved++){
newPosition = newPosition->next;
}
return newPosition->data;
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.