Esempio n. 1
0
/* 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];
}
static void KeyboardCallback(unsigned char key, int x, int y)
{
	static Random random;
	int sceneIndex = random.rand()&1;

	switch (key)
	{
	case 27:	exit(0); break;
	case ' ':			CreateCube(sceneIndex, NxVec3(0.0f, 20.0f, 0.0f), 1+(rand()&3)); break;
	case 's':			CreateStack(sceneIndex, 10); break;
	case 'b':			CreateStack(sceneIndex, 30); break;
	case 't':			CreateTower(sceneIndex, 30); break;
	case 'x':			gShadows = !gShadows; break;
	case 'p':			gPause = !gPause; break;
	case 101: case '8':	Eye += Dir * 2.0f; break;
	case 103: case '2':	Eye -= Dir * 2.0f; break;
	case 100: case '4':	Eye -= N * 2.0f; break;
	case 102: case '6':	Eye += N * 2.0f; break;
	case 'w':
		{
		NxVec3 t = Eye;
		NxVec3 Vel = Dir;
		Vel.normalize();
		Vel*=200.0f;
		CreateCube(sceneIndex, t, 8, &Vel);
		}
		break;
	}
}
double midfixCal(void)
{
int flag;
unsigned char ope[5];
unsigned char pre_ope;
double term;
double op1,op2;
int priv,pre_priv;//优先级

Stack M = CreateStack( StackLen );//这个栈用来将中缀表达式转换成后缀
Stack S = CreateStack( StackLen );//这个栈用来计算后缀表达式的值

printf("Enter the midfix expression only + * - / () allowed.\n");

while(1){
	flag = scanf("%[+*-/()=]",ope);
	if(flag == 0){
		if(scanf("%lf",&term) == 1){
			getchar();
			Push( term ,S);
		}
	}
	else if( ope[0] == '+' || ope[0] == '*' || ope[0] == '-' || ope[0] == '/' || ope[0] == '(' ){
		getchar();
		priv = AssignPriv( ope[0] );

		while(!IsEmpty(M)){
			pre_ope = (unsigned char)Top(M);
			pre_priv = AssignPriv(pre_ope);

			if(pre_priv >= priv && pre_ope != '(' ){
				Pop(M);
				CalPostFix(pre_ope,S);//计算后缀表达式的值
			}
			else
				break;
		}
		Push( (double)ope[0],M);
	}
	else if( ope[0] == ')'){
		getchar();
		//将栈中的元素直到 '('弹出
		while( ( (unsigned char)Top(M) ) != '('){
			pre_ope = TopAndPop(M);
			CalPostFix(pre_ope, S);
		}
		Pop(M);//弹出'('
	}
	else if( ope[0] == '=' ){
		while(!IsEmpty(M)){
			pre_ope = TopAndPop(M);
			CalPostFix(pre_ope,S);
		}
		return TopAndPop(S);//返回S栈顶的元素,即是最终结果	
	}
}//while结束
}
int main()
{
    struct Stack *s1 = CreateStack(); //Stack 1 is the main stack , both enqueue and dequeue is done here
    struct Stack *s2 = CreateStack(); // temporary stack for intermediate functioning
    Enqueue(s1,s2,3);
    Enqueue(s1,s2,6);
    Enqueue(s1,s2,1);
    Dequeue(s1);
    printf("%d is popped\n",Dequeue(s1)->element);
    return 0;
}
Esempio n. 5
0
/* To initialize everything to begin. */
void
Initialize(Stack *out, Stack *spur, Stack *in, StackEntry ntrains)
{
  CreateStack(out);
  CreateStack(spur);
  CreateStack(in);

  while (ntrains > 0) {
	Push(ntrains, out);
	ntrains--;
  }
}
Esempio n. 6
0
int main()
{
    int N, Sn, X;
    Stack S;
    int done = 0;

    scanf("%d", &N);
    S = CreateStack(N);
    while ( !done ) {
        switch( GetOp() ) {
        case push: 
            scanf("%d %d", &Sn, &X);
            if (!Push(X, S, Sn)) printf("Stack %d is Full!\n", Sn);
            break;
        case pop:
            scanf("%d", &Sn);
            X = Top_Pop(S, Sn);
            if ( X==ERROR ) printf("Stack %d is Empty!\n", Sn);
            break;
        case end:
            PrintStack(S, 1);
            PrintStack(S, 2);
            done = 1;
            break;
        }
    }
    return 0;
}
void PostOrderNonRecursive(struct BinaryTreeNode *root){
    TreeStack *S = CreateStack(10);
    while(1) {
        //自分をスタックにpushして左に行けるところまで行く。
        if(root) {
            Push(S, root);
            root = root->left;
        }
        else {
            if(IsEmptyStack(S)) {
                printf("Stack is Empty\n");
                return;
            }
            //右にも行けなくなったらpopして1つ前のノードを取り出す(1つ戻る)
            else if(Top(S)->right == NULL) {
                root = Pop(S);
                printf("%d", root->data);
                //
            }
            while(root == Top(S)->right) {
                    printf("%d", Top(S)->data);
                    root = Pop(S);
            }
            
            if(!IsEmptyStack(S)){
                    root = Top(S)->right;
            }else{
                root = NULL;
            }
        }
    }
    DeleteStack(S);
}
Esempio n. 8
0
int main() 
{
    int i;
    Stack S = CreateStack();

    /* test: IsEmpty */
    printf("[IsEmpty]IsEmpty? The answer is: %d\n", IsEmpty(S));
    
    /* test: push num 0-9 */
    printf("[Push]Push num 0-9\n");
    for (i = 0; i < 10; i++)
        Push(i, S);

    /* test: Pop top 3 */
    printf("[Pop & Top]Pop top 3:  ");
    i = 3;
    while (i>0 && !IsEmpty(S)) {
        printf("%d ", Top(S));
        Pop(S);
        i--;
    }
    printf("\n");

    /* test: MakeEmpty */
    MakeEmpty(S);
    printf("[MakeEmpty]MakeEmpty success? [%d]\n", IsEmpty(S));

    return 0;
}
Esempio n. 9
0
int main(){
printf("Enter size of input, soldier!\n");
int n;
scanf("%d", &n);

printf("Enter the string, DARLINNNNNG \n");
char s[n];

//scanf("%s", &s);
//printf("%s", s);

for (int i=0; i<=n;i++){
	scanf("%c",&s[i]);
}

struct ArrayStack *S = CreateStack();


for (int i=0; i<=n;i++){
	Push(S,s[i]);

}

for(int i=0;i<=n;i++){
	printf("%c",Pop(S));
}


return 0;	

}
Esempio n. 10
0
void KomLoop(void) {
  int defaultCmd;
  g_unreadRepliesStack = CreateStack();

  for(;;) {
    if(StackSize(g_unreadRepliesStack) > 0) {
      defaultCmd = CMD_NEXTREPLY;
    } else if(hasUnreadInConf(mote2)) {
      defaultCmd = CMD_NEXTTEXT;
    } else if(HasUnreadMail()) {
      defaultCmd = CMD_GOMAIL;
    } else if(FindNextUnreadConf(mote2) >= 0) {
      defaultCmd = CMD_NEXTCONF;
    } else {
      defaultCmd = CMD_SEETIME;
    }

    displayPrompt(defaultCmd);
    if(shouldLogout()) {
      break;
    }
  }

  DeleteStack(g_unreadRepliesStack);
}
Esempio n. 11
0
int main()
{
    Stack S = CreateStack(2*numberOfOperator);
    char string[1000];
    fgets(string,1000,stdin);
    char* str =NULL;
    str = strtok(string, " \n");
    while(str != NULL) {
        if(notOp(str[0]))
        {
            Token t= malloc(sizeof(struct token));
            t->str=str;
            t->Type = Term;
            Push(t,S);
        }
        else {
            Operator Op = str[0];
            Token token2 = TopAndPop(S);
            Token token1 = TopAndPop(S);
            Token token3 = concatenate(token1,token2,Op);
            Push(token3,S);
        }
        str = strtok(NULL, " \n");
    }
    printf("%s\n",TopAndPop(S)->str);
    return 0;
}
//输入为后缀表达式
PtrToNode CreateExprTree(const char *ExprStr)
{
	int i;
	Stack S;
	PtrToNode pTmp;

	S = CreateStack(50);
	for(i=0; ExprStr[i] != '\0'; ++i)
	{
		pTmp = (PtrToNode)malloc(sizeof(struct Node));
		if(ExprStr[i]!='+' && ExprStr[i]!='-' && ExprStr[i]!='*' && ExprStr[i]!='/')
		{
			pTmp->value = ExprStr[i];
			pTmp->pLeft = NULL;
			pTmp->pRight = NULL;
			Push(S, pTmp);
		}
		else
		{
			pTmp->value = ExprStr[i];
			pTmp->pRight = Pop(S);
			pTmp->pLeft = Pop(S);
			Push(S, pTmp);
		}
	}
	pTmp = Pop(S);
	DisposeStack(S);
	return pTmp;
}
Esempio n. 13
0
 int main(void)
{    STACK* Top=CreateStack();

   int i=0,p=2,q=4,r=6;
   Top= push(Top,&i);
   i=10;
   Top= push(Top,&i);
   i=20;
   Top= push(Top,&i);
   i=40;
     Top= push(Top,&i);
   //  for(int i=0;i<10;i++)
//    {
//             printf("\n Top before push is %d and value of i is %d",Top,i);
//        Top= push(Top,&i);
//           // printf("\n Top after push is %d value of i si %d",Top,i);
//     }
     
     printf("\n\nEntering pop next Top after push is %d and address is %d",*(int*)(Top->STACK_TOP->data),Top->STACK_TOP);
     for(int j=0;j<3;j++)
     {
       Top= pop(Top);
    }
      getch();
      return 0;   
}    
int evaluate( char *exp)
{
	struct Stack *stack = CreateStack();
	int p,q;
	while(*exp)
	{
		if(*exp == '' || *exp == '\t')
		{
			exp++;
			continue;
		}
		else if( isdigit(*exp))
		{
			Push(stack, (*exp)-48);
		}
		else
		{
			p = Pop(stack);
			q = Pop(stack);
			Push(stack,calculate(p,q,exp));

		}
		exp++;
		
	}
Esempio n. 15
0
void printReverseLevelOrder(tree_node *root){
	Stack *st = CreateStack();
	Queue *q = CreateQueue();
	enqueue(q, root);
	enqueue(q, NULL);
	while(!isEmpty(q)){
		tree_node *tmp = dequeue(q);
		if(tmp == NULL){
			//level changed
			if(!isEmpty(q)){
				enqueue(q, NULL);
			}
		} else {
			push(st, tmp);
			if(tmp->left != NULL)			
				enqueue(q, tmp->left);
			if(tmp->right != NULL)			
				enqueue(q, tmp->right);
		}
	}
	while(!isEmptyStack(st)){
		printf("%d \t", pop(st)->data);
	}
	printf("\n");
	free(q);
	free(st);
}
Esempio n. 16
0
void Queen(int * board, int n)
{
	Stack * StateStack;
	if ( CreateStack(&StateStack, sizeof(State), n * n) == false )
	{
		printf("Loi: Khong du bo nho!\n");
		exit(0);
	}
	
	int i, k;
	
	i = 0;
	k = 0;
	
	State * state;
	
	do
	{
		if (k < n)
		{
			for (i = 0; i < n; ++i)
			{
				state = CreateState(k, i);
				push(StateStack, state);
			}
		}
		
		state = pop(StateStack);
		k = state->k;
		i = state->i;
		board[k] = i;
		
		free(state);
		
		if (k == n - 1)
		{
			do
			{
				if (CheckBoard(board, n) == true)
				{
					Mark(board, n);
				}
				
				state = pop(StateStack);
				k = state->k;
				i = state->i;
				board[k] = i;
				
				free(state);
				
			} while (k == n-1);
		}
		
		++k;
		
	} while (CheckEmpty(StateStack) != true);
	
	DestroyStack(StateStack);
}
Esempio n. 17
0
int main()
{

	char *a="{[()]}";
	Stack S=CreateStack();
	Matcher(a,S);
	return 0;
}
int main(){
    char array[20] = "6523+8*+3+*";
    Stack S;
    int i = 40;
    S = CreateStack( i );
    Postfix_expression( array,S );
    return 0;
}
Esempio n. 19
0
int main(){
    printf("Hello\n");
    struct DynArrayStack *S = CreateStack();
    for(int i = 0; i < 10; i++){
        Push(S, 1);
    }
    Pop(S);
    return 1;
}
Esempio n. 20
0
void TransmitExpression(ElemType *arr)
{
	valuation();
	assert(arr);
	Stack S;
	CreateStack(&S);                                 //创建一个栈
	ElemType x= 0;
	int i = 0;
	int j = 0;
	while (arr[i] != '\0')
	{ 
		if (judge_type(arr[i]) == 1)                          //判断是不是数字
		{
			post[j++] = arr[i];                               //数字则直接存入post[]
			if (judge_type(arr[i+1]) != 1)
				post[j++]=' ';                                //以空格隔开
			i++;
		}
		else
		{
			if (S.ebp == S.esp)                            //如果栈为空,压栈
			{
				push(&S, arr[i]);
				i++;
			}
			else
			{
				switch (judge_priority(&S, arr[i]))
				{
				case '<':                              //如果栈顶的优先级低。则直接入栈
					push(&S, arr[i]);
					i++;
					break;
				case '>':                             //如果读取的优先级高,则栈顶先退栈
					pop(&S, &x);
					post[j++] = x;
					post[j++] = ' ';
					break;
				case '=':
					pop(&S, &x);
					i++;
					break;
				}
			}
		}
	}
	while (pop(&S, &x))
	{
		if (x != '('&&x != ')')
		{
			post[j++] = x;
			post[j++] = ' ';
		}
	}
	printf("%s\n", post);
	free(S.ebp);
}
Esempio n. 21
0
int main()
{
	Stack s;
	int n;
	printf("Input n you want to CreateStack\n");
	scanf("%d", &n);
	s = CreateStack(n);
	PrintStack(s);
}
Esempio n. 22
0
//3.18a
void languageExamine(char *s)
{
	Stack S = CreateStack(100);

	while (*s != '\0') {
		switch (*s) {
			case 'b':
				if (*(s + 1) == 'e' || *(s + 2) == 'g' || *(s + 3) == 'i' || *(s + 4) == 'n')
					Push('b', S);
				break;
			case '(':
				Push('(', S);
				break;
			case '[':
				Push('[', S);
				break;
			case '{':
				Push('{', S);
				break;
			case 'e':
				if (*(s + 1) == 'n' || *(s + 2) == 'd')
					if (TopAndPop(S) != 'b') {
						printf("warning: losing \"begin\" before \"end\"\n");
						return;
					}
				break;
			case ')':
				if (TopAndPop(S) != '(') {
					printf("warning: losing \"(\" before\")\"\n");
					return;
				}
				break;
			case ']':
				if (TopAndPop(S) != '[') {
					printf("warning: losing \"[\" before\"]\"\n");
					return;
				}
				break;
			case '}':
				if (TopAndPop(S) != '{') {
					printf("warning: losing \"{\" before\"}\"\n");
					return;
				}
				break;
			default:
				break;
		}
		s++;
	}
	if (!IsEmpty(S))
		printf("Wrong Syntax! Something missing but tired to check:)\n");
	else
		printf("Correct Sytax\n");
	
	DisposeStack(S);
}
Esempio n. 23
0
int main(void)
{
	Stack s1;
	Stack s2;
	s1 = CreateStack(10);
	s2 = CreateStack(10);
	Item it = 1;
	AddQueue(it, s1,s2);
	it = 2;
	AddQueue(it,s1,s2);
	it = 3;
	AddQueue(it,s1,s2);
	DeleteQueue(&it,s1,s2);
	printf("%d\n",it);
	DeleteQueue(&it,s1,s2);
	printf("%d\n",it);
	DeleteQueue(&it,s1,s2);
	printf("%d\n",it);
}
Esempio n. 24
0
void Push(Stack stack, Set set)
{
	if (stack != NULL)
	{
		Stack p = stack->next;
		stack->next = CreateStack();
		stack->next->set = set;
		stack->next->next = p;
	}
}
Esempio n. 25
0
/* Hook up events to fatal signals, so we can clean up if we're killed. */
void SignalHandler::OnClose( handler h )
{
	if( saved_sigs == NULL )
	{
		saved_sigs = new SaveSignals;

		bool bUseAltSigStack = true;

#if defined(LINUX)
		/* Linuxthreads (pre-NPTL) sigaltstack is broken. */
		if( !UsingNPTL() )
			bUseAltSigStack = false;
#endif

		/* Allocate a separate signal stack.  This makes the crash handler work
		 * if we run out of stack space. */
		const int AltStackSize = 1024*64;
		void *p = NULL;
		if( bUseAltSigStack )
			p = CreateStack( AltStackSize );

		if( p != NULL )
		{
			stack_t ss;
			ss.ss_sp = (char*)p; /* cast for Darwin */
			ss.ss_size = AltStackSize;
			ss.ss_flags = 0;
			if( sigaltstack( &ss, NULL ) == -1 )
			{
				LOG->Info( "sigaltstack failed: %s", strerror(errno) );
				p = NULL; /* no SA_ONSTACK */
			}
		}
		
		struct sigaction sa;

		sa.sa_flags = 0;
		if( p != NULL )
			sa.sa_flags |= SA_ONSTACK;
		sa.sa_flags |= SA_NODEFER;
		sa.sa_flags |= SA_SIGINFO;
		sigemptyset(&sa.sa_mask);

		/* Set up our signal handlers. */
		sa.sa_sigaction = SigHandler;
		for( int i = 0; signals[i] != -1; ++i )
			sigaction( signals[i], &sa, NULL );

		/* Block SIGPIPE, so we get EPIPE. */
		sa.sa_handler = SIG_IGN;
		sigaction( SIGPIPE, &sa, NULL );
	}
	handlers.push_back(h);
}
Esempio n. 26
0
int main() {
    Stack *s = CreateStack(1);
    printf("栈是否已满:%s\r\n", IsFull(s) ? "是" : "否");
    printf("栈是否为空:%s\r\n", IsEmpty(s) ? "是" : "否");
    Push(s, 1);
    Push(s, 2);
    printf("出栈元素为%d\r\n", Pop(s));
    printf("出栈元素为%d\r\n", Pop(s));
    Pop(s);
    return 0;
}
Esempio n. 27
0
int main(){
    Stack stack;
    stack = CreateStack(1000);
    int temp;
    for(temp=0;temp<100;temp++){
        Push(temp, stack);
    }
    while(!IsEmpty(stack)){
        printf("%d\n", TopAndPop(stack));
    }
    return 0;
}
Esempio n. 28
0
int main(int argc, const char * argv[]) {
    Stack S;
    S = CreateStack();
    MakeEmpty(S);
    for (int x = 0; x < 10; x++) {
        Push(x, S);
    }
    for (int x = 0; x < 10; x++) {
        printf("%d\n", Top(S));
        Pop(S);
    }
    return 0;
}
Esempio n. 29
0
int main(void) {
	Stack S;
	int i=0;
	int data;
	S = CreateStack();
	for(i=0;i<10;i++)
		Push(S,i);
	for(i=0;i<10;i++)
	{
		data = Pop(S);
		printf("%d  ",data);
	}
}
Esempio n. 30
0
osg::Node* createScene()
{
	osg::Group* root = new osg::Group();

	osg::Box *planeBox = new osg::Box(osg::Vec3(0,0,0),500.0,500.0,0.1);
	osg::Geode* plane = new osg::Geode();
	plane->addDrawable(new osg::ShapeDrawable(planeBox));

	root->addChild(plane);
	//root->addChild(CreateCube(NxVec3(0.0,0.0,50.0),5));
	root->addChild(CreateStack(10));
	//printf("createScene Success!\n");
	return root;
}