gpt4 book ai didi

c - 使用链表创建堆栈时出错

转载 作者:行者123 更新时间:2023-11-30 18:41:24 25 4
gpt4 key购买 nike

我正在使用链表创建一个堆栈(我只在推送部分)。我已将我的结构声明为

struct LinkedStack {
int data;
struct LinkedStack* next;
};

我有 3 个全局声明的指向 LinkedStack 的指针。

struct LinkedStack *first = NULL;
struct LinkedStack *previous = NULL;
struct LinkedStack *current = NULL;

main()中我写了这个

int data = 0, choice = 0;
if(current == NULL) {
printf("\nNo Memory Allocated");
}

printf("\n1. Push Data");
printf("\n2. Pop Data");
printf("\n3. Display The Stack");
printf("\n4. Exit");

scanf("%d", &choice);

switch(choice) {
case 1:
printf("Enter the data:: ");
scanf("%d", &data);
push(&current, data);
break;
}

我写的推送函数是这样的-

void push(struct LinkedStruct **s, int usrdata) {
if(first == NULL) {
first = *s;
}
if(previous != NULL) {
previous->next = *s;
}

*s->data = usrdata;
previous = *s;
*s->next = NULL;
}

但这会导致编译失败。错误是-

Request for member 'data' in something which isn't a structure or an union
Request for member 'next' in something which isn't a structure or an union

这些错误的行号如下:

  *s->data = usrdata;
previous = *s;
*s->next = NULL;

你能告诉我为什么会发生这种情况吗?

最佳答案

“箭头”运算符具有更高的precedence比解引用运算符。因此,从编译器的角度来看,您已经编写了例如

*(s->data)=usrdata;

您需要使用括号:

(*s)->data=usrdata;

关于c - 使用链表创建堆栈时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21795921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com