typedef struct elt*Stack; 为什么这里有*

use*_*999 1 c c++ stack pointers data-structures

这是使用链接列表的Stack实现的完整代码.这是来自詹姆斯·阿斯普内斯耶鲁大学的数据结构笔记(它有什么用?)

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct elt {
    struct elt *next;
    int value;
};

/* 
 * We could make a struct for this,
 * but it would have only one component,
 * so this is quicker.
 */
typedef struct elt *Stack;

#define STACK_EMPTY (0)

/* push a new value onto top of stack */
void
stackPush(Stack *s, int value)
{
    struct elt *e;

    e = malloc(sizeof(struct elt));
    assert(e);

    e->value = value;
    e->next = *s;
    *s = e;
}

int
stackEmpty(const Stack *s)
{
    return (*s == 0);
}

int
stackPop(Stack *s)
{
    int ret;
    struct elt *e;

    assert(!stackEmpty(s));

    ret = (*s)->value;

    /* patch out first element */
    e = *s;
    *s = e->next;

    free(e);

    return ret;
}

/* print contents of stack on a single line */
void
stackPrint(const Stack *s)
{
    struct elt *e;

    for(e = *s; e != 0; e = e->next) {
        printf("%d ", e->value);
    }

    putchar('\n');
}

int
main(int argc, char **argv)
{
    int i;
    Stack s;

    s = STACK_EMPTY;

    for(i = 0; i < 5; i++) {
        printf("push %d\n", i);
        stackPush(&s, i);
        stackPrint(&s);
    }

    while(!stackEmpty(&s)) {
        printf("pop gets %d\n", stackPop(&s));
        stackPrint(&s);
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我可以理解大部分代码.但我无法绕过这一部分 typedef struct elt *Stack;

为什么在Stack之前有一个*它是什么意思?

我发现了指针的概念,特别是在难以掌握的函数的返回类型中.提前致谢.

gio*_*gim 5

我可以理解大部分代码.但是我无法绕过这个部分的typedef struct elt*Stack;

那是使用typedef*.这只意味着你写的时候

Stack x;
Run Code Online (Sandbox Code Playgroud)

在你的代码中它意味着:

struct elt * x;
Run Code Online (Sandbox Code Playgroud)

如果你使用

Stack *s;
Run Code Online (Sandbox Code Playgroud)

在你的代码中它将意味着:

struct elt ** s;
Run Code Online (Sandbox Code Playgroud)

我发现了指针的概念,特别是在难以掌握的函数的返回类型中.提前致谢.

然后我建议你在继续使用该代码之前先了解指针和指针.


*我认为C和C++中的typedef存在一些细微差别:请参见此处