主题:请大家帮帮忙
ligonghaozi
[专家分:10] 发布于 2007-10-05 18:06:00
我刚刚学习数据结构,对指针不是非常熟悉,下面的代码是实现栈的链表存储结构,但是编译的时候出错了,看不懂是什么错误,请大家帮帮忙!
错误提示是:
--------------------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;
}
}
最后更新于:2007-10-05 18:48:00
回复列表 (共10个回复)
沙发
akon228 [专家分:40] 发布于 2007-10-05 20:51:00
#include<stdio.h>
#include<stdlib.h>
#define ElemType char
typedef struct Lsnode
{
ElemType data;
struct Lsnode *next;
}Lsnode;
typedef struct pointer
{
Lsnode *top,*end;
}Pointer;
void InitStack(Pointer s);
void Push(ElemType x,Pointer &s);
ElemType Pop(Pointer &s);
void OutStack(Pointer &s);
ElemType GetTop(Pointer &s);
void main()
{
Pointer p;
int cord,y;
ElemType 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:
{
InitStack(p);
}break;
case 2:
{
printf("\n请输入数据a=");
scanf("%c",&a);
Push(a,p);
OutStack(p);
}break;
case 3:
{
a=Pop(p);
printf("\n a=%d",a);
OutStack(p);
}break;
case 4:
{
y=GetTop(p);
printf("\n y=%d",y);
OutStack(p);
}break;
case 5:exit(0);
}
}while(cord<=5);
}
void InitStack(Pointer s)
{
s.top=s.end=(Lsnode *)malloc(sizeof(Lsnode));
s.top->next=NULL;
}
/*进栈*/
void Push(ElemType x,Pointer &s)
{
Lsnode *p;
p=(Lsnode *)malloc(sizeof(Lsnode));
p->data=x;
s.end->next=p;
s.end=p;
s.end->next=NULL;
}
/*删除栈顶元素*/
ElemType Pop(Pointer &s)
{
Lsnode *p;
ElemType x;
if(s.top->next!=NULL)
{
p=s.top->next;
s.top->next=p->next;
x=p->data;
free(p);
return x;
}
else
{
printf("Stack null!\n");
return'#';
}
}
/*各个元素的输出*/
void OutStack(Pointer &s)
{
Lsnode* p;
p=s.top->next;
printf("\n");
while(p!=NULL)
{
printf(" data=%c",p->data);
p=p->next;
}
printf("\n 输出结束 \n\n");
}
/*取出栈顶元素*/
ElemType GetTop(Pointer &s)
{
ElemType x;
Lsnode * p;
if(s.top->next==NULL)
{
printf("The Stack is Underflow!\n");
}
else
{
p=s.top->next;
x=p->data;
}
return x;
}
你的问题很多,帮你改了该。自己看看吧
3 楼
ligonghaozi [专家分:10] 发布于 2007-10-05 23:30:00
我把你的代码运行一下,编译成功,但是运行的时候有问题,就是插入一个元素的时候
说“遇到问题需要关闭,我们对此引起不便表示抱歉~~~~”。这是为什么?
4 楼
akon228 [专家分:40] 发布于 2007-10-06 00:51:00
I made a little mistake on last one. I revise it and it might be alright this time. Try it! Any other problems, let me know.
#include <iostream>
#define ElemType char
#define NULL 0
typedef struct Lsnode
{
ElemType data;
struct Lsnode *next;
}Lsnode;
typedef struct pointer
{
Lsnode *top;
Lsnode *end;
}Pointer;
void InitStack(Pointer &s);
void Push(ElemType x,Pointer &s);
ElemType Pop(Pointer &s);
void OutStack(Pointer &s);
ElemType GetTop(Pointer &s);
void main()
{
Pointer p;
int cord,y;
ElemType 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:
{
InitStack(p);
}break;
case 2:
{
printf("\n请输入数据a=");
std::cin>>a;
Push(a,p);
OutStack(p);
}break;
case 3:
{
a=Pop(p);
printf("\n a=%d",a);
OutStack(p);
}break;
case 4:
{
y=GetTop(p);
printf("\n y=%c",y);
OutStack(p);
}break;
case 5:exit(0);
}
}while(cord<=5);
}
void InitStack(Pointer &s)
{
s.top=s.end=new Lsnode;
s.top->next=NULL;
}
/*进栈*/
void Push(ElemType x,Pointer &s)
{
Lsnode *p;
p=new Lsnode;
p->data=x;
p->next=NULL;
s.end->next=p;
s.end=p;
}
/*删除栈顶元素*/
ElemType Pop(Pointer &s)
{
Lsnode *p;
ElemType x;
if(s.top->next!=NULL)
{
p=s.top->next;
s.top->next=p->next;
x=p->data;
delete p;
return x;
}
else
{
printf("Stack null!\n");
return'#';
}
}
/*各个元素的输出*/
void OutStack(Pointer &s)
{
Lsnode* p;
p=s.top->next;
printf("\n");
while(p!=NULL)
{
printf(" data=%c",p->data);
p=p->next;
}
printf("\n 输出结束 \n\n");
}
/*取出栈顶元素*/
ElemType GetTop(Pointer &s)
{
ElemType x;
Lsnode * p;
if(s.top->next==NULL)
{
printf("The Stack is Underflow!\n");
}
else
{
p=s.top->next;
x=p->data;
}
return x;
}
5 楼
ligonghaozi [专家分:10] 发布于 2007-10-07 09:51:00
好像又有点错误了,就是删除栈顶元素的时候,返回的元素不是栈顶元素,也不是栈里面的其他元素,而是其他数字。可能是Pop函数有问题。
6 楼
ligonghaozi [专家分:10] 发布于 2007-10-07 10:08:00
还有一个问题就是,当我,插入一个元素后,如果输入的元素是超过个位(即超过10以上的数包括10),则输出的是十位。好比我插入的是59这个元素,但是程序输出的是5;又好比我输入158,但程序输出的是1。??为什么,我看看程序也没什么问题!
7 楼
8825155600 [专家分:0] 发布于 2007-10-07 13:32:00
请问一下:outstack函数是什么功能,谢谢
8 楼
ligonghaozi [专家分:10] 发布于 2007-10-08 21:43:00
是将栈的元素依次输出~~~
9 楼
jhony [专家分:40] 发布于 2007-10-09 11:24:00
那个outstack不合常情,栈是一个一个的弹出,
下面除了这个函数不能实现,其他都行了
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef char ElemType;
typedef struct Lsnode
{ElemType data;
struct Lsnode *next;}
Lsnode;
void OutStack();
void InitStack(Lsnode *top);
void Push(ElemType x);
ElemType Pop();
ElemType GetTop();
Lsnode *top,*p,*q;
void main()
{
int cord,y;
Lsnode *top=new Lsnode;
int a;
do{
cout<<" 主菜单 \n";
cout<<" 1 初始化线性栈 \n";
cout<<" 2 插入一个元素 \n ";
cout<<" 3 删除在栈顶元素 \n";
cout<<" 4 取出栈顶元素 \n";
cout<<" 5 结束程序运行 \n";
cout<<"----------------------\n";
cout<<"请输入您的选择(1,2,3,4,5)"; cin>>cord;
switch(cord)
{
case 1:{
Lsnode *top1;
//top1= (Lsnode *)malloc(sizeof(Lsnode));
top1= new Lsnode ;
InitStack(top1);
}break;
case 2:{
cout<<"\n请输入数据a=";cin>>a;
Push(a);
//OutStack();
}break;
case 3:
a=Pop();
cout<<a<<endl;
//OutStack();
break;
case 4:{
y=GetTop();cout<<y;
//OutStack();
}break;
case 5:exit(0);
}
}while(cord<=5);
}
/*初始化栈*/
void InitStack(Lsnode *top)
{top->next=NULL;
}
/*进栈*/
void Push(ElemType x)
{
p=new Lsnode ;
//top=new Lsnode;
p->data=x;
p->next=top;
top=p;
}
/*删除栈顶元素*/
ElemType Pop()
{
int x;
Lsnode *t;
if(top!=NULL)
{
t=top;
top=top->next;
x=t->data;
delete t;
return x;
}
else
{
cout<<"Stack null!\n";
return 0;
}
}
/*各个元素的输出
void OutStack()
{
cout<<endl;
while(q!=NULL){
q=top;
cout<<q->data;
q=q->next;
}
cout<<"\n 输出结束 \n\n";
}
*/
/*取出栈顶元素*/
ElemType GetTop()
{
if(top==NULL){
return 0;
}
else{
return top->data;
}
}
10 楼
ligonghaozi [专家分:10] 发布于 2007-10-09 13:05:00
这是我改过后的栈的链表存储结构(c实现的),在这里Outlin是可以实现的:
/*栈的链表存储结构*/
#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct Lsnode
{ElemType data;
struct Lsnode *next;}Lsnode;
void OutStack(Lsnode *top);
void InitStack(Lsnode *top);
void Push(Lsnode *top,ElemType x);
ElemType Pop(Lsnode *top);
ElemType GetTop(Lsnode *top);
Lsnode *top1,*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:{
top1=(Lsnode *)malloc(sizeof(Lsnode));
InitStack(top1);
}break;
case 2:{
printf("\n请输入数据a=");
scanf("%d",&a);
Push(top1,a);OutStack(top1);
}break;
case 3:{
a=Pop(top1);printf("\n a=%d",a);
OutStack(top1);
}break;
case 4:{
y=GetTop(top1);printf("\n y=%d",y);
OutStack(top1);
}break;
case 5:exit(0);
}
}while(cord<=5);
}
/*初始化栈*/
void InitStack(Lsnode *top)
{top->next=NULL;
}
/*进栈*/
void Push(Lsnode *top,ElemType x)
{p=new 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;
delete p;
return x;
}
else{printf("Stack null!\n");return'#';}
}
/*各个元素的输出*/
void OutStack(Lsnode *top)
{p=top->next;
printf("\n");
while(p!=NULL){
printf("\n 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;
}
return x;
}
我来回复