ListNode* Solution::removeElements(ListNode *head, int val) { if (head) return nullptr; head->next = removeElements(head->next, val); return head->val == val ? head->next : head; }
ListNode* removeElements(ListNode* head, int val) { if(!head) return NULL; if(head->val == val) { head = head->next; return removeElements(head, val); } else { head->next = removeElements(head->next, val); return head; } }
ListElement* removeAllEqual(ListElement* list) { int middle = length(list) / 2; int toremove = getElement(list, middle)->value; return removeElements(list, toremove); }
void DomTree::removeElements(std::vector<DomElement *> &children) { while(children.size()) { if(children[0]->getAttributes().size()) { removeAttributes(children[0]->getAttributes()); } if(children[0]->getChildren().size()) removeElements(children[0]->getChildren()); delete children[0]; children.erase(children.begin()); } }
void DomTree::clearChildren() { while(elements.size()) { if(elements[0]->getAttributes().size()) { removeAttributes(elements[0]->getAttributes()); } if(elements[0]->getChildren().size()) removeElements(elements[0]->getChildren()); delete elements[0]; elements.erase(elements.begin()); } }
int a29main(int argc, char *argv[]) { struct List list; init(&list); insert(&list, 1, "18.11.11"); insert(&list, 1, "19.11.11"); insert(&list, 5, "20.11.11"); insert(&list, 5, "21.11.11"); insert(&list, 0, "22.11.11"); print(&list); removeElements(&list, 1); print(&list); removeElements(&list, 0); print(&list); removeElements(&list, 5); print(&list); clear(&list); return 0; }
int main() { // init tree int arr[] = {1, 2, 2, 4, 5}; // init list ListNode *head = create_list(arr, sizeof(arr) / sizeof(int)); removeElements(head, 2); print_list(head); return 1; }
int main() { Node* list = NULL; list = addElement(list, 6); list = addElement(list, 6); list = addElement(list, 6); list = addElement(list, 6); list = addElement(list, 6); list = addElement(list, 6); list = addElement(list, 5); showList(list); list = removeElements(list, 6); showList(list); return 0; }
For example, my version based on recursion and it wouldn't work on long list. But it was accepted. ListNode* removeElements(ListNode* head, int val) { if (head == NULL) return head; if (head->next == NULL && head->val == val) { delete head; return NULL; } if (head->val == val) { ListNode *next = head->next; head->val = next->val; head->next = next->next; delete next; return removeElements(head, val); } if (removeElements(head->next, val) == NULL) head->next = NULL; return head; }
int main(){ //int vs[] ={1,1}; //int vs[] ={1,2,3,4,4,5,5}; int vs[] ={1,2,6,3,4,5,5}; struct ListNode *h = make_list(vs,7); struct ListNode *af = removeElements(h,5); if(NULL==af){ puts("NULL Head!"); } else{ print_list(af); } puts("end of app"); }
int main(int argc ,char** argv) { int a[1]= {1}; int i=0; list L; L = init_list(); print(L); printf("insert node\n"); for(i=0;i<1;i++) { insert_node( L,a[i]); } print( L); L = removeElements(L,1); print( L); }
bool LayeredVolume::createRasterLayers(const MeshLib::Mesh &mesh, const std::vector<GeoLib::Raster const*> &rasters, double minimum_thickness, double noDataReplacementValue) { if (mesh.getDimension() != 2) return false; _elevation_epsilon = calcEpsilon(*rasters[0], *rasters.back()); if (_elevation_epsilon <= 0) return false; // remove line elements, only tri + quad remain MeshLib::ElementSearch ex(mesh); ex.searchByElementType(MeshLib::MeshElemType::LINE); MeshLib::Mesh* top (removeElements(mesh, ex.getSearchedElementIDs(), "MeshLayer")); if (top==nullptr) top = new MeshLib::Mesh(mesh); if (!MeshLib::MeshLayerMapper::layerMapping(*top, *rasters.back(), noDataReplacementValue)) return false; MeshLib::Mesh* bottom (new MeshLib::Mesh(*top)); if (!MeshLib::MeshLayerMapper::layerMapping(*bottom, *rasters[0], 0)) { delete top; return false; } this->_minimum_thickness = minimum_thickness; _nodes = MeshLib::copyNodeVector(bottom->getNodes()); _elements = MeshLib::copyElementVector(bottom->getElements(), _nodes); delete bottom; // map each layer and attach to subsurface mesh const std::size_t nRasters (rasters.size()); for (std::size_t i=1; i<nRasters; ++i) this->addLayerToMesh(*top, i, *rasters[i]); // close boundaries between layers this->addLayerBoundaries(*top, nRasters); this->removeCongruentElements(nRasters, top->getNElements()); delete top; return true; }
void BPlusIndexP::moveElements(IndexPage* source, IndexPage* destination, int startIndex, int endIndex) { copyElements(source, destination, startIndex, endIndex); removeElements(source, startIndex, endIndex); }
struct ListNode* removeElements(struct ListNode* head, int val) { return head?(head->next = removeElements(head->next, val), (head->val == val)?head->next:head):NULL; }
ListNode* removeElements(ListNode* head, int val) { if (!head) return NULL; head->next = removeElements(head->next, val); return (head->val == val) ? head->next : head; }
bool DetectPolygons::processFrameContainer(BaseFrameContainer *frm, BaseFrameSource *frameSource) { Shapes* shapes = frm->getShapes(); std::vector<Point> approxCurve; unsigned int size = 0; ///for each contour, analyze if it is a paralelepiped likely to be the marker for (unsigned int i=0;i<shapes->getContours().size();i++) { unsigned int contourSize = shapes->getContours()[i].size(); //check it is a possible element by first checking is has enough points if (contourSize>(unsigned int)(frm->getFrame()->getMat().cols /15)) { //approximate to a polygon - 1. limit slozitosti (pocet bodu krat cislo vs konstanta), 2. uzavreny double complexity = double(contourSize)*complexityKoef; complexity = complexity > maxComplexity ? maxComplexity : (complexity < minComplexity ? minComplexity : complexity); cv::approxPolyDP( cv::Mat (shapes->getContours()[i]),approxCurve , complexity , onlyClosed); size = approxCurve.size(); unsigned int one, two; if (approxCurve.size() >= 3) { // remove inline points for (unsigned int i = 0; i < size; i++) { one = (i + 1)%size; two = (i + 2)%size; // rozdil smernic double x1 = approxCurve[i].x, y1 = approxCurve[i].y, x2 = approxCurve[one].x, y2 = approxCurve[one].y, x3 = approxCurve[two].x, y3 = approxCurve[two].y; float k = std::abs(((y2-y1)/(x2-x1)) - ((y3-y1)/(x3-x1))); float d1 = std::sqrt((float) (approxCurve[i].x-approxCurve[one].x)*(approxCurve[i].x-approxCurve[one].x) + (approxCurve[i].y-approxCurve[one].y)*(approxCurve[i].y-approxCurve[one].y)); float d2 = std::sqrt((float) (approxCurve[i].x-approxCurve[two].x)*(approxCurve[i].x-approxCurve[two].x) + (approxCurve[i].y-approxCurve[two].y)*(approxCurve[i].y-approxCurve[two].y)); // if angle is too small or points are too close if (k < 0.2 || d1 < 0.04*d2) { approxCurve.erase(approxCurve.begin()+one); size--; } } } if (size >= minimalPolygonLines && size <= maximalPolygonLines) { //and is convex if (!onlyConvex || cv::isContourConvex(Mat (approxCurve))) { float minDist=1e10; if (minDistance > 0) { for (int i=0;i<size;i++) { float d= std::sqrt((float) (approxCurve[i].x-approxCurve[(i+1)%size].x)*(approxCurve[i].x-approxCurve[(i+1)%size].x) + (approxCurve[i].y-approxCurve[(i+1)%size].y)*(approxCurve[i].y-approxCurve[(i+1)%size].y)); if (d<minDist) minDist=d; } } if (minDist>=minDistance) { shapes->getMarkers().push_back(Marker()); for (unsigned int i=0;i<size;i++) { shapes->getMarkers().back().push_back( Point2f(approxCurve[i].x,approxCurve[i].y)); } } } } } } /// remove these elements whise corners are too close to each other //first detect candidates size = shapes->getMarkers().size(); if (minimalPolygonLines==4 && maximalPolygonLines == 4) { for (unsigned int i=0;i<shapes->getMarkers().size();i++) { //trace a line between the first and second point. //if the thrid point is at the right side, then the points are anti-clockwise double dx1 = shapes->getMarkers()[i][1].x - shapes->getMarkers()[i][0].x; double dy1 = shapes->getMarkers()[i][1].y - shapes->getMarkers()[i][0].y; double dx2 = shapes->getMarkers()[i][2].x - shapes->getMarkers()[i][0].x; double dy2 = shapes->getMarkers()[i][2].y - shapes->getMarkers()[i][0].y; double o = (dx1*dy2)-(dy1*dx2); if (o < 0.0) //if the third point is in the left side, then sort in anti-clockwise order { swap(shapes->getMarkers()[i][1],shapes->getMarkers()[i][3]); } } } vector<pair<int,int> > TooNearCandidates; for (unsigned int i=0;i < size;i++) { // cout<<"Marker i="<<i<<MarkerCanditates[i]<<endl; //calculate the average distance of each corner to the nearest corner of the other marker candidate for (unsigned int j=i+1;j<size;j++) { unsigned int vertexCount = shapes->getMarkers()[i].size(); if (vertexCount == shapes->getMarkers()[j].size()) { float dist=0; for (unsigned int c=0;c<vertexCount;c++) dist+= std::sqrt( (shapes->getMarkers()[i][c].x-shapes->getMarkers()[j][c].x)*(shapes->getMarkers()[i][c].x-shapes->getMarkers()[j][c].x)+(shapes->getMarkers()[i][c].y-shapes->getMarkers()[j][c].y)*(shapes->getMarkers()[i][c].y-shapes->getMarkers()[j][c].y)); dist/=vertexCount; //if distance is too small if (dist< 14) { TooNearCandidates.push_back(pair<int,int>(i,j)); } } } } //mark for removal the element of the pair with smaller perimeter vector<bool> toRemove (shapes->getMarkers().size(),false); for (unsigned int i=0;i<TooNearCandidates.size();i++) { if ( perimeter(shapes->getMarkers()[TooNearCandidates[i].first ])>perimeter(shapes->getMarkers()[ TooNearCandidates[i].second] )) toRemove[TooNearCandidates[i].second]=true; else toRemove[TooNearCandidates[i].first]=true; } //remove the invalid ones removeElements(shapes->getMarkers(),toRemove); return true; }
AMF0Object::~AMF0Object() { removeElements(); }
void PICComponent::initPackage( MicroInfo * microInfo ) { MicroPackage * microPackage = microInfo ? microInfo->package() : 0l; if ( microPackage ) { m_bCreatedInitialPackage = true; //BEGIN Get pin IDs QStringList allPinIDs = microPackage->pinIDs(); QStringList ioPinIDs = microPackage->pinIDs( PicPin::type_bidir | PicPin::type_input | PicPin::type_open ); // Now, we make the unwanted pin ids blank, so a pin is not created for them const QStringList::iterator allPinIDsEnd = allPinIDs.end(); for ( QStringList::iterator it = allPinIDs.begin(); it != allPinIDsEnd; ++it ) { if ( !ioPinIDs.contains(*it) ) *it = ""; } //END Get pin IDs //BEGIN Remove old stuff // Remove old text TextMap textMapCopy = m_textMap; const TextMap::iterator textMapEnd = textMapCopy.end(); for ( TextMap::iterator it = textMapCopy.begin(); it != textMapEnd; ++it ) removeDisplayText(it.key()); // Remove the old pins deletePICComponentPins(); // Remove old nodes NodeInfoMap nodeMapCopy = m_nodeMap; const NodeInfoMap::iterator nodeMapEnd = nodeMapCopy.end(); for ( NodeInfoMap::iterator it = nodeMapCopy.begin(); it != nodeMapEnd; ++it ) { if ( !ioPinIDs.contains(it.key()) ) removeNode( it.key() ); } removeElements(); //END Remove old stuff //BEGIN Create new stuff initDIPSymbol( allPinIDs, 80 ); initDIP(allPinIDs); PicPinMap picPinMap = microPackage->pins( PicPin::type_bidir | PicPin::type_input | PicPin::type_open ); const PicPinMap::iterator picPinMapEnd = picPinMap.end(); for ( PicPinMap::iterator it = picPinMap.begin(); it != picPinMapEnd; ++it ) m_picComponentPinMap[it.key()] = new PICComponentPin( this, it.value() ); //END Create new stuff removeDisplayText( "no_file" ); addDisplayText( "picid", QRect(offsetX(), offsetY()-16, width(), 16), microInfo->id() ); } else { setSize( -48, -72, 96, 144 ); removeDisplayText( "picid" ); addDisplayText( "no_file", sizeRect(), i18n("(No\nprogram\nloaded)") ); } //BEGIN Update button positions int leftpos = (width()-88)/2+offsetX(); button("run")->setOriginalRect( QRect( leftpos, height()+4+offsetY(), 20, 20 ) ); button("pause")->setOriginalRect( QRect( leftpos+23, height()+4+offsetY(), 20, 20 ) ); button("reset")->setOriginalRect( QRect( leftpos+46, height()+4+offsetY(), 20, 20 ) ); button("reload")->setOriginalRect( QRect( leftpos+69, height()+4+offsetY(), 20, 20 ) ); updateAttachedPositioning(); //END Update button positions }
DomTree::~DomTree() { removeElements(elements); removeAttributes(attributes); }