void recursive_reverse(NODE* head_ref) { NODE first; NODE rest; /* empty list */ if (*head_ref == NULL) return; /* suppose first = {1, 2, 3}, rest = {2, 3} */ first = *head_ref; rest = first->next; /* List has only one node */ if (rest == NULL) return; /* put the first element on the end of the list */ recursive_reverse(&rest); first->next->next = first; /* tricky step -- see the diagram */ first->next = NULL; /* fix the head pointer */ *head_ref = rest; }//end of recursive delete
void recursive_reverse(char *s, int len) { char swap; if (len < 2) return; swap = s[0]; s[0] = s[len - 1]; s[len - 1] = swap; recursive_reverse(s + 1, len - 2); }
//Reverse Function [RECURSIVE] myNode* recursive_reverse(myNode* root){ if(root->next!=(myNode*)0){ recursive_reverse(root->next); root->next->next=root; return (root); }else{ head=root; return (root); } }
int main(int argc, char **argv) { NODE head1=(NODE)0; NODE head2=(NODE)0; head1=addItem(head1,5); head1=addItem(head1,7); head1=addItem(head1,72); recursive_reverse(&head1); //printf("List:",nthNODE(head1,0)->data); //head2=addItem(head2,5); //head2=addItem(head2,9); copy_linked_lists(head1,&head2); printf("\nequal?:%d\n",compareLinkedList(head1,head2)); free_linked_list(&head2); if(head2==NULL) printf("freed\n"); delete_NODE(head1->next); head1=addItem(head1,98); print(head1); head1->next->next->next=head1; printf("\nCycle exists?:%d",cycle_detection(head1)); printf("\ncycle starts at:%d",cycleNode(head1)->data); //print(head1); return 0; }
int main(){ head=(myNode*)0; add(1); add(2); add(3); add(4); add(5); add(6); print_list(); if(head!=(myNode*)0){ temp=recursive_reverse(head); temp->next=(myNode*)0; } print_list(); return 0; }
int main(void) { int i; node *l1, *l2, *l3; /* append() */ printf("=== Tesing Append ===\n"); l1 = NULL; for (i=0; i<10; i++) { append(&l1, i); } print(l1); /* concatenate() */ printf("=== Tesing Concatenate ===\n"); l1 = NULL; l2 = NULL; for (i=0; i<10; i++) { append(&l1, i); append(&l2, (20+i)); } l3 = concatenate(l1, l2); print(l3); /* iterative_reverse */ printf("=== Iterative Reverse ===\n"); l1 = NULL; for (i=0; i<10; i++) { append(&l1, i); } iterative_reverse(&l1); print(l1); /* recursivee_reverse */ printf("=== Recursive Reverse ===\n"); l1 = NULL; for (i=0; i<10; i++) { append(&l1, i); } l2 = recursive_reverse(NULL, l1); print(l2); /* Destroy */ printf("=== Destroy ===\n"); l1 = NULL; for (i=0; i<10; i++) { append(&l1, i); } destoy(&l1); if (!l1) { printf("List was Destroyed\n"); } /* Delete */ printf("=== Delete 3, 5, 6, 9 ===\n"); l1 = NULL; for (i=0; i<10; i++) { append(&l1, i); } print(l1); delete(&l1, 0); delete(&l1, 5); delete(&l1, 6); delete(&l1, 8); delete(&l1, 9); print(l1); return 0; }
void reverse(char *s) { recursive_reverse(s, strlen(s)); }