Jump to content

C: Fresh pair of eyes

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
thieflock

thieflock

    Newbie

  • Members
  • PipPip
  • 29 posts
So I was on here earlier as WingedPanther gave me some advice but I think my problem is more than that.

I am passing the head of my linked list to the following function. This function is supposed to work like a bubble sort, swapping the nodes when necessary. After the function is called and printed nothing has changed.

The swap function:


void bubble_sort_title(Box temp, int nodes, FILE *outputp) 

{		

	Box head = temp, p1 = NULL, p2 = NULL, p3 = NULL, p4 = NULL;

	int i, j;

	

	//int nodes is used as the nodes size

	for(i = 0; i < nodes; i++) {

		

		temp = head;

		

		for(j=0; j < nodes; j++) {


			if(strcmp(temp->title, temp->next->title) > 0) {


				if (temp->prev == NULL) { //if head is to be swapped

					p1 = temp;

					p2 = temp->next;

					p3 = p2->next;

					p1->next = p3;

					p2->next = p1;

					p3->prev = p1;

					p1->prev = p2;

					p2->prev = NULL;

					head = p2;

					temp = head;

					temp = temp->next;

				} else if (temp->next->next == NULL) { //if tail is to be swapped

					p1 = temp;

					p2 = temp->prev;

					p3 = temp->next;

					p2->next = p3;

					p3->next = p1;

					p1->next = NULL;

					p1->prev = p3;

					p3->prev = p2;

					temp = temp->next;

				} else if (temp->prev != NULL && temp->next->next != NULL) { //if middle is to be swapped

					p1 = temp->prev;

					p2 = temp;

					p3 = temp->next;

					p4 = p3->next;

					p1->next = p3;

					p4->prev = p2;

					p3->next = p2;

					p2->prev = p3;

					p3->prev = p1;

					p2->next = p4;

					temp = temp->next;

				}

			}

		}

	}

}


What is wrong with me logic?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It looks like you are NOT using pointers. The result of that is you are creating and manipulating copies of nodes, not the nodes themselves.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
thieflock

thieflock

    Newbie

  • Members
  • PipPip
  • 29 posts
Thanks WingedPanther.