我想用c语言中的stack反转一个句子。
你怎么样。
我写了以下程序

#include<stdio.h>
#include<conio.h>
struct rev
{
    char *var;
    struct rev *next;
};
struct rev *top=NULL;
struct rev *temp,*test;
main()
{
    int start, j = 0;
    char *p="Hi, How are you? Hope everything is fine";
    char *s;
    char *t;
    *t = '\0';
    s = p;
    while(1) {
        if(*p == ' '|| *p == '\0') {
            printf("Inside if \n");
            *(t + j) = '\0';
            printf("This is t %s\n",t);

            if(top == NULL) {
                temp = (struct rev *)malloc(sizeof(struct rev));
                temp->next = NULL;
                temp->var=t;
                top = temp;
            } else {
                temp = (struct rev *)malloc(sizeof(struct rev));
                printf("This is going in stack %s\n", t);
                temp->var = t;
                temp->next = top;
                top = temp;
                printf("This is top %s\n", top->var);
            }
            j = 0;
        } else {
            *(t+j) = *p;
            printf("%c\n", *p);
            j++;
        }
        if(*p == '\0') {
            break;
        }

        //printf("%c",*p);
        p++;
    }
    struct rev *show;
    show = top;
    while(show != NULL) {
        printf("%s\n", show->var);
        show = show->next;
    }

    getch();
}

它存储正确,但在遍历时只给出最后一个元素。
我想不出是什么问题。
这是我的输出窗口:-

最佳答案

首先,你的char *t只是一个指针,让它指向malloced内存,然后继续。。。我不明白代码是怎么运行的。。。当t实际上指向垃圾时,您正在执行*(t + j)
在第一次查看时,您正在覆盖t。。。在分析字符串之后。即您设置j = 0并覆盖先前存储的字符串,并且您的struct rev持有指向此t的指针,因此
你将得到you? you? you?作为输出。而不是让char *var中的struct rev指向t。。您的char *var指向一个malloced内存并执行strcpystrtok
我只是对你的代码做了一个粗略的修改,它在linux+gcc上对我有效。。。代码如下:

#include<stdio.h>
#include <stdlib.h>
struct rev
{
    char *var;
    struct rev *next;
};
struct rev *top=NULL;
struct rev *temp,*test;
main()
{
    int start, j = 0;
    char *p="How are you?";
    char *s;
    char *t;
    t = malloc(1000);
    if (t == NULL) {
        //OUT OF MEMORY
        exit(1);
    }
    s = p;
    while(1) {
        if(*p == ' '|| *p == '\0') {
            printf("Inside if \n");
            *(t + j) = '\0';
            printf("This is t %s\n",t);

            if(top == NULL) {
                temp = (struct rev *)malloc(sizeof(struct rev));
                temp->next = NULL;
                temp->var = malloc(100);
                if (temp->var == NULL) {
                   //OUT OF MEMORY
                   exit(1);
                }
                strcpy(temp->var, t);
                top = temp;
            } else {
                temp = (struct rev *)malloc(sizeof(struct rev));
                printf("This is going in stack %s\n", t);
                temp->var = malloc(100);
                 if (temp->var == NULL) {
                   //OUT OF MEMORY
                   exit(1);
                }
                strcpy(temp->var, t);
                temp->next = top;
                top = temp;
                printf("This is top %s\n", top->var);
            }
            j = 0;
        } else {
            *(t+j) = *p;
            printf("%c\n", *p);
            j++;
        }
        if(*p == '\0') {
            break;
        }

        //printf("%c",*p);
        p++;
    }
    struct rev *show;
    show = top;
    while(show != NULL) {
        printf("%s\n", show->var);
        show = show->next;
    }

    //getch();
}

输出如下:
H
o
w
Inside if
This is t How
a
r
e
Inside if
This is t are
This is going in stack are
This is top are
y
o
u
?
Inside if
This is t you?
This is going in stack you?
This is top you?
you?
are
How

注:我不理解你在实现代码的哪个部分。。。你正在使用list并告诉你想要一个堆栈。堆栈和列表是两种不同的数据结构。

10-07 13:35
查看更多