标题:【求助】用字符串链表实现字符串反转的问题
只看楼主
neverlandzzy
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-3-26
结帖率:100%
 问题点数:0 回复次数:0 
【求助】用字符串链表实现字符串反转的问题
想用字符串链表实现字符串的部分反转:
例如,输入: 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;
}
搜索更多相关主题的帖子: 如何 word 字符串 
2011-10-27 11:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-353563-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017107 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved