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

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

棧 — 順序棧

2019-11-09 19:36:29
字體:
來源:轉載
供稿:網友

代碼

/* 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");}

截圖

這里寫圖片描述


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大关县| 威宁| 方山县| 博白县| 阳朔县| 镇江市| 平阳县| 星子县| 博爱县| 新宾| 色达县| 霍城县| 桐城市| 龙井市| 麻城市| 景德镇市| 莱芜市| 金乡县| 腾冲县| 沛县| 遵义县| 扬州市| 临桂县| 措勤县| 台中县| 奇台县| 县级市| 青阳县| 耒阳市| 全州县| 沙洋县| 宁德市| 民乐县| 谢通门县| 巍山| 柘荣县| 万安县| 彭山县| 北辰区| 大兴区| 常熟市|