int main(int argc,char** argv) { STACK *p=createstack(); int i; for(i=0;i<10;i++){ element e; e.key=i*5; if(push(p,e)){ printf("push error\n"); return -1; } } stackempty(p); for(i=0;i<11;i++){ element e; if(pop(p,&e)==-1){ printf("pop error\n"); return -1; } else{ printf("top element of the element of stack is %d\n",e.key); } } return 0; }
bool gettop(stack* s,char* c) { if(stackempty(s)) return false; *c = *(s->top -1); return true; }
static inline list MSD(list a, int n) { list res = NULL; stack *s; if (n < 2) return a; initmem(stackmem, sizeof(struct stackrec), n/50); push(a, NULL, n, 0); while (!stackempty()) { s = pop(); if (!s->size) { /* finished */ s->tail->next = res; res = s->head; continue; } if (s->size <= BYTE_BREAK) onebyte(s->head, s->pos); else twobytes(s->head, s->pos); } freemem(stackmem); return res; }
bool pop(stack* s,char* c) { if(stackempty(s)) return false; *c = *(--s->top); return true; }
traverse(struct node *t){ push(t); while(!stackempty()){ t = pop(); visit(t); if(t->r != z) push(t->r); if(t->l != z) push(t->l); } }
int stack_pop() { if(stackempty(s)) //出栈前先判断当前栈中是否有内容 printf("stack is empty\n"); else { return s->data[s->top--]; //出栈后s->top的值会自减1 } }
int pop(stack *st){ if(stackempty(st)){ puts("stack underflow\n"); return 0; } int temp = st->top; st->top--; return st->arr[temp]; }
void get(stackentry1 *item,stack1 *s) { if(stackempty(s)) { printf("stack is empty"); } else { *item=s->entry[--(s->top)]; } }
void pop(stackentry *item,stack *s) { if(stackempty(s)) { printf("stack is empty"); } else { *item=s->entry[--(s->top)]; } }
int main(){ stack st; int in; buildstack(&st); while(scanf("%d",&in)&& in != -1){ push(&st, in); } while(!stackempty(&st)){ printf("%d - ",pop(&st)); } return 0; }
void intraverse2(TREE T){ TREE p; Sqstack S; initstack(&S);p=T; while(p||stackempty(S)){//节点不为空或者栈不空时循环 if(p) { push(&S,p); p=p->left; } else{ pop(&S,&p); print(p->data); p=p->right; } } }
int main() { struct tree *root=NULL; insert(&root,10); insert(&root,20); insert(&root,5); insert(&root,4); insert(&root,6); struct stack *s; s=createstack(); struct tree *c=root; c->vl=0; c->vr=0; push(s,c); while( !stackempty(s) ) { while( ((tree*)stacktop(s))->left!=NULL && !((tree*)stacktop(s))->vl ) { struct tree *n=(tree*)stacktop(s); n->vl=1; push(s,n->left); } while( ((tree*)stacktop(s))->right!=NULL && !((tree*)stacktop(s))->vr ) { struct tree *n=(tree*)stacktop(s); n->vr=1; push(s,n->right); } struct tree *n=(tree*)pop(s); printf("%d ",n->d); } getch(); }
// recursively decides what to do based on status of stack void evalstack(statement *stmt) { stackobj *obj; if (stackempty()) push(token, token_type); else { if (checkprec(token, token_type)) push(token, token_type); else { obj = pop(); buffermeta(stmt, obj->type, obj->name); evalstack(stmt); GC_free(obj); } } }
static void mark_dfs (unsigned j, int cnt, int *Ap, int *Ai, int *clique) { int i, k; push (j); while (!stackempty()) { j = pop(); clique[j] = cnt; for (k = Ap[j]; k < Ap[j+1]; k++) { i = Ai[k]; if (clique[i] == 0) { push (i); clique[i] = -1; // to only push once } } } }
static void ontostack(bucket *b, int pos) { b->tail->next = NULL; if (b->size <= INSERTBREAK) { if (b->size > 1) b->head = ListInsertsort(b->head, &b->tail, pos); b->size = 0; /* finished */ } if (!b->size && !stackempty() && !top()->size) { top()->tail->next = b->head; top()->tail = b->tail; } else { push(b->head, b->tail, b->size, pos); b->size = 0; } b->head = NULL; }
int combination(int n,int m){ int sum=0; elemtype x; struct stack stk; initstack(&stk); x.n=n;x.m=m; push(&stk,x); while(!stackempty(&stk)){ x=pop(&stk); if(x.n==x.m||!x.m)sum++; else{ x.n--; push(&stk,x); x.m--; push(&stk,x); } } return sum; }
int combination(int n, int m){ struct stack s; initstack(&s);//初期化 comb a={n,m}; int ans=0; push(&s,a); while(!stackempty(&s)){ a=pop(&s); if(a.n==a.m||a.m==0||a.n==1){ return ans+1; }else{ a.n=a.n-1; push(&s,a); a.m=a.m-1; push(&s,a); } } return ans; }
int pop() { if (stackempty()) return 0; else return stack[--p]; }
void base_quicksort7(unsigned int a[], int N) { int l, r; int i; int m; int il, ir; /* names follow pl, pm, and pn from bently/mcilroy. used ir instead of in */ stackinit(N); describe_predictor(&global_predictor[0], "i"); describe_predictor(&global_predictor[1], "j"); describe_predictor(&global_predictor[2], "partition end"); describe_predictor(&global_predictor[3], "insertion"); describe_predictor(&global_predictor[4], "median"); /* describe_predictor(&global_predictor[4], "median of 7 ab"); */ describe_predictor(&global_predictor[5], "median of 7 bc"); describe_predictor(&global_predictor[6], "median of 7 ac"); describe_predictor(&global_predictor[7], "median of 7 cb"); describe_predictor(&global_predictor[8], "median of 7 ca"); describe_predictor(&global_predictor[9], "median of 7 ab2"); describe_predictor(&global_predictor[10], "median of 7 bc2"); describe_predictor(&global_predictor[11], "median of 7 ac2"); describe_predictor(&global_predictor[12], "median of 7 cb2"); describe_predictor(&global_predictor[13], "median of 7 ca2"); describe_predictor(&global_predictor[14], "median of 3 cmp1"); describe_predictor(&global_predictor[15], "median of 3 cmp2"); describe_predictor(&global_predictor[16], "median of 3 cmp3"); r = N-1; l = 0; while(1) { int n = r - l; int n6 = n/6; int n3 = n/3; if (r - l <= THRESHHOLD) { if (stackempty()) break; l = pop(); r = pop(); continue; } /* pseudo - Median of 7 partitioning*/ m = (l+r)/2; if (n > 40) { il = med3(a, l, l + n6, l + n3); /* the 2 is for seperate branch predictors, as it's inlined */ ir = med3_2(a, r - n3, r - n6, r); exch(a[l], a[il]); exch(a[r], a[ir]); exch(a[m], a[r-1]); } pred_compexch(a[l], a[r-1], 14); pred_compexch(a[l], a[r], 15); pred_compexch(a[r-1], a[r], 16); i = partition(a,l+1,r-1); /* here is the bug */ /* then key is being copied more times than necessary. the reason for this is that it is not being removed when it is taken as the key */ /* instead, it is being put in place more than once */ /* example: i == 1, j == 10; key = a[1]; key < pivot, so key is swapped with a[2], the key is now in a[1] and a[2]. uh oh */ if (i-l > r-i) { push(i-1,l); l = i+1; } else { push(r,i+1); r = i-1; } } stackclear(); /* the +1 isnt immediately obvious. its because THRESHHOLD is the difference between l and r up above */ if (2*THRESHHOLD > N) insertion_sentinel(a,N); else insertion_sentinel(a,2*THRESHHOLD); insertion(a, N); /* add the predictors up */ add_predictor(&global_predictor[4], &global_predictor[5]); add_predictor(&global_predictor[4], &global_predictor[6]); add_predictor(&global_predictor[4], &global_predictor[7]); add_predictor(&global_predictor[4], &global_predictor[8]); add_predictor(&global_predictor[4], &global_predictor[9]); add_predictor(&global_predictor[4], &global_predictor[10]); add_predictor(&global_predictor[4], &global_predictor[11]); add_predictor(&global_predictor[4], &global_predictor[12]); add_predictor(&global_predictor[4], &global_predictor[13]); add_predictor(&global_predictor[4], &global_predictor[14]); add_predictor(&global_predictor[4], &global_predictor[15]); add_predictor(&global_predictor[4], &global_predictor[16]); init_predictor(&global_predictor[5]); init_predictor(&global_predictor[6]); init_predictor(&global_predictor[7]); init_predictor(&global_predictor[8]); init_predictor(&global_predictor[9]); init_predictor(&global_predictor[10]); init_predictor(&global_predictor[11]); init_predictor(&global_predictor[12]); init_predictor(&global_predictor[13]); init_predictor(&global_predictor[14]); init_predictor(&global_predictor[15]); init_predictor(&global_predictor[16]); }
void CRadix(LPPSTR a, UINT n) { UINT kbsd, kbsd1, i, j, stage, d, MEMSIZE; UINT *cptr, gs, count[AS]; LPSTR tj, tk, ax, tl, kb, ss, tt, GrpKB[AS]; LPPSTR GrpKP[AS], ak, ta, tc, t; if (sizeof(LPPSTR)>sizeof(unsigned char)*BS) MEMSIZE=sizeof(LPPSTR); else MEMSIZE=sizeof(unsigned char)*BS; /* workspace */ ta = (LPPSTR)malloc(n * MEMSIZE); /* memory for key buffers */ tk = (LPBYTE)malloc(n * sizeof(unsigned char) * BS); tj=tk; push(a, tk, n, 0); for (i=AL; i<AH; i++) count[i]=0; while (!stackempty()) { pop(a, tk, n, stage); if (tk) { /* set the counters and fill the key buffers if necessary */ if ((d=stage%BS)!=0) for (i=0, tl=tk; i<n; i++, tl+=(BS-d)) count[*tl]++; else { if (n>KBC) FillKeyBuffer(a, tk, count, n, stage); else { RDFK(GrpKP, a, n, ta, count, stage); continue; } } /* check if there is only 1 group */ cptr=&count[AL]; while (*cptr<1) cptr++; if (*cptr<n) gs=n; else gs=0; /* calculate both key ptr and key buffer addresses */ kbsd=BS-d, kbsd1=kbsd-1; GrpKP[AL]=a; GrpKB[AL]=tk; for (ak=a, ax=tk, i=AL; i<AH; i++) { GrpKP[i+1]=ak+=count[i]; GrpKB[i+1]=ax+=count[i]*kbsd1; } /* permute the key ptrs */ memcpy(ta, a, sizeof(LPSTR)*gs); for (i=0, ax=tk, tc=ta; i<gs; i++, ax+=kbsd, tc++) { *GrpKP[*ax]=*tc; GrpKP[*ax]++; } /* permute the key buffers */ memcpy(ta, tk, sizeof(unsigned char)*n*kbsd); for (i=0, kb=(LPBYTE)ta; i<n; i++, *t+=kbsd1) { t=&GrpKB[*kb]; ss=*t; tt=kb+1; for (j=0; j<kbsd1; j++) { *ss=*tt; ss++; tt++; } kb+=kbsd; } /* down 1 level */ for (ak=a, ax=tk, i=AL; i<AH; i++) { if (splittable(i)) { push(ak, ax, count[i], stage+1); } else if (count[i]>1 && i>0) isort(ak, count[i], stage); ak+=count[i]; ax+=count[i]*(kbsd1); count[i]=0; } } else RDFK(GrpKP, a, n, ta, count, stage); } free((void*)ta); free((void*)tj); }
//ÏÈÐò·ÇµÝ¹é±éÀú void PreOrderUn(BiTree T)//SqStack *s, { SqStack s; Initstack(&s); while(T!=NULL || !stackempty(s)) { while(T!=NULL) { push(&s,T); printf("%2c",T->data); T=T->lchild; } if (!stackempty(s)) { T=pop(&s,T); T=T->rchild; } } // push(&s,T); // while(T!=NULL || !stackempty(s)) // { // printf("%2c",T->data); // T=push(&s,T); // if(T->lchild) // push(&s,T); // if(T->rchild) // push(&s,T); // } // push(s,T); // BiTree Q=T; // while(!stackempty(s)) // { // pop(s,T); // if(Q!=NULL) // { // printf("%c",Q->data); // push(s,Q->rchild); // push(s,Q->lchild); // } // } // while(T) // { // while(T) // { // push(s,T); // printf("%2c",T->data); // T=T->lchild; // } // T=pop(s,T); // T=T->rchild; // } // else // T=T->rchild; // T=pop(&s,T); // T=T->rchild; }