读书人

请大家棒棒忙,该如何处理

发布时间: 2012-02-17 17:50:41 作者: rapoo

请大家棒棒忙
我刚刚学习数据结构,对指针不是非常熟悉,下面的代码是实现栈的链表存储结构,但是编译的时候出错了,看不懂是什么错误,请大家帮帮忙!
错误提示是:
--------------------Configuration: Cpp2 - Win32 Debug--------------------
Compiling...
Cpp2.cpp
E:\数据结构\第二次上机\栈的链表结构\Cpp2.cpp(35) : error C2664: 'Push' : cannot convert parameter 1 from 'struct Lsnode ** ' to 'struct Lsnode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\数据结构\第二次上机\栈的链表结构\Cpp2.cpp(38) : error C2664: 'Pop' : cannot convert parameter 1 from 'struct Lsnode ** ' to 'struct Lsnode *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

Cpp2.obj - 1 error(s), 0 warning(s)



代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;
typedef struct Lsnode
{ElemType data;
struct Lsnode *next;}
Lsnode;
void OutStack(Lsnode *h);
void InitStack(Lsnode *top);
void Push(Lsnode *top,ElemType x);
ElemType Pop(Lsnode *top);
ElemType GetTop(Lsnode *top);
Lsnode *top,*p,*q;
void main()
{int cord,y;
int a;
do{
printf(" 主菜单 \n");
printf(" 1 初始化线性栈 \n");
printf(" 2 插入一个元素 \n ");
printf(" 3 删除在栈顶元素 \n");
printf(" 4 取出栈顶元素 \n");
printf(" 5 结束程序运行 \n");
printf("----------------------\n");
printf("请输入您的选择(1,2,3,4,5)"); scanf("%d",&cord);
switch(cord)
{
case 1:{
Lsnode *top1;
top1=(Lsnode *)malloc(sizeof(Lsnode));
InitStack(top1);
}break;
case 2:{
printf("\n请输入数据a=");scanf("%d",&a);
Push(&top,a);OutStack(q);
}break;
case 3:{
a=Pop(&top);printf("\n a=%d",a);
OutStack(q);
}break;
case 4:{
y=GetTop(q);printf("\n y=%d",y);
OutStack(q);
}break;
case 5:exit(0);
}
}while(cord<=5);
}

/*初始化栈*/
void InitStack(Lsnode *top)
{top->next=NULL;
}

/*进栈*/
void Push(Lsnode *top,ElemType x)
{p=(Lsnode *)malloc(sizeof(Lsnode));
p->data=x;
p->next=top->next;
top->next=p;}


/*删除栈顶元素*/
ElemType pop(Lsnode *top)
{int x;
if(top->next!=NULL)
{p=top->next;
top->next=p->next;
x=p->data;
free(p);
return x;
}
else{printf("Stack null!\n");return'#';}
}


/*各个元素的输出*/
void OutStack(Lsnode *top)
{p=top->next;
printf("\n");
while(p!=NULL){
printf(" data=%d",p->data);
p=p->next;
}
printf("\n 输出结束 \n\n");
}


/*取出栈顶元素*/
ElemType GetTop(Lsnode *top)
{ElemType x;
if(top->next==NULL){
printf("The Stack is Underflow!\n");
}
else{
p=top->next;
x=p->data;
}
}


[解决办法]
cannot convert parameter 1 from 'struct Lsnode ** ' to 'struct Lsnode *
---------------------------------------------
错误信息说的很清楚啊,类型不匹配。
case 1:{
Lsnode *top1;
top1=(Lsnode *)malloc(sizeof(Lsnode));
InitStack(top1);
}break;
case 2:{
printf("\n请输入数据a=");scanf("%d",&a);
Push(&top,a);OutStack(q);
}break;
case 3:{
a=Pop(&top);printf("\n a=%d",a);
OutStack(q);
}break;

push和pop的参数类型为void Push(Lsnode *top,ElemType x); ElemType Pop(Lsnode *top);你给的类型为&top,把&去掉就OK了。

读书人网 >软件架构设计

热点推荐