true c0d3

04/12/2009

Transformar uma lista em um vetor

Filed under: C/C++, Programação — Etiquetas:, , — ceb10n @ 3:49 pm

Irei postando ao longo do tempo, alguns exercícios da faculdade que fiz recentemente.

Transforme uma lista simplesmente encadeada em um vetor. Descubra antes o tamanho da lista para que o vetor possa ser convenientemente alocado.
protótipo: void criaVetor(Lista *L, int *vetor)

/*=============================================================================*
* includes                                                                     *
*=============================================================================*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/*=============================================================================*
* typedef                                                                      *
*=============================================================================*/

struct lista {
       int iNum;
       struct lista *pLista;
};

typedef lista Lista;

/*=============================================================================*
* declaracoes                                                                  *
*==============================================================================*
/*-----------------------------------------------------------------------------*
* declaracoes - prototipos de funcoes                                          *
*-----------------------------------------------------------------------------*/

Lista* insereLista(Lista *pLista, int iNum);
int getTamanhoLista(Lista *pLista);
void criaVetor(Lista *pLista, int *pVet);

/*-----------------------------------------------------------------------------*
* funcao "main"                                                                *
*-----------------------------------------------------------------------------*/

int main(int argc, char *argv[]) {    
    Lista *pLista = NULL;
    int *pVet = NULL;
    
    for (int i = 0; i < 10; i++) {
        int iNum = (int) rand() % 10;
        pLista = insereLista(pLista, iNum);
    }
    
    criaVetor(pLista, pVet);
    
    system("pause");    
}

/*-----------------------------------------------------------------------------*
* corpo das funções - insereLista                                              *
*-----------------------------------------------------------------------------*/

Lista* insereLista(Lista *pLista, int iNum) {
       Lista *pAux = NULL;
       pAux = (Lista *) malloc(sizeof(Lista));
       pAux->iNum = iNum;
       pAux->pLista = pLista;
       return pAux;
}

/*-----------------------------------------------------------------------------*
* corpo das funções - getTamanhoLista                                          *
*-----------------------------------------------------------------------------*/

int getTamanhoLista(Lista *pLista) {
    int iTam = 0;
    Lista *pAux = pLista;
    while (pAux != NULL) {
          iTam++;
          pAux = pAux->pLista;
    }
    return iTam;
}

/*-----------------------------------------------------------------------------*
* corpo das funções - criaVetor                                                *
*-----------------------------------------------------------------------------*/

void criaVetor(Lista *pLista, int *pVet) {
     int iTamLista = 0;
     int i = 0;
     Lista *pAux = pLista;
     
     iTamLista = getTamanhoLista(pLista);
     pVet = (int *) malloc(sizeof(int) * iTamLista);
     
     
     while (pAux != NULL) {
           pVet[i] = pAux->iNum;
           printf("\nPosicao vetor %d : %d ", i, pVet[i]);
           pAux = pAux->pLista;
           i++;
     }
     printf("\n\n");
}

Deixe um Comentário »

Ainda sem comentários.

RSS feed para os comentários a este artigo. TrackBack URI

Deixar um comentário

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Modificar )

Imagem do Twitter

You are commenting using your Twitter account. Log Out / Modificar )

Facebook photo

You are commenting using your Facebook account. Log Out / Modificar )

Connecting to %s

Tema: Shocking Blue Green. Blog em WordPress.com.

Seguir

Get every new post delivered to your Inbox.