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

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


  Mostrar Mensajes
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++ / Re: listas enlazadas simples en: 15 Abril 2015, 05:00 am
Genial !!

Muchas gracias por la ayuda, dejo el código completo funcionando perfectamente.

Ahora voy a tratar de implementarlo para una lista doblemente enlazada. Cualquier cosa, hago otro post.

Saludos :)


Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct listNode{
  5. char data;
  6. struct listNode *nextPtr;
  7. }LISTNODE;
  8.  
  9. typedef LISTNODE *LISTNODEPTR;
  10.  
  11. void insert(LISTNODEPTR *, char);
  12. char deleter(LISTNODEPTR *, char);
  13. void printList(LISTNODEPTR);
  14. int isEmpty(LISTNODEPTR);
  15. void instruccions(void);
  16.  
  17. int main()
  18. {
  19. LISTNODEPTR startPtr = NULL;
  20. char choice, item;
  21.  
  22. instruccions();
  23. fflush(stdin);
  24. scanf("%c", &choice);
  25.  
  26. while (choice != '3')
  27. {
  28. switch (choice)
  29. {
  30. case '1':
  31. printf("Ingrese un elemento a la lista\n");
  32. fflush(stdin);
  33. scanf("%c", &item);
  34. insert(&startPtr, item);
  35. printList(startPtr);
  36. break;
  37. case '2':
  38. if (!isEmpty(startPtr))
  39. {
  40. printf("Escriba el elemento que quiera eliminar\n");
  41. fflush(stdin);
  42. scanf("%c", &item);
  43.  
  44. if (deleter(&startPtr, item))
  45. {
  46. printf("El elemento %c fue eliminado\n", item);
  47. printList(startPtr);
  48. }
  49. else
  50. {
  51. printf("%c Elemento no encontrado\n\n", item);
  52. }
  53. }
  54. else
  55. {
  56. printf("La lista está vacia\n");
  57. }
  58. break;
  59.  
  60. default:         //si la opcion es invalida le vuelve a preguntar
  61. printf("Opcion invalida \n");
  62. break;
  63. }
  64.  
  65. instruccions();
  66. fflush(stdin);
  67. scanf("%c", &choice);
  68.  
  69. }
  70.  
  71. printf("FIN DEL PROGRAMA");
  72.  
  73. return 0;
  74. }
  75.  
  76. void instruccions(void) //mostrar opciones
  77. {
  78. printf("Elija su opcion:\n"
  79. "1 Para insertar un elemento en la lista\n"
  80. "2 Para eliminar un elemento de la lista \n"
  81. "3 Para cerrar el programa\n");
  82. }
  83.  
  84. void insert(LISTNODEPTR *sPtr, char value)
  85. {
  86. LISTNODEPTR currentPtr, previousPtr, newPtr;
  87. currentPtr = *sPtr;
  88. previousPtr = NULL;
  89. while (currentPtr != NULL&&currentPtr->data<value)
  90. {
  91. previousPtr = currentPtr;
  92. currentPtr = currentPtr->nextPtr;
  93. }
  94. newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE));
  95. newPtr->data = value;
  96. newPtr->nextPtr = currentPtr;
  97. if (previousPtr != NULL)
  98. {
  99. previousPtr->nextPtr = newPtr;
  100. }
  101. else
  102. {
  103. *sPtr = newPtr;
  104. }
  105.  
  106. }
  107. char deleter(LISTNODEPTR *sPtr, char value)
  108. {
  109. LISTNODEPTR tempPtr, previousPtr, currentPtr;
  110.  
  111. if (value == (*sPtr)->data)
  112. {
  113. tempPtr = *sPtr;
  114. *sPtr = (*sPtr)->nextPtr;
  115. free(tempPtr);
  116. return value;
  117. }
  118. else
  119. {
  120. previousPtr = *sPtr;
  121. currentPtr = (*sPtr)->nextPtr;
  122.  
  123. while (currentPtr != NULL&&currentPtr->data != value)
  124. {
  125. previousPtr = currentPtr;
  126. currentPtr = currentPtr->nextPtr;
  127. }
  128. if (currentPtr != NULL)
  129. {
  130. tempPtr = currentPtr;
  131. previousPtr->nextPtr = currentPtr->nextPtr;
  132. free(tempPtr);
  133. return value;
  134. }
  135. }
  136. return 0;
  137. }
  138.  
  139. int isEmpty(LISTNODEPTR sPtr)
  140. {
  141. return sPtr == NULL;
  142. }
  143.  
  144. void printList(LISTNODEPTR currentPtr)
  145. {
  146. if (currentPtr == NULL)
  147.  
  148. printf("La lista esta vacia\n\n");
  149.  
  150. else
  151. {
  152. printf("LA LISTA ES:\n");
  153. while (currentPtr != NULL)
  154. {
  155. printf(" %c-->", currentPtr->data);
  156. currentPtr = currentPtr->nextPtr;
  157. }
  158. printf("NULL \n\n");
  159. }
  160. }
  161.  
  162.  
3  Programación / Programación C/C++ / Re: listas enlazadas simples en: 13 Abril 2015, 18:14 pm
Muchas gracias! Ahora si logra compilar  :D

Lo que no logro resolver es borrar elementos luego de insertarlos, es eso posible?
Es decir que si elijo "case 1" me deja insertarlos pero si despues quisiera borrarlos, como deberia hacerlo?

Falta la ultima parte de liberar memoria, voy a averiguar como se hace por que no me lo explicaron

Sorry por las GESHI, no logro cambiarles el color

Asi me quedo el codigo:

#include <stdio.h>
#include <stdlib.h>

typedef struct listNode{
   char data;
   struct listNode *nextPtr;
}LISTNODE;
 
typedef LISTNODE *LISTNODEPTR;

void insert(LISTNODEPTR *, char);
char deleter(LISTNODEPTR *, char);
void printList(LISTNODEPTR);
int isEmpty(LISTNODEPTR);
void instruccions(void);

main()
{
   LISTNODEPTR startPtr = NULL;
   char choice;
   char item;

   instruccions();
   scanf("%d", &choice);

   do
   {
      switch (choice)
      {
      case 1:
         printf("enter a character\n");
         scanf("%c\n", &item);
         insert(&startPtr, item);
         printList(startPtr);
         break;
      case 2:
         if (!isEmpty(startPtr))
         {
            printf("enter character to be deleted");
            scanf(" %c\n", &item);

            if (deleter(&startPtr, item))
            {
               printf("%c deleted", item);
               printList(startPtr);
            }
            else
            {
               printf("%c not found\n", item);
            }
         }
         else
         {
            printf("List is empty\n");
         }
         break;

      case 3:
         printf("invalid choice \n");
      default:         //si la opcion es invalida le vuelve a preguntar
         printf("invalid choice \n");
         break;
      }
   }
   
   while (choice != 3);
   return 0;
}

void instruccions(void) //mostrar opciones
{
   printf("enter your choice:\n"
      "1 to insert an element into the list\n"
      "2 to delete an element from the list\n"
      "3 to end\n");
}

void insert(LISTNODEPTR *sPtr, char value)
{
   LISTNODEPTR newPtr, previousPtr, currentPtr;

   newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE));

   if (newPtr != NULL)
   {
      newPtr->data = value;
      newPtr->nextPtr = NULL;

      previousPtr = NULL;
      currentPtr = *sPtr;

      while (currentPtr != NULL&&value > currentPtr->data)
      {
         previousPtr = currentPtr;
         currentPtr = currentPtr->nextPtr;
      }

      if (previousPtr == NULL)
      {
         newPtr->nextPtr = *sPtr;
         *sPtr = newPtr;
      }
      else
      {
         previousPtr->nextPtr = newPtr;
         newPtr->nextPtr = currentPtr;
      }
   }
   else
   {
      printf("%c not inserted no memmory avalaible\n", value);
   }

}
char deleter(LISTNODEPTR *sPtr, char value)
{
   LISTNODEPTR tempPtr, previousPtr, currentPtr;

   if (value == (*sPtr)->data)
   {
      tempPtr = *sPtr;
      *sPtr = (*sPtr)->nextPtr;
      free(tempPtr);
      return value;
   }
   else
   {
      previousPtr = *sPtr;
      currentPtr = (*sPtr)->nextPtr;

      while (currentPtr != NULL&&currentPtr->data != value)
      {
         previousPtr = currentPtr;
         currentPtr = currentPtr->nextPtr;
      }
      if (currentPtr != NULL)
      {
         tempPtr = currentPtr;
         previousPtr->nextPtr = currentPtr->nextPtr;
         free(tempPtr);
         return value;
      }
   }
   return 0;
}

int isEmpty(LISTNODEPTR sPtr)
{
   return sPtr == NULL;
}

void printList(LISTNODEPTR currentPtr)
{
   if (currentPtr == NULL)

      printf("List is empty");

   else
   {
      printf("the list is:");
      while (currentPtr != NULL)
      {
         printf("%c --> ", currentPtr->data);
         currentPtr = currentPtr->nextPtr;
      }
      printf("NULL \n\n");
   }
}
4  Programación / Programación C/C++ / Re: listas enlazadas simples en: 13 Abril 2015, 16:36 pm
Gracias!!

Rindo ese tema en pocos días y tengo que demostrar que funcionan bien las opciones.
5  Programación / Programación C/C++ / Re: listas enlazadas simples en: 13 Abril 2015, 03:30 am
1- Es mi primer post, no tenia idea de las GeShi
2- Es una lista enlazada que a partir de una eleccion (switch) ejecuta una u otra funcion
3- El error es este: fatal error LNK1169: one or more multiply defined symbols found
Solo encontre que faltaba cerrar la llave en la declaracion de la funcion Insert.
Gracias
6  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