Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)

 duffer 1Sep2007 05:11

How to sort doubly-linked list? BFP

My first post! Congratulations :)
BFP = Big F**king Problem :)

First...I'm sorry if my English is wrong!(I'm from Bulgaria)
Third: Before one week, I know only few about Programming in C, some arrays, basic things, the loops, but my source is over 1200 lines, so help me to finish it, please.

So...
The idea is to sort the first two elements, than to compare the next two and something like that...Yet I'm very very CONFUSED.

this is my struct for an element of the doubly-linked list:

Code: C

`struct element{    struct worker a;    struct element *next;    struct element *previous;};typedef struct element element;element *head, *tail;`

In struct worker, I must sort the workers in a firm by the length of service (in increasing) in that firm, I know the date of the appoin for every worker I added in the list.
For Example:
I must sort that dates:
11.11.2003
11.12.2003
11.11.2006
11.11.2004
11.11.2005
11.11.2007
like that: 11.11.2007>11.11.2006>11.11.2005>11.11.2004>11.12> 2003>11.11.2003
Important: We don't know the number of the workers, and when we sort the list, we can add more workers, and than we must sort with the new again for example.

in that code, I add workers to the list:
Code: C

`void createElement(struct worker a){    element *newElement;    if (head == NULL)    {        newElement=(struct element *)malloc (sizeof(struct element));        newElement->a=a; //tuk vlizat proverenite danni i ukazatelq gi izvlicha        head=newElement;        newElement->previous = NULL;    }    else    {        newElement = (struct element *)malloc (sizeof(struct element));        newElement->a=a;        tail->next=newElement;        newElement->previous=tail;    }    tail = newElement;    newElement->next=NULL;}`

and the dates of elements (workers) are like the example before...in that row.

FOR SORT:
I tried lot of ways, but nothing work!!!

like that for example:

Code: C

`void sortWorkers(){    element *findElement;    int f=1;    findElement=head;    while(findElement!=NULL)    {        while(f!=0)        {            if(strcmp(head->a.date, findElement->next->a.date)>0)            {                head=findElement;                head->previous->next=findElement->next;                head->next->previous=findElement->previous;                findElement=findElement->next;                f=0;            }          /*    else if(strcmp(findElement->a.date, findElement->next->a.date)<0)            {                head=findElement;                head->previous->next=findElement->next;                head->next->previous=findElement->previous;                findElement=findElement->next;                f=0;            } */            else if(strcmp(head->a.date, findElement->next->a.date)<=0)            {                f=0;            }        }findElement=findElement->next;        f=1;    }}`

or:
Code: C

`void sortWorkers(){    element *find;    element *temp;    find=head;    do    {        if(strcmp(find->a.date, find->next->a.date)<0)        {            temp=find->next->previous;            temp->next->next->previous=find->next->previous;            temp->next=find->next->previous;            temp->next->previous=find->previous;            temp->next->next=find->next->next;            find=temp;        }        else if(strcmp(find->a.date, find->next->a.date)>=0)        {            find=find->next;        }    }while(find->next!=NULL);}`

or other ways, that I haven't yet, but they don't work.

THANKS A LOT!

 DaWei 1Sep2007 05:34

Re: How to sort doubly-linked list? BFP

Sorry, that pale colored text is too hard to read. Further, you haven't put the code inside tags to preserve it's indentation. Let me recommend you read the "BEFORE you make a query" thread. You'll find a link in the upper right corner of this page.

Don't know why you would want to make it difficult for potential helpers, but there you have it.

 duffer 1Sep2007 06:24

Re: How to sort doubly-linked list? BFP

Sorry, but for now, you can just select the text with the mouse. I thought that this colors are the formated C-code...like in other posts :| Sorry again!

 shabbir 1Sep2007 07:03

Re: How to sort doubly-linked list? BFP

Quote:
 Originally Posted by duffer Sorry, but for now, you can just select the text with the mouse. I thought that this colors are the formated C-code...like in other posts :| Sorry again!
You don't need to be doing that manually and in the code block you can specify the language of the code and if its a known language it will be formatted automatically and right now I have done that for you.

 duffer 1Sep2007 23:01

Re: How to sort doubly-linked list? BFP

Quote:
 Originally Posted by shabbir You don't need to be doing that manually and in the code block you can specify the language of the code and if its a known language it will be formatted automatically and right now I have done that for you.
Thanks, for next I would know that.
P.S. Now I test to have two temp elements for each element that will be swapped, but something don't work.
Can anyone help, just to say, can I do that, that I want, is it possible as a logical idea?
Thanks

 DaWei 2Sep2007 00:07

Re: How to sort doubly-linked list? BFP

swap (a, b) => move a to temp, move b to a, move temp to b.

 shabbir 2Sep2007 07:10

Re: How to sort doubly-linked list? BFP

I have not gone through the complete thread but just from the title it seems Sort Linked List article would interest you.

 DaWei 2Sep2007 07:27

Re: How to sort doubly-linked list? BFP

Let me point out that there are at least 3 ways to sort a linked list. Suppose that your inspection of the variables used in the sort criteria indicate that you should swap elements 2 and 7.

You can exchange all the data for elements 2 and 7, except for the links.

You can exchange the links for elements 2 and 7.

You can establish an array of pointers to the elements and merely swap the pointers in the array.

 All times are GMT +5.5. The time now is 21:56.