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 :'( :(

#include <stdio.h>
#include <stdlib.h>
#define MAX 20

typedef int Objeto;

typedef struct Elemento
{
        Objeto dato;
        struct Elemento *siguiente;
                
        }Nodo;



/*protoripo de las funciones*/       
void inserPrimero(Nodo** lista, Objeto entrada);
Nodo* crearNodo(Objeto x);


        

int main()
{
     Objeto d;
     Nodo *lista, *ptr;
     int k, i, n;
     lista = NULL; /*Aquí se crea una lista vacía*/  
    
    /*----------------------- generar random numbers ---------------------------*/
    
     srand((unsigned)time(NULL)); 
    
     
     for (d = rand()%MAX; d; )/* Ciclo termina cuando se genera el número 0 */
	{	
        inserPrimero(&lista, d);
		d = rand()%MAX;
        //d = random(MAX);
	}
	/*-------------------- identificar numeros aleatorios ----------------------*/
     
	printf("\n\n");
    printf("Conjunto de numeros aleatorios hasta encontrar el 0\n\n");
    for (i = 0, ptr = lista; ptr!=NULL; ptr = ptr -> siguiente) //recorrer la lista
    {
        printf ("%d", ptr -> dato);
        k++;
        printf("%c", (k%15 ? ' ' : '\n'));
        
    }
    
    printf("\n\n");
    
    /* -------------------------identificar numeros pares -----------------------*/
    printf("PARES\n\n");
    for (k = 0, ptr = lista; ptr!=NULL; ptr = ptr -> siguiente) 
   
    
		if (ptr -> dato%2 == 0) /* se recorre la lista para escribir los pares */
		{	printf("%d ", ptr -> dato);
			k++;
			printf("%c",(k%15 ? ' ' : '\n')); 	/* Despliega 20 datos por línea */
		}
	printf("\n\n");
 
 
  /* -------------------------- ordenar los numeros pares ---------------------*/
 
 //HERE: ... I WANT TO SORT MY EVEN NUMBERS 
}


void inserPrimero(Nodo** lista, Objeto entrada)
{	Nodo *nuevo ;
	nuevo = crearNodo(entrada);
	nuevo -> siguiente = *lista;
	*lista = nuevo;	
}

Nodo* crearNodo(Objeto x)
{	Nodo *a ;
	a = (Nodo*)malloc(sizeof(Nodo)); /* asigna nuevo nodo */
	a -> dato = x;		 
	a -> siguiente = NULL;
	return a;
}

Thanks in advance :S:)

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 :(

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.

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

>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.

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

>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:

int *list_to_array ( Nodo *list, int n )
{
  int *result = malloc ( n * sizeof *result );

  if ( result != NULL ) {
    /*
      There's no need for a separate counter
      because the order of the array doesn't
      matter. We can build it from back to front
    */
    while ( --n >= 0 && list != NULL ) {
      result[n] = list->dato;
      list = list->siguiente;
    }

    if ( !( n < 0 && list == NULL ) ) {
      /*
        If we get here, the number of
        nodes and n didn't match. Handle
        the error however you'd like
      */
    }
  }

  return result;
}

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 :S)

Thank you very much


:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.