C
C#2y ago
bialasik__

❔ LinkedList

List<string> people = new()
{
"Xavier", "Alex",
"James", "Laura",
"Adil", "Cheryl",
"Charlie"
};
LinkedList<string> classList = new();

char[] alpha = "bcdefghijklmnopqrstuvwxy".ToCharArray();
foreach(string person in people)
{
LinkedListNode<string> firstNode = classList.First;
person.ToLower();
if (person.StartsWith('a'))
{
firstNode = classList.AddFirst(person);
}
else if (person.StartsWith('z'))
{
classList.AddLast(person);
}
else
{
for (int i = 0; i < alpha.Length; i++)
{
if (person.StartsWith(alpha[i]))
{
if (firstNode == null)
{
firstNode = classList.AddFirst(person);
}
classList.AddAfter(firstNode, person);
}
}
}
}
List<string> people = new()
{
"Xavier", "Alex",
"James", "Laura",
"Adil", "Cheryl",
"Charlie"
};
LinkedList<string> classList = new();

char[] alpha = "bcdefghijklmnopqrstuvwxy".ToCharArray();
foreach(string person in people)
{
LinkedListNode<string> firstNode = classList.First;
person.ToLower();
if (person.StartsWith('a'))
{
firstNode = classList.AddFirst(person);
}
else if (person.StartsWith('z'))
{
classList.AddLast(person);
}
else
{
for (int i = 0; i < alpha.Length; i++)
{
if (person.StartsWith(alpha[i]))
{
if (firstNode == null)
{
firstNode = classList.AddFirst(person);
}
classList.AddAfter(firstNode, person);
}
}
}
}
I then tried:
foreach(string person in people)
{
LinkedListNode<string> firstNode = classList.First;
person.ToLower();
if (person.StartsWith('a'))
{
classList.AddFirst(person);
}
else
{
for (int i = 0; i < alpha.Length; i++)
{
if (person.StartsWith(alpha[i]))
{
classList.AddLast(person);
}
}
}
}
foreach(string person in people)
{
LinkedListNode<string> firstNode = classList.First;
person.ToLower();
if (person.StartsWith('a'))
{
classList.AddFirst(person);
}
else
{
for (int i = 0; i < alpha.Length; i++)
{
if (person.StartsWith(alpha[i]))
{
classList.AddLast(person);
}
}
}
}
4 Replies
bialasik__
bialasik__2y ago
I need to get names from a list and add them to a LinkedList<> without using Sort() or OrderBy()
DaVinki
DaVinki2y ago
If you’re trying to get the sorted linked list, you could sort the list yourself first and then turn it into a linked list
ChucklesTheBeard
your teacher is probably aiming for an "insertion sort" - that search term should help
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.