Lista Encadeada em C (Matrícula e nome)
Autor: Luciana Sondermann | Publicado: 2011-05-13T18:07:00-03:00

#include &lt;stdio.h&gt;
#include&lt;stdlib.h&gt;
struct registro
{
&nbsp;&nbsp;&nbsp; long int matricula;
&nbsp;&nbsp;&nbsp; char nome[81];
&nbsp;&nbsp;&nbsp; struct registro *next;
};
struct registro *raiz;
void adiciona()
{
&nbsp;&nbsp;&nbsp; struct registro *novoreg=(struct registro*)malloc(sizeof(struct registro));
&nbsp;&nbsp;&nbsp; if(novoreg==NULL){printf("Mem%cria insuficiente!",162); exit(1);}
&nbsp;&nbsp;&nbsp; printf("\nInforme a matr%ccula do funcion%crio: ",161,160);
&nbsp;&nbsp;&nbsp; scanf("%d",&amp;novoreg-&gt;matricula);
&nbsp;&nbsp;&nbsp; getchar();
&nbsp;&nbsp;&nbsp; printf("Informe o nome: ");
&nbsp;&nbsp;&nbsp; gets(novoreg-&gt;nome);
&nbsp;&nbsp;&nbsp; novoreg-&gt;next=raiz-&gt;next;
&nbsp;&nbsp;&nbsp; raiz-&gt;next=novoreg;
}
void exclui()
{
&nbsp;&nbsp;&nbsp; struct registro *reg_atual, *reg_ant;
&nbsp;&nbsp;&nbsp; long int matric;
&nbsp;&nbsp;&nbsp; reg_atual=raiz-&gt;next;
&nbsp;&nbsp;&nbsp; reg_ant = raiz;&nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; printf("\nInforme a matr%ccula do funcion%crio que ser%c exclu%cdo: ",161,160,160,161);
&nbsp;&nbsp;&nbsp; scanf("%d",&amp;matric);
&nbsp;&nbsp;&nbsp; while(reg_atual!=NULL)
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(matric==reg_atual-&gt;matricula)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; reg_ant-&gt;next=reg_atual-&gt;next;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("\nMatr%ccula exclu%cda com sucesso!",161,161);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; free(reg_atual);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; reg_ant = reg_atual;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reg_atual=reg_atual-&gt;next;
&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp; printf("Matr%ccula n%co encontrada!\n",161,198);
}
void imp_lista()
{
&nbsp;&nbsp;&nbsp; struct registro *lista;
&nbsp;&nbsp;&nbsp; lista=raiz-&gt;next;
&nbsp;&nbsp;&nbsp; if(lista==NULL)
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf ("\n\n\a\a\a\a\tLista Vazia\n");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; system ("pause");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; while(lista!=NULL)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("\nMatr%ccula: %d",161,lista-&gt;matricula);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("\nFuncion%crio: %s",160,lista-&gt;nome);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; lista=lista-&gt;next;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }
}

int main ()
{
&nbsp;&nbsp;&nbsp; int op=3;
&nbsp;&nbsp;&nbsp; raiz=(struct registro *)malloc(sizeof (struct registro));
&nbsp;&nbsp;&nbsp; if(raiz==NULL) exit(1);
&nbsp;&nbsp;&nbsp; raiz-&gt;next=NULL;
&nbsp;&nbsp;&nbsp; do
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("Digite 1 para adicionar um novo registro, 2 para excluir, 3 para listar e 4 para sair: ");
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; scanf("%d",&amp;op);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if(op==1)adiciona();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if(op==2)exclui();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if(op==3)imp_lista();
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; else if(op==4)exit(1);
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; printf("\n");
&nbsp;&nbsp;&nbsp; }while(op!=4);
&nbsp;&nbsp;&nbsp; free(raiz);
&nbsp;&nbsp;&nbsp; return 0;
}