int main(void) { struct score tmp, *p; LLIST *list; int i; list = llist_creat(sizeof(struct score)); /* if error */ for (i = 0; i < 9; i++) { tmp.id = i; tmp.ch = 100 - i; tmp.math = 100 - i * 2; tmp.en = 100 - i * 3; snprintf(tmp.name, NAMESIZE, "stu%d", i); llist_add(list, &tmp, LLIST_ADD_FORWARD); } llist_travel(list, print_score); llist_delet(list, "stu6", name_cmp); printf("\n"); llist_travel(list, print_score); #if 0 p = llist_find(list, "stuxx", name_cmp); if (p == NULL) { printf("Can not find.\n"); } else { print_score(p); } #endif llist_destroy(list); return 0; }
//变长结构体版本 //类的概念:数据和方法都放入头节点 int main(int argc, const char *argv[]) { int i, ret; LLIST *handler; SCORE_ST tmp, *tmp_p; handler = llist_creat(sizeof(SCORE_ST)); for (i = 0; i < 7; i++) { tmp.id = i; snprintf(tmp.name, NAMESIZE, "middle school student%d", i); tmp.math = rand() %100; tmp.chinese = rand() %100; //myprint(&tmp); llist_insert(handler, &tmp, LLIST_FORWARD); } llist_travel(handler, myprint); printf("-----------------------------------\n"); char *name = "stu3"; tmp_p = llist_find(handler, name, cmp_name); if (tmp_p) { myprint(tmp_p); }else{ printf("not find\n"); } printf("-----------------------------------\n"); int id = 2; ret = llist_fetch(handler, &id, cmp_id, &tmp); if (ret) { printf("fetch "); myprint(&tmp); } printf("-----------------------------------\n"); name ="stu0"; ret = llist_delete(handler, name, cmp_name); if (ret) { printf("delete %s\n", name); } printf("-----------------------------------\n"); llist_travel(handler, myprint); printf("-----------------------------------\n"); name = malloc(NAMESIZE); for (i = 1; i < 7; i++) { snprintf(name, NAMESIZE, "stu%d", i); ret = llist_delete(handler, name, cmp_name); if (ret) { printf("delete %s\n", name); } } llist_travel(handler, myprint); llist_destroy(handler); return 0; }
int main(void) { struct score tmp, *datap; int i, id; LLIST *list; int ret; list = llist_creat(sizeof(struct score)); for (i = 0; i < 7; i++) { tmp.id = i; tmp.math = 100 - i; snprintf(tmp.name, NAMESIZE, "stu%d", i); llist_insert(list, &tmp, LLIST_BACKWARD); } llist_travel(list, print_s); id = 4; ret = llist_fetch(list, &tmp, &id, id_cmp); printf("\n"); if (ret == -1) { printf("Can not find.\n"); } else { print_s(&tmp); } printf("\n"); llist_travel(list, print_s); #if 0 id = 8; llist_delet(list, &id, id_cmp); printf("\n"); llist_travel(list, print_s); id = 2; datap = llist_find(list, &id, id_cmp); if (datap == NULL) { printf("Can not find.\n"); } else { print_s(datap); } #endif llist_save(list, "stu.data"); llist_destroy(list); LLIST *list2 = llist_load("stu.data"); llist_travel(list2, print_s); llist_destroy(list2); return 0; }
static void schan_members_rename(struct llist_header *head, char *oldnick, char *newnick) { struct stats_member *m; m = llist_find(head, (void *)oldnick); if (!m) return; Assert(newnick); m->nick = nrealloc(m->nick, strlen(newnick) + 1); strcpy(m->nick, newnick); }
int main() { struct score tmp,*datap; int i,id,ret; char name[] = "sstu1"; LLIST *handle; handle = llist_create(sizeof(struct score)); if(handle == NULL) return -1; for(i = 0; i < 6 ;i++) { tmp.id = i; tmp.math = 100-i; snprintf(tmp.name,NAMESIZE,"stu%d",i); llist_insert(handle,&tmp,LLIST_BACKWARD); } llist_travel(handle,print_s); printf("\n\n"); id = 3; ret = llist_fetch(handle,&id,id_cmp,&tmp); if(ret != -1) print_s(&tmp); printf("\n\n"); llist_travel(handle,print_s); llist_destroy(handle); // llist_delete(handle,&id,id_cmp); // llist_travel(handle,print_s); #if 0 // datap = llist_find(handle,&id,id_cmp); datap = llist_find(handle,name,name_cmp); if(datap == NULL) printf("can not find!\n"); else print_s(datap); #endif return 0; }
void pwospf_hello_type(struct ip* ip_header, byte* payload, uint16_t payload_len, pwospf_header_t* pwospf_header, interface_t* intf) { printf(" ** pwospf_hello_type(..) called \n"); printf(" ** pwospf_hello_type(..) packet with length: %u \n", payload_len); // make hello packet pwospf_hello_packet_t* hello_packet = (pwospf_hello_packet_t*) malloc_or_die(sizeof(pwospf_hello_packet_t)); memcpy(&hello_packet->network_mask, payload, 4); memcpy(&hello_packet->helloint, payload + 4, 2); memcpy(&hello_packet->padding, payload + 6, 2); // display packet printf(" ** pwospf_hello_type(..) displayed header contents:\n"); display_hello_packet(hello_packet); // check mask and hello interval if(intf->subnet_mask == hello_packet->network_mask && intf->helloint == ntohs(hello_packet->helloint)) { printf(" ** pwospf_hello_type(..) network mask and hello interval correct\n"); // if the interface has no neighbors if(intf->neighbor_list == NULL) { printf(" ** pwospf_hello_type(..) interface has no neighbors, adding new neighbor\n"); add_neighbor(intf, pwospf_header->router_id, ip_header->ip_src.s_addr, hello_packet->helloint, hello_packet->network_mask); } else { // interface has existing neighbors // generate neighbor object neighbor_t* neighbor = make_neighbor(pwospf_header->router_id, ip_header->ip_src.s_addr, hello_packet->helloint, hello_packet->network_mask); // get last instance so that lsu info can be put back pthread_mutex_lock(&intf->neighbor_lock); node* ret = llist_find( intf->neighbor_list, predicate_id_neighbor_t,(void*) &pwospf_header->router_id); pthread_mutex_unlock(&intf->neighbor_lock); if( ret!= NULL) { neighbor_t *neighbor_ret = (neighbor_t*) ret->data; neighbor->last_adverts = neighbor_ret->last_adverts; neighbor->last_lsu_packet = neighbor_ret->last_lsu_packet; } // lock neighbor list pthread_mutex_lock(&intf->neighbor_lock); // if exists, then update, otherwise add intf->neighbor_list = llist_update_beginning_delete(intf->neighbor_list, predicate_neighbor_t, (void*) neighbor); pthread_mutex_unlock(&intf->neighbor_lock); } // update info in neighbor db // get instance of router struct sr_instance* sr_inst = get_sr(); struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst); update_neighbor_vertex_t_rid(router, ip_header->ip_src.s_addr, pwospf_header->router_id); } else { printf(" ** pwospf_hello_type(..) network mask or hello interval incorrect\n"); } }
int main(void) { LLIST *llist; struct score tmp, *datap; int i; int id; struct sum_st sum = {0,0}; llist = llist_creat(sizeof(struct score)); /* if error */ for (i = 0; i < 7; i++) { tmp.id = i; snprintf(tmp.name, NAMESIZE, "stu%d", i); tmp.ch = 100 - i; tmp.math = 100 - 2 * i; llist_insert(llist, &tmp); /* if error */ } llist_travel(llist, print_score, NULL); printf("\n"); #if 1 id = 3; llist_delet(llist, &id, id_cmp); llist_travel(llist, print_score, NULL); #else datap = llist_find(llist, &id, id_cmp); if (datap == NULL) { printf("Can not find.\n"); } else { print_score(datap, NULL); } llist_travel(llist, get_sum, &sum); printf("sum: ch = %d, math = %d\n", sum.ch, sum.math); #endif llist_destroy(llist); return 0; }
int main() { LLIST *handle; int i ,ret, id = 3; char *name = "person2"; struct person_st tmp, *datap; handle = llist_create(sizeof(struct person_st)); if(handle== NULL) return -1; for(i = 0; i < 6; i++) { tmp.id = i; tmp.age = 20 + i; snprintf(tmp.name,NAMESIZE,"person%d",i); llist_insert(handle,&tmp,LLIST_FORWARD); } llist_travel(handle,print_s); printf("\n\n"); ret = llist_fetch(handle,name,name_cmp,&tmp); if(ret == 0) print_s(&tmp); printf("\n\n"); // llist_delete(handle,&id,id_cmp); // llist_travel(handle,print_s); #if 0 //datap = llist_find(handle,&id,id_cmp); datap = llist_find(handle,name,name_cmp); if(datap == NULL) printf("ca not find!\n"); else print_s(datap); #endif llist_destroy(handle); return 0; }
void pwospf_lsu_type(struct ip* ip_header, byte* payload, uint16_t payload_len, pwospf_header_t* pwospf_header, interface_t* intf) { printf(" ** pwospf_lsu_type(..) called \n"); printf(" ** pwospf_lsu_type(..) packet with length: %u \n", payload_len); struct sr_instance* sr_inst = get_sr(); struct sr_router* router = (struct sr_router*)sr_get_subsystem(sr_inst); // make lsu packet pwospf_lsu_packet_t* lsu_packet = (pwospf_lsu_packet_t*) malloc_or_die(sizeof(pwospf_lsu_packet_t)); memcpy(&lsu_packet->seq, payload, 2); memcpy(&lsu_packet->ttl, payload + 2, 2); memcpy(&lsu_packet->no_of_adverts, payload + 4, 4); // display packet printf(" ** pwospf_lsu_type(..) displayed header contents:\n"); display_lsu_packet(lsu_packet); // calculate number of adverts int num_adverts = ((payload_len - 8)/sizeof(pwospf_ls_advert_t)); printf(" ** pwospf_lsu_type(..) number of adverts: %d\n", num_adverts); // check if I generated the LSU if(pwospf_header->router_id != intf->router_id) { // not generated by self // check if neighbor ever sent hello packet pthread_mutex_lock(&intf->neighbor_lock); node* ret = llist_find( intf->neighbor_list, predicate_id_neighbor_t,(void*) &pwospf_header->router_id); pthread_mutex_unlock(&intf->neighbor_lock); if(ret != NULL) { // have received a hello packet from this neighbor in the recent past neighbor_t *neighbor = (neighbor_t*) ret->data; display_neighbor_t((void*) neighbor); // check if lsu received for first time if(neighbor->last_adverts == NULL) { printf(" ** pwospf_lsu_type(..) first lsu received from this neighbor\n"); neighbor->last_lsu_packet = *lsu_packet; neighbor->last_adverts = (byte*) malloc_or_die(payload_len - 8); memcpy(neighbor->last_adverts, payload + 8, payload_len - 8); // add to list pthread_mutex_lock(&intf->neighbor_lock); intf->neighbor_list = llist_update_beginning_delete(intf->neighbor_list, predicate_id_neighbor_t_neighbor_t, (void*) neighbor); pthread_mutex_unlock(&intf->neighbor_lock); display_neighbor_t((void*) neighbor); // create router entry router_entry_t router_entry; router_entry.router_id = neighbor->id; router_entry.area_id = 0; //add vertex with router entry info add_neighbor_vertex_t(router, router_entry); //create subnet entry subnet_entry_t* subnet_entry; //add adverts to db printf(" ** pwospf_lsu_type(..) adding adverts to neighbor db\n"); int i; pwospf_ls_advert_t* ls_advert = (pwospf_ls_advert_t*) malloc_or_die(sizeof(pwospf_ls_advert_t)); for(i = 0; i < ntohl(lsu_packet->no_of_adverts) ; i++) { memcpy(&ls_advert->subnet, payload + sizeof(pwospf_lsu_packet_t) + sizeof(pwospf_ls_advert_t) * i, 4); memcpy(&ls_advert->mask, payload + sizeof(pwospf_lsu_packet_t) + 4 + sizeof(pwospf_ls_advert_t) * i, 4); memcpy(&ls_advert->router_id, payload + sizeof(pwospf_lsu_packet_t) + 8 + sizeof(pwospf_ls_advert_t) * i, 4); display_ls_advert(ls_advert); subnet_entry = create_subnet_entry_t_advert(ls_advert); add_subnet_entry_t(router, router_entry, subnet_entry); } // update vertices update_neighbor_vertex_t(router, router_entry); //calculate new routing table via Dijkstra calculate_routing_table(router); //check if route/multipath is enabled if(router->reroute_multipath_status == TRUE) { //run multipath reroute_multipath(router); } //dijkstra2(router); //free(lsu_packet); //free(ret); //free(neighbor); //free(ls_advert); } else { // check if the previous seq was same if(neighbor->last_lsu_packet.seq != lsu_packet->seq) { // nope, new seq neighbor->last_lsu_packet = *lsu_packet; // flood to all neighbors pwospf_flood_lsu(ip_header, pwospf_header, lsu_packet, neighbor, intf); // check if content the same printf("last adverts :%s\n", (char*) neighbor->last_adverts); printf("new: %s\n", (char*) (payload + 8)); if(memcmp(neighbor->last_adverts, payload + 8, payload_len - 8) != 0) { // nope new content printf(" ** pwospf_lsu_type(..) new content neighbor db\n"); if(ntohl(neighbor->last_lsu_packet.no_of_adverts) != ntohl(lsu_packet->no_of_adverts)) { // free mem, as new adverts might need different memory free(neighbor->last_adverts); // now allocate new mem space neighbor->last_adverts = (byte*) malloc_or_die(payload_len - 8); } memcpy(neighbor->last_adverts, payload + 8, payload_len - 8); neighbor->last_lsu_packet = *lsu_packet; // update packet info in neighbor list pthread_mutex_lock(&intf->neighbor_lock); intf->neighbor_list = llist_update_beginning_delete(intf->neighbor_list, predicate_id_neighbor_t_neighbor_t, (void*) neighbor); pthread_mutex_unlock(&intf->neighbor_lock); // check if entry exists in DB // create router entry router_entry_t router_entry; router_entry.router_id = neighbor->id; router_entry.area_id = 0; //refresh vertex with router entry info, clear previous subnets refresh_neighbor_vertex_t(router, router_entry); //create subnet entry subnet_entry_t* subnet_entry; int i; pwospf_ls_advert_t* ls_advert = (pwospf_ls_advert_t*) malloc_or_die(sizeof(pwospf_ls_advert_t)); if(check_neighbor_vertex_t_router_entry_t_id(router, neighbor->id) == 1) { printf(" ** pwospf_lsu_type(..) neighbor already known, checking if any new adverts\n"); //neighbor present in db, check if any new adverts for(i = 0; i < ntohl(lsu_packet->no_of_adverts) ; i++) { memcpy(&ls_advert->subnet, payload + sizeof(pwospf_lsu_packet_t) + sizeof(pwospf_ls_advert_t) * i, 4); memcpy(&ls_advert->mask, payload + sizeof(pwospf_lsu_packet_t) + 4 + sizeof(pwospf_ls_advert_t) * i, 4); memcpy(&ls_advert->router_id, payload + sizeof(pwospf_lsu_packet_t) + 8 + sizeof(pwospf_ls_advert_t) * i, 4); display_ls_advert(ls_advert); subnet_entry = create_subnet_entry_t_advert(ls_advert); add_subnet_entry_t(router, router_entry, subnet_entry); } // update vertices update_neighbor_vertex_t(router, router_entry); } //run Djikstra's algo to calculate new routing table calculate_routing_table(router); //check if route/multipath is enabled if(router->reroute_multipath_status == TRUE) { //run multipath reroute_multipath(router); } //dijkstra2(router); } else { printf(" ** pwospf_lsu_type(..) error, content same as previous from this neighbor, only updating time stamp in DB\n"); // update timestamp // create router entry router_entry_t router_entry; router_entry.router_id = neighbor->id; router_entry.area_id = 0; update_neighbor_vertex_t_timestamp(router, router_entry); } } else { printf(" ** pwospf_lsu_type(..) error, seq same as previous one from this neighbor, dropping\n"); } } } else { printf(" ** pwospf_lsu_type(..) error, neighbor hasn't said hello recently, dropping\n"); } } else { //drop packet printf(" ** pwospf_lsu_type(..) error, LSU generated by self. dropping\n"); //free stuff free(lsu_packet); } }
static struct stats_member *schan_members_find(struct llist_header *head, char *nick) { return llist_find(head, (void *)nick); }
t_oper *find_oper(t_operlist **l, t_oper *m, t_oper_cmp cmp) { return ((t_oper *)llist_find((t_llist **)l, (void *)m, (t_llist_cmp)cmp)); }
int main(void) { int n, ret; void *ret1; LLIST *handle = NULL; LLIST *find = NULL; handle = llist_create(sizeof(int), NULL, NULL, NULL); while (1) { printf("please input num : "); scanf("%d", &n); if (n == -1) break; llist_insert(&n, APPEND, handle); } llist_travel(printf_s, NULL, handle); printf("\n"); printf("insert num : "); scanf("%d", &n); llist_insert(&n, 3, handle); llist_travel(printf_s, NULL, handle); printf("\n"); printf("delete num : "); scanf("%d", &n); llist_delete(&n, cmp, handle); llist_travel(printf_s, NULL, handle); printf("\n"); printf("find num : "); scanf("%d", &n); ret1 = llist_find(&n, cmp, handle); if(ret1 != NULL) printf("find = %d\n", *(int*)ret1); printf("\n"); printf("findall num : "); scanf("%d", &n); find = llist_findall(&n, cmp, handle); llist_travel(printf_s, find, handle); printf("\n"); llist_sort(cmp, handle); printf("sort:"); llist_travel(printf_s, NULL, handle); printf("\n"); ret = llist_store("./db", handle); if(ret == 0) printf("存储成功!\n"); llist_destroy(&handle); return 0; }