#sorting linked list

13 messages · Page 1 of 1 (latest)

crisp robinBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
opaque tulip
#

Sorting a circular list seems strange. Doesn't the circular property prevent sorting since technically every element is behind every other element?

#

A debugger helps for these situations where you step through the code and see what the values are and can check which step messes up.

shrewd swift
#

It doesn't necessarily prevent sorting since you would adjust the pointers if you need to swap the nodes. Ill try debugging it to see exactly where it's getting stuck, but so far, it runs fine as long as the first/head node isn't the one getting swapped out

#

I did try to search it up too and people said similar things that it's weird to sort a circular list, but yea i'm not sure

opaque tulip
#

Did I misunderstand what a circular linked list is?
1 -> 2 -> 3 ->
The 3 points back to 1. How would you sort this?

shrewd swift
#

that's correct. I'm not sure if I'm right but this is what I'm guessing:
If the list you gave me was adjusted as: 1 -> 3 -> 2
2 would point back to 1 (head)
During the sort I would have to swap 3 and 2 to make it: 1 -> 2 -> 3
To make sure it stays in the condition of circular linked list, I would traverse through it to make sure the last node points back to the head node

#

I

opaque tulip
#

So, it's like a regular linked list, you just ignore the pointer back to the head.

#

For sorting purposes I mean.

#

Instead of the usual nullptr to express that the list ended you use head instead.

shrewd swift
#

Yea basically. So while i'm traversing in a regular linked list, I would check if the node points to nullptr as an indication of the list ending, but here, Im checking that I landed back at the head of the list