6.3w+
社区成员
#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);
}
//测试:
//输入:
2
1
2
//输出:
56 2 1...
#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++;
}
}