Start New Discussion within our Software Development Community

I have an assignment where I need to Sort the linked list according to Int Variable after the user has input the information.

When I try using bubble sort I'm stuck because I don't know how go back to the beginning.

If a user enters : 5 4 3 2 1

When Sorting it makes : 4 3 2 1 5

but then I'm stuck at 5 because I'm at the end of the linked list so I can't finish my Bubble Sort. (I can't use any builtin function or template because I need to learn this)

btw: I need to Sort the way they point to each other not by changing their Int value

void Sort_List(LinkList *list,int count)
{
	LinkList *temp;

	temp=list;

// for(int j=0;j<count-1;j++)
//{
				
		for(int i=0;i<count-1;i++)
		{			
				if (list->var>list->next->var)
						{
							list=list->next;
							if (list->next!=NULL)
							{
							temp->next=list->next;
							}
							else
							{
								temp->next=NULL;
							}
							list->next=temp;
								
			
						}					

					list=list->next;			

		}	

		
//}



}

Please help me learn this. Thanks

Usually, a linked-list class would have a way to access at least one end of the list. For example, std::list has begin() and end() methods. If you're using your own linked-list class, you will have to write in methods to access the beginning and end of the list.

Alternatively, if your linked-list class if doubly-linked, then you can go backwards through the list the same number of elements as you originally went forward. I think I'd prefer to define a begin() and end() method though, because this has other uses too.

This article has been dead for over six months. Start a new discussion instead.