Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- QUESTÃO 1
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct No{
- int gamb;
- struct No* anterior;
- }No;
- No* criarPilha(){
- return NULL;
- }
- No* push(No* topo, int numero){
- No* novoNo = malloc(sizeof(No));
- novoNo->gamb = numero;
- if(topo == NULL){
- novoNo->anterior = NULL;
- topo = novoNo;
- return topo;
- }
- novoNo->anterior = topo;
- topo = novoNo;
- return topo;
- }
- No* pop(No* topo){
- if(topo == NULL){
- printf("\n Pilha vazia!");
- return NULL;
- }
- No* vai_ser_removido = topo;
- topo = topo->anterior;
- free(vai_ser_removido);
- return topo;
- }
- void buscar(No* topo, int itemBuscado){
- if(topo == NULL){
- printf("\nPilha vazia!\n");
- return;
- }
- No* aux = topo;
- while(aux != NULL){
- if(aux->gamb == itemBuscado){
- printf("\nNumero %d encontrado!\n", itemBuscado);
- return;
- }
- aux = aux->anterior;
- }
- printf("\nNumero nao encontrado! \n");
- return;
- }
- void imprimirPilha(No* topo){
- if(topo == NULL){
- printf("\nPilha vazia! \n");
- return;
- }
- No* aux = topo;
- while(aux != NULL){
- printf("\n Elemento: %d \n", aux->gamb);
- aux = aux->anterior;
- }
- return;
- }
- void primeiroUltimo(No* topo){
- if(topo == NULL){
- printf("\nLista vazia!\n");
- return;
- }
- No* aux = topo;
- printf("\nUltimo: %d \n", aux->gamb);
- while(aux->anterior != NULL){
- aux = aux->anterior;
- }
- printf("\nPrimeiro: %d \n", aux->gamb);
- return;
- }
- int main()
- {
- No* topo = criarPilha();
- int X, y, aux, i, n;
- x = 0;
- y = 1;
- printf("Digite um numero para definir o numero de termos: ");
- scanf("%d", &n);
- for(i = 0; i < n; i++)
- {
- aux = x + y;
- x = y;
- y = aux;
- topo = push(topo, aux);
- }
- imprimirPilha(topo);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement