Results 1 to 30 of 30

Thread: Create Bubble Sort Function For Linked List

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Create Bubble Sort Function For Linked List

    That's my entired code to rand up several numbers, now I need a bubble sort to organize the list... the trick is I cant use NO arrays... Hmmm... any help?
    Last edited by fallnwrld; Feb 15th, 2002 at 08:31 PM.
    -RaY
    VB .Net 2010 (Ultimate)

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    off topic: since you're already using C++, why don't you put that into a class?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Argh

    I'm new to C++, I'm having complications with this as it is, to put it in class would just make it more trouble some for me.

    I understand the concept. It's like an array... you can remove and add elements using the function i can see that... and if used the ++ command instead of skipping to the next integer number in an array type situation it skips every 4 bytes being that an interger is 4 bytes so I know how to navigate through the code, I just can't come up with it functioning and i've trade for 4 days. This bothers me, and was hoping someone on here can help. Thanks.
    -RaY
    VB .Net 2010 (Ultimate)

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    you could use the list in STL
    PHP Code:
    #include <list>
    using namespace std;

    ...

    list<
    intmylist;

    ...

    mylist.sort(); 
    btw if you're new to C++ then you should learn the basics first, check out the Faq for tutorials.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Nope

    I am required to create a bubbleSort Function
    ::Sighs:: VB is so much more fun!

    i get a error for if (pList => pList + 1){..... I want to see if the variable in pList is greater then or equal too the next number on the list then put higher one in a temp file, then put lower one in its place, then put the number in the temp file into where the lower number is.

    void bubbleSort(const LinkedList *const pList){
    if (pList != NULL){
    Node *pTemp = pList->pHead;
    if (pList => pList + 1){



    }


    }
    -RaY
    VB .Net 2010 (Ultimate)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Yes I'm new

    I also want to be VB programmer. Yet my degree requires me to take this c++ class. The class is once a week and was kind of tough for him to go over pointers in one day. Now take home assignments are 50% of grade... i need to get a C or higher in this class or im screwed.
    -RaY
    VB .Net 2010 (Ultimate)

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Angry Frustrating

    What I'm trying to do is put first number in list in pTemp1 and second number in list in pTemp2... then compare and switch.. i get 3 compile errors with this code

    C:\LinkedList\LinkList.cpp(131) : error C2166: l-value specifies const object
    C:\LinkedList\LinkList.cpp(133) : error C2059: syntax error : '>'
    C:\LinkedList\LinkList.cpp(133) : error C2143: syntax error : missing ';' before '{'
    Error executing cl.exe.
    If you know the problem, help me out....

    LinkList.obj - 3 error(s), 0 warning(s)


    void bubbleSort(const LinkedList *const pList){
    if (pList != NULL){
    Node *pTemp1 = pList->pHead;
    Node *pTemp2 = pList->pHead++;

    if (pTemp1 => pTemp2 + 1){


    }
    }


    }
    -RaY
    VB .Net 2010 (Ultimate)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344
    fixed one of errors

    void bubbleSort(const LinkedList *const pList){
    if (pList != NULL){
    Node *pTemp1 = pList->pHead;
    Node *pTemp2 = pTemp1->pNext;

    if (pTemp1 => pTemp2 + 1){


    }
    }


    }
    -RaY
    VB .Net 2010 (Ultimate)

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    I'm flooding this board ;o\

    -> is the same as => correct?

    if (pTemp1 -> pTemp2 + 1){

    and i get this error C:\LinkedList\LinkList.cpp(133) : error C2039: 'pTemp2' : is not a member of 'Node'

    i see why its giving me that error, but how are they able to write give pTemp2..NEVERMIND they can because they are making it NULL and i'm trying to assign it a value...
    -RaY
    VB .Net 2010 (Ultimate)

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    -> is dereferencing your pointer and selecting the member
    A->B
    is the same as
    (*A).B
    if B is not a member of A then it gives you that error, brb
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    if (pTemp1 -> pTemp2 + 1){

    means that

    if pTemp2 member of pTemp1 is not -1

    since if expects a boolean and the aritmetical 0 is false, otherways true.

    bubblesort iterates the list and switches the next element with the current if it is larger (or smaller), this continues until no switches are performed.

    sorry, if you put your code within code tags and indent, i might also have look at it.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Here is File thanks for your help, i'll try what u said

    ..
    Last edited by fallnwrld; Feb 15th, 2002 at 08:12 PM.
    -RaY
    VB .Net 2010 (Ultimate)

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344
    PHP Code:

    void bubbleSort
    (const LinkedList *const pList){
        if (
    pList != NULL){
          
    Node *pTemp1 pList->pHead;     
          
    Node *pTemp2 NULL;
          
    pTemp2 pTemp1->pNext;
          
    int Temp3 0;    
              if (
    pTemp1 -> pTemp2 1){ 
                
    pTemp3 pTemp1;
                
    pTemp1 pTemp2 1;
                
    pTemp2 pTemp3;
        
              }
        }



    -RaY
    VB .Net 2010 (Ultimate)

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    if u dont want to download

    For those who are helping I keep changing the bubbleSort function instead of creating a reply to save load time when trying to help me.

    PHP Code:
    #include <iostream> 

    using std::cout
    using std::endl

    #include <assert.h> 
    #include <ctime> 

    //LIST NODE 
    struct Node

        
    int data
        
    Node *pNext
    }; 

    //SINGLY LINKED LIST STRUCTURE 
    struct LinkedList

        
    Node *pHead
        
    int size
    }; 

    //RETURNS A NEW LINKED LIST 
    // 
    // Returns 
    // Returns a pointer to an empty linked list 
    LinkedList *createLinkedList(); 


    // INSERTS A NEW NODE AT THE HEAD OF THE LINKED LIST 
    // 
    // Input parameters: 
    // pList pointer to the linked list to receive the new element 
    // element data value to be assigned to the new node 
    void insertAtHead(LinkedList *const pList, const int element); 


    // PRINTS DATA VALUES OF A LINKED LIST TO THE SCREEN 
    // 
    // Example output: 
    // 12 34 56 69 24 2 15 
    // 
    // Input Parameters: 
    // pList pointer to the linked list to be printed 
    void print(const LinkedList *const pList); 

    void bubbleSort(const LinkedList *const pList);
    // REMOVES THE LINKED LIST FROM MEMORY 
    // 
    // Input Parameters: 
    // pList pointer to the linked list to be removed 
    void destroy(LinkedList *pList); 


    // DRIVER 
    int main(){ 
    //Create linked list 
    LinkedList *pList createLinkedList(); 
    int size 10

    srand(time(0)); 

    //Insert elements onto list 
    int valuei
    for(
    i=0;i<size;i++)

         
    value rand()%100
         
    cout << value << " "

         
    insertAtHead(pListvalue); 

    cout << endl

    print(
    pList); 

    //IMPLEMENT BUBBLE SORT 
    bubbleSort(pList); 

    print(
    pList); 

    //clean up 
    destroy(pList); 
    pList NULL

    return 
    0



    //RETURNS A NEW LINKED LIST 
    LinkedList *createLinkedList(){ 
    LinkedList *pList = new LinkedList// allocated memory 
    assert(pList); // test for memory error 

    //initalize list members 
    pList->pHead NULL
    pList->size 0

    return 
    pList



    // INSERTS A NEW NODE AT THE HEAD OF THE LINKED LIST 
    void insertAtHead(LinkedList *const pList, const int element)

         if(
    pList != NULL// test pointer is non-null 
         
    {     
              
    Node *pNode = new Node// allocate memory 
              
    assert(pNode); // test for memory error 

              //initalize node members 
              
    pNode->data element
              
    pNode->pNext pList->pHead

              
    pList->pHead pNode// insert at head of list 
              
    pList->size++; 
         } 



    // PRINTS DATA VALUES OF A LINKED LIST TO THE SCREEN 
    void print(const LinkedList *const pList)

         if(
    pList != NULL)
         { 
              
    Node *pTemp pList->pHead
              while(
    pTemp != NULL)
              { 
                   
    cout << pTemp->data << " "
                   
    pTemp pTemp->pNext
              } 
          
    cout << endl
         } 



    void bubbleSort(const LinkedList *const pList)
    {
        if (
    pList != NULL)
        {
          
    Node *pTemp1 pList->pHead;     
          
    Node *pTemp2 pTemp1->pNext;
          
    Node *pTemp3 NULL;
          
    int intCounter 0;          
              
              while (
    intCounter <= 30)
              {
                   if (
    pTemp1 -> pTemp2 1)
                   { 
                       
    pTemp3 pTemp1;
                       
    pTemp1 pTemp2 1;
                       
    pTemp2 pTemp3;
                   }
              
    intCounter++;
              }
        }


    }

    // REMOVES THE LINKED LIST FROM MEMORY 
    void destroy(LinkedList *pList)

         if(
    pList != NULL)
         { 
              
    Node *pTemp1 pList->pHead
              
    Node *pTemp2 NULL

              
    // Remove nodes from memory starting from the head of the list 
              
    while(pTemp1 != NULL// test for non-null 
              
    {
                  
    pTemp2 pTemp1->pNext
                  
    delete(pTemp1); // deallocate memory 
                  
    pTemp1 pTemp2
              } 

          
    // Remove list from memory 
          
    delete(pList); 
         } 

    Last edited by fallnwrld; Feb 15th, 2002 at 08:37 PM.
    -RaY
    VB .Net 2010 (Ultimate)

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344
    got it down to two errors

    Compiling...
    LinkList.cpp
    C:\LinkedList\LinkList.cpp(144) : error C2039: 'pTemp2' : is not a member of 'Node'
    C:\LinkedList\LinkList.cpp(11) : see declaration of 'Node'
    C:\LinkedList\LinkList.cpp(148) : error C2106: '=' : left operand must be l-value
    Error executing cl.exe.


    im thinking I don't have to have a pNext if I have pTemp2 + 1;

    see what I mean? Both where design to do the same thing if anything its going to skip to the 3rd one.
    -RaY
    VB .Net 2010 (Ultimate)

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    PHP Code:
    void bubbleSort(const LinkedList *const pList){
        if (
    pList != NULL){
            
    bool f;
            do{
                
    f=false;
                
    Node *pTemp1 pList->pHead;     
                
    Node *pTemp2 pTemp1->pNext;
                if (
    pTemp1->data pTemp2->data){
                    
    pList->pHead pTemp2;
                    
    pTemp1->pNext=pTemp2->pNext;
                    
    pTemp2->pNext =pTemp1;
                    }
                
    Node *pTemp3 pTemp1;
                while (
    pTemp2->pNext){
                    
    pTemp2=pTemp2->pNext;
                    
    pTemp1=pTemp1->pNext;
                    if (
    pTemp1<pTemp2){
                        
    pTemp3->pNext pTemp2;
                        
    pTemp1->pNext pTemp2->pNext;
                        
    pTemp2->pNext =pTemp1;
                        
    f=true;
                        }
                    
    pTemp3=pTemp3->pNext;
                    }
                }while(
    f);
            }
        } 
    There you go, I haven't tested it yet though, have fun
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  17. #17
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    doesn't he want to test the values behind the pointers, not the pointers themselves?
    if (*pTemp1 < *pTemp2){
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by CornedBee
    doesn't he want to test the values behind the pointers, not the pointers themselves?
    if (*pTemp1 < *pTemp2){
    actually the member data of of the dereferenced value (in node)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Thanks for ur help

    Got this error, I'm sure I'll figure it out though. Thanks for all your help!
    Compiling...
    LinkList.cpp
    C:\LinkedList\LinkList.cpp(148) : error C2166: l-value specifies const object
    Error executing cl.exe.
    -RaY
    VB .Net 2010 (Ultimate)

  20. #20
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    which line is that?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    line 148 is(marked in code)

    pList -> pHead = pTemp2; (Marked in code)

    JUST THE FUNCTION, FULL CODE BELOW
    _________________________________________
    PHP Code:
    void bubbleSort(const LinkedList *const pList)
    {
        if (
    pList != NULL)
        {
            
    bool f;
            do
            {
                
    f=false;
                
    Node *pTemp1 pList -> pHead;     
                
    Node *pTemp2 pTemp1 -> pNext;
                if (
    pTemp1 -> data pTemp2->data)
                {
                    
    pList -> pHead pTemp2//I am line 148
                    
    pTemp1 -> pNext pTemp2 -> pNext;
                    
    pTemp2 -> pNext pTemp1;
                }
                
    Node *pTemp3 pTemp1;
                while (
    pTemp2 -> pNext)
                {
                    
    pTemp2 pTemp2 -> pNext;
                    
    pTemp1pTemp1 -> pNext;
                    if (
    pTemp1 pTemp2)
                    {
                        
    pTemp3 -> pNext pTemp2;
                        
    pTemp1 -> pNext pTemp2 -> pNext;
                        
    pTemp2 -> pNext pTemp1;
                        
    f=true;
                    }
                 
    pTemp3=pTemp3->pNext;
                 }
            }while(
    f);
        }  

    ALL THE CODE
    ______________________________________________

    PHP Code:

    #include <iostream> 

    using std::cout
    using std::endl

    #include <assert.h> 
    #include <ctime> 

    //LIST NODE 
    struct Node

        
    int data
        
    Node *pNext
    }; 

    //SINGLY LINKED LIST STRUCTURE 
    struct LinkedList

        
    Node *pHead
        
    int size
    }; 

    //RETURNS A NEW LINKED LIST 
    // 
    // Returns 
    // Returns a pointer to an empty linked list 
    LinkedList *createLinkedList(); 


    // INSERTS A NEW NODE AT THE HEAD OF THE LINKED LIST 
    // 
    // Input parameters: 
    // pList pointer to the linked list to receive the new element 
    // element data value to be assigned to the new node 
    void insertAtHead(LinkedList *const pList, const int element); 


    // PRINTS DATA VALUES OF A LINKED LIST TO THE SCREEN 
    // 
    // Example output: 
    // 12 34 56 69 24 2 15 
    // 
    // Input Parameters: 
    // pList pointer to the linked list to be printed 
    void print(const LinkedList *const pList); 

    void bubbleSort(const LinkedList *const pList);
    // REMOVES THE LINKED LIST FROM MEMORY 
    // 
    // Input Parameters: 
    // pList pointer to the linked list to be removed 
    void destroy(LinkedList *pList); 


    // DRIVER 
    int main(){ 
    //Create linked list 
    LinkedList *pList createLinkedList(); 
    int size 10

    srand(time(0)); 

    //Insert elements onto list 
    int valuei
    for(
    i=0;i<size;i++)

         
    value rand()%100
         
    cout << value << " "

         
    insertAtHead(pListvalue); 

    cout << endl

    print(
    pList); 

    //IMPLEMENT BUBBLE SORT 
    bubbleSort(pList); 

    print(
    pList); 

    //clean up 
    destroy(pList); 
    pList NULL

    return 
    0



    //RETURNS A NEW LINKED LIST 
    LinkedList *createLinkedList(){ 
    LinkedList *pList = new LinkedList// allocated memory 
    assert(pList); // test for memory error 

    //initalize list members 
    pList->pHead NULL
    pList->size 0

    return 
    pList



    // INSERTS A NEW NODE AT THE HEAD OF THE LINKED LIST 
    void insertAtHead(LinkedList *const pList, const int element)

         if(
    pList != NULL// test pointer is non-null 
         
    {     
              
    Node *pNode = new Node// allocate memory 
              
    assert(pNode); // test for memory error 

              //initalize node members 
              
    pNode->data element
              
    pNode->pNext pList->pHead

              
    pList->pHead pNode// insert at head of list 
              
    pList->size++; 
         } 



    // PRINTS DATA VALUES OF A LINKED LIST TO THE SCREEN 
    void print(const LinkedList *const pList)

         if(
    pList != NULL)
         { 
              
    Node *pTemp pList->pHead
              while(
    pTemp != NULL)
              { 
                   
    cout << pTemp->data << " "
                   
    pTemp pTemp->pNext
              } 
          
    cout << endl
         } 



    void bubbleSort(const LinkedList *const pList)
    {
        if (
    pList != NULL)
        {
            
    bool f;
            do
            {
                
    f=false;
                
    Node *pTemp1 pList -> pHead;     
                
    Node *pTemp2 pTemp1 -> pNext;
                if (
    pTemp1 -> data pTemp2->data)
                {
                    
    pList -> pHead pTemp2//I am line 148
                    
    pTemp1 -> pNext pTemp2 -> pNext;
                    
    pTemp2 -> pNext pTemp1;
                }
                
    Node *pTemp3 pTemp1;
                while (
    pTemp2 -> pNext)
                {
                    
    pTemp2 pTemp2 -> pNext;
                    
    pTemp1pTemp1 -> pNext;
                    if (
    pTemp1 pTemp2)
                    {
                        
    pTemp3 -> pNext pTemp2;
                        
    pTemp1 -> pNext pTemp2 -> pNext;
                        
    pTemp2 -> pNext pTemp1;
                        
    f=true;
                    }
                 
    pTemp3=pTemp3->pNext;
                 }
            }while(
    f);
        }  
    }


    // REMOVES THE LINKED LIST FROM MEMORY 
    void destroy(LinkedList *pList)

         if(
    pList != NULL)
         { 
              
    Node *pTemp1 pList->pHead
              
    Node *pTemp2 NULL

              
    // Remove nodes from memory starting from the head of the list 
              
    while(pTemp1 != NULL// test for non-null 
              
    {
                  
    pTemp2 pTemp1->pNext
                  
    delete(pTemp1); // deallocate memory 
                  
    pTemp1 pTemp2
              } 

          
    // Remove list from memory 
          
    delete(pList); 
         } 

    Last edited by fallnwrld; Feb 18th, 2002 at 02:01 AM.
    -RaY
    VB .Net 2010 (Ultimate)

  22. #22
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    remove the const before LinkedList*const, dereferencing that pointer will still leave the Linkedlist immutable (thats why you can't assign a member)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    yeah

    I moved it out of the function, and I moved it out of the prototype for the function. It compiles! lol, thanks a lot for your help. It doesn't sort the list though for some reason. I'm tinkering with the algor. now, and it's helping me learn. If it's something you can spot off back let me know, I appreciate it.
    -RaY
    VB .Net 2010 (Ultimate)

  24. #24
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Code:
             if (pTemp1 -> data < pTemp2->data)
                {
                    pList -> pHead = pTemp2; //I am line 148
                    pTemp1 -> pNext = pTemp2 -> pNext;
                    pTemp2 -> pNext = pTemp1;
                }
                Node *pTemp3 = pTemp1;
                while (pTemp2 -> pNext)
                {
                    pTemp2 = pTemp2 -> pNext;
                    pTemp1= pTemp1 -> pNext;
                    if (pTemp1 < pTemp2)
    *snip*
    That last line should be as the abovemost (note how terrifying this design model is when you need to locate the mistyped bug)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    the above line must be top most?

    Not sure what you meant by that. From the snippet you show me the only difference in code I saw was indentation. That shouldn't matter because the {} tell you when that statement starts or ends. I kept looking at code, and then you said last line must be top most... the last line in snippet you showed me was the if statement, so maye you where saying that if statement should come first.... that didn't help as well. Hopefully Bork will be a little easier to understand ;oP lol. Don't give up on me yet, I will get this!
    -RaY
    VB .Net 2010 (Ultimate)

  26. #26
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    BORK will definitely be easier to get the hang of

    I meant that you need to dereference and get the member data, becase whats the point in comparing two pointers, it even differs each time you run it!

    replace

    if (pTemp1 < pTemp2)

    with

    if (pTemp1 -> data < pTemp2->data)
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    Yeah

    Yeah I actually tried that... this is function... (OFF TOPIC: is Bork going to be a visual language such as VB?) None of the numbers are being sorted, not even partially. ;o\


    PHP Code:

    void bubbleSort
    (LinkedList *const pList)
    {
        if (
    pList != NULL// if Null skip bubbleSort 
        
    {
            
    bool f//Create boolean to exit while loop
            
    do
            {
                
    f=false//set boolean value to false
                
    Node *pTemp1 pList -> pHead;     
                
    Node *pTemp2 pTemp1 -> pNext;
                if ( 
    pTemp1 -> data pTemp2 -> data //if < then enter if statement
                
    {
                    
    pList -> pHead pTemp2
                    
    pTemp1 -> pNext pTemp2 -> pNext;
                    
    pTemp2 -> pNext pTemp1;
                }
                
    Node *pTemp3 pTemp1;
                while ( 
    pTemp2 -> pNext)
                {
                    
    pTemp2 pTemp2 -> pNext;
                    
    pTemp1 pTemp1 -> pNext;
                    if ( 
    pTemp1 -> data pTemp2 -> data )
                    {
                        
    pTemp3 -> pNext pTemp2;
                        
    pTemp1 -> pNext pTemp2 -> pNext;
                        
    pTemp2 -> pNext pTemp1;
                        
    true//to exit while loop
                    
    }
                 
    pTemp3 pTemp3 -> pNext;
                 }
            }while(
    f);
        }  

    -RaY
    VB .Net 2010 (Ultimate)

  28. #28
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    What a nice idea Bork could actually take streamed data directly from an IDE, which would allow designing visual concepts a lot easier.

    btw, put "f = true;" in the first if with the head, it's probably why it's not sorting, in case it finds the first then it might just exit the whole thing
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    argh

    I put f=true; up and down that bad boy, no luck what so ever. ;o\


    I try to put debug statements to print when it reaches a statement... It appears to be stuck in a endless loop somewhere.

    It says
    Gets to first while statement
    Gets to first while statement
    Gets to 2nd if statement
    KEEPS DOING THAT OVER AND OVER!

    PHP Code:

    void bubbleSort
    (LinkedList *const pList)
    {
        if (
    pList != NULL// if Null skip bubbleSort 
        
    {
            
    cout << "its not null" << endl;
            
    bool f//Create boolean to exit while loop
            
    do
            {
                
    cout << "gets to the do statement." << endl;
                
    f=false//set boolean value to false
                
    Node *pTemp1 pList -> pHead;     
                
    Node *pTemp2 pTemp1 -> pNext;
                if ( 
    pTemp1 -> data pTemp2 -> data //if < then enter if statement
                
    {
                    
    cout << "gets to 1st if statement" << endl;
                    
    pList -> pHead pTemp2
                    
    pTemp1 -> pNext pTemp2 -> pNext;
                    
    pTemp2 -> pNext pTemp1;
                }
                
    Node *pTemp3 pTemp1;
                while ( 
    pTemp2 -> pNext)
                {
                    
    cout << "gets to 1st while statement." << endl;
                    
    pTemp2 pTemp2 -> pNext;
                    
    pTemp1 pTemp1 -> pNext;
                    if ( 
    pTemp1 -> data pTemp2 -> data )
                    {
                        
    cout << "gets to 2nd if statement." << endl;
                        
    pTemp3 -> pNext pTemp2;
                        
    pTemp1 -> pNext pTemp2 -> pNext;
                        
    pTemp2 -> pNext pTemp1;
                        
    true//to exit while loop
                    
    }
                 
    pTemp3 pTemp3 -> pNext;
                 }
            }while(
    f);
        }  

    Last edited by fallnwrld; Feb 18th, 2002 at 04:05 PM.
    -RaY
    VB .Net 2010 (Ultimate)

  30. #30
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    It just might be that the list is not correctly intialized, that it does not end with a NULL pNext pointer?
    You could also output the pointer values (the addresses they point to) to find this out. If the same pointer value comes out over and over you have a problem.
    Have you tried to live debug it?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width