void insert(iterator pos, T const& value) { if (pos == begin()) { push_front(value); } else if (pos == end()) { push_back(value); } else { ListNode <T>* insertNode = new ListNode<T> {value, pos.prev().m_node, pos.m_node}; pos.prev().m_node -> m_next = insertNode; pos.m_node -> m_prev = insertNode; ++m_size; } }
void link_components(Parent1 component, Parent2 header, Integer num_nodes, Integer num_components) { // Make the non-representative vertices point to their component Parent1 representative = component; for (Integer v = 0; v != num_nodes; ++v) if (component[v] >= num_components || header[component[v]] != v) component[v] = component[representative[v]]; // initialize the "head" of the lists to "NULL" std::fill_n(header, num_components, num_nodes); // Add each vertex to the linked list for its component Parent1 next = component; for (Integer k = 0; k != num_nodes; ++k) push_front(next, header[component[k]], k); }
int main() { int choice, input; front = rear = MAX/2; while(1) { printf("\n1. Insert Front\n2. Insert Back\n3. Delete Front\n4. Delete Back\n5. Display\n6. Exit\n"); printf("Enter Your Choice: "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter valued to insert front:\n"); scanf("%d", &input); push_front(input); break; case 2: printf("Enter valued to insert back:\n"); scanf("%d", &input); push_back(input); break; case 3: printf("Valued to deleted front: %d\n", pop_front()); break; case 4: printf("Valued to deleted back: %d\n", pop_back()); break; case 5: display(); break; case 6: return 0; default: printf("Remember this program is created by Ashish Gaikwad\n"); } } return 0; }
TListIterator insert(TListIterator position, const T &value) { if (position == begin()) { push_front(value); return begin(); } if (position == end()) { push_back(value); return TListIterator(end_); } TNode<T> *curNode = position.getNode(); TNode<T> *prevNode = curNode->prev_; TNode<T>* newNode = createNode(value); newNode->prev_ = prevNode; newNode->next_ = curNode; prevNode->next_ = newNode; curNode->prev_ = newNode; ++size_; return TListIterator(newNode); }
void init_object(t_env *raytracer, const char *file) { int elem; char *tmp; int i; elem = xopen(file, O_RDONLY); while ((tmp = get_next_line(elem))) { i = -1; while (++i < MAX && my_strncmp(tmp, functions[i].object_name, my_strlen(functions[i].object_name)) != 1); if (i < MAX) push_front(&(raytracer->objects[i]), (*(functions[i].init_func))(tmp)); else if (my_strncmp(tmp, "camera", 6) != 0) init_cam(&raytracer->camera, tmp); else if (my_strncmp(tmp, "plan", 4) != 0) init_plan(&raytracer->plan, tmp); printf("%s\n", tmp); } }
void reverse() { auto anchor = _handle; if (!anchor || !anchor->next) { return; } // Node to be moved decltype(anchor) temp = anchor->next; // Terminate the first node anchor->next = nullptr; while(temp) { // Next node to be moved anchor = temp->next; // Move it push_front(temp); // Onto the next temp = anchor; } }
void Run() { int i; while(1) { menu(); printf("선택:"); fflush(stdin); scanf("%d",&i); switch(i) { case 1:push_front();break; case 2:printall();break; case 3:search();break; case 4:change();break; case 5:dele();break; default:return; } } }
bool DiameterSessionEntryList::Add(DiameterSessionId &id, AAA_JobData &data) { DiameterSessionEntry *newEntry = new DiameterSessionEntry(data); if (newEntry) { DiameterSessionCounter nullCounter, *idCounter = &id; if (*idCounter == nullCounter) { ++ m_LastKnownCounter; *((DiameterSessionCounter*)newEntry) = m_LastKnownCounter; id.High() = newEntry->High(); id.Low() = newEntry->Low(); } else { newEntry->High() = id.High(); newEntry->Low() = id.Low(); } newEntry->Data() = data; push_front(newEntry); return true; } return false; }
void LinkedList::insert_at(int pos, int data) { size_t getSize = size(); if (pos <= 0 || getSize == 0) { push_front(data); } else if (static_cast<int>(getSize) <= pos) { push_back(data); } else { Node * curr = head; for (int num = 1; num < pos && curr->next != 0; ++num) { curr = curr->next; } Node *new_node = new Node(data); new_node->next = curr->next; curr->next = new_node; } }
void PacketQueue::insertSorted( const PacketData & p, quint32 max_sequence ){ if ( empty() ){ push_back( p ); } else { if ( !sequenceMoreRecent( p.sequence, front().sequence, max_sequence ) ){ push_front( p ); } else if ( sequenceMoreRecent( p.sequence, back().sequence, max_sequence ) ){ push_back( p ); } else { for ( PacketQueue::iterator itor = begin(); itor != end(); itor++ ){ //assert( itor->sequence != p.sequence ); if ( sequenceMoreRecent( itor->sequence, p.sequence, max_sequence ) ){ insert( itor, p ); break; } } } } }
bool list<T>::insert(T const& data, int index){ if(empty() && index == 0){ push_front(data); return true; } else if(index > numElements || index < 0 || (empty() && index != 0)){ return false; } node *aux = begin.next; node *new_node = new node; new_node->data = data; for(int i = 0; i < index; aux = aux->next, i++); new_node->next = aux; new_node->previous = aux->previous; new_node->previous->next = new_node; aux->previous = new_node; numElements++; return true; }
void insert(lista *l, int pozycja, int wartosc) { int i; element *wsk; element *nowy=(element*)malloc(sizeof(element)); if(pozycja==1) push_front(l, wartosc); else { nowy->wartosc=wartosc; wsk=l->poczatek; for(i=1; i<pozycja-1; i++) wsk=wsk->nastepny; nowy->nastepny=wsk->nastepny; nowy->poprzedni=wsk; wsk->nastepny=nowy; wsk->nastepny->poprzedni=nowy; (l->rozmiar)++; } }
int main() { srand(time(NULL)); deque* d1 = create_deque(int_cmp); for(uint i = 0; i < 10; ++i) { int* elem = (int*) s_malloc(sizeof(int)); *elem = (rand() % 100) + 1; push_back(d1, elem); } display_deque_int(d1); printf("\n\nDeque 2 with pushfront\n\n"); deque* d2 = create_deque(int_cmp); for(uint i = 0; i < 10; ++i) { int* elem = (int*) s_malloc(sizeof(int)); *elem = (rand() % 100) + 1; push_front(d2, elem); } display_deque_int(d2); reorder_deque(d1, sizeof(int)); display_deque_int(d1); free_deque_front(d1, s_free); free_deque_back(d2, s_free); deque* d3 = create_deque(str_cmp); char* s1 = strdup("Jessica"); char* s2 = strdup("Melanie"); char* s3 = strdup("Batuhan"); char* s4 = strdup("Graves"); push_back(d3, s1); push_back(d3, s2); push_back(d3, s3); push_back(d3, s4); printf("\n\nDeque 3\n\n"); display_deque_str(d3); free_deque_front(d3, s_free); return 0; }
int main() { int TC; scanf("%d",&TC); char perintah[12]; int a; while (TC--) { scanf("%s",&perintah); // Bagian Memasukkan Data if(strcmp (perintah, "push_back") == 0) { scanf("%d",&value); push_back(value); } else if(strcmp (perintah, "push_front") == 0) { scanf("%d",&value); push_front(value); } // Bagian mengeluarkan Data else if(strcmp (perintah, "pop_front") == 0) { pop_front(); } else if(strcmp (perintah, "pop_back") == 0) { pop_back(); } } for(a = head; a <= tail; a++) printf("%d\n",Data[a]); return 0; }
typename List_doubly_linked<type>::iterator List_doubly_linked<type>::insert (typename List_doubly_linked<type>::iterator position, const type &x){ if(position.get_node_ptr() == sentinel_head.get_next_ptr()){ /* Inserting at the head of the list. */ push_front(x); return this->begin(); } else if(position.get_node_ptr() == &sentinel_tail){ /* Inserting at the end of the list. */ push_back(x); return iterator(sentinel_tail.get_prev_ptr()); } else{ /* Inserting at the middle of the list. */ Node<type> *node = new Node<type>(x); Node<type> *temp = position.get_node_ptr()->get_prev_ptr(); temp->set_next_ptr(node); node->set_prev_ptr(temp); position.get_node_ptr()->set_prev_ptr(node); node->set_next_ptr(position.get_node_ptr()); ++ list_size; return iterator(node); } }
void test_linked_list() { struct linked_list *list = new_list(); assert(NULL == list->head); assert(0 == list_size(list)); assert(NULL == front(list)); struct file f; strncpy(f.fname, "TEST", 65); strncpy(f.owner, "Tester", 33); f.size = 128; struct node *node = new_node(f); push_front(node, list); display_list(list); struct file ff; strncpy(ff.fname, "TEST2", 65); strncpy(ff.owner, "Tester2", 33); ff.size = 128; struct node *node2 = new_node(ff); push_back(node2, list); display_list(list); pop_back(list); pop_front(list); display_list(list); delete_list(list); }
inbox_result emplace_front(Ts&&... xs) { return push_front(new value_type(std::forward<Ts>(xs)...)); }
RRC_status_t rrc_rx_tx(uint8_t Mod_id, const frame_t frameP, const eNB_flag_t eNB_flagP,uint8_t index,int CC_id){ uint8_t UE_id; int32_t current_timestamp_ms, ref_timestamp_ms; struct timeval ts; if(eNB_flagP == 0) { // check timers if (UE_rrc_inst[Mod_id].Info[index].T300_active == 1) { if ((UE_rrc_inst[Mod_id].Info[index].T300_cnt % 10) == 0) LOG_D(RRC, "[UE %d][RAPROC] Frame %d T300 Count %d ms\n", Mod_id, frameP, UE_rrc_inst[Mod_id].Info[index].T300_cnt); if (UE_rrc_inst[Mod_id].Info[index].T300_cnt == T300[UE_rrc_inst[Mod_id].sib2[index]->ue_TimersAndConstants.t300]) { UE_rrc_inst[Mod_id].Info[index].T300_active = 0; // ALLOW CCCH to be used UE_rrc_inst[Mod_id].Srb0[index].Tx_buffer.payload_size = 0; rrc_ue_generate_RRCConnectionRequest (Mod_id, frameP, index); return (RRC_ConnSetup_failed); } UE_rrc_inst[Mod_id].Info[index].T300_cnt++; } if (UE_rrc_inst[Mod_id].sib2[index]) { if (UE_rrc_inst[Mod_id].Info[index].N310_cnt == N310[UE_rrc_inst[Mod_id].sib2[index]->ue_TimersAndConstants.n310]) { UE_rrc_inst[Mod_id].Info[index].T310_active = 1; } } else { // in case we have not received SIB2 yet if (UE_rrc_inst[Mod_id].Info[index].N310_cnt == 100) { UE_rrc_inst[Mod_id].Info[index].N310_cnt = 0; return RRC_PHY_RESYNCH; } } if (UE_rrc_inst[Mod_id].Info[index].T310_active == 1) { if (UE_rrc_inst[Mod_id].Info[index].N311_cnt == N311[UE_rrc_inst[Mod_id].sib2[index]->ue_TimersAndConstants.n311]) { UE_rrc_inst[Mod_id].Info[index].T310_active = 0; UE_rrc_inst[Mod_id].Info[index].N311_cnt = 0; } if ((UE_rrc_inst[Mod_id].Info[index].T310_cnt % 10) == 0) LOG_D(RRC, "[UE %d] Frame %d T310 Count %d ms\n", Mod_id, frameP, UE_rrc_inst[Mod_id].Info[index].T310_cnt); if (UE_rrc_inst[Mod_id].Info[index].T310_cnt == T310[UE_rrc_inst[Mod_id].sib2[index]->ue_TimersAndConstants.t310]) { UE_rrc_inst[Mod_id].Info[index].T310_active = 0; rrc_t310_expiration (frameP, Mod_id, index); return (RRC_PHY_RESYNCH); } UE_rrc_inst[Mod_id].Info[index].T310_cnt++; } if (UE_rrc_inst[Mod_id].Info[index].T304_active==1) { if ((UE_rrc_inst[Mod_id].Info[index].T304_cnt % 10) == 0) LOG_D(RRC,"[UE %d][RAPROC] Frame %d T304 Count %d ms\n",Mod_id,frameP, UE_rrc_inst[Mod_id].Info[index].T304_cnt); if (UE_rrc_inst[Mod_id].Info[index].T304_cnt == 0) { UE_rrc_inst[Mod_id].Info[index].T304_active = 0; UE_rrc_inst[Mod_id].HandoverInfoUe.measFlag = 1; LOG_E(RRC,"[UE %d] Handover failure..initiating connection re-establishment procedure... \n"); //Implement 36.331, section 5.3.5.6 here return(RRC_Handover_failed); } UE_rrc_inst[Mod_id].Info[index].T304_cnt--; } // Layer 3 filtering of RRC measurements if (UE_rrc_inst[Mod_id].QuantityConfig[0] != NULL) { ue_meas_filtering(Mod_id,frameP,index); } ue_measurement_report_triggering(Mod_id,frameP,index); if (UE_rrc_inst[Mod_id].Info[0].handoverTarget > 0) LOG_I(RRC,"[UE %d] Frame %d : RRC handover initiated\n", Mod_id, frameP); if((UE_rrc_inst[Mod_id].Info[index].State == RRC_HO_EXECUTION) && (UE_rrc_inst[Mod_id].HandoverInfoUe.targetCellId != 0xFF)) { UE_rrc_inst[Mod_id].Info[index].State= RRC_IDLE; return(RRC_HO_STARTED); } } else { // eNB check_handovers(Mod_id,frameP); // counetr, and get the value and aggregate #ifdef LOCALIZATION /* for the localization, only primary CC_id might be relevant*/ gettimeofday(&ts, NULL); current_timestamp_ms = ts.tv_sec * 1000 + ts.tv_usec / 1000; ref_timestamp_ms = eNB_rrc_inst[Mod_id].reference_timestamp_ms; for (UE_id=0; UE_id < NUMBER_OF_UE_MAX; UE_id++) { if ((current_timestamp_ms - ref_timestamp_ms > eNB_rrc_inst[Mod_id].aggregation_period_ms) && rrc_get_estimated_ue_distance(Mod_id,frameP,UE_id, CC_id,eNB_rrc_inst[Mod_id].loc_type) != -1) { LOG_D(LOCALIZE, " RRC [UE/id %d -> eNB/id %d] timestamp %d frame %d estimated r = %f\n", UE_id, Mod_id, current_timestamp_ms, frameP, rrc_get_estimated_ue_distance(Mod_id,frameP,UE_id, CC_id,eNB_rrc_inst[Mod_id].loc_type)); LOG_D(LOCALIZE, " RRC status %d\n", eNB_rrc_inst[Mod_id].Info.UE[UE_id].Status); push_front(&eNB_rrc_inst[Mod_id].loc_list, rrc_get_estimated_ue_distance(Mod_id,frameP,UE_id, CC_id,eNB_rrc_inst[Mod_id].loc_type)); eNB_rrc_inst[Mod_id].reference_timestamp_ms = current_timestamp_ms; } } #endif } return (RRC_OK); }
void BDDElement::ajouterAttribut(QString attribut) { push_front(attribut); }
//! compute the approximation of Y[i]@X[i] inline T compute(const expand<T> &xp, const unit_t i, const array<T> &X, const array<T> &Y, const unit_t N, T *dYdX=0) { assert(N>0); assert(X.size()==Y.size()); assert(unit_t(X.size())==N); //______________________________________________________________ // // prepare with central point //______________________________________________________________ const T Xc = xp.get_x(i,X,N); const T Yc = xp.get_y(i,Y,N); points.free(); push_back(0,Yc); //______________________________________________________________ // // add next points //______________________________________________________________ { const T Xup = Xc + max_of<T>(0,upper_range); unit_t iup = i; size_t cnt = 0; while(true) { const T Xi=xp.get_x(++iup,X,N); if(Xi>Xup&&cnt>0) break; ++cnt; push_back(Xi-Xc,xp.get_y(iup,Y,N)); } } //______________________________________________________________ // // add prev points //______________________________________________________________ { const T Xdn = Xc - max_of<T>(0,lower_range); unit_t idn = i; size_t cnt = 0; while(true) { const T Xi=xp.get_x(--idn,X,N); if(Xi<Xdn&&cnt>0) break; ++cnt; push_front(Xi-Xc,xp.get_y(idn,Y,N)); } } //______________________________________________________________ // // use local fit //______________________________________________________________ polynomial(); if(dYdX) { *dYdX = coeff[2]; } return coeff[1]; }
void main() { SeqList mylist; InitSeqList(&mylist); ElemType Item; int pos; int select = 1; while(select) { printf("**********************************************\n"); printf("** [1] push_back [2] push_front **\n"); printf("** [3] pop_back [4] pop_front **\n"); printf("** [5] insert_pos [6] show_list **\n"); printf("** [7] del_pos [8] del_val **\n"); printf("** [9] find_pos [10]find_Elem **\n"); printf("** [11]sort [12]resver **\n"); printf("** [13]clear [14]merge **\n"); printf("** [15]destroy [0]quit_system **\n"); printf("**********************************************\n"); printf("请选择:>"); scanf("%d",&select); if(select == 0) { break; } switch(select) { case 1: { printf("请输入要插入的数据(-1结束):>"); while(scanf("%d",&Item),Item!=-1) //此处为逗号表达式,真假值由最后一个表达式决定, //所以此处在扫描到Item不是-1时就会自动执行一下下面的函数, //所以每输入一次非-1的数就头插一次。 { push_back(&mylist,Item); } break; } case 2: { printf("请输入要插入的数据(-1结束):>"); while(scanf("%d",&Item),Item!=-1) //此处为逗号表达式,真假值由最后一个表达式决定, //所以此处在扫描到Item不是-1时就会自动执行一下下面的函数, //所以每输入一次非-1的数就头插一次。 { push_front(&mylist,Item); } break; } case 3: { pop_back(&mylist); break; } case 4: { pop_front(&mylist); break; } case 5: { ElemType e; printf("请输入要插入的位置,当前有元素%d:",mylist.size); scanf("%d",&pos); printf("请输入要插入的元素值:"); scanf("%d",&e); insert_pos(&mylist,pos,e); break; } case 6: { show_list(&mylist); break; } case 7: { printf("请输入要删除的位置,当前有元素%d:",mylist.size); scanf("%d",&pos); del_pos(&mylist,pos); break; } case 8: { printf("请输入要删除的元素:"); scanf("%d",&Item); printf("start and Item=%d\n",Item); del_val(&mylist,Item); break; } case 9: { printf("请输入要查找线性表元素的位置:"); scanf("%d",&pos); find_pos(&mylist,pos,&Item); printf("线性表的第%d个元素为:%d\n",pos,Item); break; } case 10: { printf("请输入要查找的元素:"); scanf("%d",&Item); find_Elem(&mylist,Item,&pos); printf("元素%d所在位置为:%d\n",Item,pos); break; } case 11: { sort(&mylist); break; } case 12: { printf("resver start.\n"); resver(&mylist); printf("resver end!\n"); break; } case 13: { clear(&mylist); break; } case 14: { printf("目前无法测试,因为只有一个线性表,无法做合并操作。\n"); //merge(SeqList *list1,SeqList *list2,SeqList *list_merge); break; } case 15: { destroy(&mylist); break; } default: { printf("输入的选择不存在,请重新输入。\n"); select=1; break; } } } }
void wrap(std::string address, boost::optional<std::string> delim) { if (delim) push_front(*delim); push_front(address); }
bool push_front(const T & data) { return push_front(reinterpret_cast<unsigned char*>(&data), sizeof(T)); }
/* This main function does a little testing Like all good CS Majors you should test your code here. There is no substitute for testing and you should be sure to test for all edge cases e.g., calling remove_front on an empty list. */ int main(void) { /* Now to make use of all of this stuff */ list* llist = create_list(); /* What does an empty list contain? Lets use our handy traversal function */ printf("TEST CASE 1\nAn Empty list should print nothing here:\n"); traverse(llist, print_person); printf("\n"); /* Lets add a person from front and then print */ push_front(llist, create_person("Andrew", 24)); printf("TEST CASE 2\nA List with one person should print that person:\n"); traverse(llist, print_person); printf("\n"); /* Lets remove two persons from front and then print */ remove_front(llist, free_person); // remove a list with more than 1 elements remove_front(llist, free_person); // remove a list with only 1 element printf("TEST CASE 3\nAnother Empty list should print nothing here:\n"); traverse(llist, print_person); printf("\n"); /* Lets add two people and then print */ push_front(llist, create_person("Nick", 22)); push_front(llist, create_person("Randal", 21)); printf("TEST CASE 4\nA List with two people should print those two people:\n"); traverse(llist, print_person); printf("\n"); /* Lets copy this list */ list* llist2 = copy_list(llist, copy_person); printf("TEST CASE 5\nA copied list should print out the same two people:\n"); traverse(llist2, print_person); printf("\n"); /* Lets kill the list */ empty_list(llist, free_person); printf("TEST CASE 6\nAfter freeing all nodes the list should be empty:\n"); traverse(llist, print_person); printf("\n"); /* Let's make a list of people, and remove certain ones! */ /* Should remove anyone whose name is 8+ characters long */ push_front(llist, create_person("Josephine", 27)); push_front(llist, create_person("Dave", 34)); push_front(llist, create_person("Benjamin", 23)); push_front(llist, create_person("Lisa", 41)); push_front(llist, create_person("Maximilian", 24)); remove_if(llist, long_name, free_person); printf("TEST CASE 7\nShould only print 2 people with short names:\n"); traverse(llist, print_person); printf("\n"); /* Testing over clean up*/ empty_list(llist, free_person); free(llist); empty_list(llist2, free_person); free(llist2); // MY TESTS!!! // Test case 8 -- create_list(); is_empty() and size() when the list is empty list* myList = create_list(); printf("TEST CASE 8\nShould print 1 and then 0:\n"); printf("%d\t", is_empty(myList)); printf("%d\n", size(myList)); printf("\n"); // Test case 9 -- front() and back() when the list is empty printf("TEST CASE 9\nShould print nothing:\n"); print_person(front(myList)); print_person(back(myList)); printf("\n"); // Test case 10 -- push_front() and push_back() and traverse() push_front(myList, create_person("Dan", 24)); push_back(myList, create_person("Sun", 24)); push_front(myList, create_person("Someone", 100)); push_back(myList, create_person("Somebody", 1)); printf("TEST CASE 10\nShould print 4 people in the order of Someone-Dan-Sun-Somebody:\n"); traverse(myList, print_person); printf("\n"); // Test case 11 -- size() and is_empty() when the list is not empty printf("TEST CASE 11\nShould return 0 and then 4:\n"); printf("%d\t", is_empty(myList)); printf("%d\n", size(myList)); printf("\n"); // Test case 12 -- front() and back() when the list is not empty printf("TEST CASE 12\nShould print Someone then Somebody:\n"); print_person(front(myList)); print_person(back(myList)); printf("\n"); // Test case 13 -- copy_list() list* myListCopy = copy_list(myList, copy_person); printf("TEST CASE 13\nA copied list should print 4 people in the order of Someone-Dan-Sun-Somebody:\n"); traverse(myListCopy, print_person); printf("\n"); // Test case 14 -- remove_front() and remove_back() when the list is not empty remove_front(myList, free_person); remove_back(myList, free_person); printf("TEST CASE 13\nShould print 2 people in the order of Dan-Sun:\n"); traverse(myList, print_person); printf("\n"); // Test case 15 -- remove_if() push_front(myList, create_person("LLLLLLLLLLLLL", 1)); push_front(myList, create_person("MMMMMMMMM", 1)); push_front(myList, create_person("AAA", 3)); push_back(myList, create_person("DDD", 5)); push_back(myList, create_person("T", 10)); push_back(myList, create_person("VVVVVVVVVV", 1)); remove_if(myList, long_name, free_person); printf("TEST CASE 13\nShould print 5 people:\n"); traverse(myList, print_person); printf("\n"); // Test case 16 -- empty_list() empty_list(myList, free_person); printf("TEST CASE 16\nShould print nothing:\n"); traverse(myList, print_person); printf("\n"); // Test case 17 -- remove_front() and remove_back() when the list is empty remove_front(myList, free_person); remove_back(myList, free_person); printf("TEST CASE 17\nNo error should occur: \n"); printf("\n"); // Test case 18 -- push big data into the copied list to test push_front() and copy_list() for (int i = 0; i < 1000000; i++) { push_front(myListCopy, create_person("BIG", 1)); } printf("TEST CASE 18\nShould print 1000004 (add a large number of data): \n"); printf("%d\n", size(myListCopy)); printf("\n"); empty_list(myList, free_person); free(myList); empty_list(myListCopy, free_person); free(myListCopy); return 0; }
int main(void) { Vector object; Vector *v_copy = NULL; VectorIter *ptr; unsigned int x, y; unsigned int value; memset(&object, 0, sizeof (object)); x = RUNS; construct(Vector, &object,sizeof(x),FREEOBJ); set_alloc(Vector, &object, ckalloc); set_dealloc(Vector, &object, ckfree); set_compare(Vector, &object, intcmp); set_print(Vector, &object, print); for (y = 0; y < x; y++) { srand((x * y) / (x - y) + (x + y / x)); switch ((rand() % NUMCASES) + BASE) { case 0: case 10: case 11: case 1: case 12: case 13: case 2: case 14: case 3: case 15: case 4: value = rand() % BOUND; push_front(Vector, &object, &value, DYNAMIC); break; /*pop_front(Vector, &object); break;*/ case 5: case 6: case 16: /*pop_back(Vector, &object); break;*/ case 7: case 17: back(Vector, &object); case 8: case 9: front(Vector, &object); value = rand() % BOUND; push_back(Vector, &object, &value, DYNAMIC); break; default: break; }; } destruct(Vector, &object); fprintf(stderr, "Now testing the iterators!\n"); construct_func(Vector,&object,sizeof(x),FREEOBJ, ckalloc, ckfree, intcmp, print, memcpy); for (x = 0; x < RUNS; x++) { value = rand() % RUNS; push_back(Vector, &object, &value, STATIC); } ptr = create(VectorIter, &object); head(VectorIter, ptr); /*printf("Head = %d\n",*(unsigned int *)retrieve(VectorIter,ptr));*/ tail(VectorIter,ptr); /*printf("Tail = %d\n",*(unsigned int *)retrieve(VectorIter,ptr));*/ head(VectorIter,ptr); do { value = *(unsigned int *) retrieve(VectorIter, ptr); /*printf("%d ",value);*/ } while (!next(VectorIter, ptr)); assign(VectorIter, ptr, &object); tail(VectorIter, ptr); do { value = *(unsigned int *) retrieve(VectorIter, ptr); } while (!prev(VectorIter, ptr)); /*printf("\n");*/ for(x = 0; x < RUNS; x++) { switch(rand() % 2) { case 1: next(VectorIter,ptr); break; case 2: prev(VectorIter,ptr); break; } } destroy(VectorIter, ptr); v_copy = duplicate(Vector,&object); ptr = create(VectorIter,v_copy); do{ value = *(unsigned int *)retrieve(VectorIter,ptr); /*printf("%d ",value);*/ }while(!next(VectorIter,ptr)); /*printf("\n");*/ destroy(VectorIter,ptr); destruct(Vector,v_copy); destruct(Vector,&object); free(v_copy); return EXIT_SUCCESS; }
int main(void) { int wartosc; int pozycja; int flaga=1; int wyb; lista *test=stworz(); while(flaga==1) { druk_menu(999); scanf("%d", &wyb); system("cls"); druk_menu(wyb); switch(wyb) { case 1: printf("podaj wartosc: "); scanf("%d", &wartosc); push_front(test, wartosc); printf("umieszczono na liscie\n"); break; case 2: printf("podaj wartosc: "); scanf("%d", &wartosc); push_back(test, wartosc); printf("umieszczono na liscie\n"); break; case 3: printf("podaj wartosc: "); scanf("%d", &wartosc); printf("podaj pozycja: "); scanf("%d", &pozycja); if(pozycja<=test->rozmiar && pozycja>0) { insert(test, pozycja, wartosc); printf("umieszczono na liscie\n"); } else printf("pozycja ktora wybrales jest niewlasciwa\nlub na liscie jesty tylko jeden element\n"); break; case 4: if(isEmpty(test)) printf("lista jest pusta\n"); else { printf("twoja liczba to: %d\n", pop_front(test)); } break; case 5: if(isEmpty(test)) printf("lista jest pusta\n"); else { printf("twoja liczba to: %d\n", pop_back(test)); } break; case 6: if(isEmpty(test)) printf("lista jest pusta\n"); else { printf("podaj pozycje: "); scanf("%d", &pozycja); if(pozycja<=test->rozmiar && pozycja>0) printf("twoja liczba to: %d\n", remove_l(test, pozycja)); else printf("pozycja ktora wybrales jest niewlasciwa\n"); } break; case 7: if(isEmpty(test)) printf("lista jest pusta\n"); else { printf("podaj pozycje: "); printf("twoja liczba to: %d\n", front(test)); } break; case 8: if(isEmpty(test)) printf("lista jest pusta\n"); else { printf("podaj pozycje: "); printf("twoja liczba to: %d\n", back(test)); } break; case 9: if(isEmpty(test)) printf("lista jest pusta\n"); else { printf("podaj pozycje: "); scanf("%d", &pozycja); if(pozycja<=test->rozmiar && pozycja>0) printf("twoja liczba to: %d\n", at(test, pozycja)); else printf("pozycja ktora wybrales jest niewlasciwa\n"); } break; case 10: clear(test); printf("lista wyczyszczona\n"); break; case 11: printf("rozmiar listo wynosi: %d\n", size(test)); break; case 12: if(isEmpty(test)) printf("lista jest pusta\n"); else printf("lista posiada elementy\n"); break; case 13: if(isEmpty(test)) printf("lista jest pusta\n"); else printf("adres pierwszego elementu to %p\n", begin(test)); break; case 14: if(isEmpty(test)) printf("lista jest pusta\n"); else printf("adres ostatniego elementu to %p\n", end(test)); break; case 15: if(isEmpty(test)) printf("lista jest pusta\n"); else print_forward(test); break; case 16: if(isEmpty(test)) printf("lista jest pusta\n"); else print_backward(test); break; case 0: flaga=0; break; default: printf("wybrales zle\nsprobuj ponownie\n"); break; } printf("\n> > > potwierdz enterem < < <\n"); fflush(stdin); getchar(); system("cls"); } destroy(test); return 0; }
/* 测试程序: */ int main() { int i; init(); #if 1 // push_front test: puts("push_front test:"); for(i=0; i<MAXN+2; i++) { push_front(2*i+1); show(); } puts("pop_front test:"); for(i=0; i<MAXN+2; i++) { pop_front(); show(); } #endif #if 1 // push_back test: puts("push_back test:"); for(i=0; i<MAXN+2; i++) { push_back((i+1)*10); show(); } puts("pop_back test:"); for(i=0; i<MAXN+1; i++) { pop_back(); show(); } #endif #if 1 // insert test: puts("insert test:"); for(i=0; i<MAXN+2; i++) { insert(idata, (i+1)*10); show(); } puts("clear...\n"); clear(); #endif #if 1 // insert_after test: puts("insert_after test:"); push_back(-99); for(i=0; i<MAXN+1; i++) { insert_after(idata, i+1); show(); } puts("clear...\n"); clear(); #endif #if 1 // find test: puts("find test:"); for(i=0; i<MAXN/2; i++) { push_front(MAXN-i); push_back(MAXN/2-i); //show(); } show(); info(); for(i=0; i<MAXN; i++) { int val = rand()%(2*MAXN); pointer p = find(val); if( p != NPTR ) printf("%3d %3d found at %d\n", val, dataof(p), p); else printf("%3d not found\n", val); } #endif #if 1 puts("\nfind_prev test:"); for(i=0; i<MAXN; i++) { int val = rand()%(2*MAXN); pointer p = find_prev(val); if( p != NPTR ) printf("%3d %3d found at %d's next.\n", val, dataof(nextof(p)), p); else printf("%3d not found\n", val); } #endif #if 1 // find_prev and insert_after test: clear(); puts("\nfind_prev and insert_after test:"); for(i=0; i<MAXN/2; i++) { push_front(MAXN/2-i); } show(); for(i=0; i<MAXN/2; i++) { int val = rand()%(2*MAXN), n=-(i+1); pointer p = find_prev(val); if( p != NPTR ) { printf("insert %d to front of %d:", n, val); insert_after(p, n); show(); } } #endif #if 1 // find and insert test: clear(); puts("\nfind and insert test:"); for(i=0; i<MAXN/2; i++) { push_front(MAXN/2-i); } show(); for(i=0; i<MAXN/2; i++) { int val = rand()%MAXN, n=-(i+1); pointer p = find(val); if( p != NPTR ) { printf("insert %d to after of %d:", n, val); insert_after(p, n); show(); } } #endif puts("end of main()."); return 0; }
bool Route::Path::Find(s32 to, int limit) { const int pathfinding = hero->GetLevelSkill(Skill::Secondary::PATHFINDING); const s32 from = hero->GetIndex(); s32 cur = from; s32 alt = 0; s32 tmp = 0; std::map<s32, cell_t> list; std::map<s32, cell_t>::iterator it1 = list.begin(); std::map<s32, cell_t>::iterator it2 = list.end(); list[cur].cost_g = 0; list[cur].cost_t = 0; list[cur].parent = -1; list[cur].open = 0; const Directions directions = Direction::All(); clear(); while(cur != to) { LocalEvent::Get().HandleEvents(false); for(Directions::const_iterator it = directions.begin(); it != directions.end(); ++it) { if(Maps::isValidDirection(cur, *it)) { tmp = Maps::GetDirectionIndex(cur, *it); if(list[tmp].open) { const u32 costg = GetPenaltyFromTo(cur, tmp, *it, pathfinding); // new if(-1 == list[tmp].parent) { if((list[cur].passbl & *it) || PassableFromToTile(*hero, cur, tmp, *it, to)) { list[cur].passbl |= *it; list[tmp].direct = *it; list[tmp].cost_g = costg; list[tmp].parent = cur; list[tmp].open = 1; list[tmp].cost_d = 50 * Maps::GetApproximateDistance(tmp, to); list[tmp].cost_t = list[cur].cost_t + costg; } } // check alt else { if(list[tmp].cost_t > list[cur].cost_t + costg && ((list[cur].passbl & *it) || PassableFromToTile(*hero, cur, tmp, *it, to))) { list[cur].passbl |= *it; list[tmp].direct = *it; list[tmp].parent = cur; list[tmp].cost_g = costg; list[tmp].cost_t = list[cur].cost_t + costg; } } } } } list[cur].open = 0; it1 = list.begin(); alt = -1; tmp = MAXU16; DEBUG(DBG_OTHER, DBG_TRACE, "route, from: " << cur); // find minimal cost for(; it1 != it2; ++it1) if((*it1).second.open) { const cell_t & cell2 = (*it1).second; #ifdef WITH_DEBUG if(IS_DEBUG(DBG_OTHER, DBG_TRACE) && cell2.cost_g != MAXU16) { int direct = Direction::Get(cur, (*it1).first); if(Direction::UNKNOWN != direct) { VERBOSE("\t\tdirect: " << Direction::String(direct) << ", index: " << (*it1).first << ", cost g: " << cell2.cost_g << ", cost t: " << cell2.cost_t << ", cost d: " << cell2.cost_d); } } #endif if(cell2.cost_t + cell2.cost_d < tmp) { tmp = cell2.cost_t + cell2.cost_d; alt = (*it1).first; } } // not found, and exception if(MAXU16 == tmp || -1 == alt) break; #ifdef WITH_DEBUG else DEBUG(DBG_OTHER, DBG_TRACE, "select: " << alt); #endif cur = alt; if(0 < limit && GetCurrentLength(list, cur) > limit) break; } // save path if(cur == to) { while(cur != from) { push_front(Route::Step(list[cur].parent, list[cur].direct, list[cur].cost_g)); cur = list[cur].parent; } } else { DEBUG(DBG_OTHER, DBG_TRACE, "not found" << ", from:" << from << ", to: " << to); } return !empty(); }
static type call(T const& first, map<Rest...> const& rest) { return type(push_front(rest, first)); }
void insert(byte* elem) { push_front(elem); }