Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- #define MAX 5
- typedef struct
- {
- int chave;
- int valor;
- }REGISTRO;
- typedef struct
- {
- REGISTRO A[MAX];
- int nElementos;
- }LISTA;
- void inicializar(LISTA *l)
- {
- l->nElementos = 0;
- }
- REGISTRO set_Registro(int ch, int vl)
- {
- REGISTRO reg;
- reg.chave = ch;
- reg.valor = vl;
- return reg;
- }
- int size (LISTA *l)
- {
- return(l->nElementos);
- }
- void imprimir (LISTA *l)
- {
- int i;
- for(i= 0 ; i<size(l); i++)
- printf("\n %i -+- %i", l->A[i].chave , l->A[i].valor);
- }
- int buscar(LISTA *l, int chave)
- {
- int i;
- for(i=0; i<l->nElementos; i++)
- {
- if(chave == l->A[i].chave) return (i);
- }
- return (-1);
- }
- bool anexar(LISTA *l, REGISTRO r)
- {
- if(buscar(l,r.chave)!=(-1)) return false;
- if(l->nElementos == MAX) return false;
- l->A[l->nElementos]= r;
- l->nElementos++;
- return true;
- }
- bool inserir(LISTA *l, int i, REGISTRO r)
- {
- int j;
- if((l->nElementos >= MAX) || (i<0) || (i > (l->nElementos)))
- return false;
- for(j = l->nElementos -1; j >= i; j--)
- {
- l->A[j +1] = l->A[j];
- }
- l->A[i] = r;
- l->nElementos++;
- return true;
- }
- bool excluir(LISTA *l, int ch)
- {
- int pos = buscar(l, ch);
- if(pos == -1) return false; // chave já existente
- int i;
- for(i = pos ; i <(l->nElementos -1); i++ )
- {
- l->A[i] = l->A[i+1];
- }
- l->nElementos = l->nElementos -1;
- return true;
- }
- main()
- {
- LISTA Lista;
- inicializar(&Lista);
- anexar(&Lista,set_Registro(1,10));
- anexar(&Lista,set_Registro(2,20));
- anexar(&Lista,set_Registro(3,30));
- imprimir(&Lista);
- getch();
- inserir(&Lista,3, set_Registro(4,40));
- imprimir(&Lista);
- getch();
- excluir(&Lista, 4);
- imprimir(&Lista);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment