Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct aluno{
- int matricula;
- char nome[15];
- char curso[20];
- int idade;
- struct aluno *prox;
- }No;
- No*criar(){
- return NULL;
- }
- No *inserir(No *inicio)
- {
- No *aux;
- No *novo = (No *)malloc(sizeof(No));
- novo->prox = NULL;
- setbuf(stdin, NULL);
- printf("Forneca a matricula: ");
- scanf("%d", &novo->matricula);
- printf("Forneca o nome: ");
- scanf("%s", novo->nome);
- printf("Forneca o curso: ");
- scanf("%s", novo->curso);
- printf("Forneca a idade: ");
- scanf("%d", &novo->idade);
- if(inicio == NULL)
- {
- inicio = novo;
- inicio->prox = NULL;
- }else
- {
- aux = inicio;
- while(aux->prox != NULL)
- {
- aux = aux->prox;
- }
- aux->prox = novo;
- novo->prox = NULL;
- }
- return inicio;
- }
- void imprimir(No *inicio)
- {
- No *aux;
- aux = inicio;
- if(aux == NULL)
- {
- printf("Lista vazia maninho");
- }
- while(aux != NULL)
- {
- printf("Nome: %s", aux->nome);
- printf("\ncurso: %s", aux->curso);
- printf("\nIdade: %d", aux->idade);
- printf("\nmatricula: %d\n\n", aux->matricula);
- aux = aux->prox;
- }
- }
- No *buscar(No *inicio, int mat)
- {
- No *aux;
- aux = inicio;
- while(aux != NULL)
- {
- if(aux->matricula == mat)
- {
- printf("Elemento encontrado\n");
- printf("Nome: %s", aux->nome);
- printf("\ncurso: %s", aux->curso);
- printf("\nIdade: %d", aux->idade);
- printf("\nmatricula: %d\n\n", aux->matricula);
- return aux;
- }
- aux = aux->prox;
- }
- return NULL;
- }
- No *excluir(No *inicio, int mat)
- {
- No *aux, *aux_ant;
- aux = inicio;
- aux_ant = NULL;
- while(aux != NULL && aux->matricula != mat)
- {
- aux_ant = aux;
- aux = aux->prox;
- }
- if(aux == NULL)
- {
- printf("Matricula nao encontrada\n");
- return inicio;
- }
- if(aux_ant == NULL)
- {
- aux = aux->prox;
- return aux;
- }else
- {
- aux_ant->prox = aux->prox;
- }
- free(aux);
- return inicio;
- }
- void finalizar(No *inicio)
- {
- No *aux;
- aux = inicio;
- while(aux != NULL)
- {
- inicio = inicio->prox;
- free(aux);
- aux = inicio;
- }
- }
- int main()
- {
- No *inicio = criar();
- int y, x;
- x = 0;
- while(x != 3)
- {
- printf("Escolha a opcao desejada:\n");
- printf("\n1 - Inserir novo aluno");
- printf("\n2 - Imprimir lista de alunos");
- printf("\n3 - Buscar");
- printf("\n4 - Excluir");
- printf("\n5 - Sair\n\n");
- scanf("%d", &y);
- if(y == 1)
- {
- inicio = inserir(inicio);
- }
- if(y == 2)
- {
- imprimir(inicio);
- }
- if(y == 3)
- {
- int mat;
- printf("Digite o numero da matricula do Aluno: ");
- scanf("%d", &mat);
- buscar(inicio, mat);
- }
- if(y == 4)
- {
- int mat;
- printf("Digite o numero da matricula do Aluno: ");
- scanf("%d", &mat);
- excluir(inicio, mat);
- }
- if(y == 5)
- {
- x = 3;
- }
- }
- finalizar(inicio);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement