quarta-feira, 24 de setembro de 2014

Pilha - Invertendo um Vetor de Inteiros

Em linguagem C temos várias estruturas de dados, e pilha é uma estrutura
de fácil uso, e que permite remover e inserir elementos.
A regra básica da pilha, nos diz que o primeiro elemento inserido será o último
a ser removido.
Neste código a pilha opera de uma maneira especial, más não fugindo as regras,
Um vetor de inteiro foi declarado, e são inseridos, ou melhor empilhados pela
função void Empi_lha ();, más é estático, apenas a estrutura da pilha foi alocado.
Na função void Desem_pilha  (); são desempilhados, suas posições são todas trocadas
num arranjo em posições sequênciais.

Veja abaixo duas imagens do programa em execução:



Veja abaixo o código do programa:


#include <stdio.h>
#include <conio.h>
void Janela5 ( ) {
     int lin, col;
     for ( lin = 0; lin <= 36 ; lin++ ) {
         for ( col = 0; col <= 80 ; col++ ) {
              gotoxy ( col , lin );
              if ( lin == 2 ) {
                   textbackground ( LIGHTRED );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( LIGHTRED );
                   printf ( " " );
              }
              if ( lin == 36 ) {
                   textbackground ( LIGHTRED );
              }
              if ( col == 80 ) {
                   textbackground ( LIGHTRED );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
void inicio ( ) {
     system ( "title PILHA - INVERTENDO UM VETOR DE INTEIROS" );
     int i;
     do {
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 25 , 7 );
         printf ( "PILHA - INVERTENDO UM VETOR DE INTEIROS" );
         textcolor ( YELLOW );
         gotoxy ( 25 , 10 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTCYAN );
         gotoxy ( 52 , 10 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34 , 12 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 24 , 14 );
         printf ( "DIGITE    PARA SAIR OU   PARA CONTINUAR  " );
         textcolor ( LIGHTRED );
         gotoxy ( 32 , 14 );
         printf ( "1" );
         textcolor ( LIGHTRED );
         gotoxy ( 47 , 14 );
         printf ( "2" );
         gotoxy ( 41 , 16 );
         scanf ( "%d" , &i );
         fflush ( stdin );
         if ( i == 1 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 35 , 20 );
              printf ( "MUITO OBRIGADO" );
              Sleep ( 1800 );
              exit ( 0 );
         }
         if ( i == 2 ) {
              system ( "cls" );
              return;
         } else {
              textcolor ( LIGHTRED );
              gotoxy ( 37 , 20 );
              printf ( "\aOPÇÃO ERRADA" );
              Sleep ( 1800 );
              system ( "cls" );
         }
     } while ( i );
}
typedef struct Pilha {
     int vol;
     struct Pilha *prox;
} Pil_ha;
void Empi_lha ( int c , Pil_ha *to_po ) {
     Pilha *item = ( Pilha * ) malloc ( sizeof(Pilha) );
     item->vol = c;
     item->prox = to_po->prox;
     to_po->prox = item;
}
int Desem_pilha ( Pil_ha *to_po ) {
     int c;
     Pil_ha *pt;
     pt = to_po->prox;
     c = pt->vol;
     to_po->prox = pt->prox;
     free ( pt );
     return c;
}
int main ( ) {
 
int vet [ 100 ] = {
                  1,  2,  3,  4,  5,  6,  7,  8,  9, 10 ,
                 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ,
                 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ,
                 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 ,
                 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 ,
                 51, 52, 53, 54, 55, 56, 57, 58, 59, 60 ,
                 61, 62, 63, 64, 65, 66, 67, 68, 69, 70 ,
                 71, 72, 73, 74, 75, 76, 77, 78, 79, 80 ,
                 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ,
                 91, 92, 93, 94, 95, 96, 97, 98, 99,100};

     int i;
     Pil_ha cab;
     Pil_ha *top;
     top = &cab;
     top->prox = NULL;
     inicio ( );
     textcolor ( LIGHTRED );
     gotoxy ( 25 , 3 );
     printf ( "PILHA - INVERTENDO UM VETOR DE INTEIROS" );
     textcolor ( LIGHTBLUE );
     gotoxy ( 25 , 5 );
     printf ( "Veja abaixo a impressão normal do vetor " );
     textcolor ( LIGHTGRAY );
     gotoxy ( 2 , 7 );
     for ( i = 0; i < 100 ; i++ )
         printf ( " %d " , vet [ i ] );
     getche ( );
     for ( i = 0; i < 100 ; i++ )
         Empi_lha ( vet [ i ] , top );
     textcolor ( LIGHTBLUE );
     gotoxy ( 25 , 13 );
     printf ( "Veja abaixo o vetor totalmente invertido " );
     textcolor ( LIGHTGRAY );
     gotoxy ( 3 , 15 );
     while ( top->prox != NULL )
         printf ( " %d " , Desem_pilha ( top ) );
     Sleep ( 1800 );
     textcolor ( LIGHTRED );
     gotoxy ( 36 , 23 );
     printf ( "MUITO OBRIGADO" );
     getche ( );
     exit ( 0 );
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.