1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

linear register

Discussion in 'C' started by logistic, Jun 6, 2007.

  1. logistic

    logistic New Member

    Joined:
    Jun 6, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Hi, i'm sorry for bothering you, i wanted to arrange numbers (ascending), i tought everything is ok, but... where's the problem? Thanks...
    Code:
    void sort(){
    elementi *temp = first, *next, *t1, *t2;
    
    for(next = temp->next; next !=NULL; next = temp->next){
    next=temp->next;
    if(temp->value > next->value) {
    
    if(next!=last){
    t1=temp;
    t2=next;
    t1->next=next->next;
    temp=t2;
    next=t1;}
    
    else { t1=temp;
    t1->next=NULL;
    t1=last;
    temp=temp->next;
    temp->next=t1;
    }}
    
    else {  
    temp = temp->next;
    }}
    };
    
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    You should provide us with some more data like what is the content of the struct/class elementi?

    I guess you are messing with the swap of the nodes and you can refer to Swap two nodes of a linked list
     
  3. logistic

    logistic New Member

    Joined:
    Jun 6, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    It looks like that, but, for example i enter values 1, 5, 4, 10 -> i suppose to see out -> 1, 4, 5 10, but i see just -> 1, 5, 10 :confused:
    (Thanks, i also tried the way you offered in 'Swap two nodes of a linked list', but there was the same problem) :(


    Code:
    class klase {
          struct elementi {
                          int value;            //value of node
                          elementi *next;
                          };
          elementi *current, *last, *first;
          public :
    
                 klase(){last = current = first = NULL;}
                            
                 ~klase() { izdzest(); };
         
          
    void sort(){
         elementi *temp = first, *next, *t1, *t2;
    
         for(next = temp->next; next !=NULL; next = temp->next){
                  next=temp->next;
              if(temp->value > next->value) {
    
              if(next!=last){
                t1=temp;
                t2=next;
                t1->next=next->next;
                temp=t2;
                next=t1;}
    
             else { t1=temp;
                 t1->next=NULL;
                 t1=last;
                 temp=temp->next;
                 temp->next=t1;
    }}
    
             else {  
               temp = temp->next;
    }}
    }; 
     
  4. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    You have messed up with the issue and here is the code for sorting the link list.
    Code:
    void sort()
    {
    	elementi *current, *next, *temp, *previous;
    	
    	for(previous = first,current = first; current->next != NULL; current = current->next)
    	{
    		next = current->next;
    
    		if(current->value > next->value) 
    		{
    			previous->next = next;
    			current->next = next->next;
    			next->next = current;
    		}
    		if(previous != current)
    		{
    			previous = current;
    		}
    
    	}
    }
     
  5. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
  6. logistic

    logistic New Member

    Joined:
    Jun 6, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for help :)
     
  7. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    My pleasure.
     

Share This Page