C++的入栈出栈问题

yly11 2008-03-13 04:42:16
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#define size 100
typedef struct sqst
{ int data[size];
int top;
}stack;
int n;
void create(stack * &sq)
{
sq=(stack *)malloc(sizeof(stack));
sq->top=0;
int x;
cout<<"gwshu:";
cin>>n;
for(int i=1;i<=n;i++)
{cin>>x;
sq->data[sq->top]=x;
sq->top++;}

}


insert(int x,stack * sq)
{ if(sq->top==size-1)
cout<<"over!";
else
{ sq->top++;
sq->data[sq->top]=x;
return 1;
}

}

gettop(stack * sq,int &x)
{ if (sq->top==-1)
return 0;
else
{ x=sq->data[sq->top];
sq->top--;
cout<<x;
return 1;
}
}





void main()
{ stack * sq;
create(sq);

int x=56;
insert(x,sq);
int i=1;
while (i<=(n+1))
gettop(sq,x);
}



为什么不能显示入栈后的结果?
...全文
152 4 打赏 收藏 举报
写回复
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyuzhu_1984 2008-03-13
  • 打赏
  • 举报
回复
下面是我该了以后的 有输出了 但是有错误 要下班了 有空在给你看


#include <iostream>
using namespace std;

#define size 100

typedef struct sqst
{
int data[size];
int top;
}stack;

int n;

void create(stack * &sq)
{
sq=(stack *)malloc(sizeof(stack));
sq->top=0;
int x;
cout<<"gwshu:";
cin>> n;
for(int i=1;i <=n;i++)
{
cin>> x;
sq->data[sq-> top]=x;
sq->top++;
}

}


int insert(int x,stack * sq)
{
if(sq->top==size-1)
cout<<"over!";
else
{
sq->top++;
sq->data[sq->top]=x;
return 1;
}

}

int gettop(stack * sq,int &x)
{
if (sq->top==-1)
return 0;
else
{
x=sq->data[sq->top];
sq->top--;
cout<<x;
return 1;
}
}


void main()
{
stack * sq;
create(sq);

int x=56;
insert(x,sq);
int i=1;
while (i <=(n+1))
gettop(sq,x);
}
baihacker 2008-03-13
  • 打赏
  • 举报
回复

#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#define size 100
typedef struct sqst
{
int data[size];
int top;
}stack;
int n;
void create(stack * &sq)
{
sq=(stack *)malloc(sizeof(stack));
sq-> top=0;
int x;
cout <<"gwshu:";
cin >> n;
for(int i=1;i <=n;i++)
{
cin>> x;
sq-> data[sq-> top++]=x;
}

}


insert(int x,stack * sq)
{
if(sq-> top==size)
cout <<"over!";
else
{
sq-> data[sq-> top++]=x;
return 1;
}

}

gettop(stack * sq,int &x)
{
if (sq-> top==0)
return 0;
else
{
x=sq-> data[--sq-> top];
cout << x;
return 1;
}
}


void main()
{
stack * sq;
create(sq);

int x=56;
insert(x,sq);
int i=1;
while (i++ <=(n+1))
gettop(sq,x);
}


ryfdizuo 2008-03-13
  • 打赏
  • 举报
回复

//测试:
//输入:
2
1
2
//输出:
56 2 1...
ryfdizuo 2008-03-13
  • 打赏
  • 举报
回复
#include <iostream> 
#include <stdio.h>
#include <stdlib.h>
using namespace std;

#define size 100
typedef struct sqst
{
int data[size];
int top;
}stack;

int n;
void create(stack * &sq)
{
sq=(stack *)malloc(sizeof(stack));
sq-> top=0;
int x;
cout <<"gwshu:";
cin>> n;
for(int i=1;i <=n;i++)
{
cin>> x;
sq-> data[sq-> top]=x; //顺序问题
sq-> top++; //top 始终指向下一个空的位置
}
}


int insert(int x,stack * sq)
{
if(sq-> top==size-1)
cout <<"over!";

else
{
sq-> data[sq-> top]=x;
sq-> top++;
return 1;
}
}

int gettop(stack * sq,int &x)
{
if (sq-> top==-1)
return 0;
else
{
sq-> top--; //顺序问题;
x=sq-> data[sq-> top];

cout <<x<<" ";
return 1;
}
}

void main()
{
stack * sq;
create(sq);

int x=56;
insert(x,sq);
int i=1;
while (i <=(n+1))
{
gettop(sq,x);
i++;
}
}
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-13 04:42
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下