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

棧初始化演算法

發布時間: 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-07-27 09:36:03 瀏覽:132
羊創意腳本 發布:2024-07-27 09:29:30 瀏覽:894
榮耀v20升級存儲 發布:2024-07-27 09:20:19 瀏覽:485
安卓用什麼和電腦傳圖片 發布:2024-07-27 09:02:07 瀏覽:288
存儲過程就是 發布:2024-07-27 08:56:51 瀏覽:131
c語言高級試題 發布:2024-07-27 08:48:30 瀏覽:282
ip伺服器世界上有幾台 發布:2024-07-27 08:46:18 瀏覽:394
金立手機怎麼清理緩存 發布:2024-07-27 08:38:50 瀏覽:311
iphone文件夾不顯示 發布:2024-07-27 08:18:05 瀏覽:774
y510p固態硬碟做緩存 發布:2024-07-27 07:59:34 瀏覽:128