【求助】用字符串链表实现字符串反转的问题
想用字符串链表实现字符串的部分反转:例如,输入: I have a cat
输出: cat a have I
我已经写好的链表结构,插入和反转函数如下,但没有想好在main函数中,如何把以空格分开的字符串存入链表中的char *word中,
麻烦大家帮忙想下
谢谢
程序代码:
struct strings { char *word; struct strings *next; }; struct strings *head; struct strings *insertWords(struct strings *head, char *input) { struct strings *currPtr, *newPtr; newPtr = (struct strings*)malloc(sizeof(struct strings)); if (newPtr != NULL) { currPtr = head; newPtr->word = input; newPtr->next = NULL; if (currPtr == NULL) { head = newPtr; } while(currPtr->next != NULL) { currPtr = currPtr->next; } currPtr->next = newPtr; } else { printf("No enough memory!\n"); } return head; } void stringReverse(struct string *head) { struct string *p, *q, *r; if(head == NULL) { return; } p = head; q = p->next; p->next = NULL; while(q != NULL) { r = q->next; q->next = p; p = q; q = r; } head = p; }