void recursiveReverse(struct node** head_ref) { struct node* first; struct 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; /* reverse the rest list and put the first element at the end */ recursiveReverse(&rest); first->next->next = first; /* tricky step -- see the diagram */ first->next = NULL; /* fix the head pointer */ *head_ref = rest; }
int main(int argc, const char * argv[]) { //printList(appendNode(&(BuildWithLocalRef()), 2)); //doesn't work? &(2376ut) doesn't work not a lvalue Node* head=BuildOneTwoThree(); appendNodeWithPush(&head, 4); appendNode(&head, 4); appendNode(&head, 4); Node* test5=BuildWithLocalRef(); Push(&test5, 6); insertNth(&test5, 4, 3); //printList(test5); insertSort(&test5); append(&test5, &head); //printList(test5); Node *first=NULL,*second=NULL; //pop(&test5); //split(test5, &first, &second); //printList(first); //printList(second); //removeDuplicates(second); //printList(first); //printList(second); //moveNode(&first, &second); //printList(first); //printList(second); alternatingSplit(test5, &first, &second); //printList(first); //printList(second); //Node* test=shuffleMergeRecursive(first, second); //printList(test); Node* test1=BuildOneTwoThree(); Node* test2=BuildOneTwoThree(); pop(&test2); appendNode(&test2, 7);; appendNode(&test1, 5); //printList(test1); //printList(test2); append(&test1, &test2); //printList(test1); mergeSort(&test1); //printList(test1); Node* t4=BuildOneTwoThree(); Node* t5=BuildOneTwoThree(); appendNode(&t5, 6); appendNode(&t4, 6); appendNode(&t4, 7); appendNode(&t5, 8); Push(&t5, 0); Push(&(t5->next), 1); //printList(t5); //printList(t4); //printList(sortedIntersect(t4, t5)); recursiveReverse(&t5); printList(t5); return 0; }
void recursiveReverse(Node** headRef) { Node* first=*headRef; Node* rest=first->next; if (rest==NULL) { return; } recursiveReverse(&rest); first->next->next=first; first->next=NULL; *headRef=rest; }
int main(void) { struct node* root=NULL; int n,i,v,x,y; char c[2]={'a'}; while(c[0]!='Q') { scanf("%s", c); switch(c[0]) { case 'I':scanf("%d",&x); root=insert(root,x); break; case 'D':scanf("%d",&x); root=del(root,x); break; case 'L':print(root); break; case 'A':scanf("%d%d",&x,&y); root=append(root,x,y); break; } } /* print(root); root=del(root,1); print(root); root=insert(root,2); print(root); root=del(root,2); root=del(root,1); print(root); root=insert(root,3); root=append(root,3,4); root=append(root,3,5); print(root); root=del(root,4); print(root); root=del(root,3); print(root); root=del(root,5); print(root); root=del(root,1); */ recursiveReverse(&root); print(root); return 0; }
void recursiveReverse(struct node **head_ref) { struct node *first = *head_ref; if (first == NULL) return; struct node *rest = first->next; if (rest == NULL) return; recursiveReverse(&rest); first->next->next = first; first->next = NULL; *head_ref = rest; }
//recursive reverse list void recursiveReverse(Node **headRef) { Node *first; Node *rest; // rest of list if(*headRef == NULL) return; // empty list base case first = *headRef; // assume first = {1,2,3} rest = first->next; // rest = {2,3} if(rest ==NULL) return; // empty rest base case recursiveReverse(&rest); first->next->next =first; first->next =NULL; *headRef = rest; // fix head pointer }
/* Drier program to test above function*/ int main() { /* Start with the empty list */ struct node* head = NULL; int ele; printf("Enter the element\n"); while(scanf("%d",&ele)!=EOF){ insafter(&head, ele); } printList(head); reverse(&head); printf("\n Reversed Linked list by non rerusive \n"); printList(head); printf("\nReverse Linked list by recursive\n"); recursiveReverse(&head); printList(head); getchar(); }
int main() { struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode)); head->data = 1; head->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head->next->data = 2; head->next->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head->next->next->data = 3; head->next->next->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head->next->next->next->data = 4; head->next->next->next->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head->next->next->next->next->data = 5; head->next->next->next->next->next = NULL; printList(head); recursiveReverse(&head); printList(head); printf("\n # of nodes: %d", recursiveNumOfNodes(head)); struct ListNode *head1 = NULL; #if 0 head1->data = 1; head1->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head1->next->data = 2; head1->next->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head1->next->next->data = 3; head1->next->next->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head1->next->next->next->data = 4; head1->next->next->next->next = (struct ListNode *)malloc(sizeof(struct ListNode)); head1->next->next->next->next->data = 5; head1->next->next->next->next->next = NULL; printList(head1); printf("\n lists equal: %d \n", recursiveCompareLinkedLists(head, head1)); #endif recursiveCopyLL(head, &head1); printList(head1); recursiveAddNodeAtEndLL(&head1, 6); printList(head1); printf("\n List Recursive Printing: "); printListFromEndRecursive(head1); printf("\n"); }
void reverseStringRecursive(char* str) { int length = strlen(str); recursiveReverse(str, 0 , length - 1); }
void recursiveReverse(char* str, int begin, int end) { if( begin < end) { swap(&str[begin],&str[end]); recursiveReverse(str, ++begin, --end); } }