#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMNT 10
#define OVERFLOW 0
#define ERROR 0
#define OK 1
typedef struct {
int *top ;
int *base;
int stacksize;
}SqStack;
int InitStack(SqStack &L){
L.base=(int*)malloc(10*sizeof(int));
if(!L.base)exit(OVERFLOW);
L.top=L.base;
L.stacksize=10;
return OK; }
int GetTop(SqStack L,int &e)
{
if(L.base==L.top)return ERROR;
e=*(L.top-1);return OK;
}
int Push(SqStack L,int e)
{
if(L.top-L.base>=L.stacksize){
L.base=(int*)realloc(L.base,(L.stacksize+STACKINCREMNT) * sizeof(int));
L.top=L.base+L.stacksize;
L.stacksize=L.stacksize+STACKINCREMNT;
}
*(L.top++)=e;
return OK;
}
int Pop(SqStack L,int &e)
{
if(L.top!=L.base)
e=*(L.top--);
return e;
}
int main()
{
int i,n,m;
SqStack L;
InitStack (L);
for(i=0;i<6;i++){
scanf("%d",&n);
Push(L,n);}
for(m=0;m<6;m++){
Pop(L,n);
printf("%d\n",n);}
}
uj5u.com熱心網友回復:
供參考:#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMNT 10
#define OVERFLOW 0
#define ERROR 0
#define OK 1
typedef struct {
int *top ;
int *base;
int stacksize;
}SqStack;
int InitStack(SqStack &L)
{
L.base=(int*)malloc(10*sizeof(int));
if(!L.base) exit(OVERFLOW);
L.top=L.base;
L.stacksize=10;
return OK;
}
int GetTop(SqStack L,int &e)
{
if(L.base==L.top)return ERROR;
e=*(L.top-1);
return OK;
}
int Push(SqStack &L,int e) //int Push(SqStack L,int e)
{
if(L.top-L.base>=L.stacksize){
L.base=(int*)realloc(L.base,(L.stacksize+STACKINCREMNT) * sizeof(int));
L.top=L.base+L.stacksize;
L.stacksize=L.stacksize+STACKINCREMNT;
}
*(L.top++) = e;
return OK;
}
int Pop(SqStack &L,int &e) //int Pop(SqStack L,int &e)
{
if(L.top != L.base)
e=*(--L.top); //e=*(L.top--)
return OK; //e;
}
int main()
{
int i,n,m;
SqStack L;
InitStack (L);
for(i=0;i<6;i++){
scanf("%d",&n);
Push(L,n);
}
for(m=0;m<6;m++){
Pop(L,n);
printf("%d\n",n);
}
return 0;
}
uj5u.com熱心網友回復:
謝謝了,已經解決了,沒有取地址
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/277529.html
標籤:C語言
上一篇:如何安裝第三方庫?
下一篇:C++字符指標問題
