【问题标题】:Reverse a sentence using stack in C在C中使用堆栈反转句子
【发布时间】:2014-02-18 08:55:13
【问题描述】:

我想在 c 中使用堆栈来反转一个句子。 例如。你好吗 => 你怎么样。

我写了以下程序

#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();      
}

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

【问题讨论】:

标签: c data-structures stack


【解决方案1】:

阅读完整的行。在空格上标记字符串,将每个单词推入堆栈。打印时弹出堆栈。

有用的功能:

不到20行就可以完成完整的解决方案(包括结构定义、头文件等)


您的代码中的未定义行为也存在问题。您有一个指针p,它指向一个 constant 字符数组(所有字符串文字都是常量字符数组)。然后你尝试修改那个常量数组。

你可能想要这样的东西:

char arr[] = "Some string here";
char *p = arr;

您还有另一个未定义行为的情况:您有未初始化的指针t。然后,您继续取消引用它。我会说你很幸运没有撞车。

您也没有在循环中更新t,您可能应该这样做。

【讨论】:

  • 我做了同样的事情。我开始阅读字符串。遇到空格时,我将单词推入堆栈中的单词中。并继续该过程。到达字符串的末尾。我说要弹出堆栈的元素并打印它们..问题是它只是打印堆栈的最后一个(顶部)元素..
  • @vicky 那么你实际上并没有正确弹出堆栈。
  • 这就是我要问的......我无法找出错误......希望你能帮助我。谢谢
  • @vicky 用一个非常严重的问题更新了我的答案。
【解决方案2】:

首先,您的char *t 只是一个指针,使其指向分配的内存然后继续...我什至不明白代码是如何运行的...您正在做*(t + j) 而实际上是指向垃圾。

第一眼你在解析字符串后覆盖t...。即您设置j = 0 并覆盖以前存储的字符串,并且您的struct rev 持有指向此t 的指针,因此 你得到you? you? you? 作为输出。而不是在struct rev 中让char *var 指向t .. 你的char *var 指向分配的内存并执行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

PS:我不知道您在代码的哪一部分实现push|pop ...您正在使用列表并告诉您想要一个堆栈。 Stack 和 List 是两种不同的数据结构。

【讨论】:

    【解决方案3】:

    您可以首先初始化变量t 以指向一个有效的内存地址。

    【讨论】:

      【解决方案4】:

      看来“秀”的逻辑写错了-

      show = top;
      while(show != NULL) {
          printf("%s\n", show->var);
          show = show->next;               
      }
      

      将 show 指向顶部并打印“show->next”是从堆栈中弹出的错误方式。

      【讨论】:

      • 您能否为原始海报提供解决方案?
      【解决方案5】:

      这里是pop函数的一个例子-

      int pop(STACK **head) {
      if (empty(*head)) {                         
         fputs("Error: stack underflow\n", stderr);
         abort();
      } else {                                    
          STACK *top = *head;
          int value = top->data;
          *head = top->next;
          free(top);
          return value;}}
      

      【讨论】:

        猜你喜欢
        • 2018-03-09
        • 2014-03-10
        • 2016-01-03
        • 1970-01-01
        • 1970-01-01
        • 2021-07-07
        • 2014-07-14
        • 2017-08-12
        • 2020-05-17
        相关资源
        最近更新 更多