【问题标题】:How to implement Stack using Char as argument?如何使用 Char 作为参数来实现 Stack?
【发布时间】:2019-10-30 17:36:51
【问题描述】:

我有这个任务,我必须实现一个堆栈来使用我的教授给出的以下结构。如您所知,我不知道如何使用以下结构实现堆栈。

struct elements{
     char word;
     struct elements *next;
}

我确实知道如何使用下面的结构来做到这一点...

struct elements{
     struct student data;
     struct elements *next;
}

要将数据插入我要使用的堆栈中...我知道下面的代码是正确的。我明白那里发生了什么。但是我看不出我怎么能用char word而不是struct student data;来做到这一点有人可以向我解释吗?我不明白。

int pushStack(Stack* ptr, struct student info){
    Elemn* node = (Elemn*)malloc(sizeof(Elemn);

    if(node == NULL && ptr == NULL){
    printf("error.");
    return 0;
    }

    node->student = info;
    node->next = (*ptr);
    *ptr = node;
    return 1;
}

【问题讨论】:

  • 为什么一个字符被命名为单词(char word;)?:)
  • @tadman 在函数声明中已经使用了指向 Stack* ptr 指针的指针 例如考虑语句 *ptr = node;
  • @VladfromMoscow 如果Stack 本身是一个非常令人讨厌的指针类型。类型应该更明确地说明它们的性质。
  • @VladfromMoscow 这是任务的一部分。我必须处理那个字符。我想使用 char 而不是 struct student data 使代码工作;但我不知道该怎么做......

标签: c data-structures stack


【解决方案1】:

您是否尝试将struct student 替换为char
当然在函数头中,也在Elemn 类中。

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2012-06-23
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多