朋友您好,这是我的第一篇文章,发帖时我可能做错了,对不起。
我正在尝试使用堆栈以相反的顺序打印String。我试图将最高值输入字符数组的那一点我在该部分遇到错误,请帮忙。
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct StackNode
{
char data;
struct StackNode* next;
};
struct StackNode* createNode(char data){
struct StackNode* stack=(struct StackNode*)malloc(sizeof(struct StackNode));
stack->data=data;
stack->next=NULL;
return stack;
}
void push(struct StackNode** root,char data)
{
struct StackNode* stack=createNode(data);
stack->next=*root;
*root=stack;
}
char top(struct StackNode** root)
{
return (*root)->data;
}
void pop(struct StackNode** root, char c[],int k)
{
int i;
for(i=0;i<=k;i++)
{
c[i]=top(&root);
*root=(*root)->next;
}
}
void print(struct StackNode* root)
{
while(root!=NULL)
{
printf("%c",root->data);
root=root->next;
printf(" ");
}
printf("\n");
}
int main()
{
struct StackNode* root=NULL;
char c[]="Sherry";
int k=strlen(c);
int i;
for(i=0;i<=k;i++)
{
push(&root,c[i]);
}
pop(&root,c,k);
for(i=0;i<=k;i++)
{
printf("%c",c[i]);
printf(" ");
}
return 0;
}
stackreviseLinkedList.c: In function ‘pop’:
stackreviseLinkedList.c:36:1: warning: passing argument 1 of ‘top’ from incompatible pointer type [enabled by default]
c[i]=top(&root);
^
stackreviseLinkedList.c:26:6: note: expected ‘struct StackNode **’ but argument is of type ‘struct StackNode ***’
char top(struct StackNode** root)
答案 0 :(得分:1)
编译器在这里明确指出了错误(免责声明:除了编译器错误,可能还有其他问题,我没有尝试运行程序!)
c[i]=top(&root); is the culprit
应该是
c[i]=top(root); // no & required
看第36行
stackreviseLinkedList.c: In function ‘pop’:
stackreviseLinkedList.c:36:1: warning: passing argument 1 of ‘top’ from incompatible pointer type [enabled by default]
c[i]=top(&root);
^
stackreviseLinkedList.c:26:6: note: expected ‘struct StackNode **’ but argument is of type ‘struct StackNode ***’
char top(struct StackNode** root)
答案 1 :(得分:0)
失败的主要问题是:
行:
c[i]=top(&root);
应该是:
c[i]=top(root);
现在不会伤害你的其他问题,但只是糟糕的编程: