Funcion borrar elemento de una lista STRUCT

Iniciado por galapok11, 17 Agosto 2016, 14:09 PM

0 Miembros y 1 Visitante están viendo este tema.

galapok11

Este funcion deberia borrar un elemento de una lista STRUCT pero no llega a funcionar

void drop_user()
{
    struct dsd_list_user *dsl_current_user;
struct dsd_list_user *dsl_temp_user;

dsl_temp_user=head;
   
    //Variable to indicate the number of user
    //int i = 0;
    int number_user = 0;

    //Assign that pointer to the first user
    dsl_current_user = dss_first;

    //Check if there are users in the list
    if (dsl_current_user==NULL)
    {
        //If there are not users, show the user is the first
        printf("\nThere are no users to drop\n\n");
        return;
    }

    //If there are just one user
    if (dsl_current_user->adsc_next == NULL)
    {
        printf("Only one user to drop it.\n");
printf("Indicate the First Name of the user which you want drop it:");
//gets(dsl_current_user->chrc_firstname);
free(dsl_current_user);
//dsl_current_user = NULL;
return;
//dsl_current_user = dsl_current_user->adsc_next;
    }
while (dsl_current_user != NULL)
{
printf("Indicate the First Name of the user which you want drop it:");
gets(dsl_current_user->chrc_firstname);
free(dsl_current_user);
dsl_current_user = dsl_current_user->adsc_next;
}
}


AlbertoBSD

Esa es una lista ligada... no se si las has manejado antes, pero es mas recomendable y si no te quieres complicar tanto que mejor lo manejes como arreglos estaticos.

Tienes el resto del codigo de dlnde copiaste eso?

Saludos
Donaciones
1Coffee1jV4gB5gaXfHgSHDz9xx9QSECVW

galapok11

Si he usado un par de llos estaticos.
Es la primera vez que hago un programo con listas enlazables... Me falla la funcion de borrar, pero cuando solo existe un usuario, si me que lo borrar, ahora me gustaria borrarlos cuando hay varios (lo de borrar segun el nombre, no me ha llegado a funcionar pero primero que me funcione lo anterior...)

Muchas gracias por tu respuesta!!  ;D ;D
Aquie te dejo el codigo del programa entero

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*+-------------------------------------------------------------------+*/
/*| Defines                                                           |*/
/*+-------------------------------------------------------------------+*/

/*+-------------------------------------------------------------------+*/
/*| Structs & Definitions                                             |*/
/*+-------------------------------------------------------------------+*/

//Define the structure list_user
struct dsd_list_user
{
    char chrc_firstname[20];
    char chrc_lastname[20];
    char chrc_address[50];
    char chrc_age[2];
    dsd_list_user *adsc_next;
};

/*+-------------------------------------------------------------------+*/
/*| Global Variables                                                  |*/
/*+-------------------------------------------------------------------+*/

/*+-------------------------------------------------------------------+*/
/*| Static Variables                                                  |*/
/*+-------------------------------------------------------------------+*/

//Create a variable to assign the strcture
//struct list_user user[MAX_USERS];

/*+-------------------------------------------------------------------+*/
/*| Internal function prototypes.                                     |*/
/*+-------------------------------------------------------------------+*/

// For starters, it should contain a structure which will keep 4 fields:
// -- Firstname
// -- Lastname
// -- Age
// -- Address
// -- (and the next pointer, of course)

// Also, I would like you to implement some functions in order to interact with the list,
// for starters, the basic ones which need to be defined are:
// -- add/remove element from the list
// -- print the whole list
// -- print a specific element of the list

//Create pointers for the first and last user
static struct dsd_list_user *dss_first, *last, *dss_temp;


//Function which shows the main menu with four options
void show_menu()
{
    printf("Menu: \n");
    printf("1- Add a new user\n");
    printf("2- Delete a user\n");
    printf("3- Show the user's list\n");
    printf("4- Exit\n\n");
    printf("Choose a option: ");
    fflush(stdout);
    //printf("\n\n\n");
    //scanf("%i",&ins_option_menu);
}
//#define SCANF
//Function which adds a new user
void add_user()
{
    //Create a pointer for add new users
    struct dsd_list_user *new_user;

    //Keep memory for the new user
    new_user = (struct dsd_list_user *) malloc(sizeof(struct dsd_list_user));

    //Show the fields to fill for creating a new user
    printf("\nNEW USER\n");
    printf("\nFirst Name: ");
    fflush(stdin);
#ifdef SCANF
    scanf("%s",new_user->chrc_firstname); 
#else
    gets(new_user->chrc_firstname);
#endif // SCANF
    fflush(stdin);
    printf("Last Name: ");
#ifdef SCANF
    scanf("%s",new_user->chrc_lastname); 
#else
    gets(new_user->chrc_lastname);
#endif // SCANF
    fflush(stdin);
    printf("Address: ");
#ifdef SCANF
    scanf("%s",new_user->chrc_address); 
#else
    gets(new_user->chrc_address);
#endif // SCANF
    fflush(stdin);
    printf("Age: ");
#ifdef SCANF
    scanf("%s",new_user->chrc_age); 
#else
    gets(new_user->chrc_age);
#endif // SCANF

//The last user of the list is always null
    new_user->adsc_next = NULL;

    //Check if there are more users in the list
    if (dss_first==NULL)
    {
        //If there are not users, show the user is the first
        printf("\nFIRST USER\n");
        //So, the new and first user, will be the first and last user.
        dss_first = new_user;
        last = new_user;
    }

    //If there are users, assign the new user to the next and obiously to the last user.
    else
    {
        last->adsc_next = new_user;
        last = new_user;
    }
    printf("USER CREATED\n\n");
}

//Function which drops an user
void drop_user()
{
    struct dsd_list_user *dsl_current_user;
//struct dsd_list_user *dsl_temp_user;
struct dsd_list_user *dsl_previous_user;
//struct dsd_list_user *dsl_next_user;
//struct dsd_list_user *dsl_first_user;
dsd_list_user *dsl_next;

//Variable to indicate the number of user
    //int i = 0;
    int number_user = 0;
int inp_index = 0;

    //Assign that pointer to the first user
    dsl_current_user = dss_first;
dsl_previous_user = last;


//Check if there are users in the list
    if (dsl_current_user==NULL)
    {
        //If there are not users, show the user is the first
        printf("\nThere are no users to drop\n\n");
        return;
    }

//If there are just one user
if (dsl_current_user->adsc_next == NULL)
{
printf("Only one user in the list to drop.");
//fflush(stdin);
//gets(dsl_current_user->chrc_firstname);
free(dss_first);
dss_first = NULL;
last = NULL;
return;
}
 
//p_gotto:
while (dsl_current_user != NULL)
{

if(dsl_current_user->adsc_next != NULL)
{
printf("There are more of one user in the list:");
//fflush(stdin);
//gets(dsl_current_user->chrc_firstname);
//dsl_current_user->adsc_next=dsl_previous_user;
//dsl_current_user = dsl_current_user->adsc_next;
free(dsl_current_user->adsc_next);
dsl_current_user->adsc_next = NULL;
//last = NULL;
return;
}
}

//goto p_gotto;
}






//Function which shows the full list
void show_list()
{
    //Create a pointer for show the full which indicate the current user to show in the list
    struct dsd_list_user *dsl_current_user;

    //Variable to indicate the number of user
    int i = 0;

    //Assign that pointer to the first user
    dsl_current_user = dss_first;

    //Start to show the full list
    printf("\n\nShowing the full list:\n\n");

    //Create a while to show the current user in that moment
    //Obiously, while the current user is not NULL
   if(dsl_current_user == NULL)
   {
  printf("There are not users in the list\n\n");
   return;
   }

while(dsl_current_user != NULL)
    {
        //Show the data of users
        printf("User number: %i \n\n", i+1);
        printf("First Name: %s\n", dsl_current_user->chrc_firstname);
        printf("Last Name: %s\n", dsl_current_user->chrc_lastname);
        printf("Address: %s\n", dsl_current_user->chrc_address);
        printf("Age: %s\n\n\n", dsl_current_user->chrc_age);

        //Assign the next user to the current for the next repetition
        dsl_current_user = dsl_current_user->adsc_next;

        //Pass to the next user
        i++;
    }

    //If the number of users is 0 -> the list is empty
    if (i==0) printf("The List is empty\n\n");
}

/*+-------------------------------------------------------------------+*/
/*| Main control procedure.                                           |*/
/*+-------------------------------------------------------------------+*/

int main()
{
    //int ins_option_menu;
    int option;

   

    do
    {
        show_menu();
        option = getchar();

        //Depending of the option selected, start a function or exit.
        if (option=='1')
        { add_user(); }
        if (option=='2')
        { drop_user();}
        //{ printf("\nNot configurated\n\n"); }
        if (option=='3')
        { show_list(); }
        if (option=='4')
        { return 0; }
        if(option < '1' || option > '4')
        { printf( "\nOption incorrect\n\n" ); }
        fflush(stdin);
    }
    while(option != 4);
    printf("\n\n");
    //system("pause");
    //goto p_goto;
}


/*+-------------------------------------------------------------------+*/
/*| FINAL                                                             |*/
/*+-------------------------------------------

AlbertoBSD

#3
El detalle que si es si es la primera vez que usas listas enlazadas hay varioa detalles al eliminar elementos:

Este es un tema donde se discutió eso
https://foro.elhacker.net/programacion_cc/borrar_nodo_en_lista_simplemente_enlazadac-t455556.0.html

Y aqui un tema de como ordenarlas

https://foro.elhacker.net/programacion_cc/ordenar_lista_simplemente_enlazada_en_lenguaje_c-t454743.0.html

Los algoritmoa son los mismos solo hay que plicarlo al tipo de lista enlazada que estas usando

Donaciones
1Coffee1jV4gB5gaXfHgSHDz9xx9QSECVW