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

Threaded View

  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

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