Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct elemento{
- int d;
- struct elemento *p;
- } elemento;
- int listarElementos(elemento *e);
- int inserirElementoInicio(elemento **inicio, int d);
- int inserirElementoFinal(elemento **inicio, int d);
- int inserirElementoIndice(elemento **inicio, int d, int idx);
- int main(){
- elemento *inicio = NULL;
- /*printf("%d\n",listarElementos(inicio));
- printf("%d\n",inserirElementoInicio(&inicio, 5));
- printf("%d\n",inserirElementoInicio(&inicio, 3));
- printf("%d\n",inserirElementoInicio(&inicio, 7));
- printf("%d\n",inserirElementoFinal(&inicio, 8));
- printf("%d\n",listarElementos(inicio));*/
- inserirElementoInicio(&inicio, 4);
- listarElementos(inicio);
- printf("\n");
- printf("\n");
- printf("\n");
- inserirElementoInicio(&inicio, 2);
- listarElementos(inicio);
- printf("\n");
- printf("\n");
- printf("\n");
- inserirElementoFinal(&inicio, 5);
- listarElementos(inicio);
- printf("\n");
- printf("\n");
- printf("\n");
- inserirElementoFinal(&inicio, 3);
- listarElementos(inicio);
- printf("\n");
- printf("\n");
- printf("\n");
- inserirElementoIndice(&inicio, 99, 2);
- listarElementos(inicio);
- printf("\n");
- printf("\n");
- printf("\n");
- removerElementoIndice(0, &inicio);
- listarElementos(inicio);
- printf("\n");
- printf("\n");
- printf("\n");
- return 0;
- }
- int listarElementos(elemento *e){
- if (e==NULL)
- return 1; //return 1: lista vazia
- while(e!=NULL){
- printf("%p -> %d | %p\n",e, e->d, e->p);
- e = e->p;
- }
- return 0; //return 0: operação realizada com sucesso
- }
- int inserirElementoInicio(elemento **i, int d){
- elemento *novo = malloc(sizeof(elemento));
- if (novo == NULL)
- return -1; // return -1: não foi possivel alocar memória
- novo->d = d;
- novo->p = *i;
- *i = novo;
- return 0; //return 0: operação realizada com sucesso
- }
- int inserirElementoFinal(elemento **i, int d){
- if (*i == NULL)
- return inserirElementoInicio(i, d);
- elemento *novo = malloc(sizeof(elemento));
- novo->d = d;
- novo->p = NULL;
- if (novo == NULL)
- return -1; //return -1: não foi possivel alocar memória
- elemento *aux = *i;
- while (aux->p != NULL){
- aux = aux->p;
- }
- aux->p = novo;
- return 0; //return 0: operação realizada com sucesso
- }
- int inserirElementoIndice(elemento **i, int d, int idx){
- if (idx < 0)
- return -2; // return -2: indice invalido
- else if (idx == 0)
- return inserirElementoInicio(i, d);
- else if (*i == NULL)
- return -3; // return -3: indice não existe
- else{
- elemento *aux = *i;
- int idxAux = 0;
- while (aux != NULL && idxAux < idx -1){
- aux = aux->p;
- idxAux++;
- }
- if (idxAux < idx -1)
- return -3; // return -3: indice não existe
- elemento *novo = malloc(sizeof(elemento));
- if (novo == NULL)
- return -1; // return -1: não foi possivel alocar memória
- novo->d = d;
- novo->p = aux->p;
- aux->p = novo;
- return 0;
- }
- }
- int removerElementoIndice (int i, elemento **r ){
- if (i < 0){
- return -1;
- }
- if (i == 0){
- if (*r == NULL){
- return -2;
- }
- elemento *aux = *r;
- *r = aux -> p;
- free (aux);
- }else{
- if (*r == NULL){
- return -2;
- }
- elemento *aux = *r;
- int rAux = 0;
- while ((aux -> p != NULL) && rAux < r - 1){
- aux = aux -> p;
- rAux++;
- }
- if (aux -> p != NULL){
- elemento *aux2 = aux -> p;
- aux -> p = aux2 -> p;
- free(aux2);
- }else{
- return -2;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement