Exemplo n.º 1
0
int main(){
  char equation[100];//to hold entered eqn.
  makeEmptyStack();//get us a stack
  printf("Please enter the equation to be evaluated.\n");

  while(1){
	scanf("%s", equation);//get eqn from stdin

	if(*equation == 'q'){//quit
		break;//get out
	}else if(*equation == 'p'){//print
		print_stack();//call print
	}else if(*equation == 't'){//peek val
		if(!isEmptyStack()){//verify we are not at end
			printf("%d\n", peek());//show val
		}//else ignore this
	}else if(*equation == 'w'){//wipe the stack
		while(!isEmptyStack()){//while not empty
			pop();//keep popping vals off
		}
	}else{//handled all 'commands', so this must be a calculation or value
		stack_ops(equation);//handle it
	}
  }
  return 0;
}
Exemplo n.º 2
0
void dfs(int startNode)
{
    printf("DFS started\n");
  int *mark=(int*)malloc(sizeof(int)*nrOfVerteces);
  int i;
    for (i=0;i<nrOfVerteces;i++)
    {
        mark[i]=UNVISITED;
    }
    push(startNode);
    while (!isEmptyStack())
    {
        int v=peekStack()->content;
        pop();
        if (mark[v]==UNVISITED)
        {
            mark[v]=VISITED;
            int *neighbours=getAllNeighborsOfVertex(v);
            int nrOfNeighbours=getNumberOfNeighborsOfVertex(v);
            for(i=nrOfNeighbours-1;i>=0;i--)
            {
                push(neighbours[i]);
            }
            printf("%d ",v);
        }
    }
    printf("\nDFS ended\n\n");
}
Exemplo n.º 3
0
void dfs(int searchNode)
{
    printf("\nDFS started:\n");
    int *visited=(int *)malloc(sizeof(int)*nrOfVertices);
    int i,v;
    for(i=0; i<nrOfVertices; i++)
    {
        visited[i]=UNVISITED;
    }
    push(searchNode);
    while(!isEmptyStack())
    {
        v=peekStack()->content;
        pop();
        if(visited[v]==UNVISITED)
        {
            visited[v]=VISITED;
            printf("%c ",v+65);
            int nrOfNeighbors=getNumberOfNeighborsOfVertex(v);
            int *neighbors=getAllNeighborsOfVertex(v);
            for(i=nrOfNeighbors-1; i>=0; i--)
            {
                int w=neighbors[i];
                if(visited[w]==UNVISITED)
                {
                    push(w);
                }
            }
        }
    }
    printf("\nDFS ended:\n");
}
Exemplo n.º 4
0
void dfsMatrix(int searchNode)
{
    int* visited=(int*)malloc(nrOfVertices*sizeof(int));
    int i;
    for (i=0; i<nrOfVertices; i++)
    {
        visited[i]=0;
    }
    push(searchNode);
    while(!isEmptyStack())
    {
        int v=peekStack()->content;
        pop();
        if (visited[v]==0)
        {
            visited[v]=1;
            int* aux=getAllNeighborsOfVertexFromMatrix(v);
            for (i=getNumberOfNeighborsOfVertexFromMatrix(v)-1; i>=0; i--)
            {

                int w=aux[i];
                if (visited[w]==0)
                    push(w);
            }
            printf("%d ",v);
        }
    }
    printf("\n");

}
Exemplo n.º 5
0
void lDfs(int startNode)
{
    int *visited = (int*)malloc(nrOfVertices*sizeof(int));
    int i, v, w;

    for (i = 0; i < nrOfVertices; i++)
        visited[i] = UNVISITED;
    push(startNode);
    while(!isEmptyStack())
    {
        v = peekStack();
        pop();
        if(visited[v] == UNVISITED)
        {
            visited[v] = VISITED;
            for (i = (getNumberOfNeighborsOfVertex(v) - 1); i >= 0; i--)
            {
                w = *(getAllNeighborsOfVertex(v) + i);
                if (visited[w] == UNVISITED)
                    push(w);
            }
            printf("\n%d", v);
        }
    }
}
Exemplo n.º 6
0
void dfs(nodeT** adjList, int searchNode)
{
    int *Visited, *neighbors;

    int i,nr,w, v;

    Visited = (int *)malloc(nrOfVerteces* sizeof(int));
    push(searchNode);
    while(!isEmptyStack())
    {
        v=peekStack()->code;
        pop();
        if(Visited[v]==0)
        {
            Visited[v]=1;
            neighbors=getAllNeighborsOfVertex(v, adjList);
            nr=getNumberOfNeighborsOfVertex(v, adjList);
            for(i=0; i<nr; i++)
            {
                w=neighbors[i];
                if(Visited[w]==0)
                    push(w);
            }
            printf("%c, ", v+65);
        }

    }
}
Exemplo n.º 7
0
int peek(){
	if(isEmptyStack()){//if already empty
		underflow();//show underflow
	}else{
		return mem[TOP];//otherwise return val
	}
}
Exemplo n.º 8
0
int pop(){  
	if(isEmptyStack()){//if already empty
		underflow();//show underflow
	}else{
		return mem[TOP--];//otherwise return val, decrement top location
	}
}
Exemplo n.º 9
0
void dfs(int searchNode)
{
    printf("\n");
    int mark[100];
    int i;
    for (i=0; i<100; i++)
    mark[i]=0;
    push(searchNode);
    while (!isEmptyStack())
    {
        int v=peekStack()->content;
        pop();
        if (mark[v]==0)
        {
            mark[v]=1;
            int ct=getNumberOfNeighborsOfVertex(v);
            int *a=getAllNeighborsOfVertex(v);
            for (i=ct-1; i>=0; i--)
            {
               if (mark[a[i]]!=1)
               {
                    push(a[i]);
                }
            }
            printf("%d ",v);
        }
    }
}
Exemplo n.º 10
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);
}
int isPathDfs(Graph g, Vertex v, Vertex w)
{
	int isPath = FALSE;
	Stack s = newStack(); // create a new stack
	Vertex currentVertex = 0;
	int *visited = calloc(g->nV, sizeof(int)); // allocate + init to 0
	int i = 0;

	assert(visited != NULL);
	push(s, v); // push first vertex onto stack
	visited[v] = TRUE; // mark it as visited

	while ( !isEmptyStack(s) ){ // still have vertices to traverse
		currentVertex = pop(s); // get a vertex from the stack
		printf("Visiting: %d\n", currentVertex);
		visited[currentVertex] = TRUE; // mark it as visited
		if (currentVertex == w) { isPath = TRUE; }
		for (i = 0; i < g->nV; i++){ // search for a vertex we haven't visited
			if (g->edges[currentVertex][i] && !visited[i]){ // ignore pendants
				push(s, i); // push vertex onto stack
				visited[i] = TRUE;
			}
		}
	}

	free(visited);
	deleteStack(s);
	return isPath;
}
Exemplo n.º 12
0
int pop(struct simpleArrayStack *S)
{
    int val;
    if(isEmptyStack(S))
        return NULL;
    val=S->array[(S->top)--];
    return val;
}
Exemplo n.º 13
0
char Pop(struct ArrayStack *S){

	if(!isEmptyStack(S)){
		return S->array[S->top--];
	}
	return NULL;

}
Exemplo n.º 14
0
void topStack(stack *s,char *word){
	if(isEmptyStack(s)==1){
		printf("\nError Cant Find Top of Empty Stack !!\n");
		exit(-4);
	}
	copyString(word,s->words[s->size-1]);
	return;
}
Exemplo n.º 15
0
void dijkstra(int startNode)
{
    printf("Dijkstra's Algorithm started\n");
    int * distances = (int*)malloc(nrOfVerteces * sizeof(int));
    int * previous  = (int*)malloc(nrOfVerteces * sizeof(int));
    int * visited   = (int*)malloc(nrOfVerteces * sizeof(int));
    int nrOfVertecesVisited=0,i;
    for(i=0; i<nrOfVerteces; i++)
    {
        distances[i] = MAX;
        previous[i]  = UNDEFINED;
        visited[i] = UNVISITED;
    }
    distances[startNode] = 0;

    while(nrOfVertecesVisited < nrOfVerteces)
    {
        int u = getMinDistanceVertex(distances,visited);
        visited[u] = VISITED;

        int nrOfNeighbors = getNumberOfNeighborsOfVertex(u);
        int * neighbors = getAllNeighborsOfVertex(u);
        for(i=0; i<nrOfNeighbors; i++)
        {
            int w = neighbors[i];
            int alt = distances[u] + adjMatrix[u][w];
            if(alt<distances[w])
            {
                distances[w] = alt;
                previous[w] = u;
            }
        }
        nrOfVertecesVisited++;
    }

    for(i=0; i<nrOfVerteces; i++)
    {
        int u=i;
        if(i!=startNode)
        {
            while(previous[u] != UNDEFINED)
            {
                push(u);
                u = previous[u];
            }

            printf("Path from %c to %c is: %c ",startNode + 65, i+65, startNode + 65);
            while(!isEmptyStack())
            {
                printf("-> %c ",peekStack()->content + 65);
                pop();
            }
            printf("\n");
        }
    }

    printf("Dijkstra's Algorithm ended\n");
}
Exemplo n.º 16
0
char* exp2post(char* exp)
{
	Stack* stack = createStack(tamanhoaux);
	char top, str;
	char* convertedExp = (char*) malloc(tamanhoaux*sizeof(char));
	int i=0, p=-1, j=0;
	while(exp[i]!='\0')
	{
		if (!peek(stack,&top))
			p=-1;
        	else
		        p=prioridade(top);
		str = exp[i];
		if(((str >= 'A')&&(str <= 'z')) || (((str>='0')&&(str<='9'))))
		{
			convertedExp[j] = str;
			j++;
		}
		else if(str=='(')
		{
			push(stack,str);
		}
		else if(str==')')
		{
			push(stack,str);
			top=')';
			while(top!='(')
			{
		                pop(stack,&top);
		                if(top!='('&&top!=')')
		                {
		                    convertedExp[j]=top;
		                    j++;
		                }
			}
		}
		else if (prioridade(str)<=p)
        	{
			pop(stack,&top);
			convertedExp[j]=top;
			j++;
			push(stack,str);
		}
		else if(prioridade(str)>p)
	        {
			push(stack,str);
		}
		i++;
	}
	while(!isEmptyStack(stack))
        {
    	    pop(stack,&top);
    	    convertedExp[j]=top;
		j++;
    	}
	convertedExp[j] = '\0';
	return convertedExp;
}
Exemplo n.º 17
0
element popStack (stack_t* p)
{
  if (isEmptyStack(p)) {
    printf("stack is empty, cant pop");
    exit(1);
  }

  return p->contents[p->top--];
}
Exemplo n.º 18
0
//clean stack memory
void cleanStack(struct stack *s)
{
  while(!isEmptyStack(s))
    {
      popStack(s);
    }
  free(s->stk);
  free(s);
}
Exemplo n.º 19
0
//! traversals
int dfs(int startNode,int stopNode)
{
    int * visited = (int*)malloc(nrOfVerteces * sizeof(int));
    int i, v;
    int path[100];
    int k=0;

    for(i=0; i < nrOfVerteces; i++)
    {
        visited[i]=UNVISITED;
    }

    push(startNode);

    while(!isEmptyStack())
    {
        v = peekStack()->content;
        pop();

        if(visited[v] == UNVISITED)
        {
            visited[v] = VISITED;
            int nrOfNeighbors = getNumberOfNeighborsOfVertex(v);
            int * neighbors = getAllNeighborsOfVertex(v);
            for(i=nrOfNeighbors-1; i>=0; i--)
            {
                int w = neighbors[i];
                if(visited[w] == UNVISITED)
                {
                    push(w);
                }
            }
            if(v!=stopNode)
            {
                //printf("%d ",v);
                path[k]=v;
                k++;
            }
            else
            {
                // printf("%d ",v);
                path[k]=v;
                for(i=k; i>0; i--)
                    if(adjMatrix[path[i]][path[i-1]]==0)
                        path[i-1]=path[i];
                for(i=0; i<=k; i++)
                    if(path[i]!=path[i+1])
                        printf("%d ",path[i]);

                return 0;
            }
        }

    }
    return 0;
}
Exemplo n.º 20
0
void print_stack(){//print the stack out
  if(isEmptyStack()){
    return;//get out if we are at the end
  }else{
    int temp = pop();//temp to hold value
    printf("%d\n", temp);//display val
    print_stack();//recur
    push(temp);//add value back when we get returned here
  }
}
Exemplo n.º 21
0
int Pop(stack *S)
{
    if(isEmptyStack(S))
    {
        printf("Stack is empty !!! Invalid deletion\n ");
        return 0;
    }

    else
        return S->array[S->top--];
}
Exemplo n.º 22
0
//print stack.  You have to print from the stack. You can not call a print function from dlinklist
void printStack(struct stack *s)
{
//	struct data* tDta = NULL;
	struct stack *tS = createStack();

	while(!isEmptyStack(s))
	{
		struct data* tDta = top(s);
		printData(tDta);
		pushStack(tS,createData(tDta->v1,tDta->v2));
		popStack(s);
	}

	while(!isEmptyStack(tS))
	{
		struct data* tDta = top(tS);
		pushStack(s,createData(tDta->v1,tDta->v2));
                popStack(tS);
	}
	cleanStack(tS);
}
Exemplo n.º 23
0
int GetTop(pSqStack s,pElem e)
{
	int ret;
	ret = isEmptyStack(s);
	if(YES==ret)
	{
		printf("栈空,无法取栈顶元素\n");
		return ER;
	}
	else{
			*e = (s->base)[s->top-1];
			return OK;
		}
}
void postOrderNonRecursive(struct binaryTreeNode *root) {
    struct Stack *s = createStack();
    struct binaryTreeNode *previous = NULL;

    do {
        while (root != NULL) {
            push(s, root);
            root = root->left;
        }

        while (root == NULL && !isEmptyStack(s)) {
            root = top(s);
            if (root->right == NULL || root->right == previous) {
                printf("%d " ,root->data);
                pop(s);
                previous = root;
                root = NULL;
            }
            else
                root = root->right;
        }
    } while (!isEmptyStack(s));
}
Exemplo n.º 25
0
int Pop(pSqStack s)
{
	int ret;
	ret = isEmptyStack(s);
	if(YES==ret)
	{
		printf("栈空,无法弹栈\n");
		return ER;
	}
	else{
			s->top--;
			return OK;
		}
}
void inOrderNonRecursive(struct binaryTreeNode *root) {
    struct Stack *s = createStack();
    while (1) {
        while (root) {
            push(s, root);
            // Got left subtree and keep on adding to stack
            root = root->left;
        }
        if (isEmptyStack(s))
            break;
        root = pop(s);
        printf("%d ", root->data);
        // Indicates completion of left subtree and current node, now go to right subtree
        root = root->right;
    }

    destroyStack(s);
}
Exemplo n.º 27
0
int main()
{
    StackT stack1;
    StackT *stackPtr1 = &stack1;
    initStack(stackPtr1);
    
    printf("Pushing 'A' 'B' 'C' 'D' onto stack1\n");
    pushStack(stackPtr1, 'A');
    pushStack(stackPtr1, 'B');
    pushStack(stackPtr1, 'C');
    pushStack(stackPtr1, 'D');
    
    printf("Popping and displaying all items from stack 1.\n");
    while(!isEmptyStack(stackPtr1))
        printf("%c ", popStack(stackPtr1));
    printf("\n\n");

    return 0;
}
Exemplo n.º 28
0
/**********************************
函数功能:二进制输出(栈实现)
参数1:十进制数
参数2(output):二进制字符
返回值:无
说明:进制转换的求余操作一直到商为0才停止
作者: Lee.C
完成时间:2015-05-01
修改时间:
修改说明:
**************************************/
void toBinary2(int dec, char *des)
{
	Item tempStackValue = 0, i = 0;
	SqStack S;
	InitStack(&S);

	while(dec)
	{
		tempStackValue = dec % 2;
		Push(&S, &tempStackValue);
		dec /= 2;
	}


	while(!isEmptyStack(&S))
	{
		Pop(&S, &tempStackValue);
		des[i++] = '0' + tempStackValue;
	}
}
// pre order using non recursive way
void preOrderNonRecursive(struct binaryTreeNode *root) {
    struct Stack *s = createStack();

    while (1) {
        while (root) {
            // Process current node
            printf("%d ", root->data);
            push(s, root);

            // If left subtree exists, add to stack
            root = root->left;
        }

        if (isEmptyStack(s))
            break;

        root = pop(s);
        // Indicate completion of left subtree and current node, now to to right subtree
        root = root->right;
    }

    destroyStack(s);
}
Exemplo n.º 30
0
int main() {
	Stack *s;
	int i, removed, returned, e=5;

	s = createStack();
	initializeStack(s);
	for (i=0; i<10; i++)
	{
		if(push(s, 5+i+1) == true)
		{
		printf("O valor %d foi inserido!\n", 5+i+1);
		}
		else
		{
		printf("O valor %d não pôde ser inserido.\n", 5+i+1);
		}
	}

	for (i=0; i<7; i++)
	{
		if (pop(s, &removed)==true)
		{
			printf("Topo removido com sucesso, o valor era: %d\n", removed);
		}
		else
		{
			printf("Não foi possível remover o topo\n");
		}
	}

	if (top(s, &returned) == true)
	{
		printf("O valor do topo é: %d\n", returned);
	}
	else
	{
		printf("Não há nada no topo.\n");
	}
	
	printStack(s);

	if (containsStack(s, &e) == false)
	{
		printf("O valor 5 não está presente na pilha.\n");
	}
	else
	{
		printf("O valor 5 está presente na pilha.\n");
	}

	e++;
	if (containsStack(s, &e) == false)
	{
		printf("O valor 6 não está presente na pilha.\n");
	}
	else
	{
		printf("O valor 6 está presente na pilha.\n");
	}

	printf("Tamanho da pilha: %d\n",sizeStack(s));

	if (isEmptyStack(s) == true)
	{
			printf("A pilha está vazia.\n");
	}
	else
	{
		printf("A pilha não está vazia.\n");
	}
return(0);	
}