Irei postando ao longo do tempo, alguns exercícios da faculdade que fiz recentemente.
Leia um arquivo tipo texto e o transforme em uma lista simplesmente encadeada. A função deverá receber um ponteiro para o arquivo já aberto e um ponteiro para uma lista inicializada com NULL.
/*=============================================================================*
* includes *
*=============================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*=============================================================================*
* define *
*=============================================================================*/
#define PATH_LEN 25
#define BUFF_LEN 255
/*=============================================================================*
* typedef *
*=============================================================================*/
struct lista {
int iNum;
struct lista *pLista;
};
typedef lista Lista;
/*=============================================================================*
* declaracoes *
*==============================================================================*
/*-----------------------------------------------------------------------------*
* declaracoes - prototipos de funcoes *
*-----------------------------------------------------------------------------*/
FILE* abreArquivo(char *);
Lista* insereLista(Lista *, int);
void exibeLista(Lista *);
/*-----------------------------------------------------------------------------*
* funcao "main" *
*-----------------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
FILE *pArq = NULL;
Lista *pLista = NULL;
char *pPath = NULL;
char *buff = NULL;
buff = (char *) malloc(BUFF_LEN * sizeof(char));
pPath = (char *) malloc(PATH_LEN * sizeof(char));
printf("Digite o caminho do arquivo: ");
scanf("%s", pPath);
pArq = abreArquivo(pPath);
if (pArq) {
do {
fgets(buff, BUFF_LEN, pArq);
pLista = insereLista(pLista, atoi(buff));
}
while (!feof(pArq));
printf("\n*** Lista ***\n\n");
exibeLista(pLista);
}
else {
printf("\nError");
}
system("pause");
return 0;
}
/*-----------------------------------------------------------------------------*
* corpo das funções - abreArquivo *
*-----------------------------------------------------------------------------*/
FILE* abreArquivo(char *pPath) {
FILE* pArq = NULL;
pArq = fopen(pPath, "r");
if (pArq)
return pArq;
else
return NULL;
}
/*-----------------------------------------------------------------------------*
* 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 - exibeLista *
*-----------------------------------------------------------------------------*/
void exibeLista(Lista *pLista) {
Lista *pAux = NULL;
pAux = pLista;
while (pAux != NULL) {
printf("\n %d", pAux->iNum);
pAux = pAux->pLista;
}
return;
}