void print_queue(void *q_this) { struct queue_hist *q = (struct queue_hist *)q_this; struct elem_hist *e = (struct elem_hist *)(q->last_elem); fprintf(stdout, "\nprint queue @ = %p\n-----------\n", q_this); for(int i = 0; i < q->get_nb_elem(q_this); i++) { print_elem((void *)e); e = (struct elem_hist *)(((struct elem_hist *)e)->q_next); } }
int main() { int t = 0; int a[10000] = {0,}; int i = 0; int start = -1; int len ; scanf("%d",&t); len = t; while(t--){ scanf("%d",&a[i]); i++; } print_elem(a,len,start); return 0; }
void queue_print(char *name, queue_t *queue, void print_elem(void*)) { printf("%s: ", name); // Testa se a fila não está vazia if (queue) { printf("["); queue_t* aux = queue; do { print_elem(aux); printf(" "); aux = aux->next; } while (aux != queue); printf("\b]\n"); return; } printf("[]\n"); }
int tree_view(int fdin, u_int rootdir) { AVT_ELEM elem; // one element if ((lseek(fdin, rootdir, SEEK_SET) != rootdir) || (read(fdin, &elem, sizeof(AVT_ELEM)) != 40)) { return(1); } print_elem(fdin, elem); if (elem.bits.fdir && elem.tree) tree_view(fdin, elem.tree); if (elem.left) tree_view(fdin, elem.left); if (elem.right) tree_view(fdin, elem.right); return(0); };
// recursivity!!! void pid_print_tree(unsigned index, unsigned parent, int nowrap) { print_elem(index, nowrap); // Remove unused parameter warning (void)parent; unsigned i; for (i = index + 1; i < (unsigned)max_pids; i++) { if (pids[i].parent == (pid_t)index) pid_print_tree(i, index, nowrap); } for (i = 0; i < index; i++) { if (pids[i].parent == (pid_t)index) pid_print_tree(i, index, nowrap); } }
void breadth_first_search(int m, int n, pair<int, int> beg) { //矩阵有m行,n列,行下标从0到m-1,列下标从0到n-1 //起点beg的x坐标为beg.first(列标),y坐标为beg.second(行标) //x坐标是列标,即取值范围从0到n-1 //y坐标是行标,即取值范围从0到m-1(编程时经常容易搞混行列与xy坐标的关系) //visited标记节点是否被访问过 int visited[MAX][MAX]; memset(visited, 0, MAX * MAX *sizeof(int)); queue<pair<int, int> > q; //将点进队之后就要将visit数组进行标识 //这样做可以“保证”队列中总是存储着:“将要被访问,而且从未访问过的点” //何时对visited数组标记有很多种做法 //不同的方法会有不同的效果,但它们都存在缺陷,这些缺陷在bfs中并不会体现 //而在后面的双向广度搜索和启发式搜索中会体现出来,所以请记住这个细节 q.push(beg); visited[beg.second][beg.first] = 1; while(!q.empty()) { pair<int, int> p = q.front(); q.pop(); //xy坐标的概念和数组中行列的概念总是相反的,容易搞混 print_elem(p); //对p点四个方向的邻节点进行考虑 //还需判断矩阵越界 if(p.first - 1 >= 0 && !visited[p.first - 1][p.second]) { q.push(pair<int, int>(p.first - 1, p.second)); visited[p.first - 1][p.second] = 1; } if(p.first + 1 < n && !visited[p.first + 1][p.second]) { q.push(pair<int, int>(p.first + 1, p.second)); visited[p.first + 1][p.second] = 1; } if(p.second - 1 >= 0 && !visited[p.first][p.second - 1]) { q.push(pair<int, int>(p.first, p.second - 1)); visited[p.first][p.second - 1] = 1; } if(p.second + 1 < m && !visited[p.first][p.second + 1]) { q.push(pair<int, int>(p.first, p.second + 1)); visited[p.first][p.second + 1] = 1; } } }
int main(void) { link p; insert(make_node(10)); insert(make_node(5)); insert(make_node(90)); p = search(5); delete(p); free_node(p); traverse(print_elem); destroy(); push(make_node(100)); push(make_node(200)); push(make_node(250)); while (p= pop()) { print_elem(p); free_node(p); } return 0; }
static void print_stack(struct elem *e) { if (e) { while (e) { print_elem(e); if ( /* there is another node to print */ e->parent && /* this one has already been printed */ e->_print_count == print_count ) { printf(" ..."); break; } e->_print_count = print_count; e = e->parent; } } else { printf("[empty prediction]"); } printf("\n"); }
void print_action(int log_level, const char *prefix, crm_action_t *action) { print_elem(log_level, prefix, FALSE, action); }
void pid_print_list(unsigned index, int nowrap) { print_elem(index, nowrap); }
void handle_winch(int i) { (void)i; get_size(); print_elem(GET(list), 1, 0); }
int main(int argc, char *argv[]) { int opt; int prevoptind; opterr = 0; // variables associated with options char *projects[PROJECT_CMD_CNT]; int projects_cnt = 0; char *types[TYPE_CMD_CNT]; int types_cnt = 0; char *collectors[COLLECTOR_CMD_CNT]; int collectors_cnt = 0; char *peerasns[PEERASN_CMD_CNT]; int peerasns_cnt = 0; char *prefixes[PREFIX_CMD_CNT]; int prefixes_cnt = 0; char *communities[COMMUNITY_CMD_CNT]; int communities_cnt = 0; struct window windows[WINDOW_CMD_CNT]; char *endp; int windows_cnt = 0; char *interface_options[OPTION_CMD_CNT]; int interface_options_cnt = 0; int rib_period = 0; int live = 0; int output_info = 0; int record_output_on = 0; int record_bgpdump_output_on = 0; int elem_output_on = 0; bgpstream_data_interface_option_t *option; int i; /* required to be created before usage is called */ bs = bgpstream_create(); if(!bs) { fprintf(stderr, "ERROR: Could not create BGPStream instance\n"); return -1; } datasource_id_default = datasource_id = bgpstream_get_data_interface_id(bs); datasource_info = bgpstream_get_data_interface_info(bs, datasource_id); assert(datasource_id != 0); /* allocate memory for bs_record */ bgpstream_record_t *bs_record = bgpstream_record_create(); if(bs_record == NULL) { fprintf(stderr, "ERROR: Could not create BGPStream record\n"); bgpstream_destroy(bs); return -1; } while (prevoptind = optind, (opt = getopt (argc, argv, "d:o:p:c:t:w:j:k:y:P:lrmeivh?")) >= 0) { if (optind == prevoptind + 2 && (optarg == NULL || *optarg == '-') ) { opt = ':'; -- optind; } switch (opt) { case 'p': if(projects_cnt == PROJECT_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d projects can be specified on " "the command line\n", PROJECT_CMD_CNT); usage(); exit(-1); } projects[projects_cnt++] = strdup(optarg); break; case 'c': if(collectors_cnt == COLLECTOR_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d collectors can be specified on " "the command line\n", COLLECTOR_CMD_CNT); usage(); exit(-1); } collectors[collectors_cnt++] = strdup(optarg); break; case 't': if(types_cnt == TYPE_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d types can be specified on " "the command line\n", TYPE_CMD_CNT); usage(); exit(-1); } types[types_cnt++] = strdup(optarg); break; case 'w': if(windows_cnt == WINDOW_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d windows can be specified on " "the command line\n", WINDOW_CMD_CNT); usage(); exit(-1); } /* split the window into a start and end */ if((endp = strchr(optarg, ',')) == NULL) { windows[windows_cnt].end = BGPSTREAM_FOREVER; } else { *endp = '\0'; endp++; windows[windows_cnt].end = atoi(endp); } windows[windows_cnt].start = atoi(optarg); windows_cnt++; break; case 'j': if(peerasns_cnt == PEERASN_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d peer asns can be specified on " "the command line\n", PEERASN_CMD_CNT); usage(); exit(-1); } peerasns[peerasns_cnt++] = strdup(optarg); break; case 'k': if(prefixes_cnt == PREFIX_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d peer asns can be specified on " "the command line\n", PREFIX_CMD_CNT); usage(); exit(-1); } prefixes[prefixes_cnt++] = strdup(optarg); break; case 'y': if(communities_cnt == COMMUNITY_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d communities can be specified on " "the command line\n", PREFIX_CMD_CNT); usage(); exit(-1); } communities[communities_cnt++] = strdup(optarg); break; case 'P': rib_period = atoi(optarg); break; case 'd': if((datasource_id = bgpstream_get_data_interface_id_by_name(bs, optarg)) == 0) { fprintf(stderr, "ERROR: Invalid data interface name '%s'\n", optarg); usage(); exit(-1); } datasource_info = bgpstream_get_data_interface_info(bs, datasource_id); break; case 'o': if(interface_options_cnt == OPTION_CMD_CNT) { fprintf(stderr, "ERROR: A maximum of %d interface options can be specified\n", OPTION_CMD_CNT); usage(); exit(-1); } interface_options[interface_options_cnt++] = strdup(optarg); break; case 'l': live = 1; break; case 'r': record_output_on = 1; break; case 'm': record_bgpdump_output_on = 1; break; case 'e': elem_output_on = 1; break; case 'i': output_info = 1; break; case ':': fprintf(stderr, "ERROR: Missing option argument for -%c\n", optopt); usage(); exit(-1); break; case '?': case 'v': fprintf(stderr, "bgpreader version %d.%d.%d\n", BGPSTREAM_MAJOR_VERSION, BGPSTREAM_MID_VERSION, BGPSTREAM_MINOR_VERSION); usage(); exit(0); break; default: usage(); exit(-1); } } for(i=0; i<interface_options_cnt; i++) { if(*interface_options[i] == '?') { dump_if_options(); usage(); exit(0); } else { /* actually set this option */ if((endp = strchr(interface_options[i], ',')) == NULL) { fprintf(stderr, "ERROR: Malformed data interface option (%s)\n", interface_options[i]); fprintf(stderr, "ERROR: Expecting <option-name>,<option-value>\n"); usage(); exit(-1); } *endp = '\0'; endp++; if((option = bgpstream_get_data_interface_option_by_name(bs, datasource_id, interface_options[i])) == NULL) { fprintf(stderr, "ERROR: Invalid option '%s' for data interface '%s'\n", interface_options[i], datasource_info->name); usage(); exit(-1); } bgpstream_set_data_interface_option(bs, option, endp); } free(interface_options[i]); interface_options[i] = NULL; } interface_options_cnt = 0; if(windows_cnt == 0) { fprintf(stderr, "ERROR: At least one time window must be specified using -w\n"); usage(); exit(-1); } /* if the user did not specify any output format * then the default one is per elem */ if(record_output_on == 0 && elem_output_on == 0 && record_bgpdump_output_on == 0) { elem_output_on = 1; } /* the program can now start */ /* allocate memory for interface */ /* projects */ for(i=0; i<projects_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_PROJECT, projects[i]); free(projects[i]); } /* collectors */ for(i=0; i<collectors_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_COLLECTOR, collectors[i]); free(collectors[i]); } /* types */ for(i=0; i<types_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_RECORD_TYPE, types[i]); free(types[i]); } /* windows */ for(i=0; i<windows_cnt; i++) { bgpstream_add_interval_filter(bs, windows[i].start, windows[i].end); } /* peer asns */ for(i=0; i<peerasns_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_PEER_ASN, peerasns[i]); free(peerasns[i]); } /* prefixes */ for(i=0; i<prefixes_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_PREFIX, prefixes[i]); free(prefixes[i]); } /* communities */ for(i=0; i<communities_cnt; i++) { bgpstream_add_filter(bs, BGPSTREAM_FILTER_TYPE_ELEM_COMMUNITY, communities[i]); free(communities[i]); } /* frequencies */ if(rib_period > 0) { bgpstream_add_rib_period_filter(bs, rib_period); } /* datasource */ bgpstream_set_data_interface(bs, datasource_id); /* live */ if(live != 0) { bgpstream_set_live_mode(bs); } /* turn on interface */ if(bgpstream_start(bs) < 0) { fprintf(stderr, "ERROR: Could not init BGPStream\n"); return -1; } if(output_info) { if(record_output_on) { printf(BGPSTREAM_RECORD_OUTPUT_FORMAT); } if(elem_output_on) { printf(BGPSTREAM_ELEM_OUTPUT_FORMAT); } } /* use the interface */ int get_next_ret = 0; bgpstream_elem_t * bs_elem; do { get_next_ret = bgpstream_get_next_record(bs, bs_record); if(get_next_ret && record_output_on) { print_bs_record(bs_record); } if(get_next_ret && bs_record->status == BGPSTREAM_RECORD_STATUS_VALID_RECORD) { if(record_bgpdump_output_on) { bgpstream_record_print_mrt_data(bs_record); } if(elem_output_on) { /* check if the record is of type RIB, in case extract the ID */ if(bs_record->attributes.dump_type == BGPSTREAM_RIB) { /* print the RIB start line */ if(bs_record->dump_pos == BGPSTREAM_DUMP_START) { print_rib_control_message(bs_record); } } while((bs_elem = bgpstream_record_get_next_elem(bs_record)) != NULL) { if(print_elem(bs_record, bs_elem) != 0) { goto err; } } /* check if end of RIB has been reached */ if(bs_record->attributes.dump_type == BGPSTREAM_RIB && bs_record->dump_pos == BGPSTREAM_DUMP_END) { print_rib_control_message(bs_record); } } } } while(get_next_ret > 0); /* de-allocate memory for bs_record */ bgpstream_record_destroy(bs_record); /* turn off interface */ bgpstream_stop(bs); /* deallocate memory for interface */ bgpstream_destroy(bs); return 0; err: bgpstream_record_destroy(bs_record); bgpstream_stop(bs); bgpstream_destroy(bs); return -1; }