Last active
November 5, 2015 00:51
-
-
Save waltton/133bf51978f716b1073e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*PUC-Minas | |
Programação de Computadores II - teoria | |
Tipo abstrato de dados - pilha | |
Objetivo: Empilhar números inteiros gerados aleatoriamente no intervalo [1 a 100] em uma pilha de | |
tamanho N informado pelo usuário, encadeada com ponteiro. Mostrar a pilha antes e depois de remover | |
um elemento. | |
Alunos: | |
535965 - Camila Rezende Lopes | |
524774 - Matheus Perdigao Campos | |
535966 - Rúbia Evelyn | |
524790 - Vitor Alexandre Diniz Souza | |
524759 - Waltton Santana de Carvalho Morais | |
*/ | |
//bibliotecas | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <conio.h> | |
#include <time.h> | |
//definicao da estrutura da pilha | |
struct Pilha | |
{ | |
int numero; | |
struct Pilha *prox; | |
} Pilha; | |
//funções | |
struct Pilha *pilha_criar() | |
{ | |
return NULL; | |
} | |
struct Pilha* pilha_retirar(struct Pilha *L) | |
{ | |
struct Pilha *prox; | |
if(L != NULL) | |
{ | |
if(L->prox == NULL) | |
{ | |
free(L); | |
return NULL; | |
} | |
else | |
{ | |
prox = L->prox; | |
free(L); | |
return prox; | |
} | |
} | |
else | |
{ | |
printf("A pilha já está vazia"); | |
} | |
} | |
struct Pilha *pilha_empilhar (struct Pilha *L) | |
{ | |
struct Pilha *novo, *pilha; | |
int qte, i; | |
do | |
{ | |
printf("Informe a quantidade de numeros aleatorios devem ser gerados: "); | |
scanf("%d", &qte); | |
} | |
while(qte < 0); | |
pilha = L; | |
srand(time(NULL)); | |
for(i = 0; i < qte; i++) | |
{ | |
novo = malloc(sizeof(Pilha)); | |
if(novo == NULL){ | |
printf("Nao foi possivel alocar memoria para o novo item.\n"); | |
return L; | |
} | |
novo->numero = (rand() % 100) + 1; | |
novo->prox = pilha; | |
pilha = novo; | |
} | |
return novo; | |
} | |
void pilha_mostrar(struct Pilha *L) | |
{ | |
struct Pilha *aux; | |
aux = L; | |
while(aux != NULL) | |
{ | |
printf("%d\t", aux->numero); | |
aux = aux->prox; | |
} | |
} | |
main() | |
{ | |
struct Pilha *Topo, *p; | |
Topo=pilha_criar( ); | |
Topo=pilha_empilhar(Topo); | |
printf("\nPilha antes de retirar um elemento. Tecle algo...\n"); | |
getch(); | |
pilha_mostrar (Topo); | |
Topo=pilha_retirar (Topo); | |
p=Topo; | |
printf("\nPilha depois de retirar um elemento. Tecle algo...\n"); | |
getch(); | |
pilha_mostrar(p); | |
printf("\nTecle algo para encerrar...\n"); | |
getch(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment