當前位置:首頁 » 操作系統 » 棧初始化演算法

棧初始化演算法

發布時間: 2024-05-28 19:35:25

A. 求用C語言編寫一個程序實現順序棧初始化,出棧,入棧,判棧空,判棧滿,急需,謝謝

#define STACK_SIZE 100
#define PUSH_POP_SUCCESS 1
#define PUSH_POP_ERROR 0
struct _stackbuf {
int _collection[STACK_SIZE];
int _top;
};
typedef struct _stackbuf S_STACK;
typedef unsigned int u_int_f;
// 入棧
u_int_f push(S_STACK *stack, int d){
if (stack->_top >= STACK_SIZE) return PUSH_POP_ERROR;
stack->_collection[stack->_top++] = d;
return PUSH_POP_SUCCESS;
}
// 出棧
u_int_f pop(S_STACK *stack, int *e){
if (!stack->_top) return PUSH_POP_ERROR;
*e=stack->_collection[--(stack->_top)];
return PUSH_POP_SUCCESS;
}
int main(){
S_STACK stack = { {0},0 };
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
int gv = 0;
pop(&stack, &gv);
printf("%d\n", gv);
system("PAUSE");
return 0;
}

B. C語言編程實現順序棧的初始化,入棧,出棧,取棧頂元素,顯示操作

#define STACKSIZE 100
int mstack[STACKSIZE],top,bottom;
void mInitStack() { top=bottom=0; }
void mPush(int x) { if ( top-bottom<=STACKSIZE ) { mstack[top]=x; top++; } }
int mPop() { int r=0; if ( top>bottom ) { r=mstack[top]; top--; } return r; }
void mShowStack() { int i; printf("["); for ( i=bottom;i<top;i++ ) printf("%d ",mstack[i]); printf("] "); }
void main()
{
int i,n,x,loop=1,s;
char buffer[80];
mInitStack();
scanf("%d",&n); for ( i=0;i<n;i++ ) { scanf("%d",&x); mPush(x); }
mShowStack();
while ( loop )
{ buffer[1]=0; gets(buffer); s=1;
switch ( buffer[1] )
{ case 'O':
case 'o': x=mPop(); break;
case 'U':
case 'u': x=atoi(buffer+5); mPush(x); break;
case 'n':
case 'N': loop=0; break;
default: s=0; break;
}
mShowStack();
}
mShowStack();

}

熱點內容
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:100
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:208
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554
雲伺服器的優點與缺點 發布:2024-09-08 06:30:34 瀏覽:734
上傳下載賺錢 發布:2024-09-08 06:14:51 瀏覽:258