void SingleList::addHead(int value){ ISingleNode* newSingleNode = new SingleNode(); newSingleNode->setValue(value); newSingleNode->setNext(Head); Head = newSingleNode; }
void SingleList::printlist(){ ISingleNode *start = this->head; while(start!=NULL){ cout << "\nPrinting list item "<< start->getValue() << "\t" ; start = start->getNext(); } cout <<"End \n"; }
void SingleList::addHead(int value){ count++; ISingleNode *node = new SingleNode(); node->setValue(value); if(this->head == NULL) node->setNext(NULL); else node->setNext(this->head); this->setHead(node); }
void SingleList::sequenceOrderSort(){ ISingleNode * first = Head, * second = first->getNext(); int count = 0; first->setNext(second->getNext()); second->setNext(first); Head = second; count = 3; ISingleNode * curr = first, * next = first->getNext(); second = next; //print(); while (next != NULL) { for (int i = 1; i < count && second->getNext() != NULL; i++){ second = second->getNext(); } ISingleNode * temp = NULL, * temp1 = NULL; first = next; temp = first; next = first->getNext(); temp1 = next->getNext(); first->setNext(second->getNext()); while (first != second){ next->setNext(first); first = next; next = temp1; if (next) temp1 = next->getNext(); } curr->setNext(first); curr = temp; second = temp->getNext(); count++; } //print(); }
void SingleList::orderSort2(){ ISingleNode * first = Head, * second = Head->getNext(); if (first == NULL || first->getNext() == NULL){ return; } Head = second; while (true){ ISingleNode * temp; temp = second->getNext(); second->setNext(first); if (temp == NULL || temp->getNext() == NULL){ first->setNext(temp); break; } first->setNext(temp->getNext()); first = temp; second = first->getNext(); } }
void SingleList::rearrange(int i, ISingleNode*& prev, ISingleNode*& base){ /* if(prev == NULL) cout<< "i is "<< i << " Null prev is "<< prev << " base is" << base->getValue() << "\n"; else cout<< "i is "<< i << " prev is "<< prev->getValue() << " base is" << base->getValue() << "\n"; */ if(base == NULL) return; ISingleNode *saved_base = base; ISingleNode *end = base; ISingleNode *new_prev = NULL; ISingleNode *p = NULL; int j = i -1; int k = 0; while( end!=NULL && j!=0){ k++; p =end; end = end->getNext(); if(end == NULL){ if(j!=0){ end = p; i = k; break; } else{ base = end; return; } } j--; } new_prev = end; ISingleNode *final_base ; if(end == NULL) final_base = end; else final_base = end->getNext(); /* if(final_base!=NULL) cout <<"\n final base"<<final_base->getValue(); else cout <<"\n NULL final base"<<final_base; */ ISingleNode *new_base = end; ISingleNode *deleted_node; ISingleNode *to_del = base; j = i-1; while(j!=0){ new_base = base->getNext(); deleted_node = this->delete_node(prev, base); //cout<< "\n After delete: \n"; //this->printlist(); this->insert_node(end, deleted_node); //cout<< "\n After insert: \n"; //this->printlist(); //end = end->getNext(); base = new_base; j--; } base = final_base; prev = saved_base; }
void SingleList::orderSort3(){ ISingleNode * first = Head, * second = Head->getNext(), * third = Head->getNext()->getNext(); if (Head == NULL || Head->getNext() == NULL || Head->getNext()->getNext() == NULL){ return; } Head = third; while (true){ ISingleNode * temp; temp = third->getNext(); second->setNext(first); third->setNext(second); if (temp == NULL || temp->getNext() == NULL){ first->setNext(temp); break; } if (temp == NULL || temp->getNext() == NULL || temp->getNext()->getNext() == NULL){ first->setNext(temp->getNext()); first->getNext()->setNext(temp); break; } first->setNext(temp->getNext()->getNext()); first = temp; second = first->getNext(); third = second->getNext(); } }