本文整理汇总了C++中InitStack函数的典型用法代码示例。如果您正苦于以下问题:C++ InitStack函数的具体用法?C++ InitStack怎么用?C++ InitStack使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了InitStack函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TopologicalOrder
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
示例2: OperaType
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
示例3: main
//编写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;
}
示例4: RPN
/**
* 习题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);
}
}
}
示例5: Start
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;
}
示例6: TopologicalOrder
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;
}
}
示例7: main
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;
}
示例8: EvalExpres
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'); /* 返回运算结果*/
}
示例9: parenthesisMatch
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);
}
示例10: conversion
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);
}
示例11: lineEdit
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);
}
示例12: main
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;
}
示例13: LineEdit
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
示例14: kuohao_pipei
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;
}
示例15: main
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;
}