elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Introducción a Git (Primera Parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Funcion borrar elemento de una lista STRUCT
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Funcion borrar elemento de una lista STRUCT  (Leído 2,156 veces)
galapok11

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Funcion borrar elemento de una lista STRUCT
« en: 17 Agosto 2016, 14:09 pm »

Este funcion deberia borrar un elemento de una lista STRUCT pero no llega a funcionar
Código
  1. void drop_user()
  2. {
  3.    struct dsd_list_user *dsl_current_user;
  4. struct dsd_list_user *dsl_temp_user;
  5.  
  6. dsl_temp_user=head;
  7.  
  8.    //Variable to indicate the number of user
  9.    //int i = 0;
  10.    int number_user = 0;
  11.  
  12.    //Assign that pointer to the first user
  13.    dsl_current_user = dss_first;
  14.  
  15.    //Check if there are users in the list
  16.    if (dsl_current_user==NULL)
  17.    {
  18.        //If there are not users, show the user is the first
  19.        printf("\nThere are no users to drop\n\n");
  20.        return;
  21.    }
  22.  
  23.    //If there are just one user
  24.    if (dsl_current_user->adsc_next == NULL)
  25.    {
  26.        printf("Only one user to drop it.\n");
  27. printf("Indicate the First Name of the user which you want drop it:");
  28. //gets(dsl_current_user->chrc_firstname);
  29. free(dsl_current_user);
  30. //dsl_current_user = NULL;
  31. return;
  32. //dsl_current_user = dsl_current_user->adsc_next;
  33.    }
  34. while (dsl_current_user != NULL)
  35. {
  36. printf("Indicate the First Name of the user which you want drop it:");
  37. gets(dsl_current_user->chrc_firstname);
  38. free(dsl_current_user);
  39. dsl_current_user = dsl_current_user->adsc_next;
  40. }
  41. }
  42.  
  43.  


En línea

AlbertoBSD
Programador y
Moderador Global
***
Desconectado Desconectado

Mensajes: 3.696


🏴 Libertad!!!!!


Ver Perfil WWW
Re: Funcion borrar elemento de una lista STRUCT
« Respuesta #1 en: 17 Agosto 2016, 17:05 pm »

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


En línea

galapok11

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Re: Funcion borrar elemento de una lista STRUCT
« Respuesta #2 en: 17 Agosto 2016, 17:14 pm »

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
Código
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. /*+-------------------------------------------------------------------+*/
  7. /*| Defines                                                           |*/
  8. /*+-------------------------------------------------------------------+*/
  9.  
  10. /*+-------------------------------------------------------------------+*/
  11. /*| Structs & Definitions                                             |*/
  12. /*+-------------------------------------------------------------------+*/
  13.  
  14. //Define the structure list_user
  15. struct dsd_list_user
  16. {
  17.    char chrc_firstname[20];
  18.    char chrc_lastname[20];
  19.    char chrc_address[50];
  20.    char chrc_age[2];
  21.    dsd_list_user *adsc_next;
  22. };
  23.  
  24. /*+-------------------------------------------------------------------+*/
  25. /*| Global Variables                                                  |*/
  26. /*+-------------------------------------------------------------------+*/
  27.  
  28. /*+-------------------------------------------------------------------+*/
  29. /*| Static Variables                                                  |*/
  30. /*+-------------------------------------------------------------------+*/
  31.  
  32. //Create a variable to assign the strcture
  33. //struct list_user user[MAX_USERS];
  34.  
  35. /*+-------------------------------------------------------------------+*/
  36. /*| Internal function prototypes.                                     |*/
  37. /*+-------------------------------------------------------------------+*/
  38.  
  39. // For starters, it should contain a structure which will keep 4 fields:
  40. // -- Firstname
  41. // -- Lastname
  42. // -- Age
  43. // -- Address
  44. // -- (and the next pointer, of course)
  45.  
  46. // Also, I would like you to implement some functions in order to interact with the list,
  47. // for starters, the basic ones which need to be defined are:
  48. // -- add/remove element from the list
  49. // -- print the whole list
  50. // -- print a specific element of the list
  51.  
  52. //Create pointers for the first and last user
  53. static struct dsd_list_user *dss_first, *last, *dss_temp;
  54.  
  55.  
  56. //Function which shows the main menu with four options
  57. void show_menu()
  58. {
  59.    printf("Menu: \n");
  60.    printf("1- Add a new user\n");
  61.    printf("2- Delete a user\n");
  62.    printf("3- Show the user's list\n");
  63.    printf("4- Exit\n\n");
  64.    printf("Choose a option: ");
  65.    fflush(stdout);
  66.    //printf("\n\n\n");
  67.    //scanf("%i",&ins_option_menu);
  68. }
  69. //#define SCANF
  70. //Function which adds a new user
  71. void add_user()
  72. {
  73.    //Create a pointer for add new users
  74.    struct dsd_list_user *new_user;
  75.  
  76.    //Keep memory for the new user
  77.    new_user = (struct dsd_list_user *) malloc(sizeof(struct dsd_list_user));
  78.  
  79.    //Show the fields to fill for creating a new user
  80.    printf("\nNEW USER\n");
  81.    printf("\nFirst Name: ");
  82.    fflush(stdin);
  83. #ifdef SCANF
  84.    scanf("%s",new_user->chrc_firstname);  
  85. #else
  86.    gets(new_user->chrc_firstname);
  87. #endif // SCANF
  88.    fflush(stdin);
  89.    printf("Last Name: ");
  90. #ifdef SCANF
  91.    scanf("%s",new_user->chrc_lastname);  
  92. #else
  93.    gets(new_user->chrc_lastname);
  94. #endif // SCANF
  95.    fflush(stdin);
  96.    printf("Address: ");
  97. #ifdef SCANF
  98.    scanf("%s",new_user->chrc_address);  
  99. #else
  100.    gets(new_user->chrc_address);
  101. #endif // SCANF
  102.    fflush(stdin);
  103.    printf("Age: ");
  104. #ifdef SCANF
  105.    scanf("%s",new_user->chrc_age);  
  106. #else
  107.    gets(new_user->chrc_age);
  108. #endif // SCANF
  109.  
  110. //The last user of the list is always null
  111.    new_user->adsc_next = NULL;
  112.  
  113.    //Check if there are more users in the list
  114.    if (dss_first==NULL)
  115.    {
  116.        //If there are not users, show the user is the first
  117.        printf("\nFIRST USER\n");
  118.        //So, the new and first user, will be the first and last user.
  119.        dss_first = new_user;
  120.        last = new_user;
  121.    }
  122.  
  123.    //If there are users, assign the new user to the next and obiously to the last user.
  124.    else
  125.    {
  126.        last->adsc_next = new_user;
  127.        last = new_user;
  128.    }
  129.    printf("USER CREATED\n\n");
  130. }
  131.  
  132. //Function which drops an user
  133. void drop_user()
  134. {
  135.    struct dsd_list_user *dsl_current_user;
  136. //struct dsd_list_user *dsl_temp_user;
  137. struct dsd_list_user *dsl_previous_user;
  138. //struct dsd_list_user *dsl_next_user;
  139. //struct dsd_list_user *dsl_first_user;
  140. dsd_list_user *dsl_next;
  141.  
  142. //Variable to indicate the number of user
  143.    //int i = 0;
  144.    int number_user = 0;
  145. int inp_index = 0;
  146.  
  147.    //Assign that pointer to the first user
  148.    dsl_current_user = dss_first;
  149. dsl_previous_user = last;
  150.  
  151.  
  152. //Check if there are users in the list
  153.    if (dsl_current_user==NULL)
  154.    {
  155.        //If there are not users, show the user is the first
  156.        printf("\nThere are no users to drop\n\n");
  157.        return;
  158.    }
  159.  
  160. //If there are just one user
  161. if (dsl_current_user->adsc_next == NULL)
  162. {
  163. printf("Only one user in the list to drop.");
  164. //fflush(stdin);
  165. //gets(dsl_current_user->chrc_firstname);
  166. free(dss_first);
  167. dss_first = NULL;
  168. last = NULL;
  169. return;
  170. }
  171.  
  172. //p_gotto:
  173. while (dsl_current_user != NULL)
  174. {
  175.  
  176. if(dsl_current_user->adsc_next != NULL)
  177. {
  178. printf("There are more of one user in the list:");
  179. //fflush(stdin);
  180. //gets(dsl_current_user->chrc_firstname);
  181. //dsl_current_user->adsc_next=dsl_previous_user;
  182. //dsl_current_user = dsl_current_user->adsc_next;
  183. free(dsl_current_user->adsc_next);
  184. dsl_current_user->adsc_next = NULL;
  185. //last = NULL;
  186. return;
  187. }
  188. }
  189.  
  190. //goto p_gotto;
  191. }
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. //Function which shows the full list
  199. void show_list()
  200. {
  201.    //Create a pointer for show the full which indicate the current user to show in the list
  202.    struct dsd_list_user *dsl_current_user;
  203.  
  204.    //Variable to indicate the number of user
  205.    int i = 0;
  206.  
  207.    //Assign that pointer to the first user
  208.    dsl_current_user = dss_first;
  209.  
  210.    //Start to show the full list
  211.    printf("\n\nShowing the full list:\n\n");
  212.  
  213.    //Create a while to show the current user in that moment
  214.    //Obiously, while the current user is not NULL
  215.   if(dsl_current_user == NULL)
  216.   {
  217.  printf("There are not users in the list\n\n");
  218.   return;
  219.   }
  220.  
  221. while(dsl_current_user != NULL)
  222.    {
  223.        //Show the data of users
  224.        printf("User number: %i \n\n", i+1);
  225.        printf("First Name: %s\n", dsl_current_user->chrc_firstname);
  226.        printf("Last Name: %s\n", dsl_current_user->chrc_lastname);
  227.        printf("Address: %s\n", dsl_current_user->chrc_address);
  228.        printf("Age: %s\n\n\n", dsl_current_user->chrc_age);
  229.  
  230.        //Assign the next user to the current for the next repetition
  231.        dsl_current_user = dsl_current_user->adsc_next;
  232.  
  233.        //Pass to the next user
  234.        i++;
  235.    }
  236.  
  237.    //If the number of users is 0 -> the list is empty
  238.    if (i==0) printf("The List is empty\n\n");
  239. }
  240.  
  241. /*+-------------------------------------------------------------------+*/
  242. /*| Main control procedure.                                           |*/
  243. /*+-------------------------------------------------------------------+*/
  244.  
  245. int main()
  246. {
  247.    //int ins_option_menu;
  248.    int option;
  249.  
  250.  
  251.  
  252.    do
  253.    {
  254.        show_menu();
  255.        option = getchar();
  256.  
  257.        //Depending of the option selected, start a function or exit.
  258.        if (option=='1')
  259.        { add_user(); }
  260.        if (option=='2')
  261.        { drop_user();}
  262.        //{ printf("\nNot configurated\n\n"); }
  263.        if (option=='3')
  264.        { show_list(); }
  265.        if (option=='4')
  266.        { return 0; }
  267.        if(option < '1' || option > '4')
  268.        { printf( "\nOption incorrect\n\n" ); }
  269.        fflush(stdin);
  270.    }
  271.    while(option != 4);
  272.    printf("\n\n");
  273.    //system("pause");
  274.    //goto p_goto;
  275. }
  276.  
  277.  
  278. /*+-------------------------------------------------------------------+*/
  279. /*| FINAL                                                             |*/
  280. /*+-------------------------------------------
  281.  
En línea

AlbertoBSD
Programador y
Moderador Global
***
Desconectado Desconectado

Mensajes: 3.696


🏴 Libertad!!!!!


Ver Perfil WWW
Re: Funcion borrar elemento de una lista STRUCT
« Respuesta #3 en: 17 Agosto 2016, 17:24 pm »

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

« Última modificación: 17 Agosto 2016, 17:27 pm por AlbertoBSD » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
como sacar cualquier elemento de lista
Programación C/C++
karmi 1 3,586 Último mensaje 11 Noviembre 2010, 09:16 am
por Akai
borrar elemento de la lista enlazada simple
Programación C/C++
d91 2 3,281 Último mensaje 9 Junio 2014, 13:44 pm
por d91
problemas al borrar un elemento en el archivo
Programación C/C++
geshiro 8 3,332 Último mensaje 30 Junio 2016, 02:24 am
por geshiro
Invertir una Lista en C - La funcion Pop no me da el ultimo elemento.?
Programación C/C++
palacio29 3 3,612 Último mensaje 27 Octubre 2016, 01:01 am
por MAFUS
Problema con una funcion para verificar si un elemento esta o no
Programación C/C++
palacio29 3 3,136 Último mensaje 14 Junio 2019, 22:47 pm
por K-YreX
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines