void NonCollisionHashTable::rebuild() { clear(); setSize(keys.getSize() * keys.getSize()); generateUniversalHashFunction(table.size(), primeNumber, hash); if (!keys.isEmpty()) { MyNode<int>* it = keys.getBegin(); while (it != 0) { if (!simpleInsert(it->item)) { it = keys.getBegin(); clear(); generateUniversalHashFunction(table.size(), primeNumber, hash); } else { it = it->next; } } } needRebuild = false; }
void GMLWriter::write(char* n, MyList<BCUser*> list) { ofstream outputFile; outputFile.open(n); if (outputFile.is_open()) { outputFile<< "graph [\n"; for (int i= 0; i<list.size(); i++)//gets info for each User { outputFile<<" node ["<<endl; outputFile<<" id "<<list[i]->getIdNum()<<endl; outputFile<<" name \""<<list[i]->getName()<<"\""<<endl; outputFile<<" age "<<list[i]->getAge()<<endl; outputFile<<" zip "<<list[i]->getZip()<<endl; outputFile<<" ]"<<endl; } for (int j = 0; j < list.size(); j++) { if (list[j]->getFriendId().size()!=0)//get info for each edge { for (int k=0; k<list[j]->getFriendId().size(); k++ ){ outputFile<<" edge ["<<endl; outputFile<<" source "<< list[j]->getIdNum()<<endl; outputFile<<" target "<< list[j]->getFriendId()[k]<<endl; outputFile<<" ]"<<endl; } } } outputFile<<"]"<<endl; outputFile.close(); } }
template <class Type> bool MyList<Type>::push(Type _element, const int &_position){ if(id == _position){ MyList<Type> *newElement = new MyList(_element); newElement->setID(id); newElement->setNext(this); newElement->setPrevious(previous); // std::cout<<"created\n"; if(previous!=NULL){ previous->setNext(newElement); } setPrevious(newElement); // std::cout<<"links set\n"; this->changeID(id + 1); // std::cout<<"id changed\n"; return true; } else{ if(next != NULL){ return next->push(_element, _position); } else{ return false; } } }
void Test_MyList::count() { MyList B; B.addLast(1); QCOMPARE(B.count(), 1); }
friend MyList<T> operator + (const MyList<T> &list_1, const T &item) //add an item to the end of list_1 to create a new list and return it { MyList tmplist = list_1; tmplist.push_back(item); return tmplist; }
void Test_MyList::addLast() { MyList A; A.addLast(1); QCOMPARE(A.getItem(0), 1); A.addLast(2); QCOMPARE(A.getItem(1), 2); }
int main(int argc, char** argv){ MyList<string> args = mkArgs ( argc, argv) ; return ({ MyList< int > numbers = ({ MyList < int > temp( 1,10 ); temp;}); MyList< int > squaredNumbers = (numbers.map(square)); MyList< int > evenSquaredNumbers = ( squaredNumbers.filter(even)); ( ({ int p1 = ({ cout << (squaredNumbers) << endl ; cout.good() ? 0 : 1 ; }) ; int p2 = ({ cout << (evenSquaredNumbers) << endl ; cout.good() ? 0 : 1 ; }) ;
void FixedSet::Initialize(const vector<int>& numbers) { size_t sizeOfSecondTables = 0; size_t tableSize = numbers.size(); MyList<size_t> secondTablesForRebuild; /* clock_t t1 = clock();*/ secondHashTables.resize(tableSize); // clock_t t2 = clock(); // cout << (double)(t2 - t1) / CLOCKS_PER_SEC << endl; generateUniversalHashFunction(tableSize, primeNumber, hash); for (size_t index = 0; index < tableSize; ++index) { size_t hashValue = hash(numbers[index]); if (!secondHashTables[hashValue].insert(numbers[index])) { if (!secondHashTables[hashValue].isNeedRebuild()) { secondHashTables[hashValue].setRebuild(true); secondTablesForRebuild.pushFront(hashValue); } --sizeOfSecondTables; } else { ++sizeOfSecondTables; } } MyNode<size_t>* it = secondTablesForRebuild.getBegin(); for (; it != 0 && !secondTablesForRebuild.isEmpty(); it = it->next) { size_t indexOfSecondHashTable = it->item; secondHashTables[indexOfSecondHashTable].rebuild(); size_t secondTableSize = secondHashTables[indexOfSecondHashTable].getSize(); sizeOfSecondTables += secondTableSize; } if (sizeOfSecondTables > 4 * tableSize) { for (size_t index = 0; index < tableSize; ++index) { if (!secondHashTables[index].isEmpty()) { secondHashTables[index].clear(); secondHashTables[index].deleteKeys(); secondHashTables[index].setRebuild(false); secondHashTables[index].setSize(0); secondHashTables[index].setEmpty(true); } } Initialize(numbers); } }
int main(int argc, char** argv) { MyList myList; myList.append(2); myList.insertN(1,2); myList.insertN(3,3); myList.prepend(0); myList.display(); return 0; }
friend MyList<T> operator + (const MyList<T> &l1, const T &item){ MyList<T> tmp; tmp.n = l1.n + 1; int t = tmp.get_SIZE(); while( t < tmp.n) tmp.double_space(); tmp[l1.n] = item; return tmp; }
template <class Type> void MyList<Type>::pushBack(Type _element){ size++; if(next==NULL){ MyList<Type> *newElement = new MyList(_element); newElement->setID(id + 1); newElement->setPrevious(this); next = newElement; } else{ next->pushBack(_element); } }
friend MyList operator + (const MyList &l1, const MyList &l2){ MyList<T> tmp; int i; tmp.n = l1.n + l2.n; int t = tmp.get_SIZE(); while(t < tmp.n) tmp.double_space(); for (i = 0; i < l1.n; ++i) tmp[i] = l1[i]; for (i = 0; i < l2.n; ++i) tmp[i+l1.n] = l2[i]; return tmp; }
int _tmain(int argc, _TCHAR* argv[]) { int cnt; MyList list; list.Init(); list.GetResult(); return 0; }
MyList(const MyList &l):size(l.get_size()),len(l.get_size()){ // deep copy the other MyList a = new T [size]; try{ if (a==NULL) throw (AlloFail()); } catch(AlloFail){ cerr << "Allocation failure" << endl; exit(1); } for (long i=0; i<len; ++i){ a[i] = l[i]; } }
void NonCollisionHashTable::clear() { if (!keys.isEmpty()) { MyNode<int>* it = keys.getBegin(); bool empty = table[hash(it->item)].second; for (; it != 0 && !empty; it = it->next) { empty = table[hash(it->item)].second; table[hash(it->item)] = make_pair<int, bool>(0, true); } } }
int RedBlackTree::Remove(const char * szStr, int Offset) { MyList *lst = Find(szStr) ; unsigned int hashValue = crc32(szStr) ; if (lst != NULL && lst->GetSize() > 1) { lst->Delete(Offset) ; return 1 ; } else if (lst != NULL) { Delete(m_pRoot, hashValue) ; return 1 ; } return 0; }
/* * The method takes a string of characters and for each character calculates the char frequency. * Than creates a list of nodes for binary tree * * @param charStr The character string * @return The list of nodes sorted by char frequency */ MyList<Node> createListNode(string *charStr){ MyUnordered_map<char, int> symbols; for (size_t i = 0; i < charStr->size(); i++){ symbols[charStr->at(i)] = 0; } for (size_t i = 0; i < charStr->size(); i++){ symbols[charStr->at(i)]++; } MyList<Node> listNode; for (auto it = symbols.begin(); it != symbols.end(); ++it){ Node node(it -> value, it -> key); listNode.push_back(node); } listNode.sort(); return listNode; }
int main () { //We will create a buffer of 1000 bytes to store a list managed_heap_memory heap_memory(1000); MyList * mylist = heap_memory.construct<MyList>("MyList") (heap_memory.get_segment_manager()); //Obtain handle, that identifies the list in the buffer managed_heap_memory::handle_t list_handle = heap_memory.get_handle_from_address(mylist); //Fill list until there is no more memory in the buffer try{ while(1) { mylist->insert(mylist->begin(), 0); } } catch(const bad_alloc &){ //memory is full } //Let's obtain the size of the list MyList::size_type old_size = mylist->size(); //<- (void)old_size; //-> //To make the list bigger, let's increase the heap buffer //in 1000 bytes more. heap_memory.grow(1000); //If memory has been reallocated, the old pointer is invalid, so //use previously obtained handle to find the new pointer. mylist = static_cast<MyList *> (heap_memory.get_address_from_handle(list_handle)); //Fill list until there is no more memory in the buffer try{ while(1) { mylist->insert(mylist->begin(), 0); } } catch(const bad_alloc &){ //memory is full } //Let's obtain the new size of the list MyList::size_type new_size = mylist->size(); //<- (void)new_size; //-> assert(new_size > old_size); //Destroy list heap_memory.destroy_ptr(mylist); return 0; }
int main(){ MyList<double> list; //std::cout<<"init\n"; list.pushBack(1.0); //std::cout<<"pushBack\n"; list.push(2.0, 0); list.push(3.0, 0); list.push(4.0, 0); list.push(4.0, 0); list.push(5.0, 0); list.popBack(); list.show(); list.push(6.0, 0); list.push(7.0, 0); list.push(8.0, 0); list.show(); //std::cout<<"push\n"; return 0; }
void test() { MyList<Hero> myList; ifstream file("heros.txt"); copy(istream_iterator<Hero>(file), istream_iterator<Hero>(), back_inserter(myList)); myList.sort([](const Hero &a, const Hero &b) { return a.power > b.power; }); cout << "Sorted by power:" << endl; for (auto i: myList) cout << i << endl; myList.sort([](const Hero &a, const Hero &b) { int scorea = a.strength + a.intelligence + a.power + a.charisma + a.luck; int scoreb = b.strength + b.intelligence + b.power + b.charisma + b.luck; return scorea > scoreb; }); cout << "Sorted by total score:" << endl; for (auto i: myList) cout << i << endl; }
void MyList<G>::operator=(MyList<G> new_list) { MyNode<G>* temp; clear(); temp = new_list.front(); while(temp) { push_back(temp->getData()); temp = temp->getNext(); } }
bool NonCollisionHashTable::insert(int key) { if (empty) { setSize(1); generateUniversalHashFunction(1, primeNumber, hash); empty = false; } keys.pushFront(key); return simpleInsert(key); }
CALCAMNT KStats::median(){ int index; CALCAMNT result; CALCAMNT *dp; int bound = 0; MyList list; for ( dp=data.first(); dp != 0; dp=data.next() ){ list.inSort(dp); } #ifdef DEBUG_STATS for(int l = 0; l < (int)list.count();l++){ printf("Sorted %Lg\n",*list.at(l)); } #endif bound = list.count(); if (bound == 0){ error_flag = TRUE; return 0.0; } if ( bound == 1) return *list.at(0); if( bound % 2){ // odd index = (bound - 1 ) / 2 + 1; result = *list.at(index - 1 ); } else { // even index = bound / 2; result = ((*list.at(index - 1)) + (*list.at(index)))/2; } return result; }
MyList<T> MyList<T>:: get_item(int start, int end) { MyList<T> b; try{ if (start < 0) start = n + start; if (end < 0) end = n + end; if (start > end) return b; if (start > n || end > n || start < 0 || end < 0) throw 1; int i; b.n = end - start + 1; while (size < b.n) b.double_space(); for (i = start; i <= end; ++i ) b.a[i-start] = a[i]; return b; } catch(int){ cout << "List index out of range" << endl; } }
void main() { cout<<"hello myList:"<<endl; MyList list; cout<<"定义一个链表对象。"<<endl; cout<<"删除链表首元素"<<endl; list.removeFirst(); int a = 1; cout<<"加入元素1"<<endl; list.add(a); a = 2; cout<<"加入元素2"<<endl; list.add(a); a = 3; cout<<"加入元素3"<<endl; list.add(a); cout<<"长度:"<<list.length<<endl; cout<<"用length从头察看链表元素结果如下:"<<endl; MyListNode* p = list.first; for(int i = 0; i < list.length; i ++) { cout<<p->element<<','; p = p->next; } cout<<endl; cout<<"用最后一个元素是否为空从头察看链表元素结果如下:"<<endl; for(p = list.first; p != NULL; p = p->next) { cout<<p->element<<','; } cout<<endl; cout<<"开始去掉首元素"<<endl; list.removeFirst(); cout<<"长度:"<<list.length<<endl; cout<<"开始去掉所有元素"<<endl; //for(int j = 0; j < list.length; j ++)//注意length也是动态变化的 while(list.length != 0) { list.removeFirst(); } cout<<"长度:"<<list.length<<endl; cout<<"再删除链表首元素"<<endl; list.removeFirst(); cout<<"测试完毕!"<<endl; }
bool NonCollisionHashTable::simpleInsert(int key) { if (!keys.isEmpty()) { size_t hashValue = hash(key); if (table[hashValue].second == false) { return false; } else { table[hashValue] = make_pair(key, false); } } return true; }
int main() { MyList<int> ml; int choose; int pos, entry; while (1) { cout << "1.insert 2.remove 3. replace 4.size 5.empty 6.retrive 7.display" << endl; cin >> choose; switch (choose) { case 1: cin >> pos >> entry; ml.insert(pos, entry); break; case 2: cin >> pos; ml.remove(pos, entry); cout << entry << endl; break; case 3: cin >> pos >> entry; ml.replace(pos, entry); break; case 4: cout << ml.size() << endl; break; case 5: cout << ml.empty() << endl; break; case 6: cin >> pos; ml.retrieve(pos, entry); cout << entry << endl; break; case 7: ml.display(); } } }
int main() { MyList<int> list; list.insert(1); list.insert(2); list.insert(3); list.insert(4); list.insert(5); list.insert(6); list.insert(7); list.insert(8); list.traverse(print_int); cout << endl; cout << "reverse list..." << endl; //list.reverse(); list.traverse(print_int); cout << endl; }
int main() { MyList list; list.push_back(3); list.push_back(2); list.push_back(10); list.push_back(5); list.push_back(4); cout << "Note: The program did not specify means of input. As such, the program makes a list of 5 integers" << " as seen above this line in the code and sorts them.\n" << endl; cout << "Before sort: " << list.toString(); cout << endl; list.sort(); cout << "After Sort: "<< list.toString() << endl; return 0; }
int main(){ MyList r; // Insert Data r.insertAfter(3); r.insertAfter(5); //r.goHead(); r.insertAfter(9); //r.goNext(); r.insertAfter(8); //r.remove(); // Display Data //r.goHead(); while (!r.end()) { cout << r.get().getData() << ' '; r.goNext(); } cout << endl; system("pause"); return 0; }