Buscar

Exercício de Fila Dinâmica

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

#include <stdio.h> 
#include <stdlib.h>
#include <stdbool.h>
struct aluno{
	int matricula;
	char nome[50];
};
struct elemento{
	struct aluno dados;
	struct elemento *prox;
};
typedef struct elemento* Lista;
typedef struct elemento Elem;
Lista *li;
Lista* Lista_Construtor(){ //construtor da lista	
	Lista* li = (Lista*)malloc(sizeof(Lista));
	if(li != NULL){
		*li = NULL;
		return li;
	}else{
		printf("Erro ao construir a lista");
		return NULL;
	}
}
//li = Lista_Construtor
bool Insert_Final_Lista(Lista* li, struct aluno a1){ //insere elemento no fim da lista
	if(li == NULL)
	{
		return false;
	}
	Elem *no = (Elem*)malloc(sizeof(Elem));
	if(no == NULL)
	{
		return false;
	}
	no->dados = a1;
	no->prox = NULL;
	if((*li)==NULL)
	{
		*li = no;
	}else{
		Elem *aux = *li;
		while(aux->prox != NULL)
		{
			aux = aux->prox;
		}
		aux->prox = no;
		printf("Matricula: %d inserido\n", aux->dados.matricula);
		printf("Nome: %s inserido\n", aux->dados.nome);
		return true;
	}
}
bool Remove_Lista_Inicio(Lista* li){ //remove elemento do início da lista
	if(li== NULL)
	{
		return false;
	}
	if((*li)==NULL)
	{
		return false;
	}
	Elem *no = *li;
	*li = no->prox;
	printf("Matricula removida: %d", no->dados.matricula);
	printf("Nome removido: %s", no->dados.nome);
	free(no);
	return true;	
}
int main(){ //Executa a Lista para verificacao
 li = Lista_Construtor();
 struct aluno a1;
	int opcao, item, pos, valor;
 char c;
 	do
 {
 printf("\n <<LISTA CIRCULAR>> \n\n");
		printf("Escolha a operacao: \n 1 - Inserir elemento \n 2 - Excluir elemento \n");
 scanf("%d", &opcao);
 switch(opcao)
 {
 case 1:{ //Inserir no fim da lista
				Lista_Construtor();
				printf("\n Matricula: ");
 	scanf("%d", &a1.matricula); 
 printf("\n Nome: ");
 	scanf("%s", &a1.nome); 	
				Insert_Final_Lista(li, a1);
 }break;
 case 2:{//Excluir elemento no Inicio da Lista
 Remove_Lista_Inicio(li);
 }break;
			default:{
 printf("\n Valor invalido \n");
 }
 }
 printf("\n Continua: 's'-sim 'n'-nao \n");
 scanf("%s", &c);
 system("cls");
 }while(c=='s');
 system("pause");
 return 0;
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais