struct neighbor_xy_list *findPath(struct grid *gd, int startX, int startY, int endX, int endY) { struct open_list *head = ol_new(); struct open_list *current = head; struct node *startNode = getNodeAt(gd, startX, startY); struct node *endNode = getNodeAt(gd, endX, endY); struct node *activeNode; int counter = 0; /* Initialize the start node */ startNode->h = 0; startNode->g = 0; startNode->f = 0; startNode->parent = NULL; current = ol_insert_right(current, startNode); startNode->opened = true; head = ol_listsort(head); current = head->left; while (head != current) { /* List is empty when current marker rests on head */ if (DEBUG) printf("Cycle %d\n", counter); activeNode = current->list_node; current = ol_del_free(current); activeNode->closed = true; if (activeNode == endNode) { struct neighbor_xy_list *goal; ol_clean(head); goal = backtrace(activeNode); return goal; } /* Begin identifying successors... */ _identifySuccessors(gd, activeNode, current, endNode); head = ol_listsort(head); /* Instead of sorting it everytime a item is added, I will sort when I need to grab the lowest value. */ current = head->right; if (DEBUG) { listOpenList(head); printf("Currently active node:\n"); displayNodeInfo(current->list_node); } counter++; if (counter >= 5000) { ol_clean(head); printf("\n----------\nLimit reached\n----------\n"); return NULL; } } printf("\n----------\nReturning NULL because head = current\n----------\n"); return NULL; }
bool LinkedList<ItemType>::remove(int position) { bool ableToRemove = (position >= 1) && (position <= itemCount); if (ableToRemove) { // point at the node to be removed Node<ItemType>* curPtr = getNodeAt(position); // case 1: to remove the only node on a list of length 1 if ((position == 1) && (itemCount == 1)) { headPtr = nullptr; tailPtr = nullptr; } // case 2: to remove the first node in list of length > 1 else if ((position == 1) && (itemCount > 1)) { Node<ItemType>* nextPtr = getNodeAt(position + 1); nextPtr->setPrev(nullptr); headPtr = nextPtr; nextPtr = nullptr; } // case 3: to remove the last node in list of length > 1 else if ((position == itemCount) && (itemCount > 1)) { Node<ItemType>* prevPtr = getNodeAt(position - 1); prevPtr->setNext(nullptr); tailPtr = prevPtr; prevPtr = nullptr; } // case 4: to remove a node in the middle of list of length > 1 else if ((1 < position) && (position < itemCount) && (1 < itemCount) ) { // Create node pointers to hold our place in the chain Node<ItemType>* prevPtr = getNodeAt(position - 1); Node<ItemType>* nextPtr = getNodeAt(position + 1); // Disconnect indicated node from chain by connecting the // previous node with the next node prevPtr->setNext(nextPtr); nextPtr->setPrev(prevPtr); } // end if // Return node to system curPtr->setNext(nullptr); curPtr->setPrev(nullptr); delete curPtr; curPtr = nullptr; itemCount--; // Decrease count of entries } // end if return ableToRemove; } // end remove
bool LinkedList<ItemType>::insert(int newPosition, const ItemType& newEntry) { bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1); if (ableToInsert) { // Create a new node containing the new entry Node<ItemType>* newNodePtr = new Node<ItemType>(newEntry); if ((newPosition == 1) && (getLength() == 0)) { // case for first item on empty list headPtr = newNodePtr; newNodePtr->setNext(nullptr); newNodePtr->setPrev(nullptr); tailPtr = newNodePtr; } else if ((newPosition == 1) && (getLength() > 0)) { // case for first item on non-empty list Node<ItemType>* nextPtr = getNodeAt(newPosition); newNodePtr->setNext(headPtr); newNodePtr->setPrev(nullptr); nextPtr->setPrev(newNodePtr); headPtr = newNodePtr; } else if ((newPosition > 1) && (newPosition < itemCount)) { // case for item on non-empty list, not in last place // Create node pointers to hold our place in the chain Node<ItemType>* prevPtr = getNodeAt(newPosition - 1); Node<ItemType>* nextPtr = getNodeAt(newPosition); // Attach the new node between prevPtr and nextPtr newNodePtr->setNext(prevPtr->getNext()); newNodePtr->setPrev(nextPtr->getPrev()); // Re-point the pointers for prevPtr and nextPtr to the new node prevPtr->setNext(newNodePtr); nextPtr->setPrev(newNodePtr); } else if (newPosition == itemCount + 1){ // special case for last item on non-empty list Node<ItemType>* prevPtr = getNodeAt(newPosition - 1); newNodePtr->setPrev(tailPtr); newNodePtr->setNext(nullptr); prevPtr->setNext(newNodePtr); tailPtr = newNodePtr; } itemCount++; // Increase count of entries } // end if return ableToInsert; } //end insert
// Removes one element from an array and returns the value of that element. player_point_3d_t* PSPoints2Draw::removeAt(int index) { node * aNode, * deleteNode; if(index == 0) { aNode = startNode->link; deleteNode = startNode; startNode = aNode; length--; return &deleteNode->point; } aNode = getNodeAt(index-1); if( aNode == NULL ) return NULL; deleteNode = aNode->link; aNode->link = deleteNode->link; length--; return &deleteNode->point; }
// Adds one alement to the array and returns the new length of the array. int PSPoints2Draw::addAt(player_point_3d_t * newPoint, int index) { node * aNode, * newNode; if(index == 0) { newNode = new node; newNode->point.px = newPoint->px; newNode->point.py = newPoint->py; newNode->link = startNode; startNode = newNode; length++; return length; } aNode = getNodeAt(index-1); if( aNode == NULL ) return -1; newNode = new node; newNode->point.px = newPoint->px; newNode->point.py = newPoint->py; newNode->link = aNode->link; aNode->link = newNode; length++; return length; }
void Map::selectSelected() { Node *n; n = getNodeAt(selector->row, selector->column); if(n != Model::getSelf()->nullNode) { n->select(true); if(n->ship != Model::getSelf()->nullShip) { if(Model::getSelf()->playerArray[0]->fleet->hasShip(n->ship)) Model::getSelf()->selectedShip = n->ship; } else { if(Model::getSelf()->selectedShip != NULL) { Model::getSelf()->selectedShip->moveToNode(n); Model::getSelf()->selectedShip = Model::getSelf()->nullShip; } } } else { Model::getSelf()->selectedNode->select(false); Model::getSelf()->selectedShip = Model::getSelf()->nullShip; } }
bool LinkedList<ItemType>::insert(int newPosition, const ItemType& newEntry) { bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1); if (ableToInsert) { // Create a new node containing the new entry Node<ItemType>* newNodePtr = new Node<ItemType>(newEntry); // Attach new node to chain if (newPosition == 1) { // Insert new node at beginning of chain newNodePtr->setNext(headPtr); headPtr = newNodePtr; } else { // Find node that will be before new node Node<ItemType>* prevPtr = getNodeAt(newPosition - 1); // Insert new node after node to which prevPtr points newNodePtr->setNext(prevPtr->getNext()); prevPtr->setNext(newNodePtr); } // end if itemCount++; // Increase count of entries } // end if return ableToInsert; } // end insert
/** * Complete the following function */ void LListInt::remove(int loc) { //valid location from 0 to size -1 if(size_ == 0){ return; } if(loc < 0 || loc > size_ -1){ return; } Item* temp = getNodeAt(loc); if(size_== 1){ head_ = NULL; tail_ = NULL; } else if(loc == 0){ head_ = temp -> next; temp -> next ->prev = NULL; } else if(loc == size_-1){ temp->prev->next = NULL; tail_ = temp -> prev; } else{ temp->next->prev = temp -> prev; temp -> prev -> next = temp -> next; } delete temp; size_ --; }
void MyMap::printMap(void) { for(int y = 0; y < vertical; ++y){ for(int x = 0; x < horizontal; ++x){ MapNode* yx = getNodeAt(y,x); if(yx == NULL) continue; char symbol = ' '; if(yx->isLocked()) symbol = '2'; else if(yx->isPath()) symbol = 'X'; std::cout << symbol; } std::cout << std::endl; } }
int MyMap::listAdjacent(std::vector<MapNode*>& adjacents, MapNode* node) { for(int i = 0; i < 8; i++){ unsigned int n_x = node->getX(); unsigned int n_y = node->getY(); switch(i){ case 0: n_x++; n_y++; break; case 1: n_x--; n_y--; break; case 2: n_x++; n_y--; break; case 3: n_x--; n_y++; break; case 4: n_x--; break; case 5: n_x++; break; case 6: n_y--; break; case 7: n_y++; break; } if(n_x >= horizontal) continue; if(n_y >= vertical) continue; adjacents.push_back(getNodeAt(n_y,n_x)); } return adjacents.size(); }
char* ParameterTemplate::getParameterName(DWORD index) { ParameterTemplateNode* node = getNodeAt(index); if (node == nullptr) return nullptr; else return node->Name; }
void HUGE_INT::removeNode(const int &position) { // check if position is within // the bounds of the list (1 to n) bool removed = (position >= 1) && (position <= this->getSize()); if (removed) { Node *p = head, *q = NULL; // first digit if (position == 1) { head = p->next; } // some other digit else { q = getNodeAt(position - 1); p = q->next; q->next = p->next; } // delete all parts of node freeNode(p); // adjust size and printWidth this->setSize(this->getSize() - 1); this->setPrintWidth(this->getPrintWidth() - 1); } return; }// end removeNode
DWORD ParameterTemplate::getParameterType(DWORD index) { ParameterTemplateNode* node = getNodeAt(index); if (node == nullptr) return FF_FAIL; else return node->Type; }
bool LinkedList<ItemType>::remove(int position) { bool ableToRemove = (position >= 1) && (position <= itemCount); if (ableToRemove) { Node<ItemType>* curPtr = nullptr; if (position == 1) { // Remove the first node in the chain curPtr = headPtr; // Save pointer to node headPtr = headPtr->getNext(); } else { // Find node that is before the one to delete Node<ItemType>* prevPtr = getNodeAt(position - 1); // Point to node to delete curPtr = prevPtr->getNext(); // Disconnect indicated node from chain by connecting the // prior node with the one after prevPtr->setNext(curPtr->getNext()); } // end if // Return node to system curPtr->setNext(nullptr); delete curPtr; curPtr = nullptr; itemCount--; // Decrease count of entries } // end if return ableToRemove; } // end remove
// Removes one element from an array and returns the value of that element. frame_point_data_t* PSFramePoints::removeAt(int index) { node * aNode, * deleteNode; if(index == 0) { aNode = startNode->link; deleteNode = startNode; startNode = aNode; length--; return &deleteNode->data; } aNode = getNodeAt(index-1); if( aNode == NULL ) return NULL; deleteNode = aNode->link; aNode->link = deleteNode->link; length--; return &deleteNode->data; }
// For unit test use with integers template <class ItemType> void LinkedList <ItemType>::print() { for (int i = 0; i < count; i++) { cout << "Item " << i + 1 << " = " << getNodeAt(i+1)->getItem() << endl; } cout << endl; return; }
int const & LListInt::get(int loc) const { if(loc < 0 || loc >= size_){ throw std::invalid_argument("bad location"); } Item *temp = getNodeAt(loc); return temp->val; }
/** * Access Operator */ int const & LListInt::operator[](int position) const { if(position < 0 || position >= size_){ throw std::invalid_argument("bad location"); } Item *temp = getNodeAt(position); return temp->val; }
/** * Complete the following function */ void LListInt::insert(int loc, const double& val) { //making sure valid location value if(loc >= 0 && loc <= size_) { //insert into empty list if(size_ == 0) { Item* temp = new Item; head_ = temp; tail_ = temp; temp->val = val; temp->next = NULL; temp->prev = NULL; size_++; } //insert into front of list else if(loc==0) { Item* temp = new Item; temp->prev = NULL; temp->next = head_; head_->prev = temp; head_ = temp; temp->val = val; size_++; } //insert into back of list else if(loc == size_) { Item* temp = new Item; tail_->next = temp; temp->next = NULL; temp->val = val; temp->prev = tail_; tail_ = temp; size_++; } //insert middle of list else { Item* temp = new Item; Item* currptr = getNodeAt(loc); temp->prev = currptr->prev; temp->prev->next = temp; currptr->prev = temp; temp->next = currptr; temp->val = val; size_++; } } else { return; } }
void _identifySuccessors(struct grid *gd, struct node *activeNode, struct open_list *current, struct node *endNode) { int endX = endNode->x; int endY = endNode->y; int *jumpPoint; struct neighbor_xy_list *neighbors_head = _findNeighbors(gd, activeNode); struct neighbor_xy_list *neighbors_current = neighbors_head; while (neighbors_head != (neighbors_current = neighbors_current->right)) { if (DEBUG) { if (isWalkableAt(gd, neighbors_current->x, neighbors_current->y)) printf("Neighbor x:%d/y:%d is walkable!\n", neighbors_current->x, neighbors_current->y); else printf("Neighbor x:%d/y:%d is NOT walkable!\n", neighbors_current->x, neighbors_current->y); } jumpPoint = _jump(gd, neighbors_current->x, neighbors_current->y, activeNode->x, activeNode->y, endNode); if (DEBUG) printf("Jump point not set!\n\n"); if (jumpPoint != NULL) { int jx, jy, d, ng; struct node *jumpNode; if (DEBUG) printf("Jump point set!\n\n"); jx = jumpPoint[0]; jy = jumpPoint[1]; free(jumpPoint); malloc_count--; /* [ Malloc Count ] */ jumpNode = getNodeAt(gd, jx, jy); if (jumpNode->closed) { continue; } d = euclidean(abs(jx - activeNode->x), abs(jy - activeNode->y)); ng = activeNode->g + d; if (!jumpNode->opened || ng < jumpNode->g) { jumpNode->g = ng; if (!jumpNode->h) jumpNode->h = manhattan(abs(jx - endX), abs(jy - endY)); /* jumpNode->h = jumpNode->h || manhattan(abs(jx - endX), abs(jy - endY)); // ASK FIDELIS !! */ jumpNode->f = jumpNode->g + jumpNode->h; if (DEBUG) printf("Node g:%d h:%d f:%d\n", jumpNode->g, jumpNode->h, jumpNode->f); jumpNode->parent = activeNode; if (!jumpNode->opened) { current = ol_insert_right(current, jumpNode); jumpNode->opened = true; } else { ol_listsort(current->right); } } } } neighbor_xy_clean(neighbors_head); }
Relation* RelationList::findRelation(Node* node0, Node* node1){ for (int i=0; i<getNumNodes(); i++) { Relation* relation = (Relation*)getNodeAt(i); if ( (relation->getNode0()==node0 && relation->getNode1()==node1) || (relation->getNode0()==node1 && relation->getNode1()==node0) ) { return relation; } } return NULL; }
// Returns the value of an element. player_point_3d_t* PSPoints2Draw::itemAt(int index) { node * aNode; aNode = getNodeAt(index); if( aNode == NULL ) return NULL; return &aNode->point; }
// Returns the value of an element. frame_point_data_t* PSFramePoints::itemAt(int index) { node * aNode; aNode = getNodeAt(index); if( aNode == NULL ) return NULL; return &aNode->data; }
RelationList* RelationList::findRelations(Node* node){ RelationList* filteredRelations=new RelationList(); for (int i=0; i<getNumNodes(); i++) { Relation* relation = (Relation*)getNodeAt(i); if ( relation->getNode0()==node || relation->getNode1()==node ) { filteredRelations->addRelation(relation); } } return filteredRelations; }
/** * Complete the following function */ void LListInt::insert(int loc, const int& val) { if(loc < 0 || loc > size_){ throw std::invalid_argument("bad location"); } if(loc == 0) { //insert in front of list if(size_ == 0) { //empty list Item* temp = new Item; head_ = temp; tail_ = temp; temp->val = val; temp->prev = NULL, temp->next = NULL; } else { //nonempty list Item* temp = new Item; temp->val = val; temp->next = head_; head_->prev = temp; temp->prev = NULL; head_ = temp; } size_++; } else if(loc == size()) { //insert in back of list Item* temp = new Item; temp->val = val; temp->prev = tail_; temp->next = NULL; tail_->next = temp; tail_ = temp; size_++; } else { //insert in list Item* tempP = getNodeAt(loc-1); Item* tempN = getNodeAt(loc); Item* temp = new Item; temp->prev = tempP; tempP->next = temp; temp->next = tempN; tempN->prev = temp; temp->val = val; size_++; } } //insert
ParameterValue ParameterTemplate::getParameterDefault(DWORD index) { ParameterTemplateNode* node = getNodeAt(index); if (node == nullptr) { ParameterValue ret; ret.Data = FF_FAIL; return ret; } else return node->DefaultValue; }
// Updates the value of an element. int PSPoints2Draw::updateAt(player_point_3d_t * newPoint, int index) { node * aNode; aNode = getNodeAt(index); if( aNode == NULL ) return -1; aNode->point.px = newPoint->px; aNode->point.py = newPoint->py; return 0; }
/** * Complete the following function */ void LListInt::remove(int loc) { if(loc < 0 || loc >= size_){ throw std::invalid_argument("bad location"); } if(loc == 0) { //remove front of list if(empty()) { //empty list return; } else if (size_ == 1) { //one item in list Item* temp = head_; delete temp; head_ = NULL, tail_ = NULL; --size_; } else { // >1 item in list Item* temp = head_; head_ = head_->next; delete temp; --size_; } } else if(loc == size() - 1) { //remove back of list Item* temp = tail_; tail_ = tail_->prev; tail_->next = NULL; delete temp; --size_; } else { //remove in list Item* tempP = getNodeAt(loc-1); Item* tempN = getNodeAt(loc+1); Item* temp = getNodeAt(loc); tempP->next = tempN; tempN->prev = tempP; delete temp; --size_; } } //remove
void LinkedList<ItemType>::setEntry(int position, const ItemType& newEntry) throw(PrecondViolatedExcep) { // Enforce precondition bool ableToSet = (position >= 1) && (position <= itemCount); if (ableToSet) { Node<ItemType>* nodePtr = getNodeAt(position); nodePtr->setItem(newEntry); } else { string message = "setEntry() called with an invalid position."; throw(PrecondViolatedExcep(message)); } // end if } // end setEntry
ItemType LinkedList::getEntry(int position) const { bool ableToGet = (position >= 1) && (position <= itemCount); if (ableToGet) { Node* nodePtr = getNodeAt(position); return nodePtr->getItem(); } else { std::string message = "getEntry() called with an empty list or "; message = message + "invalid position."; throw(message); } } // end getEntry