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

棧初始化演算法

發布時間: 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();

}

熱點內容
連接ftp異常中斷 發布:2025-09-16 12:52:33 瀏覽:287
3m移動辦公伺服器地址大全 發布:2025-09-16 12:22:35 瀏覽:256
什麼是直男的快樂密碼 發布:2025-09-16 12:22:32 瀏覽:910
修改資料庫存儲引擎 發布:2025-09-16 12:21:48 瀏覽:77
安徽調度伺服器品牌雲伺服器 發布:2025-09-16 12:02:17 瀏覽:777
資料庫表設計教程 發布:2025-09-16 10:50:47 瀏覽:348
朋友圈緩存如何清除 發布:2025-09-16 10:49:57 瀏覽:446
sqlserver數據類型 發布:2025-09-16 10:41:16 瀏覽:740
如何配置全站時間同步系統 發布:2025-09-16 10:19:13 瀏覽:177
java解析json文件 發布:2025-09-16 10:10:41 瀏覽:977