Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

dudosurf

Lista em C

Recommended Posts

O código abaixo apresenta erro na hr da impressão ou inserção, alguem poderia me ajudar a corrigir o erro ?

abraços

 

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

struct Tpno {
	   int cod;
	   struct Tpno *prox;
};

/* 1 -------------INSERE NO INICIO DA LISTA---------------*/
void insere(struct Tpno **inicio, struct Tpno **fim, int x)
{
struct Tpno *aux;
int pesq, cod;

 if ((*inicio)==NULL){
		(*inicio)=(struct Tpno*)malloc(sizeof(struct Tpno));
		(*fim) = (struct Tpno*)malloc(sizeof(struct Tpno));
		(*inicio)->cod=x;
		(*inicio)->prox=NULL;
		(*fim)= (*inicio);
		}
 else {
		struct Tpno *aux;
		aux=(struct Tpno*)malloc(sizeof(struct Tpno));
		(*fim) = (struct Tpno*)malloc(sizeof(struct Tpno));
		aux->cod=x;
		aux->prox=(*inicio);
		(*inicio)=aux;
		while ( aux -> prox != NULL)
			  aux = aux -> prox;
			  (*fim)-> prox = aux;
		}
}

/* 3 -------------MAIOR CÓDIGO---------------*/

void maior_codigo(struct Tpno *inicio){
	 struct Tpno *aux;
	 int maior;
	 puts("Maior Código\n\n");
			//maior = -1;
			aux=inicio;
					 while (aux != NULL) {
					 if (aux->cod > maior) 
					 maior = aux->cod;
					 aux = aux->prox;
  }
}

/* 4 -------------IMPRIME A LISTA---------------*/

void imprimir_lista(struct Tpno **inicio)
 {
  struct Tpno *aux;
  aux = *inicio;
  puts("Impressao da lista\n\n");
	  if (aux == NULL) puts ("Lista Vazia!");
	  else{
		if (aux->prox == NULL) /*Lista com um elemento apenas*/
		printf("%i\n", aux->cod);
		  else { /*Imprime lista*/
			while (aux->prox != NULL){
				printf("%i ->", aux->cod);
				aux = aux->prox;
			 }
			  printf("%i\n", aux->cod);
			 }
		  }
			 printf("\n");
 }

int menu()
{
	int opcao;
	puts (	"+----------------------------+");
	puts (	"|							|");
	puts (	"| [1] - Insere elemento	  |");
	puts (	"| [2] - Pesquisar elemento   |");
	puts (	"| [3] - Obter o maior Codigo |");
	puts (	"| [4] - Imprimir lista	   |");
	puts (	"| [5] - Sair do Programa	 |");
	puts (	"|							|");
	puts (	"+----------------------------+");
	printf("\nDigite sua opcao: ");
	scanf ("%d",&opcao);
	return (opcao);
}
	


int main()
{
	struct Tpno *inicio, *fim;
	int i, opcao, num, maior;
	inicio = fim = NULL;
	
	do{
	opcao = menu();
	switch (opcao){
		   
	case 1:
	printf("Digite um Codigo para Inserir:\n");	 
	scanf("%i", &num);
	insere(&inicio, &fim, &num);
	break;
	
	case 2:
	// pesquisar_codigo(&inicio, &fim);
	break;
	
	case 3:
	printf("%d", maior);
	maior_codigo(&inicio);
	break;
	
	case 4:
	imprimir_lista(&inicio); 
	break;
	
	case 5:
	exit(0);
	default:
	printf("\n\n Opcao invalida!\n");
}
}   while( opcao != 5);
  // system("PAUSE");
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

 

typedef struct tipo {

int num;

struct tipo *prox;

} meuTipo;

 

/**

* Testa se um elemento existe

* retorna 1 caso exista

* retorna 0 caso não exista

*/

int elementoExiste(meuTipo **inicio, int num) {

int existe = 0;

meuTipo *aux;

aux = *inicio;

 

while (aux != NULL) {

if (num == aux->num) {

existe = 1;

break;

}

aux = aux->prox;

}

return existe;

}

 

/**

* Retorna o valor do maior numero da lista

* retorna 1 caso exista

* retorna 0 caso não exista

*/

int maiorNumero(meuTipo **inicio) {

meuTipo *aux;

aux = *inicio;

 

int num = (*inicio)->num;

 

while (aux != NULL) {

if (aux->num > num) {

num = aux->num;

}

aux = aux->prox;

}

return num;

}

 

/**

* pesquisa na lista um codigo digitado

* retorna 1 caso exista

* retorna 0 caso não exista

*/

int pesquisarCodigo(meuTipo **inicio, int num) {

meuTipo *aux;

aux = *inicio;

 

int existe = 0;

 

while (aux != NULL) {

if (num == aux->num) {

existe = 1;

break;

}

aux = aux->prox;

}

return existe;

}

 

 

int inserir(int num, meuTipo **inicio) {

meuTipo *aux;

 

aux = (meuTipo *) malloc(sizeof(meuTipo));

aux->num = num;

aux->prox = NULL;

 

if (*inicio == NULL) {

*inicio = aux;

return 1;

} else {

meuTipo *aux2;

aux2 = *inicio;

 

while (aux2->prox != NULL) {

aux2 = aux2->prox;

}

 

aux2->prox = aux;

aux2 = aux;

 

return 1;

}

}

 

/**

* Exibe os valores da lista

*/

void exibirLista(meuTipo **inicio) {

meuTipo *aux2;

aux2 = *inicio;

puts("+= EXIBE A LISTA DE DADOS =+");

 

while (aux2 != NULL) {

printf("Num: %i \n", aux2->num);

aux2 = aux2->prox;

}

}

 

int excluir(meuTipo **inicio, int num) {

 

meuTipo *aux;

meuTipo *aux2;

 

aux = *inicio;

aux2 = *inicio;

 

if (aux==NULL) {

 

puts("..:: LISTA VAZIA ::..");

 

} else {

 

//caso a lista so tenha um elemento

if (aux->prox == NULL) {

 

if (aux->num == num) {

free(aux);

*inicio = NULL;

return 1;

}

 

// caso a lista tenha mais de um elemento e o elemento para ser excluido é o primeiro

} else {

 

while (aux != NULL) {

 

if (aux->num == num) {

 

//excluir o primeiro elemento quando temos mais de um elemento

if (aux == *inicio) {

*inicio = (*inicio)->prox;

free(aux);

} else {

 

//exclui o ultimo elemento

if (aux->prox == NULL) {

 

aux2->prox = NULL;

free(aux);

 

} else {

//exlcui um elemento no meio da lista

aux2->prox = aux->prox;

free(aux);

}

 

}

break;

return 1;

}

 

aux2 = aux;

aux = aux->prox;

 

}

 

}

}

 

return 1;

 

}

 

int main() {

int escolha;

int auxNum;

 

meuTipo *inicio= NULL;

 

for (;;) {

puts("1 - Inserir codigo na lista");

puts("2 - Pesquisar codigo ");

puts("3 - Maior numero");

puts("4 - Imprimir Lista");

puts("5 - Excluir elemento");

puts("0 - Sair");

 

scanf("%i", &escolha);

 

switch (escolha) {

case 1:

printf("Entre com o numero: ");

scanf("%i", &auxNum);

 

if (elementoExiste(&inicio, auxNum) == 1) {

puts("..:: ELEMENTO JA CADASTRADO ::..");

} else {

inserir(auxNum, &inicio);

}

 

break;

 

case 2:

printf("Ente com o codigo para ser pesquisado: ");

scanf("%i", &auxNum);

 

if (pesquisarCodigo(&inicio, auxNum) == 1) {

printf("O numero %i existe na lista \n ", auxNum);

} else {

printf("O numero %i naum existe na lista \n \a", auxNum);

}

 

break;

 

case 3:

printf("O maior numero eh: %i \n", maiorNumero(&inicio) );

break;

 

case 4:

exibirLista(&inicio);

break;

 

case 5:

printf("Ente com o codigo para ser excluido: ");

scanf("%i", &auxNum);

 

if (pesquisarCodigo(&inicio, auxNum) == 1) {

excluir(&inicio, auxNum);

} else {

printf("O numero %i naum existe na lista \n \a", auxNum);

}

 

 

break;

 

case 0:

return 0;

break;

 

default:

break;

}

system("pause");

system("cls");

}

 

return 0;

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.