Problem sorting a simple linked list

Reply

Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster

Problem sorting a simple linked list

 
0
  #1
Nov 5th, 2007
Hello everybody

I have a problem about sorting a linked list. The following is the code that I wrote in C, it works well at this moment; it just generates random numbers, it just prints the random numbers generated and the even numbers. But I need to sort these numbers (only the even numbers), but I don´t know how to do that. If you could help me, I will appreciate it

This is my code (sorry, is in Spanish ) but I can explain my code if you need some little help to read my code. Please help me, this homework is for tomorrow

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 20
  4.  
  5. typedef int Objeto;
  6.  
  7. typedef struct Elemento
  8. {
  9. Objeto dato;
  10. struct Elemento *siguiente;
  11.  
  12. }Nodo;
  13.  
  14.  
  15.  
  16. /*protoripo de las funciones*/
  17. void inserPrimero(Nodo** lista, Objeto entrada);
  18. Nodo* crearNodo(Objeto x);
  19.  
  20.  
  21.  
  22.  
  23. int main()
  24. {
  25. Objeto d;
  26. Nodo *lista, *ptr;
  27. int k, i, n;
  28. lista = NULL; /*Aquí se crea una lista vacía*/
  29.  
  30. /*----------------------- generar random numbers ---------------------------*/
  31.  
  32. srand((unsigned)time(NULL));
  33.  
  34.  
  35. for (d = rand()%MAX; d; )/* Ciclo termina cuando se genera el número 0 */
  36. {
  37. inserPrimero(&lista, d);
  38. d = rand()%MAX;
  39. //d = random(MAX);
  40. }
  41. /*-------------------- identificar numeros aleatorios ----------------------*/
  42.  
  43. printf("\n\n");
  44. printf("Conjunto de numeros aleatorios hasta encontrar el 0\n\n");
  45. for (i = 0, ptr = lista; ptr!=NULL; ptr = ptr -> siguiente) //recorrer la lista
  46. {
  47. printf ("%d", ptr -> dato);
  48. k++;
  49. printf("%c", (k%15 ? ' ' : '\n'));
  50.  
  51. }
  52.  
  53. printf("\n\n");
  54.  
  55. /* -------------------------identificar numeros pares -----------------------*/
  56. printf("PARES\n\n");
  57. for (k = 0, ptr = lista; ptr!=NULL; ptr = ptr -> siguiente)
  58.  
  59.  
  60. if (ptr -> dato%2 == 0) /* se recorre la lista para escribir los pares */
  61. { printf("%d ", ptr -> dato);
  62. k++;
  63. printf("%c",(k%15 ? ' ' : '\n')); /* Despliega 20 datos por línea */
  64. }
  65. printf("\n\n");
  66.  
  67.  
  68. /* -------------------------- ordenar los numeros pares ---------------------*/
  69.  
  70. //HERE: ... I WANT TO SORT MY EVEN NUMBERS
  71. }
  72.  
  73.  
  74. void inserPrimero(Nodo** lista, Objeto entrada)
  75. { Nodo *nuevo ;
  76. nuevo = crearNodo(entrada);
  77. nuevo -> siguiente = *lista;
  78. *lista = nuevo;
  79. }
  80.  
  81. Nodo* crearNodo(Objeto x)
  82. { Nodo *a ;
  83. a = (Nodo*)malloc(sizeof(Nodo)); /* asigna nuevo nodo */
  84. a -> dato = x;
  85. a -> siguiente = NULL;
  86. return a;
  87. }


Thanks in advance
Last edited by Narue; Nov 5th, 2007 at 9:47 am. Reason: Changed inline code tags to regular code tags.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster

Re: Problem sorting a simple linked list

 
0
  #2
Nov 5th, 2007
Well, i saw the link that says that we need to work hard, but I´m trying to do that, but I can´t. I know in my code that I need to build a function for a sorting method and maybe some temporal lists, I found many resources on the web, and in my books but no one is very helpful in my case. My code is in Spanish because I´m from Mexico if you don´t understand something, I can explain my code,

Thanks in advance, and sorry
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem sorting a simple linked list

 
0
  #3
Nov 5th, 2007
This link is a vain attempt to help you, even though I'm reasonably sure you're beyond help at this point.

>Please help me, this homework is for tomorrow
So...you have an assignment due tomorrow on sorting a linked list and you have exactly zero code that even attempts to sort the list? What have you been doing since the assignment was given?

I'm not interested in helping you if you can't dig up some sort of proof that you've at least tried to solve the problem on your own. Anybody with a bit of C knowledge can build a linked list with random numbers, so your framework code isn't proof. Did you really think you could post code with an /* I want my problem solved here */ comment and get us to do it for you?

Seriously, if your homework is due tomorrow and you're as clueless as you've made yourself out to be, the only way you're going to get anything but a failing grade is by plagiarizing our solutions.

p.s. It's wiser to write your code and comments in English if you intend to put it on the web. You'll get better help from more people that way, rather than relying only on help from people who understand your dialect of Spanish or playing 20 questions trying to figure out what your variable names represent. English is the language of the internet, after all. Unless you're posting to niche sites, you can expect everyone to understand English well enough to communicate.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster

Re: Problem sorting a simple linked list

 
0
  #4
Nov 5th, 2007
Well, you´re right, . . . I´m sorry for that. Yes of course, maybe I didn´t put my work of my code that I need, just for reasons that are from the web. . . and I think is not ethical. The code that I wrote its my own code, with some little help of course. I have a subject in school, mm Data Structures, and that´s the reason why I´m new in C and I´m trying to build a code with linked lists.

I´m sorry, yes, maybe I didn´t think of that in the last post.

Well I will try to post some little codes that could help me, but they didn´t work, because I need to pass the exact argument into the function. And that´s all of my problem


Sorry again, and I promise to all of you that this will never happen again

Nataly
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem sorting a simple linked list

 
0
  #5
Nov 5th, 2007
>but they didn´t work, because I need to pass the exact argument into the function.
Not working is fine. Obviously you're here because you couldn't get it to work the way you want. But if you don't post the code, it's impossible to tell you what's wrong and how to fix it. As a helper, it's extremely frustrating to want to help, but be unable to because you didn't provide enough information.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster

Re: Problem sorting a simple linked list

 
0
  #6
Nov 5th, 2007
Ok here . . this was my first problem:

I thought that I could convert my nodes into an array . . and then I could sort them with quicksort or something like that. I read that sorting a linked list, and in my case generating random numbers could be difficult

My idea is that if I need to convert my nodes into an array, my list is dynamic, because when a 0 is generated, the list stops. So, sometimes I have 12 numbers generated, or sometimes 0 numbers generated.

So, I thought that converting my nodes into an array, first I need to create a counter just to know how many nodes I´ve created. then create the array with size of the number of the counter. And finally, pass them into the "dynamic" array.



So my first part it was something like this:

/*
//First, I´m trying to convert my nodes into an array (this is not working)

for (a = 0; a < MAX; a++)
{
array [a] = aux -> siguiente;
aux = aux -> siguiente;

}
*/

In that code, I tried to create an auxiliary object, but first this is not working, maybe because the syntax is wrong or because in this part I didn´t create the exact size of the array for my nodes.


If you could help me in my problem, I will appreciate it.

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,625
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 715
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Problem sorting a simple linked list

 
0
  #7
Nov 5th, 2007
>I thought that I could convert my nodes into an array . . and
>then I could sort them with quicksort or something like that.
That's a good solution.

>I read that sorting a linked list, and in my case
>generating random numbers could be difficult
It's not overly difficult, but you do need to change your thinking a bit. The link I gave you shows a simple insertion sort on a linked list.

>So, I thought that converting my nodes into an array, first I need to
>create a counter just to know how many nodes I´ve created. then
>create the array with size of the number of the counter. And finally,
>pass them into the "dynamic" array.
Makes sense to me. I'd do it something like this:
  1. int *list_to_array ( Nodo *list, int n )
  2. {
  3. int *result = malloc ( n * sizeof *result );
  4.  
  5. if ( result != NULL ) {
  6. /*
  7.   There's no need for a separate counter
  8.   because the order of the array doesn't
  9.   matter. We can build it from back to front
  10.   */
  11. while ( --n >= 0 && list != NULL ) {
  12. result[n] = list->dato;
  13. list = list->siguiente;
  14. }
  15.  
  16. if ( !( n < 0 && list == NULL ) ) {
  17. /*
  18.   If we get here, the number of
  19.   nodes and n didn't match. Handle
  20.   the error however you'd like
  21.   */
  22. }
  23. }
  24.  
  25. return result;
  26. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 15
Reputation: NatalyC is an unknown quantity at this point 
Solved Threads: 0
NatalyC NatalyC is offline Offline
Newbie Poster

Re: Problem sorting a simple linked list

 
0
  #8
Nov 6th, 2007
ohhh thanks a lot, I will check this to implement in my code, sounds great. I will post my final code for everyone as soon as possible. Thanks for the help again



(Maybe I could need some little help, so maybe I will post another problem again hehe )

Thank you very much


Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC