void main() { ElemType e; LiStack *s; printf("栈s的基本运算如下:\n"); printf(" (1)初始化栈s\n"); InitStack(s); printf(" (2)栈为%s\n",(StackEmpty(s)?"空":"非空")); printf(" (3)依次进栈元素a,b,c,d,e\n"); Push(s,'a'); Push(s,'b'); Push(s,'c'); Push(s,'d'); Push(s,'e'); printf(" (4)栈为%s\n",(StackEmpty(s)?"空":"非空")); printf(" (5)出栈序列:"); while (!StackEmpty(s)) { Pop(s,e); printf("%c ",e); } printf("\n"); printf(" (6)栈为%s\n",(StackEmpty(s)?"空":"非空")); printf(" (7)释放栈\n"); DestroyStack(s); }
int main() { SqStack S; printf("(1)��ʼ��ջ��\n"); InitStack (S); printf("(2)�ж�ջs�Ƿ�ǿգ�"); if(StackEmpty(S)) printf("�ǿ�\n"); else printf("��\n"); printf("(3)���ν�ջԪ��a,b,c,d,e:\n"); push(S,'a'); push(S,'b'); push(S,'c'); push(S,'d'); push(S,'e'); printf("(4)�ж�ջs�Ƿ�ǿգ�"); if(StackEmpty(S)) printf("�ǿ�\n"); else printf("��\n"); printf("(5)ջ�ij����ǣ�%d\n",StackLength(S)); printf("(6)�����ջ����ջ��Ԫ��:"); StackTraverse(S); printf("\n(7)�����ջ����:"); StackGet(S); printf("\n(8)�ж�ջs�Ƿ�ǿգ�"); if(StackEmpty(S)) printf("�ǿ�\n"); else printf("��\n"); printf("(9)�ͷ�ջ"); DestroyStack(S); }
int main() { int j; SqStack s; SElemType e; if( InitStack(&s) == OK ) for(j = 1; j <= 9; j++) Push(&s, j); printf("The items in the stack is: \n"); StackTraverse(s, visit); Pop(&s, &e); printf("The item popped is: %d\n", e); printf("\n"); printf("The stack is empty now? empty:1, not empty:0\t%d\n", StackEmpty(s)); printf("\n"); GetTop(s, &e); printf("The item at the top of the stack is: %d, the length of the stack is: %d\n",e, StackLength(s)); printf("\n"); ClearStack(&s); printf("The stack cleared, is the stack empty? empty:1, not empty:0, %d\n", StackEmpty(s)); printf("\n"); DestroyStack(&s); return 0; }
void main() { ElemType e; SqStack *s; printf("Õ»sµÄ»ù±¾ÔËËãÈçÏÂ:\n"); printf(" (1)³õʼ»¯Õ»s\n"); InitStack(s); printf(" (2)ջΪ%s\n",(StackEmpty(s)?"¿Õ":"·Ç¿Õ")); printf(" (3)ÒÀ´Î½øÕ»ÔªËØa,b,c,d,e\n"); Push(s,'a'); Push(s,'b'); Push(s,'c'); Push(s,'d'); Push(s,'e'); printf(" (4)ջΪ%s\n",(StackEmpty(s)?"¿Õ":"·Ç¿Õ")); printf(" (5)Õ»µÄ³¤¶È=%d\n",StackLength(s)); printf(" (6)Êä³ö´ÓÕ»¶¥µ½Õ»µ×ÔªËØ:"); DispStack(s); printf(" (7)³öÕ»ÐòÁУº"); while (!StackEmpty(s)) { Pop(s,e); printf("%c",e); } printf("\n"); printf(" (8)ջΪ%s\n",(StackEmpty(s)?"¿Õ":"·Ç¿Õ")); printf(" (9)ÊÍ·ÅÕ»\n"); DestroyStack(s); }
/* To back up a stack for the recursion. */ Stack * BackUp(Stack *rail) { register Node *np; StackEntry item; Stack temp; CreateStack(&temp); CreateStack(&backup[bkindex]); /* 'backup' is declared as a global.*/ if (StackEmpty(rail)) { ; } else { np = rail->top; while (np != NULL) { Push(np->entry, &temp); np = np->next; } while (!StackEmpty(&temp)) { Pop(&item, &temp); Push(item, &backup[bkindex]); } } bkindex++; return &backup[bkindex - 1]; }
//后序遍历 void PostOrderUnrec(Bitree t) { SqStack s; stacknode x; StackInit(s); p=t; do { while (p!=null) //遍历左子树 { x.ptr = p; x.tag = L; //标记为左子树 push(s,x); p=p->lchild; } while (!StackEmpty(s) &&s.Elem[s.top].tag==R) { x = pop(s); p = x.ptr; visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍历右子树 p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s)); }//PostOrderUnrec
int main() { int j; SqStack s; int e; if(InitStack(&s)==OK) { for(j=1; j<=10; j++) Push(&s,j); } printf("Õ»ÖÐÔªËØÒÀ´ÎΪ£º"); StackTraverse(s); Pop(&s,&e); printf("µ¯³öµÄÕ»¶¥ÔªËØe=%d\n",e); printf("Õ»¿Õ·ñ£º%d(1:¿Õ0:·ñ)\n",StackEmpty(s)); GetTop(s,&e); printf("Õ»¶¥ÔªËØe=%dÕ»µÄ³¤¶ÈΪ%d\n",e,StackLength(s)); ClearStack(&s); printf("Çå¿ÕÕ»ºó£¬Õ»¿Õ·ñ£º%d(1:¿Õ0:·ñ)\n",StackEmpty(s)); return 0; }
//******************** void main_LineEdit(){ SeqStack S; char ch; DataType e; DataType a[50]; int i,j=0; InitStack(&S); printf("输入字符序列(#表示前一个字符无效,@表示当前行字符无效).\n"); ch=getchar(); while(ch!='\n'){ switch(ch){ case '#': if(!StackEmpty(S)) PopStack(&S,&ch); break; case '@': ClearStack(&S); break; default: PushStack(&S,ch); } ch=getchar(); } while(!StackEmpty(S)){ PopStack(&S,&e); a[j++]=e; } for(i=j-1;i>=0;i--) printf("%c",a[i]); printf("\n"); ClearStack(&S); system("pause"); }
/** * @brief detect if the queue is empty. * * @param[in] Q queue struct pointer * * @return return TRUE if empty, else return FALQE */ Status QueueEmpty(Queue* Q) { assert(Q != NULL); if (StackEmpty(&Q->s1) == TRUE && StackEmpty(&Q->s2) == TRUE) return TRUE; else return FALSE; }
void PrintTop(queue* q) { if(StackEmpty(q->a)) { while(!StackEmpty(q->b)) { StackPush(q->a, StackPop(q->b)); } } StackPrint(q->a); }
void Dequeue(queue* q) { if(StackEmpty(q->a)) { while(!StackEmpty(q->b)) { StackPush(q->a, StackPop(q->b)); } } StackPop(q->a); }
void PostOrderTraverse_1(BiTree T) { SqStack S=NULL; S=InitStack(S); BiTree p=NULL; p = T; Push(S, p); while(!StackEmpty(S)) { while(p){ if(p->lflag != 1 ) { p->lflag = 1; #if 0 if(GetTop(S) != p) { Push(S, p); } #endif if(p->lchild){ Push(S, p->lchild); p = p->lchild; } }else if(p->rflag != 1) { p->rflag = 1; #if 0 if(GetTop(S) != p) { Push(S, p); } #endif if(p->rchild){ Push(S, p->rchild); p = p->rchild; } }else if(p->lflag == 1 && p->rflag == 1){ break; } } p = Pop(S); printf("%4c", p->data); p->lflag = 0; p->rflag = 0; if(StackEmpty(S)) { break; } p = GetTop(S); } free(S->base); free(S); }
int main() { Stack s; ElemType x; cout<<"(1)³õʼ»¯Õ»s"<<endl; InitStack(s); cout<<"(2)ջΪ"<<(StackEmpty(s)?"¿Õ":"·Ç¿Õ")<<endl; cout<<"(3)ÒÀ´ÎÊäÈë×ÖĸÐòÁУ¬ÒÔ¡°#¡±½áÊø£º"<<endl; cin>>x; while( x!='#') { Push(s,x); cin>>x; } cout<<"(4)ջΪ"<<(StackEmpty(s)?"¿Õ":"·Ç¿Õ")<<endl; cout<<"(5)Õ»³¤¶ÈStackLength(s):"<<StackLength(s)<<endl; cout<<"(6a)Õ»¶¥ÔªËØGetTop(s)Ϊ£º"; cout<<GetTop(s)<<endl; cout<<"(6b)Õ»¶¥ÔªËØGetTop1(s,x)Ϊ£º"<<endl; GetTop1(s,x); cout<<x<<endl; cout<<"(7)´ÓÕ»¶¥µ½Õ»µ×ÔªËØPrintStack(s)Ϊ£º"<<endl; PrintStack(s); cout<<"(8)³öÕ»Pop1(s,x)µÄÔªËØΪ£º"<<endl; Pop1(s,x); cout<<x<<endl; cout<<"(9)³öÕ»ÐòÁÐ:"<<endl; while(!StackEmpty(s)) { cout<<Pop(s)<<" "; } cout<<endl; cout<<"(10)ջΪ:"<<(StackEmpty(s)?"¿Õ":"·Ç¿Õ")<<endl; cout<<"(11)ÒÀ´Î½øÕ»ÔªËØa,b,c:"<<endl; Push(s,'a'); Push(s,'b'); Push(s,'c'); cout<<"(12)´ÓÕ»¶¥µ½Õ»µ×ÔªËØPrintStack(s)Ϊ£º"<<endl; PrintStack(s); cout<<"(13)Çå¿ÕÕ»ClearStack(s)"<<endl; ClearStack(s); cout<<"(14)ջΪ"<<(StackEmpty(s)?"¿Õ":"·Ç¿Õ")<<endl; cout<<"(15)Ïú»ÙÕ»s:"<<endl; DestoryStack(s); cout<<"(16)Ïú»ÙÕ»ºóµ÷ÓÃPush(s,e)ºÍPrintStack(s)"<<endl; Push(s,'e'); PrintStack(s); return 0; }
/** * @brief get head element from the queue. * * @param[in] Q queue struct pointer * @param[out] e the element * * @return return OK if success, else return ERROR */ Status GetHead(Queue* Q, ElementType* e) { assert(Q != NULL && e != NULL); if (StackEmpty(&Q->s2) == FALSE) { GetTop(&Q->s2, e); return OK; } else if (StackEmpty(&Q->s1) == FALSE) { while (Pop(&Q->s1, e)) { Push(&Q->s2, *e); } return OK; } else { return ERROR; } }
void check() { // 对于输入的任意一个字符串,检验括号是否配对 SqStack s; SElemType ch[80],*p,e; if(InitStack(s)) // 初始化栈成功 { //printf("请输入表达式\n"); gets(ch); p=ch; while(*p) // 没到串尾 switch(*p) { case '(': case '[': Push(s,*p);p++; break; // 左括号入栈,且p++ case ')': case ']': if(!StackEmpty(s)) // 栈不空 { Pop(s,e); // 弹出栈顶元素 if((*p==')'&&e!='(')||(*p==']'&&e!='[')) // 弹出的栈顶元素与*p不配对 { printf("isn't matched pairs\n"); exit(ERROR); } else { p++; break; // 跳出switch语句 } } else // 栈空 { printf("lack of left parenthesis\n"); exit(ERROR); } default: p++; // 其它字符不处理,指针向后移 } if(StackEmpty(s)) // 字符串结束时栈空 printf("matching\n"); else printf("lack of right parenthesis\n"); } }
Status scanner(char* c){ LinkStack* stack = NULL; InitStack(&stack); Status ret = FALSE; int i = 0; while(c[i] != '\0'){ elemType e; if( isLeft(c[i]) ){ Push(stack,(elemType)c[i]); } if(isRight(c[i])){ char codeOut; Pop(stack,&e); codeOut = (char)e; if(!isMatch(codeOut,c[i])){ printf("not matching %c %c\n",codeOut,c[i]); ret = FALSE; goto EXIT; } } i++; } if(StackEmpty(stack)){ ret = TRUE; }else{ ret = FALSE; } EXIT: ClearStack(&stack); return ret; }
void LineEdit() { SqStack s; char ch; InitStack(s); printf("请输入一个文本文件,^Z结束输入:\n"); ch = getchar(); while (ch != EOF) { while (ch != EOF && ch != '\n') { switch (ch) { case '#': if (!StackEmpty(s)) Pop(s, ch); break; case '@': ClearStack(s); break; default: Push(s, ch); } ch = getchar(); } StackTraverse(s, copy); fputc('\n', fp); ClearStack(s); if (ch != EOF) ch = getchar(); } DestroyStack(s); }
/** Reverses all operations recorded in the stacked memory object. * * Reversing an operation means freeing memory the operation had allocated. * * @param StackedMemory Stacked memory object. * * @remark * If the stacked memory object contains no operation records, nothing happens. */ VOID StackedMemoryAllFree(PUTILS_STACK StackedMemory) { PUTILS_STACK_ITEM stackItem = NULL; PUTILS_STACKED_MEMORY_RECORD stackRecord = NULL; DEBUG_ENTER_FUNCTION("StackedMemory=0x%p", StackedMemory); while (!StackEmpty(StackedMemory)) { stackItem = StackPopNoFree(StackedMemory); stackRecord = CONTAINING_RECORD(stackItem, UTILS_STACKED_MEMORY_RECORD, StackItem); switch (stackRecord->AllocType) { case smatHeap: HeapMemoryFree(stackRecord->Address); break; case smatVirtual: VirtualMemoryFreeUser(stackRecord->Address); break; default: DEBUG_ERROR("Invalid stacked memory record type (%u)", stackRecord->AllocType); KeBugCheck(0); break; } HeapMemoryFree(stackRecord); } DEBUG_EXIT_FUNCTION_VOID(); return; }
//查找 int GraphListDepthFind(AdjList *graph, int v, int t) { int visited [MAX_VERTEX_NUM] = {0}; int i = 0; ArcNode *p; StackNode *stack; StackDataElem e; stack = (StackNode *)malloc(sizeof(StackNode)); StackInit(stack); e.a = v; StackPush(stack, e); while(StackEmpty(stack)){ StackPop(stack, &e); i = e.a; if(!visited[i]){ printf("%d", i); visited[i] = 1; } if(i == t){ return 0; } p = graph->vertex[i].fristarc; while(p != NULL){ if(!visited[p->adjvex]){ e.a = p->adjvex; StackPush(stack, e); } p = p->nextarc; } } return 0; }
Status Pop(LinkStack *S, SElemType *e, int type) { LinkStackPtr p; int len; if(StackEmpty(*S)) return ERROR; switch (type){ case 1: len = (strlen(S->top->data) + 1) * sizeof(char); *e = (char *) malloc(len); memcpy(*e, S->top->data, len); break; case 2: len = sizeof(int); *e = (int *) malloc(sizeof(int)); memcpy(*e, S->top->data, len); break; } p = S->top; S->top = S->top->next; free(p); S->count--; return OK; }
main() {stack s; char inp[20],op[20],ch; int i=0,j=0; printf("enter the infix expression\n"); scanf("%s",inp); createstack(&s); push('\0',&s); while(inp[i]!='\0') {while((pre(inp[i]))<=pre(top(&s))) {pop(&ch,&s); op[j++]=ch; } push(inp[i],&s); i++; } while(!StackEmpty(&s)) {pop(&ch,&s); op[j++]=ch; } printf("the suffix expression is\n"); printf("%s\n",op); }
Status TopologicalSort(ALGraph G) { /* 有向图G采用邻接表存储结构。若G无回路,则输出G的顶点的一个拓扑序列并返回OK, */ /* 否则返回ERROR。算法7.12 */ int i,k,count,indegree[MAX_VERTEX_NUM]; SqStack S; ArcNode *p; FindInDegree(G,indegree); /* 对各顶点求入度indegree[0..vernum-1] */ InitStack(&S); /* 初始化栈 */ for(i=0;i<G.vexnum;++i) /* 建零入度顶点栈S */ if(!indegree[i]) Push(&S,i); /* 入度为0者进栈 */ count=0; /* 对输出顶点计数 */ while(!StackEmpty(S)) { /* 栈不空 */ Pop(&S,&i); printf("%s ",G.vertices[i].data); /* 输出i号顶点并计数 */ ++count; for(p=G.vertices[i].firstarc;p;p=p->nextarc) { /* 对i号顶点的每个邻接点的入度减1 */ k=p->adjvex; if(!(--indegree[k])) /* 若入度减为0,则入栈 */ Push(&S,k); } } if(count<G.vexnum) { printf("此有向图有回路\n"); return ERROR; } else { printf("为一个拓扑序列。\n"); return OK; } }
int main() { int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; int i = 0, *top = 0; Stack stack; StackInit(&stack, sizeof(int)); while (i < sizeof(arr) / sizeof(int)) { StackPush(&stack, &arr[i]); ++i; } top = StackTop(&stack); printf("The top of the stack element is %d\n", *top); top = StackPop(&stack); printf("Pop element %d\n", *top); top = StackTop(&stack); printf("The top of the stack element is %d\n", *top); printf("The stack size is %d\n", StackSize(&stack)); StackClear(&stack); if (StackEmpty(&stack)) { printf("Stack is empty.\n"); } else { printf("Stack is not empty.\n"); } StackDestory(&stack); return 0; }
void parenthesisMatch() { char s[100]; printf("please input the string which consists of paretheses:\n"); scanf("%s", s); SqStack S; InitStack(S); for (int i = 0; s[i] != '\0'; i++) { if (s[i] == '[' || s[i] == '{' || s[i] == '(') { ElemType e; e.key = s[i]; Push(S, e); } else if (s[i] == ']' || s[i] == '}' || s[i] == ')') { ElemType e; Pop(S, e); if (e.key != s[i] - 1 && e.key != s[i] - 2) { printf("the parentheses are not matching.\n"); return; } } } if (StackEmpty(S)) { printf("the parentheses are matching.\n"); } DestroyStack(S); }
void conversion() { SqStack S; InitStack(S); int N; printf("input the decimal number:\n"); scanf_s("%d", &N); printf("input the the number system after conversion:\n"); int d; scanf_s("%d", &d); while (N) { ElemType e; e.key = N % d; Push(S, e); N = N / d; } PrintStack(S); printf("the conversion result is:\n"); while (!StackEmpty(S)) { ElemType e; Pop(S, e); printf("%d", e.key); } printf("\n"); PrintStack(S); DestroyStack(S); }
Status TopologicalOrder(ALGraph G, Stack &T) { // 算法7.13 // 有向网G采用邻接表存储结构,求各顶点事件的最早发生时间ve(全局变量)。 // T为拓扑序列定点栈,S为零入度顶点栈。 // 若G无回路,则用栈T返回G的一个拓扑序列,且函数值为OK,否则为ERROR。 Stack S;int count=0,k; char indegree[40]; ArcNode *p; InitStack(S); FindInDegree(G, indegree); // 对各顶点求入度indegree[0..vernum-1] for (int j=0; j<G.vexnum; ++j) // 建零入度顶点栈S if (indegree[j]==0) Push(S, j); // 入度为0者进栈 InitStack(T);//建拓扑序列顶点栈T count = 0; for(int i=0; i<G.vexnum; i++) ve[i] = 0; // 初始化 while (!StackEmpty(S)) { Pop(S, j); Push(T, j); ++count; // j号顶点入T栈并计数 for (p=G.vertices[j].firstarc; p; p=p->nextarc) { k = p->adjvex; // 对j号顶点的每个邻接点的入度减1 if (--indegree[k] == 0) Push(S, k); // 若入度减为0,则入栈 if (ve[j]+p->info > ve[k]) ve[k] = ve[j]+p->info; }//for *(p->info)=dut(<j,k>) }//while if (count<G.vexnum) return ERROR; // 该有向网有回路 else return OK; } // TopologicalOrder
Status MazePath(PosType start,PosType end) /* 算法3.3 */ { /* 若迷宫maze中存在从入口start到出口end的通道,则求得一条 */ /* 存放在栈中(从栈底到栈顶),并返回TRUE;否则返回FALSE */ SqStack S; PosType curpos; SElemType e; InitStack(&S); curpos=start; do { if(Pass(curpos)) { /* 当前位置可以通过,即是未曾走到过的通道块 */ FootPrint(curpos); /* 留下足迹 */ e.ord=curstep; e.seat.x=curpos.x; e.seat.y=curpos.y; e.di=0; Push(&S,e); /* 入栈当前位置及状态 */ curstep++; /* 足迹加1 */ if(curpos.x==end.x&&curpos.y==end.y) /* 到达终点(出口) */ return TRUE; curpos=NextPos(curpos,e.di); } else { /* 当前位置不能通过 */ if(!StackEmpty(S)) { Pop(&S,&e); /* 退栈到前一位置 */ curstep--; while(e.di==3&&!StackEmpty(S)) /* 前一位置处于最后一个方向(北) */ { MarkPrint(e.seat); /* 留下不能通过的标记(-1) */ Pop(&S,&e); /* 退回一步 */ curstep--; } if(e.di<3) /* 没到最后一个方向(北) */ { e.di++; /* 换下一个方向探索 */ Push(&S,e); curstep++; curpos=NextPos(e.seat,e.di); /* 设定当前位置是该新方向上的相邻块 */ } } } }while(!StackEmpty(S)); return FALSE; }
Hint VAM_MAZE_PATH(int mg[][MCOL], PosType start, PosType end, SqStack *s) { PosType curpos; int Status; SElemType Static_Stack[20]; // 20 is the MAX depth of stack. Change later if need. Status = InitStack(s, Static_Stack); if (Status != SUCCESS) {putnum(0xdead000C); return FAILURE;} SElemType e; int curstep; curpos = start; // Set the start as the current step curstep = 0; // Step Counter do { // If the current pos can be passed if (Pass(mg, curpos)) { //Leave Foot Print FootPrint(mg, curpos, MAZE_PASSED); e.di = 1; // Direction e.ord = curstep; e.seat = curpos; Push(s,e); // Push to stack if (curpos.x == end.x && curpos.y == end.y) { // Touch Down! Reach the Des return SUCCESS; } curpos = NextPos(&curpos, 1); curstep++; } else { if (!StackEmpty(s)) { Pop(s, &e); while (e.di == 4 && !StackEmpty(s)) { FootPrint(mg, e.seat, MAZE_BLOACKED); Pop(s, &e); } if (e.di < 4) { e.di++; Push(s, e); curpos = NextPos(&e.seat, e.di); } } } }while (!StackEmpty(s)); return FAILURE; }
void BiTree_free(BiTree *T) { SqStack S=NULL; S=InitStack(S); BiTree p=NULL; p = *T; Push(S, p); while(!StackEmpty(S)) { while(p){ if(p->lflag != 1 ) { p->lflag = 1; if(p->lchild){ Push(S, p->lchild); p = p->lchild; } }else if(p->rflag != 1) { p->rflag = 1; if(p->rchild){ Push(S, p->rchild); p = p->rchild; } }else if(p->lflag == 1 && p->rflag == 1){ break; } } p = Pop(S); //printf("%4c", p->data); free(p); p = NULL; if(StackEmpty(S)) { break; } p = GetTop(S); } *T = NULL; free(S->base); free(S); }
int main() { LinkStack S; int result; pNode node, topNode; //0. InitStack InitStack(S); if (!S) { printf("Init Stack failed!\n"); return -1; } PrintStack(S); //1. Push printf("\n"); PrintStack(S); printf("Push Stack\n"); node = CreateNode(5); if (!node) { printf("Create Node failed\n"); return -1; } Push(S, node); PrintStack(S); topNode = Top(S); PrintNode(topNode); //2. Pop printf("\n"); PrintStack(S); if (!StackEmpty(S)) { printf("Pop Stack\n"); Pop(S); } PrintStack(S); //3. Clear printf("\n"); PrintStack(S); if (!StackEmpty(S)) { printf("Clear Stack\n"); ClearStack(S); } PrintStack(S); }