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
ElemType OperaType(void){ //全数据为整型操作 SqStack OPTR,OPND;//OPTR运算符栈,OPND运算栈 InitStack(OPTR); Push(OPTR,'#'); InitStack(OPND); int ch,x,a,b,theta; ch=getche(); while(ch!='#' || GetTop(OPTR)!='#'){ if(!In(ch)){ Push(OPND,ch-48); ch=getche(); }else{ switch(Precede(GetTop(OPTR),ch)){ case '<': Push(OPTR,ch); ch=getche(); break; case '=': Pop(OPTR,x); ch=getche(); break; case '>': Pop(OPTR,theta); Pop(OPND,b); Pop(OPND,a); Push(OPND,Operate(a,theta,b)); break; }//switch }//if else }//while return GetTop(OPND); }// OperaTyp
//编写main函数 //维护两个栈,一个为op,用来保存操作符号,一个为num,用来保存操作数 int main() { SqStack opStack, numStack; InitStack(opStack); InitStack(numStack);//初始化两个栈 Push(opStack, '#'); SElemType op, nextop;//获取弹出的操作符 SElemType first = -1, second = -1, result = -1;//获取弹出的操作数 SElemType ch; ch = getchar(); GetTop(opStack, op); while (ch != '=' || op != '#')//如果不结束,一直获取字符 { /* 这里很关键,对数字进行移动 */ if (ch >= '0'&&ch <= '9') { SElemType temp = 0;//清0 while (ch >= '0'&&ch <= '9') { temp = temp * 10 + (ch-'0'); ch = getchar(); } Push(numStack, temp); } else { switch (compare(op, ch)) { //当下一个操作字符特权高于栈顶时,直接压栈 case -1: Push(opStack, ch); ch = getchar();//获取下一个字符 break; //当特权相等时,弹栈 case 0: Pop(opStack, op); ch = getchar();//获取下一个字符 break; //当下一个操作符特权小于栈顶时,计算并压栈 case 1: Pop(numStack, second);//首先获取的是第二个操作数 Pop(numStack, first);//获取第一个操作数 Pop(opStack, op);//获取操作符 result = canculate(first, second, op); Push(numStack, result); //不能将操作数压栈,要进行新一轮比较 //Push(opStack, ch); break; } } GetTop(opStack, op); } GetTop(numStack, result);//获取栈中的第一个元素 printf("%d\n", result); return 0; }
/** * 习题3.21,将中序表达式写成逆波兰式 */ void RPN(SqStack &S1, SqStack &S2) { char c; int temp; InitStack(S1); //存储临时运算符 InitStack(S2); //存储逆波兰式 Push(S1, '#'); c = getchar(); while (c != '#' || !StackEmpty(S1)) { if (!In(c)) { //不是运算符,是操作数 Push(S2, c); c = getchar(); //读入下一个字符 } else { //是运算符 if ('(' == c) { Push(S1, c); c = getchar(); //读入下一个字符 } else if (')' == c) { while (GetTop(S1) != '(') { Pop(S1, temp); Push(S2, temp); } if (GetTop(S1) == '(') Pop(S1, temp); c = getchar(); } else { switch (Precede(GetTop(S1), c)) { case '<': Push(S1, c); c = getchar(); break; case '>': while ('>' == Precede(GetTop(S1), c)) { Pop(S1, temp); Push(S2, temp); } Push(S1, c); c = getchar(); break; } } } if ('#' == c) { while ('#' != GetTop(S1)) { Pop(S1, temp); Push(S2, temp); } Pop(S1, temp); } } }
int Start(SqStack &s,int maze[][XSIZE]) { SqStack a; SElemType e; InitStack(a); InitStack(s); Step(a,maze); while (a.top>a.base) { Pop(a,e); Push(s,e); } return OK; }
Status TopologicalOrder(ALGraph &G,SqStack &T) { int i,k,count = 0; int indegree[MAX_VERTEX_NUM];//入度数组,存放各顶当前入度数 SqStack S;//零入度栈 也可用队列代替 ArcNode *p; FindIndegree(G,indegree);//对G的各顶点求入度indegree[] InitStack(S);//初始化零入度栈 for(i = 0; i < G.vexnum; ++i)//将零入度的顶点序号入栈 { if (!indegree[i])//入度为零 { Push(S,i); } } InitStack(T); for (i = 0; i < G.vexnum; ++i)//初始化ve为0 { G.vertices[i].data.ve = 0; } while (!StackEmpty(S))//当栈不空时 { Pop(S,i);//出栈一个零入度顶点的序号,并将其赋值给i Visit(G.vertices[i].data); Push(T,i);//i号顶点入逆拓扑排序栈T(栈底元素为拓扑排序的第1个元素) ++count;//已输出顶点数+1 for (p = G.vertices[i].firstarc; p; p = p->nextarc)//对i号的顶点的每个邻接顶点 { k = p->data.adjvex;//其序号为K if (!(--indegree[k]))//k的入度减1,若减为0,则将k入栈S { Push(S,k); } if ((G.vertices[i].data.ve + p->data.info->weight) > G.vertices[k].data.ve) {//顶点i事件的最早发生时间 + <i,k>的权值 > 顶点j事件的最早发生时间 G.vertices[k].data.ve = G.vertices[i].data.ve + p->data.info->weight; //顶点j事件的最早发生时间 = 顶点i事件的最早发生时间 + <i,k>的权值 } } } if (count < G.vexnum)//零入度顶点栈S已空,图还有顶点未输出 { printf("此有向图有回路!\n"); return ERROR; } else { return OK; } }
int main() { ElemType c, temp; sqStack s, r; int len, i, t, sum=0; InitStack(&s); InitStack(&r); printf("Please input a bin number, # to stop!\n"); scanf("%c", &c); while(c != '#') { Push(&s, c); scanf("%c", &c); } getchar(); // 把'\n'从缓冲区去掉 也可用fflush(stdin) len = StackLen(s); printf("The current length of the stack is: %d\n", len); for(i=0; i<len; i++) { Pop(&s, &c); sum += (c-48) * pow(2, i%3); if((i+1)%3 == 0 || i+1 == len) { Push(&r, sum); sum = 0; } } len = StackLen(r); printf("The current length of the new stack is: %d\n", len); printf("Convert to Oct is: "); for(i=0; i<len; i++) { Pop(&r, &c); printf("%o", c); } printf("\n"); return 0; }
int EvalExpres(void) /* 表达式求解函数*/ { int a,b,i=0,s=0; char c[80],r; InitStack(&StackR); Push(&StackR,'#'); InitStack(&StackD); printf(" 请输入表达式并以‘#’结束:"); gets(c); while(c[i]!='#' || GetTop(&StackR)!='#') { if(!In(c[i])) /* 判断读入的字符不是运算符 是则进栈*/ { if(c[i] >= '0' && c[i] <= '9') { s += c[i]-'0'; while(!In(c[++i])) /*此处实现的功能为当输入的数字为多位数时*/ { s*=10; s += c[i]-'0'; } Push(&StackD,s+'0'); s = 0; } else { printf("你输入的表达式有误!\n"); return 0; } } else switch(Proceed(GetTop(&StackR),c[i])) /* 此函数用来比较读取的运算符和栈顶运算符的优先级*/ { case '<': /* 栈顶的元素优先级高*/ Push(&StackR,c[i]); i++; break; case '=': /* 遇到匹配的小括号时则去掉他*/ Pop(&StackR); i++; break; case '>': /* 栈顶的优先级低则出栈并将结果写入栈内*/ r = Pop(&StackR); a = Pop(&StackD)-'0'; b = Pop(&StackD)-'0'; Push(&StackD,Operate(a,r,b)) ; break; } } return (GetTop(&StackD)-'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); }
void lineEdit() { SqStack S; InitStack(S); char ch; ch = getchar(); ElemType e; while (ch != EOF) { while (ch != EOF && ch != '\n') { switch (ch) { case '#': Pop(S, e); break; case '@': ClearStack(S); break; default: e.key = ch; Push(S, e); break; } ch = getchar(); } PrintStack(S); ClearStack(S); if (ch != EOF) ch = getchar(); } DestroyStack(S); }
int main() { ElemType c; sqstack s; int len, i, sum=0; InitStack(&s); printf("Please input binary number, use symbole '#' to mark the end: "); scanf("%c", &c); while(c != '#'){ Push(&s,c); scanf("%c",&c); } getchar(); // remove '\n' from cache len = StackLen(s); printf("Stack length is: %d\n",len); for(i=0;i<=len;i++){ Pop(&s,&c); sum = sum+(c-48)*pow(2,i); } printf("The decimal number is: %d", sum); return 0; }
void LineEdit(){ // P50 算法3.2 行编辑程序 // #:退格 @:退行 // 利用字符栈S,从终端接收一行并传送至调用过程的数据区 SqStack S; InitStack(S); SElemType e; char ch = getchar(); while (ch != EOF){ while (ch != EOF && ch != '\n'){ switch (ch){ case'#': Pop(S, e); break; case'@': ClearStack(S); break; default: Push(S, ch); break; } ch = getchar(); //从终端接收下一个字符 } StackTraverse(S, Visit_Display_Char); ClearStack(S); if (ch != EOF) ch = getchar(); } DestroyStack(S); }// LineEdit
bool kuohao_pipei(char *str) { /* ×¼±¸Ò»¸öÕ»£¬ÓÃÓÚ´æ·ÅɨÃèÓöµ½µÄ×óÀ¨ºÅ ´Ó×óÏòºóɨÃèÿһ¸ö×Ö·û{ Èç¹ûÓöµ½µÄÊÇ×óÀ¨ºÅ£¬ÔòÈëÕ» Èç¹ûÓöµ½µÄÊÇÓÒÀ¨ºÅ£¬Ôò °ÑÕ»¶¥×Ö·ûºÍµ±Ç°×Ö·û±È½Ï ÈôÆ¥Å䣬Ôòµ¯³öÕ»¶¥×Ö·û£¬¼ÌÐøÏòǰɨÃè Èô²»Æ¥Å䣬³ÌÐò·µ»Ø²»Æ¥Åä±êÖ¾ } µ±ËùÓÐ×Ö·û¶¼É¨ÃèÍê±Ï£¬Õ»Ó¦µ±Îª¿Õ */ SqStack S; InitStack(S); for (int i = 0; str[i] != '\0'; i++) { if (isLeftKuohao(str[i])) { Push(S, str[i]); } else if (isRightKuohao(str[i])) { char ch; Top(S, ch); if (isMatch(ch, str[i]) == false) return false; else { Pop(S, ch); } } } if (isEmpty(S)) return true; return false; }
int main() { // Let's make a stack -- Always a good idea initialize variables to some known value SStack stack = {0}; int counter = 0; // A counter for our "for loops" InitStack(&stack,10); // Let's fill the stack with numbers 0 - 9. // When this for loop is done the stack will be FULL. for(counter = 0; counter < 10; counter++) Push(&stack,counter); if(Push(&stack,22)) printf("This is we bad! We just pushed an element onto a full stack.\n\n"); else printf("This is good! We were denied while trying to \"push\" onto a full stack.\n\n"); // We'll print out the numbers. You'll see there in LIFO order (The last one we // pushed on, is the first one we'll pop off, etc). for(counter = 0; counter < 10; counter++) printf("We just popped off #%d\n",Pop(&stack)); if(FreeStack(&stack)) printf("\nYeah the memory was freed successfully!\n\n"); else printf("\nOh NO! An error occurred while trying to free the memory"); return 0; }
void LineEdit() { // 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2 SqStack s; char ch,c; int n,i; InitStack(s); scanf("%d",&n); //多case ch=getchar(); for(i=1;i<=n;i++) { ch=getchar(); while(ch!='\n') { switch(ch) { case '#':Pop(s,c); break; // 仅当栈非空时退栈 case '@':ClearStack(s); break; // 重置s为空栈 default :Push(s,ch); // 有效字符进栈 } ch=getchar(); // 从终端接收下一个字符 } StackTraverse(s,visit); // 将从栈底到栈顶的栈内字符输出 ClearStack(s); // 重置s为空栈 } DestroyStack(s); }
Status BracketMatch(char *expr){ // p49 3.2.2 括号匹配的检验 // 匹配括号,若出错则返回FALSE,否则返回TRUE SqStack S; InitStack(S); SElemType e; int i = 0; while (*(expr + i) != '\0'){ switch (*(expr + i)){ case '(': Push(S, *(expr + i)); break; case '[': Push(S, *(expr + i)); break; case ')': { Pop(S, e); if (e != '(') return FALSE; break; } case ']': { Pop(S, e); if (e != '[') return FALSE; break; } default: break; } i++; } return TRUE; }// BracketMatch
//******************** 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"); }
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; }
/** * Añadir descripcion de la funcion * @param void * @returns void */ void InitSO(void) { unsigned int i = 0; NextSchedulingDelay = 0; /** * invalidate ActiveTask descriptor */ ActiveTask.Id = 0; ActiveTask.Priority = 0; ActiveTask.State = TSK_INVALID; ActiveTask.WaitTime = 0; ActiveTask.StackDesc.StackBottom = 0; ActiveTask.StackDesc.StackTop = 0; ActiveTask.StackDesc.StackPointer = 0; for(i = 0; i < NO_TASKS; i++) { InitStack(ReadyTasks[i].StackDesc); } InitTaskStacks(); InitBSP(); }
int main() { int i,x; int a[]={10,20,30,40,50,60,70}; int b[]={70,60,50,40,30,20,10}; InitStack(); printf("������a��Ԫ�ؽ����ջ....\n"); for(i=0;i<sizeof(a)/sizeof(a[0]);i++) //������a��Ԫ�ؽ����ջ { push(1,a[i]); } printf("������b��Ԫ�ؽ��Ҷ�ջ....\n"); for(i=0;i<sizeof(b)/sizeof(b[0]);i++) //������b��Ԫ�ؽ��Ҷ�ջ { push(2,b[i]); } system("pause"); printf("���ջ�ij�ջԪ�������ǣ�"); while(lsTop!=-1) { pop(1,&x); printf("%5d",x); } printf("\n"); printf("�Ҷ�ջ�ij�ջԪ�������ǣ�"); while(rsTop!=MaxSize-1) { pop(2,&x); printf("%5d",x); } printf("\n"); return 0; }
void LineEdit() { /* 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2 */ SqStack s; char ch,c; InitStack(&s); printf("请输入一个文本文件,^Z结束输入:\n"); ch=getchar(); while(ch!=EOF) { /* EOF为^Z键,全文结束符 */ while(ch!=EOF&&ch!='\n') { switch(ch) { case '#':Pop(&s,&c); break; /* 仅当栈非空时退栈 */ case '@':ClearStack(&s); break; /* 重置s为空栈 */ default :Push(&s,ch); /* 有效字符进栈 */ } ch=getchar(); /* 从终端接收下一个字符 */ } StackTraverse(s,copy); /* 将从栈底到栈顶的栈内字符传送至文件 */ ClearStack(&s); /* 重置s为空栈 */ fputc('\n',fp); if(ch!=EOF) ch=getchar(); } DestroyStack(&s); }
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(void) { Stack *ps = InitStack(); int i,item; int x[15]; printf("please input 10 numbers\n"); printf("test stack_push() and GetTop()\n"); for(i=0;i<10;i++) { scanf("%d",&x[i]); stack_push(ps,i); GetTop(ps,&item); printf("%d ",item); } printf("\ntest stack_pop()\n"); for(i=0;i<10;i++) { stack_pop(ps,&item); printf("%d ",item); } if(stack_is_empty(ps)) printf("\nthis stack is empty!"); return 0; }
int StackOrderIsLegal(StackElementType *input,StackElementType *output,int n){ SeqStack S; StackElementType e; int i,j; i = j = 0; InitStack(&S); while(j<n){ if(StackIsEmpty(&S)){ Push(&S,input[i]); printf("Push%d\t",input[i]); i ++; continue; } GetTop(&S,&e); if(e == output[j]) { Pop(&S,&e); printf("Pop%d\t",e); i ++; j ++; } else if(i < n){ Push(&S,input[i]); printf("Push%d\t",input[i]); i ++; } else { break; } } if(j < n) return 0; return 1; }
void CPUThread::Run() { if(IsRunning()) Stop(); if(IsPaused()) { Resume(); return; } #ifndef QT_UI wxGetApp().SendDbgCommand(DID_START_THREAD, this); #endif m_status = Running; SetPc(entry); InitStack(); InitRegs(); DoRun(); Emu.CheckStatus(); #ifndef QT_UI wxGetApp().SendDbgCommand(DID_STARTED_THREAD, this); #endif }
static void LineEdit() { // 利用字符栈s,从终端接收一行并送至调用过程的数据区。算法3.2 SqStack s; char ch,c; InitStack(s); printf("请输入一个文本文件,^Z结束输入:\n"); ch=getchar(); while(ch!=EOF) { // EOF为^Z键,全文结束符 while(ch!=EOF&&ch!='\n') { switch(ch) { case '#':Pop(s,c); break; // 仅当栈非空时退栈 case '@':ClearStack(s); break; // 重置s为空栈 default :Push(s,ch); // 有效字符进栈 } ch=getchar(); // 从终端接收下一个字符 } StackTraverse(s,copy); // 将从栈底到栈顶的栈内字符传送至文件 ClearStack(s); // 重置s为空栈 fputc('\n',fp); if(ch!=EOF) ch=getchar(); } DestroyStack(s); }
void spostOrder(Btree btree) { LStack s ; InitStack(&s); Btree p = btree; Btree pre = p; int flag = 1; while(p || !IsEmpty(s)) { if(p && flag) { Push(&s,p); p = p->lchild; } else { if(IsEmpty(s)) return; GetTop(s,&p); if(p->rchild && p->rchild!=pre) { p = p->rchild; flag = 1; } else { Pop(&s,&p); printf("%d\t",p->data); flag = 0; pre = p; } } } }
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() { ElemType c; SqStack s; int len, i, sum = 0; InitStack(&s); printf("请输入二进制数,输入#符号表示输入结束:\n"); scanf("%c", &c); while (c != '#') { Push(&s, c); scanf("%c", &c); } getchar(); // 把最后的'\n'从键盘缓冲区去掉 len = StackLen(s); printf("栈的当前长度是: %d\n", len); for (i = 0; i < len; i++) { Pop(&s, &c); sum += (c - 48)*pow(2, i); } printf("转换后的十进制数是:%d\n", sum); return 0; }