Example #1
0
void zigzag(struct node* root){
	int leftToright = 1;
	struct stack* currentLevel = createStack();
	struct stack* nextLevel = createStack();
	push(currentLevel, root);
	struct node* temp;
	while(!isStackEmpty(currentLevel)){
		temp = pop(currentLevel);
		if(temp){
			printf("\n%d",temp->data);
			if(leftToright == 1){
				push(nextLevel, temp->left);
				push(nextLevel, temp->right);
			}
			else{
				push(nextLevel, temp->right);
				push(nextLevel, temp->left);
			}
		}
		if(isStackEmpty(currentLevel)){
			struct stack* tempStack;
			leftToright = 1-leftToright;
			tempStack = currentLevel;
			currentLevel = nextLevel;
			nextLevel = tempStack;
		}
	}
}
Example #2
0
char *toPrefix (char *exp)
{
	
	int tos1 = -1;
	char *prefix = (char *)calloc(SIZE, sizeof(char));
	int tos2 = -1;
	char *operator = (char *)calloc(SIZE, sizeof(char));
	
	int l = (int)strlen(exp);
	int i;
	
	for (i = l - 1; i >= 0; --i)
	  {
		char z = *(exp + i);
		
		if (isOperand(z))
			push(prefix, z, &tos1);
		
		else if (operatorPrec(z) == 0)
			push(operator, z, &tos2);
		
		else if (isOperator(z))
		  {
			while (!isStackEmpty(tos2) && operatorPrec(z) < operatorPrec(*(operator + tos2)))
			  {
				char op =  pop(operator, &tos2);
				if (isOperator(op))
					push(prefix, op, &tos1);
			}
			push(operator, z, &tos2);
		}
		
		else if (indexOf(z, "([{") != -1)
		  {
			while (operatorPrec(*(operator + tos2)) != 0)
				push(prefix, pop(operator, &tos2), &tos1);
			pop(operator, &tos2);
		}
		
		else
			continue;
		
	}
	
	while(!isStackEmpty(tos2))
		push(prefix, pop(operator, &tos2), &tos1);
	
	reverse(prefix, tos1);
	return prefix;
}
Example #3
0
void preorder (BTree* btp) {
    Stack S;
    S.top = NULL;
    Node* n = btp->root;
    do {
        while(n != NULL) {
            printf("+%s",n->str);
            push(&S,n->r);
            n = n->l;
        }
        if (!isStackEmpty(&S)) {
            n = pop(&S);
        }
    } while((!isStackEmpty(&S))||(n != NULL));
}
Example #4
0
void assert_stack_is_not_empty_when_an_element_is_pushed()
{
    Stack *stk = newStack(10);
    push(stk, 5);
    assert(FALSE == isStackEmpty(stk));
    freeStack(stk);
}
Example #5
0
char * pop (char ** stack, int *tos) {
	if (isStackEmpty (*tos)) {
		printf("\n\tSTACK UNDERFLOW\n");
		return UNDERFLOW;
	}
	return *(stack + ((*tos)--));
}
Example #6
0
void postorder_stack(BTree* btp) {
    Stack S;
    Node* c = btp->root;
    S.top = NULL;
    push(&S,c);
    Node *p = NULL;
    while (!isStackEmpty(&S)) {
        c = peek(&S);
        if (p == NULL || p->l == c || p->r == c) {
            if (c->l != NULL) {
                push(&S,c->l);
            } else if (c->r != NULL) {
                push(&S,c->r);
            } else if (c->l == NULL && c->r == NULL) {
                printf("%s ",c->str);
                c = pop(&S);
            }
        }
        if (c->l == p) {
            push(&S,c->r);
            if (c->r == NULL) {
                printf("%s ",c->str);
                c = pop(&S);
            }
        }
        if (c->r == p) {
            printf("%s ",c->str);
            c = pop(&S);
        }
        p = c;
    }
}
Example #7
0
bool hasPathSum(struct TreeNode* root, int sum) {
	if(!root) return false;

	SumStackNode *stackTop = StackCreate(); 
	stackTop = StackPush(NULL, root, 0);

	while(!isStackEmpty(stackTop)){
		int summary = stackTop->currentSum;
		TreeNode *currentNode = stackTop->treeNode;
		stackTop = StackPop(stackTop);

		while(currentNode->left || currentNode->right){
			if(currentNode->left && currentNode->right){
				stackTop = StackPush(stackTop, currentNode->right, summary);
				currentNode->right = NULL;
			}
			
			currentNode = (currentNode->left)?currentNode->left:currentNode->right;
			summary += currentNode->val;
		}
		if(summary == sum) return true;
	}

	return false;    
}
Example #8
0
void test_10_check_isStackEmpty_when_stack_is_empty(){
	int inputArr[] = {0,0,0,0};
	int result;
	Stack intData = create_ints(4, inputArr);
	result = isStackEmpty(&intData);
	ASSERT(result == 0);
}
Example #9
0
/*   listed in st subtree                                                    */
void generate(APTNode *root, FILE *out)
{
	int i;

	if (root == NULL)
	{
		printf("error: APT node can not be NULL!");
		exit(0);
	}


	clearAll(&SS);

	//used for processing <MINUStk> <F> expressions
	ZERO = createVElement("0");

	//used for computing expressions
	initStack(&opStack);
	initStack(&resStack);

	//list of all program variable names
	initStack(&distinctVarNames);

	//<VARtk> node not null?
	if (root->numChildren == 2)
	{
		//ADD INITIAL MARKER
		push(MARKER, &SS, 0);
		if (DEBUG) { printf("root has 2 children \n"); }

		//ADD VARIABLES TO LOCAL SCOPE
		pushExistingVars(root->children[0], &SS, out);

		//TRAVERSE TREE
		recGen(root->children[1], out);

		//REBUILD PREVIOUS CONTEXT
		popExistingVars(root->children[0], &SS, out);

		VElement* ve = pop(&SS);
	}else{
		//<VARtk> IS NULL
		if (DEBUG) { printf("root has 1 child \n"); }
		//TRAVERSE TREE
		recGen(root->children[0], out);
	}

	fprintf(out, "\tSTOP\n");

	while(!isStackEmpty(&distinctVarNames))          // allocate storage for program variables
	{
		VElement* v = pop(&distinctVarNames);
		fprintf(out, "%s\t0\n", v->data);
	}

	for (i = 0; i < varCntr; i++)      // allocate space for temporary variables
	{
		fprintf(out, "V%d\t0\n", i);
	}
}
Example #10
0
String pop (STACK_p_t stack) {
	if (isStackEmpty(*stack)) {
		printf("\n\tSTACK UNDERFLOW!\n\n");
		return UNDERFLOW_CHAR;
	}
	return *(stack->arr + ((stack->tos)--));
}
Example #11
0
loliObj* pop(){
	if(isStackEmpty()){
		std::cout<<"Error: Stack is empty!"<<std::endl;
		return NULL;
	}
	return &loli_stack[--obj_in_stack];
}
Example #12
0
File: stack.c Project: lireric/ssn
void* popStack(sCommonStack* psStack)
{
	if (isStackEmpty(psStack))
	{
		return NULL;
	}
	return ((void**)psStack->pStackArray)[--(psStack->top)];
}
Example #13
0
void assert_stack_is_empty_after_all_elements_are_popped()
{
    Stack *stk = newStack(10);
    push(stk, 9);
    pop(stk);
    assert(TRUE == isStackEmpty(stk));
    freeStack(stk);
}
Example #14
0
int top(Stack S){
	struct LinkedListStack *s = GetStack(S);
	if(isStackEmpty(s)){
		printf("Nothing on the top!\n");
		return -1;
	}
	return s->top->data;
}
Example #15
0
int* top(int* step, int* i, int* j, int* movedDisc) {
	if(isStackEmpty()) {
		perror("ERROR: stack is empty for top");
		return NULL;
	}
	*step = stack->top->step;
	*i = stack->top->i;
	*j = stack->top->j;
	*movedDisc = stack->top->movedDisc;
	return stack->top->data;
}
Example #16
0
//process all operators from the OPERATOR_STACK, going from top to the bottom  up to the first "(" operator or an empty stack, whichever comes first
void reduceParanthesis(FILE* out, int withLPar)
{
	char name[20];

	if (isStackEmpty(&opStack))
	{
		if (DEBUG) {printf("reduceParanthesis(): OP stack is empty\n");}
		return;
	}

	char* topOp = topElement(&opStack);
	if (!isOperator(topOp))
	{
		if (DEBUG) {printf("reduceParanthesis(): OP_POP(%s)\n", D(topOp));}
		pop(&opStack);
		return;
	}
	if (DEBUG) {printf("reduceParanthesis(): topOp = %s\n", D(topOp));}

	//1. take the number at the top of the RESULT_STACK
	VElement* temp = pop(&resStack);
	if (DEBUG) {printf("reduceParanthesis(): RES_POP(%s)\n", D(temp->data));}

	//2. load it into ACC
	fprintf(out, "\tLOAD\t%s\n", temp->data);
	while (isOperator(topOp))
	{
		if (DEBUG)
		{
			printf("reduceParanthesis(): OP stack is: \n"); displayStack(&opStack);
			printf("reduceParanthesis(): RES stack is: \n"); displayStack(&resStack);
		}

		//3. perform operation between ACC, number on top of RESULT_STACK using operator on top of OPERATOR_STACK; store result in ACC
		performOperationWithACC(out);

		topOp = topElement(&opStack);
	}//repeat until empty stack or "(" is encountered

	if (withLPar)
	{
		//remove left paranthesis and store ACC on RESULT_STACK
		strcpy(name, newName(VAR));
		fprintf(out, "\tSTORE\t%s\n", name);
		if (DEBUG) {printf("reduceParanthesis(): RES_PUSH(%s)\n", D(name));}

		push(createVElement(name), &resStack, -1);

		if (DEBUG) {printf("reduceParanthesis(): OP_POP(%s)\n", D(topOp));}

		pop(&opStack);
	}
}
Example #17
0
struct node* pop(struct stack* S){
	struct stackNode* temp;
	if(!isStackEmpty(S)){
		temp = S->top;
		S->top = S->top->next;
		return(temp->element);
	}
	else{
		printf("\nUnderflow");
		return NULL;
	}
}
Example #18
0
void preorder_stack (BTree *bt) {
    Stack S;
    Node *n = bt->root;
    S.top = NULL;
    push(&S,n);
    while (!isStackEmpty(&S)) {
        n = pop(&S);
        printf("%s ",n->str);
        push(&S,n->r);
        push(&S,n->l);
    }
}
Example #19
0
int pop(Stack S){
	struct LinkedListStack *s = GetStack(S);
	if(isStackEmpty(s)){
		fprintf(stderr, "Stack underflow! Cannot Pop\n");
		return -1;
	}
	int n = s->top->data;
	struct Node *temp = s->top->next;
	free(s->top);
	s->top=temp;
	return n;
}
Example #20
0
void printStack(Stack S){
	struct LinkedListStack *s = GetStack(S);
	if(isStackEmpty(s)){
		printf("Stack is empty nothing to print\n" );
		return;
	}
	int i;
	struct Node *temp = s->top;
	while(temp!=NULL){
		printf("%d\n",temp->data);
		temp = temp->next;
	}
}
Example #21
0
int pop()
{
	int retVal = 0;
	if(isStackEmpty())
	{
		printf("\n Stack is empty. \n");
	}
	else
	{
		retVal = stackArr[--top];
	}
	return retVal;
}
Example #22
0
int main() {
        Stack *s = stackInit();
        push(s, 4);
        push(s, 2);
        push(s, 6);
        push(s, 9);
        push(s, 7);
        while (!isStackEmpty(s)) {
                fprintf(stdout, "%d\n", pop(s));
        }
	deleteStack(s);

        return 0;
}
Example #23
0
void pop() {
	/*printf("\nPOP\n");*/
	StackItem* tmp;
	if(isStackEmpty()) {
		perror("ERROR: stack is empty for pop");
		return;
	}

	stack->num--;
	tmp = stack->top;
	stack->top = stack->top->next;
	free(tmp->data);
	free(tmp);
}
Example #24
0
struct BNode* pop(struct stack* s)
{
	struct stacknode* t;
	if(isStackEmpty(s)!=1)
	{
	 t = s->top;
	s->top=t->next;
	return t->data;
	}

	return NULL;


}
Example #25
0
void inorder_stack (BTree *bt) {
    Stack S;
    Node *n = bt->root;
    S.top = NULL;
    while (!isStackEmpty(&S) || n != NULL) {
        if (n != NULL) {
            push(&S,n);
            n = n->l;
        } else {
            n = pop(&S);
            printf("%s ",n->str);
            n = n->r;
        }
    }
}
Example #26
0
/*Method that prints the contents of the Stack Structure*/
void 
printStack(struct stack *s){
	//printf("While printing stack");
	if(isStackEmpty(s) == 0){
		struct stackNode *tNode = malloc(sizeof(struct stackNode));
		tNode = s->front;
		printf("Stack Structure is \n");
		printf("Stack size is %d\n",s->size);
		while(tNode->next != NULL){
			printf("%d->",tNode->node->value);
			tNode = tNode->next;
		}
		printf("%d\n",tNode->node->value);
	}	
	return;
}
Example #27
0
void topSort (G *g)
{
  int *visit  =  calloc (g->N, sizeof (int));
  unsigned int i;
  for (i=0;i<g->N;i++)
    visit[i] = 0;

  stack *s = initStack ();

  for (i=0;i<g->N;i++)
  {
    if (visit[i] == 0)
      topSortDfsUtil (g, i, visit, s);
  }

  while (false == isStackEmpty (s))
    printf ("%d ", pop(s));
}
Example #28
0
void stackMoudleTest(void)
{
    Stack s;
    initStack(&s);
    // 创建元素
    stactTest a1,a2,a3;
    List_head* node;
    stactTest* t;
    a1.data = 10;
    a2.data = 20;
    a3.data = 30;
    stackPush(&s, &a1.member);
    stackPush(&s, &a2.member);
    stackPush(&s, &a3.member);
    while (!isStackEmpty(&s)) {
        node = stackPop(&s);
        t = list_emtry(stactTest,node,member);
        printf("YHP--data:%d\n",t->data);
    }
}
Example #29
0
/*'Pushes' a Binary Node structure to the front of the Stack (LIFO)*/
void push(struct stack *s, struct binaryTreeNode *n, int optionD){

	//If the stack is empty, set the front and rear to 'n'
	if (isStackEmpty(s) == 1){
		s->front->node = n;
		s->front->next = NULL;
		s->rear->node = n;
		s->rear->next = NULL;
		s->start = clock();
	}
	//append the node to the Stack and move the 'front' pointer
	else{
		struct stackNode *temp = malloc(sizeof(struct stackNode));
		temp->next = s->front;
		temp->node = n;
		s->front = temp;
	}
	s->size += 1;
	return;
}
Example #30
0
int descendantsTest(){
    stackPushState(newState(0,0));
    Node* currentNode;
    int i=0;
    while(!isStackEmpty()){
        currentNode = stackPeek();
        if(currentNode->isBranched) {
            stackDeleteTop();
        } else {
            currentNode->isBranched = 1;
            if(i<3){
                stackPushState(newState(i*3+1,1));
                stackPushState(newState(i*3+2,i));
                stackPushState(newState(i*3+3,i));
                i++;
            }
        }
        stackPrintOut();
    }
    return 0;
}