国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

棧 — 順序棧

2019-11-10 17:18:39
字體:
來源:轉載
供稿:網友

代碼

/* function:sequence stack created by : xilong date: 2017.2.9*/#include "iostream"#include <stdlib.h>#include <math.h>using namespace std;#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define STACK_SIZE 20#define STACK_INCREMENT 10typedef int Status;typedef int Elemtype;typedef struct{ Elemtype *base; Elemtype *top; int stackSize;} sqStack;/* initialize the stack*/void Init_Stack(sqStack *s){ s->base = (Elemtype *)malloc(STACK_SIZE *sizeof(Elemtype)); if (!s->base) { exit(0); } s->top = s->base; s->stackSize = STACK_SIZE;}/* push*/void Push_Stack(sqStack *s, Elemtype e){ if (s->top - s->base >= s->stackSize) { s->base = (Elemtype *)realloc(s->base, (s->stackSize + STACK_INCREMENT) * sizeof(Elemtype)); if (!s->base) { exit(0); } s->top = s->base + s->stackSize; s->stackSize = s->stackSize + STACK_INCREMENT; } *(s->top) = e; s->top++;}/* Pop*/void Pop_Stack(sqStack *s, Elemtype *e){ if (s->top == s->base) { exit(0); } *e = *--(s->top);}/* destory the stack*/Status Destory_Stack(sqStack *s){ int i; for (i = 0; i < s->stackSize; i++) { free(s->base); s->base++; } s->base = s->top = NULL; s->stackSize = 0; return OK;}/* the length of the stack*/int Length_Stack(sqStack *s){ return(s->top - s->base);}void main(){ sqStack s; Elemtype e; int len, i; Init_Stack(&s); Push_Stack(&s, 1); Push_Stack(&s, 2); Push_Stack(&s, 3); Push_Stack(&s, 4); Push_Stack(&s, 5); Push_Stack(&s, 6); cout << "the length of the stack currently:"; cout << Length_Stack(&s) << endl; len = Length_Stack(&s); cout << "出棧順序為:"; for (i = 0; i < len; i++) { Pop_Stack(&s, &e); cout << e << " "; } cout << endl; //Destory_Stack(&s); system("pause");}

截圖

這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 邵阳市| 嘉荫县| 阿坝县| 吉林市| 沐川县| 西昌市| 富裕县| 平邑县| 栾川县| 台南市| 临海市| 南丹县| 漾濞| 明溪县| 双江| 泰兴市| 哈尔滨市| 长垣县| 高淳县| 蓝田县| 揭阳市| 昌图县| 怀安县| 迭部县| 赞皇县| 都匀市| 大石桥市| 成武县| 邛崃市| 武山县| 全椒县| 澳门| 汽车| 阿合奇县| 信阳市| 昌邑市| 崇文区| 平湖市| 葵青区| 土默特左旗| 南雄市|