void PrintReverse( LinkedList* pHead ) { LinkedList* ph = pHead; if( ph != NULL ) { //->运算符优先级高于单目运算符 PrintReverse( &(*ph)->next); printf("%d ",(*ph)->data); } }
void PrintReverse(char str[],int low,int n){ if(low==n){ return; } PrintReverse(str,low+1,n); printf("%c",str[low]); }
int main(){ char str[]="bunty"; int n=5; PrintReverse(str,0,n); return 0; }
int main() { printf("Input values in the form of positive integers separated by spaces (\"1 2 3\" is proper input, \"1,2,3\" is not): "); int holding; cnode *head = malloc(sizeof(cnode)); scanf("%d", &holding); head -> info = holding; head -> next = NULL; head -> previous = NULL; int input = 1; //repurposed code from recitation 1 char junk = ' '; junk = getchar(); if (junk != ' ') input = 0; while (input == 1) { if (scanf("%d", &holding) > 0) head = inserthead(head, holding); junk = getchar(); if (junk != ' ') input = 0; } printf("All numbers inserted at the head.\n\n"); printf("Doubly linked list: "); cnode *ptr = head; while (ptr -> next != NULL) { printf("%d, ", ptr -> info); ptr = ptr -> next; } printf("%d\n\n", ptr -> info); printf("To singly linked list: "); node *singleHead = CopytoSinglyLinked(head); node *point = singleHead; while (point -> next != NULL) { printf("%d, ", point -> info); point = point -> next; } printf("%d\n\n", point -> info); printf("Input a node value to return the previous of (0 is head, this may require some counting!): "); int go; scanf("%d", &go); if (singleHead -> next != NULL && go != 0) //prevents a null pointer from being passed to Previous() { point = singleHead; int x; for (x = 0; x < go; x++) if (point -> next != NULL) point = point -> next; else { printf("Input out of bounds\n\n"); point = singleHead; //prevents if statement below from being called break; } if (Previous(singleHead, point) != NULL) printf("\nNode given: %d, Previous node: %d\n\n", point -> info, Previous(singleHead, point) -> info); } else printf("NULL, no previous for value given\n\n"); printf("Printed in reverse: "); PrintReverse(singleHead); printf("\nList without duplicates: "); singleHead = RemoveDuplicates(singleHead); point = singleHead; while(point -> next != NULL) { printf("%d, ", point -> info); point = point -> next; } printf("%d\n", point -> info); return 0; }