| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 898 人关注过本帖
标题:[求助] 用链表实现多个字符串翻转
只看楼主 加入收藏
neverlandzzy
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-3-26
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:3 
[求助] 用链表实现多个字符串翻转
想用字符串链表实现字符串的部分反转:
例如,输入: I have a cat
      输出:  cat a have I

测试发现,各函数功能貌似都没问题(代码里有测试用的main())。 但在main()中输入并以空格分割字符串,插入链表时有问题,只有最后一个字符串cat能被插入,输出变成:
cat cat cat cat

希望高手能帮忙看下~

谢啦

程序代码:
#include <stdio.h>
#include <stdlib.h>

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;
            currPtr = head;
            printf("done!\n");
            return head;
            
        }

        while(currPtr->next != NULL)
        {
            currPtr = currPtr->next;
        }

        currPtr->next = newPtr;
    }

    else
    {
        printf("No enough memory!\n");
    }
    //printf("done!\n");
    return head;
}

struct strings* stringReverse(struct strings *head)
{
    struct strings *p, *q, *r;
    
    if(head == NULL)
    {
        return NULL;
    }
    
    p = head;
    q = head->next;
    p->next = NULL;
    

    while(q != NULL)
    {
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }

    head = p;
    return head;
}

void stringDisp(struct strings *head)
{
    struct strings *p1;
        
    p1 = head;

    while (p1 != NULL)
    {
        printf("%s", p1->word);
    
        printf(" ");
        p1 = p1->next;
    }

    printf("\n");
}

main()
{
    int i;
    char *strings_input = "I have a cat", *strings; 
    char temp[100] ;
        
    head = NULL;

    if(strings_input == NULL)
    {
        printf("String is empty!\n");
        exit(0);
    }

    while (*strings_input != '\0')
    {
    
        for(i = 0; *strings_input != ' '&& *strings_input != '\0'; strings_input++,i++)
        {
            temp[i]=*strings_input;
            
                    
        }
        temp[i] = '\0';

        strings = temp;
        
           if(*strings_input != '\0')
        strings_input++;
        //printf("temp = %s\n",temp);

        head = insertWords(head, strings);
        //stringDisp(head);
                
    }
    
    stringDisp(head);
    system("pause");

}


/*main()
{
    char *test1 = "first", *test2 = "second", *test3 = "third";
    head = NULL;

    head = insertWords(head,test1);
    head = insertWords(head,test2);
    head = insertWords(head,test3);

    stringDisp(head);
    head = stringReverse(head);
    stringDisp(head);
    
    system("pause");


}
*/
搜索更多相关主题的帖子: 测试 字符串 color 
2011-10-31 09:57
『点点滴滴』
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:168
专家分:1035
注 册:2007-7-9
收藏
得分:5 
程序代码:
结构体字符串指针有问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct strings
{
    char word[100];
    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;
       
        strcpy( newPtr->word , input ) ;
        newPtr->next = NULL;

        if (currPtr == NULL)
        {
            head = newPtr;
            printf("done!\n");
            return head;
           
        }

        newPtr->next = head ;
        head = newPtr ;

    }

    else
    {
        printf("No enough memory!\n");
    }
    //printf("done!\n");
    return head;
}

struct strings* stringReverse(struct strings *head)
{
    struct strings *p, *q, *r;
   
    if(head == NULL)
    {
        return NULL;
    }
   
    p = head;
    q = head->next;
    p->next = NULL;
    

    while(q != NULL)
    {
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }

    head = p;
    return head;
}

void stringDisp(struct strings *head)
{
    struct strings *p1;
       
    p1 = head;

    while (p1 != NULL)
    {
        printf("%s", p1->word);
   
        printf(" ");
        p1 = p1->next;
    }

    printf("\n");
}

int main()
{
    int i;
    char *strings_input = "I have a cat", *strings;
    char temp[100] ;
       
    head = NULL;

    if(strings_input == NULL)
    {
        printf("String is empty!\n");
        exit(0);
    }

    while (*strings_input != '\0')
    {
   
        for(i = 0; *strings_input != ' '&& *strings_input != '\0'; strings_input++,i++)
        {
            temp[i]=*strings_input;
           
                   
        }
        temp[i] = '\0';

        strings = temp ;
       
           if(*strings_input != '\0')
        strings_input++;
        //printf("temp = %s\n",temp);

        head = insertWords(head, strings);
        //stringDisp(head);
               
    }
   
    stringDisp(head);
    system("pause");

}


/*main()
{
    char *test1 = "first", *test2 = "second", *test3 = "third";
    head = NULL;

    head = insertWords(head,test1);
    head = insertWords(head,test2);
    head = insertWords(head,test3);

    stringDisp(head);
    head = stringReverse(head);
    stringDisp(head);
   
    system("pause");


}
*/

2011-10-31 10:38
neverlandzzy
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2011-3-26
收藏
得分:0 
回复 2楼 『点点滴滴』
谢了!

为啥用字符指针不行呢?我用注释掉的那段main测试没问题,插入翻转都可以,问题在哪里呢?
2011-10-31 11:36
liao06550107
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:2
帖 子:111
专家分:696
注 册:2011-10-2
收藏
得分:5 

程序代码:
#include <stdio.h>
#include <stdlib.h>

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;
            printf("done!\n");
            return head;
           
        }

        newPtr->next = head;
        head = newPtr;
    }

    else
    {
        printf("No enough memory!\n");
    }
    //printf("done!\n");
    return head;
}

struct strings* stringReverse(struct strings *head)
{
    struct strings *p, *q, *r;
   
    if(head == NULL)
    {
        return NULL;
    }
   
    p = head;
    q = head->next;
    p->next = NULL;
    

    while(q != NULL)
    {
        r = q->next;
        q->next = p;
        p = q;
        q = r;
    }

    head = p;
    return head;
}

void stringDisp(struct strings *head)
{
    struct strings *p1;
       
    p1 = head;

    while (p1 != NULL)
    {
        printf("%s", p1->word);
   
        printf(" ");
        p1 = p1->next;
    }

    printf("\n");
}

main()
{
    int i=0,j;
    char *strings_input = "I have a cat", *strings;
    char temp[10][50] ;
       
    head = NULL;

    if(strings_input == NULL)
    {
        printf("String is empty!\n");
        exit(0);
    }

    while (*strings_input != '\0')
    {
   
        for(j = 0; *strings_input != ' '&& *strings_input != '\0'; strings_input++,j++)
        {
            temp[i][j]=*strings_input;
           
                   
        }
        temp[i][j] = '\0';
        strings = temp[i];
        i++;    
           if(*strings_input != '\0')
        strings_input++;
        //printf("temp = %s\n",temp[i]);

        head = insertWords(head, strings);
       // stringDisp(head);
               
    }
   
    stringDisp(head);
    system("pause");

}

结构体中储存的是指向数组的地址(即指针)主函数中while循环每次都把数组内容重写(虽然首地址没变)而导致链表中出现多个重复数组地址。


[ 本帖最后由 liao06550107 于 2011-10-31 16:05 编辑 ]

听不同的音乐,看不同的书,游历不同的城市,邂逅不同的人,走的多了,站的高了,自然就看的远了。
2011-10-31 16:02
快速回复:[求助] 用链表实现多个字符串翻转
数据加载中...
 
   



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

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