运行不了呀,求解答!

职场小白兔 2013-03-26 04:22:21

#include<iostream>
using namespace std;
#define LinkStack struct linkstack
typedef int ElemType;
LinkStack
{
ElemType data;
LinkStack *next;
};
//链栈的初始化
LinkStack* InitLinkStack()
{
LinkStack *top;
top=new LinkStack;
top->next='\0';
cout<<"已初始化一个链栈"<<endl;
return top;
}
//入栈
LinkStack* PushLinkStack(LinkStack *top,ElemType x)
{
LinkStack *s;
s=new LinkStack;
s->data=x;
s->next=top;
top=s;
return top;
}
//出栈
void PopLinkStack(LinkStack *top,ElemType *x)
{
LinkStack *p;
p=new LinkStack;
if(top->next=='\0')
cout<<"ERROR!"<<endl;
else {
*x=top->data;
p=top;
top=top->next;

}
free(p);
}
//主函数
#include"head.h"
void main()
{

LinkStack *top;
cout<<"初始化一个链栈"<<endl;
top=InitLinkStack();
cout<<"入栈一个元素:"<<endl;
ElemType x;
cin>>x;
top=PushLinkStack(top,x);
cout<<"出栈"<<endl;
ElemType *y;
PopLinkStack(top,y);
cout<<*y<<endl;
}
...全文
64 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tonforce 2013-03-26
  • 打赏
  • 举报
回复
顶楼上。 楼主可以把您代码的用途以及怎么发生错误、错误在哪个地方等详细信息告诉大家 这样会便于您的问题尽快得到回复。 因为不明确你代码的意思,所以没细看。我下面的简单修改,不知道合不合你的意思:

//出栈
void PopLinkStack(LinkStack *top,ElemType &x)
{
	LinkStack *p;
	p=new LinkStack;
	if(top->next=='\0')
		cout<<"ERROR!"<<endl;
	else  {
		x=top->data;
		p=top;
		top=top->next;

	}
	free(p);
}

void main()
{

	LinkStack *top;
	cout<<"初始化一个链栈"<<endl;
	top=InitLinkStack();
	cout<<"入栈一个元素:"<<endl;
	ElemType x;
	cin>>x;
	top=PushLinkStack(top,x);
	cout<<"出栈"<<endl;
	ElemType y;
	PopLinkStack(top,y);
	cout<<y<<endl;
	system("pause");
} 
职场小白兔 2013-03-26
  • 打赏
  • 举报
回复
学习了。。谢谢!
事后猪葛 2013-03-26
  • 打赏
  • 举报
回复
只是给了代码,建议你先看这个http://bbs.csdn.net/topics/390335073

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧