main() { struct node* head=BuildOneTwoThree(); RecursiveReverse(&head); while(head!=NULL) { printf("%d ",head->data); head=head->next; } }
Node* RecursiveReverse(Node *head) { if(!head || !head->next) return head; Node *temp=RecursiveReverse(head->next); head->next->next = head; head->next = NULL; return temp; }
/* Recursively reverses the given linked list by changing its .next pointers and its head pointer in one pass of the list. */ void RecursiveReverse(struct node** headRef) { struct node* first; struct node* rest; if (*headRef == NULL) return; // empty list base case first = *headRef; // suppose first = {1, 2, 3} rest = first->next; // rest = {2, 3} if (rest == NULL) return; // empty rest base case RecursiveReverse(&rest); // Recursively reverse the smaller {2, 3} case // after: rest = {3, 2} first->next->next = first; // put the first elem on the end of the list first->next = NULL; // (tricky step -- make a drawing) *headRef = rest; // fix the head pointer }
void RecursiveReverse(struct node** headRef) { struct node* first; struct node* rest; if (*headRef == NULL) return; first = *headRef; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; *headRef = rest; }
//Reverses a char array recursively //Input: A char array, last character's position, first character's position //Output: Char array in reverse void RecursiveReverse(char string[], int a, int b) { char temp; if (b != strlen(string)/2) { temp = string[a]; string[a] = string[b]; string[b] = temp; RecursiveReverse(string, a-1, b+1); } else printf("Recursively reversed: %s\n", string); }
void RecursiveReverse(ERObject **headRef) { ERObject *first; ERObject *rest; if (headRef == NULL) return; first = *headRef; rest = (ERObject *) first->fNext; if (rest == NULL) return; RecursiveReverse(&rest); first->fNext->fNext = (NMLink *) first; first->fNext = NULL; *headRef = rest; }
int _tmain(int argc, _TCHAR* argv[]) { printf("Sample list"); struct node * head = NULL; // Build linked list with random numbers BuildList(&head); DisplayNode(head); // Reverse the linked list RecursiveReverse(&head); DisplayNode(head); //Sorted Insert into a new list DisplayNode(head); int a; scanf_s("adasd",&a); return 0; }
int main() { //Declares a char array Buffer1 and initializes to "This is the first buffer" char Buffer1[] = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 'f', 'i', 'r', 's', 't', ' ', 'b', 'u', 'f', 'f', 'e', 'r', '\0'}; //Declares a char array Buffer2 and initializes to "This is the second buffer" char Buffer2[] = "This is the second buffer"; //Declares a char array Buffer3 char Buffer3[80]; char Parse[] = "hello world, how are you today."; char* pBuffer; int a, b = 0; pBuffer = Buffer3; scanf("%[^\n]s", Buffer3); printf("\nBuffer 1: %s\nBuffer 2: %s\nBuffer 3: ", Buffer1, Buffer2); //Prints contents of Buffer3 via while loop while (*pBuffer != '\0') { printf("%c", *pBuffer); pBuffer++; } printf("\n"); pBuffer = Buffer3; a = (int)strlen(pBuffer) - 1; Reverse(pBuffer); RecursiveReverse(pBuffer, a, b); ParseSentence(Parse); return 0; }