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

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


  Mostrar Temas
Páginas: [1]
1  Programación / Programación C/C++ / AYUDA! Urgente LISTAS ENLAZADAS DOBLES en: 17 Abril 2015, 06:01 am
Gente, tengo un pequeño gran problema con este programa.

Cuando quiero finalizar la lista y salir del programa, me da este error "Unhandled exception at 0x00341459 in EJEM DOBLES.exe: 0xC0000005: Access violation reading location 0x00000004."

No encuentro que puede ser, ya que todo lo demás funciona perfecto.

Agradezco la ayuda!

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct snodo
  5. { int data;
  6. struct snodo *ant, *sig;
  7. } tnodo;
  8.  
  9. typedef tnodo *tpuntero;
  10.  
  11. void insertarelemento(tpuntero *, int);
  12. int eliminarelemento(tpuntero *, int);
  13. void mostrarlista(tpuntero);
  14. int listavacia(tpuntero);
  15. void borrarlista(tpuntero*);
  16. void instruccions(void);
  17.  
  18. int main()
  19. { tpuntero inicio = NULL;
  20. char choice;
  21. int item;
  22.  
  23. instruccions();
  24. fflush(stdin);
  25. scanf("%c", &choice);
  26. while (choice != '4')
  27. { switch (choice)
  28. { case '1':
  29. printf("Ingrese un elemento a la lista\n");
  30. fflush(stdin);
  31. if (scanf("%d", &item) != 1)
  32. { printf("Error, debe ingresar un numero\n"); }
  33. else
  34. { insertarelemento(&inicio, item); }
  35. break;
  36. case '2':
  37. mostrarlista(inicio);
  38. break;
  39. case '3':
  40. if (!listavacia(inicio)) //sino esta vacia
  41. { printf("Escriba el elemento que quiera eliminar\n");
  42. fflush(stdin);
  43. scanf("%d", &item);
  44.  
  45. if (eliminarelemento(&inicio, item)) //si lo encontró y borró
  46. { printf("El elemento %d fue eliminado\n", item);
  47. mostrarlista(inicio); }  //muestra la lista luego de borrarlo
  48. else    //si no lo encontro
  49. { printf("Elemento no encontrado\n\n", item); }
  50. }
  51. else
  52. { printf("La lista esta vacia\n"); }
  53. break;
  54. default:         //si la opcion es invalida le vuelve a preguntar
  55. printf("Opcion invalida \n");
  56. break;
  57. }
  58. instruccions();
  59. fflush(stdin);
  60. scanf("%c", &choice);
  61. }
  62. printf("FIN DEL PROGRAMA");
  63. borrarlista(&inicio);
  64. return 0;
  65. }
  66. void insertarelemento(tpuntero *inicio, int valor)
  67. {
  68. tpuntero anterior, actual, nuevo;
  69. actual = *inicio;
  70. anterior = NULL;
  71. while (actual != NULL&&actual->data<valor)
  72. { anterior = actual;
  73. actual = actual->sig; }
  74. nuevo = (tpuntero)malloc(sizeof(tnodo));
  75. nuevo->data = valor;
  76. nuevo->ant = anterior;
  77. nuevo->sig = actual;
  78.  
  79. if (anterior != NULL)
  80. { anterior->sig = nuevo; }
  81. else
  82. { *inicio = nuevo; }
  83. if (actual != NULL)
  84. { actual->ant = nuevo; }
  85. }
  86.  
  87. int eliminarelemento(tpuntero *inicio, int valor)
  88. {
  89. tpuntero anterior, actual, temp;
  90.  
  91. if (valor == (*inicio)->data)
  92. { temp = *inicio;
  93. *inicio = (*inicio)->sig;
  94. free(temp);
  95. return valor; }
  96. else
  97. { anterior = *inicio;
  98. actual = (*inicio)->sig;
  99.  
  100. while (actual != NULL&&actual->data != valor)
  101. { anterior = actual;
  102. actual = actual->sig; }
  103. if (actual != NULL)
  104. { temp = actual;
  105. anterior->sig = actual->sig;
  106. free(temp);
  107. return valor; }
  108. }
  109. return 0;
  110. }
  111.  
  112. void mostrarlista(tpuntero inicio)
  113. {
  114. if (inicio == NULL)
  115. printf("La lista esta vacia\n");
  116. else
  117. {
  118. tpuntero anterior;
  119. anterior = NULL;
  120. puts("IDA");
  121. while (inicio != NULL)
  122. {
  123. printf("%2d", inicio->data);
  124. anterior = inicio;
  125. inicio = inicio->sig;
  126. }
  127. printf("\n");
  128.  
  129. puts("REGRESO");
  130. while (anterior != NULL)
  131. {
  132. printf("%2d", anterior->data);
  133. anterior = anterior->ant;
  134. }
  135. printf("\n");
  136. }
  137. }
  138.  
  139. int listavacia(tpuntero inicio)
  140. { return inicio == NULL; }
  141.  
  142. void borrarlista(tpuntero *inicio)
  143. {
  144. tpuntero nuevo, actual;
  145. actual = *inicio;
  146. while (actual->ant != NULL)
  147. { actual = actual->ant; }
  148. while (actual != NULL)
  149. {
  150. nuevo = actual;
  151. actual = actual->sig;
  152. free(nuevo);
  153. }
  154. *inicio = NULL;
  155. }
  156.  
  157. void instruccions(void)
  158. { printf("Elija su opcion:\n"
  159. "1 Para insertar un elemento en la lista\n"
  160. "2 Para mostrar lista  \n"
  161. "3 Para eliminar un elemento de la lista\n"
  162. "4 Para cerrar el programa\n"); }
  163.  
2  Programación / Programación C/C++ / listas enlazadas simples en: 13 Abril 2015, 01:56 am
Por favor podrian ayudarme con este codigo?
Lo saque de un libro pero no compila, son listas enlazadas simples
No encuentro el error. Gracias!!

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct listNode{
  5. char data;
  6. struct listNode *nextPtr;
  7. };
  8. typedef struct listNode LISTNODE;
  9. typedef LISTNODE *LISTNODEPTR;
  10.  
  11. void insert(LISTNODEPTR *, char);
  12. char deleter(LISTNODEPTR *, char);
  13. int isEmpty(LISTNODEPTR);
  14. void printList(LISTNODEPTR);
  15. void instruccions(void);
  16.  
  17. main()
  18. {
  19. LISTNODEPTR startPtr = NULL;
  20. int choice;
  21. char item;
  22.  
  23. instruccions();
  24. printf("?");
  25. scanf("%d",&choice);
  26.  
  27. while (choice!= 3)
  28. {
  29. switch (choice)
  30. {
  31. case 1:
  32. printf("enter a character");
  33. scanf("n%c", &item);
  34. insert(&startPtr, item);
  35. printList(startPtr);
  36. break;
  37. case 2:
  38. if (!isEmpty(startPtr))
  39. {
  40. printf("enter character to be deleted");
  41. scanf("\n%c", &item);
  42.  
  43. if (deleter(&startPtr, item))
  44. {
  45. printf("%c deleted", item);
  46. printList(startPtr);
  47. }
  48. else
  49. {
  50. printf("%c not found\n", item);
  51. }
  52. }
  53. else
  54. {
  55. printf("List is empty\n");
  56. }
  57. break;
  58. default:         //si la opcion es invalida le vuelve a preguntar
  59. printf("invalid choice \n");
  60. instruccions();
  61. break;
  62. }
  63. printf("?");
  64. scanf("%d", &choice);
  65. }
  66. printf("end of run\n");
  67. return 0;
  68. }
  69.  
  70. void instruccions(void) //mostrar opciones
  71. {
  72. printf("enter your choice:\n"
  73. "1 to insert an element into the list\n"
  74. "2 to delete an element from the list\n"
  75. "3 to end\n");
  76. }
  77.  
  78. void insert(LISTNODEPTR *sPtr, char value)
  79. {
  80. LISTNODEPTR newPtr, previousPtr, currentPtr;
  81.  
  82. newPtr =(LISTNODEPTR) malloc(sizeof(LISTNODE));
  83.  
  84. if (newPtr != NULL)
  85. {
  86. newPtr->data = value;
  87. newPtr->nextPtr = NULL;
  88.  
  89. previousPtr = NULL;
  90. currentPtr = *sPtr;
  91.  
  92. while (currentPtr != NULL&&value > currentPtr->data)
  93. {
  94. previousPtr = currentPtr;
  95. currentPtr = currentPtr->nextPtr;
  96. }
  97.  
  98. if (previousPtr == NULL)
  99. {
  100. newPtr->nextPtr = *sPtr;
  101. *sPtr = newPtr;
  102. }
  103. else
  104. {
  105. previousPtr->nextPtr = newPtr;
  106. newPtr->nextPtr = currentPtr;
  107. }
  108. }
  109. else
  110. {
  111. printf("%c not inserted no memmory avalaible\n", value);
  112. }
  113.  
  114.  
  115.  
  116. char deleter(LISTNODEPTR *sPtr, char value)
  117. {
  118. LISTNODEPTR tempPtr, previousPtr, currentPtr;
  119.  
  120. if (value == (*sPtr)->data)
  121. {
  122. tempPtr = *sPtr;
  123. *sPtr = (*sPtr)->nextPtr;
  124. free(tempPtr);
  125. return value;
  126. }
  127. else
  128. {
  129. previousPtr = *sPtr;
  130. currentPtr = (*sPtr)->nextPtr;
  131.  
  132. while (currentPtr != NULL&&currentPtr->data != value)
  133. {
  134. previousPtr = currentPtr;
  135. currentPtr = currentPtr->NextPtr;
  136. }
  137. if (currentPtr != NULL)
  138. {
  139. tempPtr = currentPtr;
  140. previousPtr->nextPtr = currentPtr->nextPtr;
  141. free (tempPtr);
  142. return value;
  143. }
  144. }
  145. return 0;
  146. }
  147.  
  148. int isEmpty(LISTNODEPTR sPtr)
  149. {
  150. return sPtr == NULL;
  151. }
  152.  
  153. void printList(LISTNODEPTR currentPtr)
  154. {
  155. if (currentPtr == NULL)
  156.  
  157. printf("List is empty");
  158.  
  159. else
  160. {
  161. printf("the list is:");
  162. while (currentPtr != NULL)
  163. {
  164. printf("%c --> ", currentPtr->data);
  165. currentPtr = currentPtr->nextPtr;
  166. }
  167. printf("NULL \n\n");
  168. }
  169. }
  170.  


Mod: modificado el código con etiquetas GeSHi,
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines