CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Bubble Sort Issue

    I am trying to implement a bubble sort algorithm in a linked list. The code runs to completion, but when I try to print it, I get the following error: Unhandled exception at 0x01222833 in LinkedList.exe: 0xC0000005: Access violation reading location 0xfeeefef6. I have included the relevant code below.

    Code:
    void dlist::BsortUp()
    {
    	node *j = new node();
    	node *d = new node();
    	bool unsorted = true;
    
    	while (unsorted)
    	{
    		j = this -> front;
    		d = this -> front -> next;
    		unsorted = false;
    
    		while (d != NULL)
    		{
    			if (j -> data > d -> data)
    			{
    				exchange(j,d);
    				unsorted = true;
    			}
    			j = j -> next;
    			d = d -> next;
    		}
    	}
    
    	delete j;
    	delete d;
    }
    
    void dlist::exchange(node *node1, node *node2)
    {
    	node *node3 = new node();
    	node3 -> data = node1 -> data;
    	
    	node1 -> data = node2 -> data;
    	node2 -> data = node3 -> data;
    
    	delete node3;
    }
    
    void dlist::printForwards()
    {
    	node *j = this -> front;			//cursor points at the first node node::Vtype he list
    	
    	while (j != NULL){
    		cout << j -> data << endl;		//print cursor's data
    		j = j -> next;					//shift the cursor to the next node in the list
    	}
    
    	delete j;
    	cout << endl;
    }

    The code is trying to sort this:

    35
    10
    15
    50
    25
    20

    and outputs:

    10
    15
    20
    25
    35
    -2.65698e+303

    and then gives me an error.

    If anyone sees my problem, I would be so grateful. Thanks
    Last edited by Marc G; April 9th, 2010 at 07:27 AM. Reason: Added code tags

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Bubble Sort Issue

    Quote Originally Posted by jduhamel View Post
    I am trying to implement a bubble sort algorithm in a linked list. The code runs to completion, but when I try to print it, I get the following error: Unhandled exception at 0x01222833 in LinkedList.exe: 0xC0000005: Access violation reading location 0xfeeefef6. I have included the relevant code below.
    Did you use the debugger?

    I suggest you do this, since debugging is part of the process of learning how to program. Once you use the debugger, what did you notice that isn't working correctly?

    Secondly, please use code tags when posting code. The code you posted is unformatted and almost unreadable.

    Third, why are you creating new nodes just to swap data? For example this is not neccessary:
    Code:
    void dlist::BsortUp()
    {
    node *j = new node();
    node *d = new node();
    //...
      delete j;
      delete d;
    Here is all you need.
    Code:
    void dlist::BsortUp()
    {
        node *j;
        node *d;
       // no delete necessary
    You're going to assign j and d later on in the loop, so what's the "new node" supposed to accomplish? Maybe you have a misunderstanding of what "new" is supposed to do in C++.

    Code tags:

    [code] Your code goes here [/code]

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 8th, 2010 at 11:48 PM.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Bubble Sort Issue

    The inner while loop of your sort increments both J and D. This is wrong.
    I'll leave it up to you to figure out why and how to fix it

Tags for this Thread

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

Featured