int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, World!\n"; std::vector<int> this_vec = {0,1,2,3,4,5,6}; print_vec(this_vec); reverse_vector(this_vec); print_vec(this_vec); std::vector<int> big_vector = fill_vec(47); print_vec(big_vector); reverse_vector(big_vector); print_vec(big_vector); std::list<int> small_list = fill_list(15, 47); print_list(small_list); reverse_list(small_list); print_list(small_list); std::list<int> big_list = fill_list(100, 710); print_list(big_list); reverse_list(big_list); print_list(big_list); Node* a = new Node; a->value = 6; a->ptr = new Node; a->ptr->value = 7; a->ptr->ptr = new Node; a->ptr->ptr->value = 8; a->ptr->ptr->ptr = new Node; a->ptr->ptr->ptr->value = 9; a->ptr->ptr->ptr->ptr = NULL; // print out this list print_linked_list("a",a); // create an STL list with 4 elements std::list<int> b; b.push_back(10); b.push_back(11); b.push_back(12); b.push_back(13); // use the STL list as input to a creator function that creates // linked lists with the same data Node* c = make_linked_list_from_STL_list(b); // print that data print_linked_list("c",c); // // WRITE A FEW MORE TEST CASES OF make_linked_list_from_STL_list // // reverse a linked list of nodes Node* d = reverse_nodes_in_linked_list(c); // print this data print_linked_list("d",d); // // WRITE A FEW MORE TEST CASES OF reverse_nodes_in_linked_list return 0; }
int main () { Node *head = NULL; int c = 0; int set[9] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 }; print_list (head); reverse_list (head); print_list (head); printf ("\n"); head = insert_node (head, 0); print_list (head); reverse_list (head); print_list (head); printf ("\n"); while (c < 9) { head = insert_node (head, set[c]); c++; } print_list (head); head = reverse_list (head); print_list (head); }
/* Appends the element 'elem' to the end of a list. Returns 1 if everything went ok , else it returns -1 */ int append_list(TYPE_ elem, NODE **Head) { NODE *p; reverse_list(Head); insert_list(elem, 0, Head); reverse_list(Head); return 1; }
bool is_palindrome(list_node *head) { if (!head) return false; list_node *fast = head, *slow = head; while (fast && fast->next && fast->next->next) { fast = fast->next->next; slow = slow->next; } list_node *middle = slow->next; reverse_list(&middle); print(middle); list_node *left = head, *right = middle; bool flag = true; while (right) { if (right->value != left->value) { flag = false; break; } right = right->next; left = left->next; } reverse_list(&middle); return flag; }
int main(int argc, char **argv) { sc *head; int n = (argc>1)? atoi(argv[1]) : 10; head = create_list(n); print_list(head); head = reverse_list(head); print_list(head); head = reverse_list(head); print_list(head); return 0; }
void reorderList(ListNode* head) { if(head == NULL || head ->next == NULL) return ; int len =0; ListNode *cur = head; while(cur != NULL){ len += 1; cur = cur->next; } ListNode *pre = head; for(int i=0; i< len/2-1; i ++){ pre = pre->next; } ListNode *tail = reverse_list(pre->next); pre->next = NULL; print_list(head); print_list(tail); cur = head; while(cur != NULL){ if(cur ->next == NULL){ cur->next = tail; return; } ListNode *tmp = tail; tail = tail->next; tmp->next = cur->next; cur->next = tmp; cur =cur->next->next; } }
int main() { struct NODE *node = NULL; struct NODE *head = NULL; struct NODE *tnode = NULL; int tval = -1; printf("Enter values for list: 0 to end.\n"); while (tval != 0) { scanf("%d", &tval); if (tval == 0) break; node = (struct NODE*)calloc(sizeof(struct NODE), 1); node->value = tval; node->next = NULL; if (head == NULL) { head = node; } else { tnode = head; while (tnode->next != NULL) tnode = tnode->next; tnode->next = node; } } PrintList(head); reverse_list (&head); PrintList(head); return 0; }
ListNode* reverseKGroup(ListNode* head, int k) { if(k<2) return head; ListNode* res=nullptr; ListNode* left=head; ListNode* last_tail=nullptr; while(left) { ListNode* cur=left; ListNode* last=nullptr; int cnt=0; for(; left && cnt<k; ++cnt) { last=left; left=left->next; } last->next=nullptr; if(cnt==k) { ListNode* l=reverse_list(cur, last_tail); if(!res) res=l; } else { if(last_tail) last_tail->next=cur; if(!res) res=cur; } } return res; }
node* get_list_from_token_str(char *str, char* delimiter) { char *name, *eq, *value; node *this, *prev; if (!str) return NULL; this = prev = NULL; name = strtok(str, delimiter); while (name) { eq = memchr(name, '=', strlen(name)); if (!eq) break; value = eq + 1; *eq = 0; name = trim_space(name); value = trim_space(value); this = (node*) malloc(sizeof(node)); //printf("name:%s.\n", name); //printf("val:%s.\n", value); strcpy(this->name, name); strcpy(this->value, value); this->next = prev; prev = this; name = strtok(NULL, delimiter); } return reverse_list(this); }
static obj_t add_who_irritants(obj_t cont, obj_t values, obj_t ex) { //oprintf("add_who_irritants: cont_proc = %p\n", cont_proc(cont)); //oprintf("add_who_irritants: cont4_arg = %O\n", cont4_arg(cont)); //oprintf("add_who_irritants: values = %O\n", values); assert(is_cont(cont)); cont_proc_t proc = cont_proc(cont); if (proc == c_apply_proc) { assert(is_cont5(cont)); obj_t op = cont5_arg1(cont); obj_t who_sym = procedure_is_C(op) ? procedure_name(op) : FALSE_OBJ; obj_t who_ex = MAKE_RECORD(who, who_sym); obj_t irr_ex = MAKE_RECORD(irritants, reverse_list(values)); return MAKE_COMPOUND_CONDITION(who_ex, irr_ex, ex); } if (proc == c_eval_operator) { assert(is_cont4(cont)); obj_t arg = cont4_arg(cont); obj_t who_ex = MAKE_RECORD(who, CAR(arg)); obj_t irr_ex = MAKE_RECORD(irritants, CDR(arg)); return MAKE_COMPOUND_CONDITION(who_ex, irr_ex, ex); } if (proc == c_eval) { assert(is_cont4(cont)); obj_t expr = cont4_arg(cont); obj_t who_ex = MAKE_RECORD(who, expr); return MAKE_COMPOUND_CONDITION(who_ex, ex); } return ex; }
static cv_t c_eval_operator(obj_t cont, obj_t values) { assert(is_cont4(cont)); obj_t appl = cont4_arg(cont); obj_t operator = CAR(values); EVAL_LOG("appl=%O operator=%O", appl, operator); COULD_RETRY(); if (!is_procedure(operator)) SYNTAX_ERROR(operator, operator, "must be procedure"); if (!procedure_args_evaluated(operator)) { assert(procedure_is_C(operator) && "implement Scheme special forms"); if (procedure_is_raw(operator)) { return ((cont_proc_t)procedure_code(operator))(cont, values); } else { // N.B., call proc after all other allocations. obj_t arg_list = application_operands(appl); obj_t new_values = CONS(make_uninitialized(), CDR(values)); pair_set_car(new_values, apply_proc(operator, arg_list)); return cv(cont_cont(cont), new_values); } } obj_t arg_list = reverse_list(application_operands(appl)); cont = make_cont5(c_apply_proc, cont_cont(cont), cont_env(cont), operator, CDR(values)); while (!is_null(arg_list)) { cont = make_cont4(c_eval, cont, cont_env(cont), CAR(arg_list)); arg_list = CDR(arg_list); } return cv(cont, EMPTY_LIST); }
extern cv_t c_apply_proc(obj_t cont, obj_t values) { assert(is_cont5(cont)); obj_t next = cont_cont(cont); obj_t operator = cont5_arg1(cont); obj_t saved_values = cont5_arg2(cont); EVAL_LOG("op=%O values=%O operator=%O saved_values=%O", operator, values, operator, saved_values); obj_t arg_list = reverse_list(values); if (procedure_is_C(operator)) { if (procedure_is_raw(operator)) return ((cont_proc_t)procedure_code(operator))(cont, values); else { // N.B., call proc after all other allocations. obj_t new_values = CONS(make_uninitialized(), saved_values); pair_set_car_nc(new_values, apply_proc(operator, arg_list)); return cv(next, new_values); } } else { obj_t env = procedure_env(operator); obj_t formals = procedure_args(operator); obj_t actuals = arg_list; obj_t new_env = make_closed_env(env, formals, actuals); obj_t body = procedure_body(operator); // Push a value for c_eval_seq to discard. obj_t new_values = CONS(make_uninitialized(), saved_values); return cv(make_cont4(c_eval_seq, next, new_env, body), new_values); } }
bool isPalindrome(ListNode* head) { if(head == NULL || head->next == NULL) { return true; } int length = 0; for(ListNode *cur = head; cur != NULL; cur = cur->next) { length++; } ListNode *left = NULL; ListNode *right = head; for(int i = 0; i < length / 2; i++) { left = right; right = right->next; } left->next = NULL; left = head; right = reverse_list(right); while(left != NULL) { if(left->val != right->val) { return false; } left = left->next; right = right->next; } return true; }
/* print_beamer_endnotes */ void print_lyxbeamer_endnotes(GString *out, scratch_pad *scratch) { node *temp_node; scratch->used_notes = reverse_list(scratch->used_notes); node *note = scratch->used_notes; // Handle Glossary temp_node = note; while (temp_node != NULL){ if(temp_node->key == GLOSSARYSOURCE){ g_string_append(out, "\n\\begin_layout BeginFrame\nGlossary\n"); g_string_append(out,"\n\\begin_layout Standard"); g_string_append(out,"\n\\begin_inset CommandInset nomencl_print"); g_string_append(out,"\nLatexCommand printnomenclature"); g_string_append(out,"\nset_width \"auto\"\n"); g_string_append(out,"\n\\end_inset\n"); g_string_append(out,"\n\\end_layout\n"); g_string_append(out, "\n\\end_layout\n"); g_string_append(out, "\n\\begin_layout EndFrame"); g_string_append(out, "\n\\end_layout"); break; } temp_node = temp_node->next; } if (note == NULL) return; note = scratch->used_notes; if (tree_contains_key(note,CITATIONSOURCE)){ g_string_append(out, "\n\\begin_layout BeginFrame\nReferences\n"); g_string_append(out, "\n\\end_layout"); } while ( note != NULL) { if (note->key == KEY_COUNTER) { note = note->next; continue; } if (note->key == CITATIONSOURCE) { g_string_append(out, "\n\\begin_layout Bibliography\n"); g_string_append(out,"\\begin_inset CommandInset bibitem\n"); g_string_append(out,"LatexCommand bibitem\n"); g_string_append_printf(out, "key \"%s\"\n", note->str); g_string_append_printf(out, "label \"%s\"\n", note->str); g_string_append(out,"\n\\end_inset\n"); print_lyx_node(out, note, scratch, FALSE); g_string_append(out,"\n\\end_layout\n"); } else { /* footnotes handled elsewhere */ } note = note->next; } g_string_append(out, "\n\\begin_layout EndFrame"); // close last frame g_string_append(out, "\n\\end_layout"); }
object_t *evaluate_list(object_t *env, object_t *args, object_t *acc) { if(isemptylist(args)) { return reverse_list(acc); } else { return evaluate_list(env, cdr(args), cons(eval(car(args), env), acc)); } }
//=========================================================================== void PluginManager_Exit() { reverse_list(&bbplugins); struct plugins *q; dolist(q, bbplugins) q->enabled = false; load_all_plugins(); freeall(&bbplugins); }
static void reverse_list(sll **first_ref ,sll **second_ref) { if(*second_ref == NULL) { return; }else { reverse_list(&(*second_ref),&(*second_ref)->next); } (*second_ref)->next = *first_ref; (*first_ref)->next = NULL; }
fr_map_t* reverse_list(fr_map_t *head){ if(head==NULL || head->bs_next==NULL) return head; kprintf("head.frame[%d]\n", head->fr_id); fr_map_t* new_head = reverse_list(head->bs_next); head->bs_next->bs_next = head; head->bs_next = NULL; return new_head; }
struct linked_list * add_numbers(struct linked_list *number1, struct linked_list *number2) { int carry = 0; int sum = 0; struct linked_list *tno1; struct linked_list *tno2; struct linked_list *result = NULL; number1 = reverse_list(number1); number2 = reverse_list(number2); tno1 = number1; tno2 = number2; while (NULL != number1 && NULL != number2) { sum = carry + number1->data + number2->data; carry = sum / 10; sum %= 10; result = add_to_list(result, sum); number1 = number1->next; number2 = number2->next; } while (NULL != number1) { sum = carry + number1->data; carry = sum / 10; sum %= 10; result = add_to_list(result, sum); number1 = number1->next; } while (NULL != number2) { sum = carry + number2->data; carry = sum / 10; sum %= 10; result = add_to_list(result, sum); number2 = number2->next; } if (0 < carry) { result = add_to_list(result, carry); } number1 = reverse_list(tno1); number2 = reverse_list(tno2); return result; }
/* print_beamer_endnotes */ void print_beamer_endnotes(GString *out, scratch_pad *scratch) { scratch->used_notes = reverse_list(scratch->used_notes); node *note = scratch->used_notes; #ifdef DEBUG_ON fprintf(stderr, "start endnotes\n"); #endif if ((note == NULL) || ((note->key == KEY_COUNTER) && (note->next == NULL))) return; while (note != NULL) { if (note->key == CITATIONSOURCE) break; note = note->next; } if (note == NULL) return; note = scratch->used_notes; /* TODO: need CITATIONSOURCE to print bibliography */ #ifdef DEBUG_ON fprintf(stderr, "there are endnotes to print\n"); #endif pad(out, 2, scratch); g_string_append_printf(out, "\\part{Bibliography}\n\\begin{frame}[allowframebreaks]\n\\frametitle{Bibliography}\n\\def\\newblock{}\n\\begin{thebibliography}{0}\n"); while ( note != NULL) { if (note->key == KEY_COUNTER) { note = note->next; continue; } pad(out, 1, scratch); if (note->key == CITATIONSOURCE) { g_string_append_printf(out, "\\bibitem{%s}\n", note->str); scratch->padded = 2; print_latex_node(out, note, scratch); pad(out, 1, scratch); scratch->padded = 1; } else { /* footnotes handled elsewhere */ } note = note->next; } pad(out,2, scratch); g_string_append_printf(out, "\\end{thebibliography}\n\\end{frame}\n\n"); scratch->padded = 0; #ifdef DEBUG_ON fprintf(stderr, "finish endnotes\n"); #endif }
int main(){ node_t* p; p = make_node(5); p = insert_val(6, p); p = insert_val(7, p); print_list(p); p = reverse_list(p); print_list(p); }
main(int argc, char* argv[]) { ll *list = random_list(atoi(argv[1])); if( list ) { print_list(list); printf("reversed list: \n"); reverse_list(&list); print_list(list); } return 0; }
int main(int argc, char** argv) { Node* head = create_list(); print_list(head); head = reverse_list(head); print_list(head); free_list(head); return 0; }
int main() { std::list<int>::iterator it; //even sized test std::cout << "Even Test:" << std::endl; std::list<int> ints_even; for (unsigned int i=1; i<7;i++) { //6 ints_even.push_back(i); } for (it=ints_even.begin();it!=ints_even.end();it++) { std::cout << *it << std::endl; } reverse_list(ints_even); std::cout << "Reverse:" << std::endl; for (it=ints_even.begin();it!=ints_even.end();it++) { std::cout << *it << std::endl; } //odd sized test std::cout << "Odd Test:" << std::endl; std::list<int> ints_odd; for (unsigned int i=1; i<6;i++) { //5 ints_odd.push_back(i); } for (it=ints_odd.begin();it!=ints_odd.end();it++) { std::cout << *it << std::endl; } reverse_list(ints_odd); std::cout << "Reverse:" << std::endl; for (it=ints_odd.begin();it!=ints_odd.end();it++) { std::cout << *it << std::endl; } //empty test std::cout << "Empty Test:" << std::endl; std::list<int> ints_empty; reverse_list(ints_empty); for (it=ints_empty.begin();it!=ints_empty.end();it++) { std::cout << *it << std::endl; } }
/* testing program */ int main(void) { ListNode *head = NULL; ListNode *head1 = NULL; ListNode *head2 = NULL; ListNode *merge = NULL; if((head = create_list(10)) == NULL) fprintf(stderr,"failed to create list!\n"); #if 0/* reverse the list */ fprintf(stdout,"the list before reverse:\n"); print_list(head); reverse_list(&head); fprintf(stdout,"the list after reverse:\n"); print_list(head); #endif #if 0/* printf the last kth node */ print_list(head); print_the_last_kth_node(head,1); print_the_last_kth_node(head,10); print_the_last_kth_node(head,11); print_the_last_kth_node(head,5); #endif #if 0/* merge the two ordered list */ if((head1 = create_list_even(3)) == NULL) fprintf(stderr,"failed to create list!\n"); if((head2 = create_list_odd(5)) == NULL) fprintf(stderr,"failed to create list!\n"); printf("the even list:\n"); reverse_list(&head1); print_list(head1); printf("the odd list:\n"); reverse_list(&head2); print_list(head2); merge = merge_list(head1, head2); printf("the merge list:\n"); print_list(merge); #endif fprintf(stdout,"the origin list:\n"); print_list(head); print_list_reversely(head); return 0; }
static void load_nls(void) { const char *lang_file = ReadString(extensionsrcPath(), "blackbox.options.language:", NULL); if (NULL == lang_file) return; char full_path[MAX_PATH]; FILE *fp = fopen (make_bb_path(full_path, lang_file), "rb"); if (NULL == fp) return; char line[4000], key[200], new_text[4000], *np; int nl; key[0] = 0; new_text[0] = 0; np = new_text; nl = 0; for (;;) { bool eof = false == read_next_line(fp, line, sizeof line); char *s = line, c = *s; if ('$' == c || eof) { if (key[0] && new_text[0]) { struct nls *t = (struct nls *)c_alloc(sizeof *t + strlen(key)); t->hash = calc_hash(t->key, key, &t->k); t->translation = new_str(new_text); cons_node(&pNLS, t); } if (eof) break; if (' ' == s[1]) s += 2; decode_escape(key, s); new_text[0] = 0; np = new_text; nl = 0; continue; } if ('#' == c || '!' == c) continue; if ('\0' != c) { while (nl--) *np++ = '\n'; np += decode_escape(np, s); nl = 0; } nl ++; } fclose(fp); reverse_list(&pNLS); }
List *parse_expressions(FILE *stream){ List *tokens = getTokens(stream); List *tokens2 = tokens; List *expressions = NULL; while (tokens2 != NULL){ expressions = cons(consume_expression(&tokens2), expressions); } List *uncompiled_expressions = reverse_list(expressions); free_list(expressions, nop_free_fn); free_list(tokens, (free_fn_ptr)free_token); return uncompiled_expressions; }
// ----------------------------------------------------------------------------- ListNode * Solution::my_solution1(ListNode* head, int k) { // Handle special cases. if (NULL == head || NULL == head->next || k <= 1) { // If the list is empty, or it has only one node, or k <= 1, // we do not need to do anything. return head; } // Create a fake head to make the algorithm easier. ListNode fakehead(0); fakehead.next = head; ListNode * prev_list_tail = &fakehead; ListNode * curr_list_head = head; ListNode * next_list_head = head; // Just an initial value. while (true) { // Skip the next k nodes that will be reversed. int i = 0; while (next_list_head != NULL && i < k) { next_list_head = next_list_head->next; ++i; } // If we do not have enough k nodes to reverse, we just break. // We shouldn't check whether next_list_head is NULL because there is // a case that the list just has multiple k-node lists. In this case, // the next_list_head ends up at a NULL pointer but we still need to // reverse the last k nodes. if (i < k) { break; } // Reverse the k nodes. reverse_list(prev_list_tail, next_list_head, curr_list_head); // Now, the curr_list_head becomes the last node in the k-node list, // therefore, it also is the prev_list_tail for the next k-node list. prev_list_tail = curr_list_head; // Now we need to move the curr_list_head to the head node in the next // k-node list, which is also the next of prev_list_tail. curr_list_head = prev_list_tail->next; // Set the starting node for next_list_head. next_list_head = curr_list_head; } // Return the real head. return fakehead.next; }
/* mk_str_from_list - merge list into a STR */ node * mk_str_from_list(node *list, bool extra_newline) { node *result = mk_node(STR); node *rev = reverse_list(list); GString *c = concat_string_list(rev); if (extra_newline) g_string_append(c, "\n"); result->str = c->str; g_string_free(c, false); return result; }
/*reverse the link list by reversing the pointers*/ LIST* reverse_list(LIST *node){ LIST *temp; if(node == NULL) return node; temp = reverse_list(node->next); if(temp==NULL) temp=node; else{ node->next->next = node; node->next = NULL; } return temp; }