Thread: Double Link List Swap function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    57

    Double Link List Swap function

    K here is my swap function. The only problem i have is what if you want to swap the last node it crashes. Plz someone help i have been tryong to figure out a way to get that in like a week now. And we are not spostu swap the data of the node but the full node thanks. here is waht i got any tips be great.
    oops forgot the first and last node cause of the NULL and it is different senario and beside that forgot to say taht the last pointer is last first pointer is first.

    Code:
    void swap(int nodea,int nodeb)
                 {
                      if (nodea>nodeb)
                      {
                          int Temp;
                          Temp=nodeb;
                          nodeb=nodea;
                          nodea=Temp;
                      }
                      
                      Node* NodeA=first;
                      Node* NodeB=first;
                      Node* temp1;
                      Node* temp2; 
                     
                      for (int x=0;x<nodea;x++)
                          NodeA=NodeA->next;
                      for (int x=0;x<nodeb;x++)
                          NodeB=NodeB->next;
                          
                      if ((nodea+1)==nodeb||(nodea-1)==nodeb)
                      {
                          NodeB->next->prev=NodeA;
                          NodeA->prev->next=NodeB;
                          NodeB->prev=NodeA->prev;
                          NodeA->next=NodeB->next;
                          NodeA->prev=NodeB;
                          NodeB->next=NodeA;
                      }
                      else
                      {
                
                          temp1=NodeA->next;
                          temp2=NodeB->prev;
                          
                          NodeA->next=NodeB->next;
                          NodeB->prev=NodeA->prev;
                          
                          NodeB->next=temp1;
                          NodeA->prev=temp2;
                          
                          NodeA->next->prev=NodeA;
                          NodeA->prev->next=NodeA;
                          
                          NodeB->next->prev=NodeB;
                          NodeB->prev->next=NodeB;
                       }
                          
                 }
    Last edited by kennny2004; 04-12-2006 at 11:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM