Progressively Removing from the List

Asked

Viewed 29 times

0

#include <stdio.h>
#include <stdlib.h>
typedef struct ListElmt_{
 int data;
 struct ListElmt_ *next;
} ListElmt;
typedef struct List_ {
 int size;
 ListElmt *head;
 ListElmt *tail;
} List;
List *list_init(){
    List *list = (List *) malloc(sizeof(List));
 list->size = 0;
 list->head = NULL;
 list->tail = NULL;
 return list;
}
void insert(List *list,int num){
    ListElmt *node = (ListElmt *) malloc(sizeof(ListElmt));
    if(list->size == 0){
        node->data = num;
        node->next = NULL;
        list->head =  node;
        list->tail = node;
    }
    else{
        node->data = num;
        node->next = NULL;
        list->tail->next = node;
        list->tail = node;
    }
    (list->size)++;
}
int  *pop(List *list){
    if(list == NULL){
        printf("Lista vazia.\n");
        return 0;
    }
    ListElmt *node = list->head;
    list->head = node->next;
    (list->size)--;
    return node->data;
    free(node);
}
ListElmt *atpos(List *list, int pos){
    if(pos>=0 && pos<=list->size){
        ListElmt *node = list->head;
        int i;
        for(i=0;i<pos;i++)
            node = node->next;

        return node;
    }
    //printf("Posicao invalida.\n");
}
int *removeatpos(List *list, int pos){
    ListElmt *current = atpos(list,pos);
    if(current!=NULL){
        ListElmt *previous = atpos(list,pos-1);
        previous->next = current->next;
        return current->data;
        free(current);
        (list->size)--;
    }
}
void progress(List *list, int i, int j){
    if(list == NULL)
    {
        printf("Lista vazia.\n");
        return;
    }
    int aux=0;
    ListElmt *node = atpos(list,i-1);
    ListElmt *previous = atpos(list,j);
            while(node != previous){
                node = node->next;
                free(node->next);
            }

}
void printList(List *list){
    if(list == NULL)
    {
        printf("Lista vazia.\n");
        return;
    }
    ListElmt *node = list->head;
    while(node != NULL){
        printf("%d ",node->data);
        node = node->next;
    }
}
int main(){
    List *list = list_init();
    insert(list,10);
    insert(list,20);
    insert(list,30);
    insert(list,40);
    insert(list,50);
    insert(list,60);
    insert(list,70);
//int num = pop(list);
int   pos = 3,aux;
aux = pos-1;
int i=2,j=5;


   //int num =  removeatpos(list,pos-1);
   progress(list,i-1,j-1);

    printList(list);

return 0;
}

I’m having trouble with my job progress, she would have to withdraw the numbers 20 30 40 50, but she’s not doing it this way.

1 answer

0

You need a helper to do the removal otherwise the list breaks

void progress(List *list, int i, int j){
    if(list == NULL)
    {
        printf("Lista vazia.\n");
        return;
    }
    ListElmt *node = atpos(list,i-1);
    ListElmt *previous = atpos(list,j);
        while(1){
            ListElmt * aux = node->next; // cria o auxiliar
            node->next = aux->next; // remove o auxiliar da lista
            printf("removido: %d \n", aux->data);
            if(aux == previous){ // se for o ultimo a ser removido
                free(aux); //libera o auxiliar
                return; //termina a funcao
            }//caso não seja o ultimo
            free(aux); //apenas libera o auxiliar
        }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.