A question about lists in C# or Java :)

Click For Summary

Discussion Overview

The discussion revolves around a homework problem related to implementing an algorithm using linked lists in C# or Java. Participants explore the requirements of the problem, including the need to sum the two largest numbers in a list and remove them iteratively until the list is empty. The conversation touches on the efficiency of different data structures and algorithms.

Discussion Character

  • Homework-related
  • Debate/contested
  • Technical explanation

Main Points Raised

  • Some participants express confusion about the homework question and seek clarification on how to approach it.
  • One participant suggests that the task involves summing the two largest numbers in the list and removing them, repeating this process until the list is empty.
  • Another participant questions the practical application of the algorithm, suggesting that linked lists may not be the best choice and proposing the use of vectors instead.
  • Some participants argue that implementing the algorithm with linked lists is essential, despite its inefficiency, and discuss the time complexity of O(n^2) associated with linked lists.
  • There are mentions of alternative approaches, such as using merge sort, which could achieve O(n log(n)) complexity, but the focus remains on linked lists as per the homework requirements.
  • Participants emphasize the importance of attempting to write code independently before seeking complete solutions from others.
  • One participant notes that both Java and C# have linked list classes and suggests exploring the methods available in these classes as a starting point.

Areas of Agreement / Disagreement

Participants express a mix of agreement and disagreement regarding the best approach to the homework problem. While there is a consensus that linked lists should be used, opinions vary on the efficiency and practicality of the algorithm, as well as the appropriateness of using vectors instead.

Contextual Notes

Some participants highlight the limitations of the problem, such as the potential inefficiency of the algorithm when using linked lists and the lack of real-world applications for the proposed approach.

Who May Find This Useful

This discussion may be useful for students learning about data structures, particularly linked lists, and those interested in algorithm implementation in C# or Java.

dan2222
Messages
6
Reaction score
0
Homework Statement
'lst' is a linked list that contains different integers from each other. The number of values in the list is even.
Write a method that takes a list, builds a linked list called 'sumq' in which each value includes two of the largest numbers in the list for the set we have not used so far.
It means that one number should not be used more than once. The action must return the 'sumq' list.
For example, the list: lst = {5, 8, 2, -3, 7, 10, 20, 4}
The list that the method will return is: sumq = {30, 15, 9, -1 }
No need to keep the original list.


My own explanation...
It means that the first value of the list 'sumq' is the sum of the 2 largest numbers in the list 'lst' and so on
Relevant Equations
-
I could not understand the question, I have really tried to solve it but I could not.
I will appreciate any direction to the solution. Thanks in advance:)
 
Physics news on Phys.org
dan2222 said:
It means that the first value of the list 'sumq' is the sum of the 2 largest numbers in the list 'lst' and so on
I think you have this right. But notice it says that "one number should not be used more than once". So after taking the sum of the two largest numbers, you remove these numbers from the original list 'lst'. Then you repeat the process until 'lst' is empty.
 
This is an odd homework question. I suppose the point is to understand linked lists? But no real world application should implement the algorithm in this way. Ideally you would just use vectors, and treat the input as a heap.
 
It can be done with linked lists. It would work like an insert-sort in reverse.
At the core, it's asking you to sort the list and then just combine every pair of entries.
 
  • Like
Likes   Reactions: phyzguy
Halc said:
It can be done with linked lists. It would work like an insert-sort in reverse.
At the core, it's asking you to sort the list and then just combine every pair of entries.
Sure, but the algorithm is necessarily O(n^2) with a list. See how it runs with a million or so inputs.
 
valenumr said:
Sure, but the algorithm is necessarily O(n^2) with a list. See how it runs with a million or so inputs.
I don't think the homework is asking to evaluate the quality of the problem. The OP should just implement the problem as requested.
 
phyzguy said:
I don't think the homework is asking to evaluate the quality of the problem. The OP should just implement the problem as requested.
I think dumping the list into a vector will be easier, as well as more efficient.
 
Thank you all for the response!
 
willem2 said:
I think dumping the list into a vector will be easier, as well as more efficient.
How would you grade a homework problem where you asked the students to use linked lists, and someone returned a program using vectors? Would you give them full credit? I would not.
 
  • Like
Likes   Reactions: Mark44
  • #10
Anyone might have written the full code?
 
  • #11
valenumr said:
Sure, but the algorithm is necessarily O(n^2) with a list. See how it runs with a million or so inputs.
An extract-sort is, sure, but I can think of O(n log(n)) ways to do it, still using nothing but linked lists.
As pointed out by others, I don't think finding a more complicated but scalable algorithm is the point of this exercise, but doing it with linked lists IS the point.
 
  • #12
dan2222 said:
Anyone might have written the full code?
That's not what we do here. Take a stab at writing the code yourself and we'll give you feedback, but we don't provide complete answers to posted questions.
 
  • #13
Mark44 said:
That's not what we do here. Take a stab at writing the code yourself and we'll give you feedback, but we don't provide complete answers to posted questions.
I know. I have really tried to, but I just don't know. Anyways, thank you
 
  • #14
dan2222 said:
I know. I have really tried to, but I just don't know. Anyways, thank you
Both Java and C# have linked list classes. What methods do these classes expose to programmers? That might be a good start. Have there been any similar examples shown in class?
 
  • #15
dan2222 said:
I know. I have really tried to, but I just don't know. Anyways, thank you
If you, "have really tried to," please post the code you have tried. We can give feedback and advice.
 
  • #16
public static Node<int> MaxSequenceDelete(Node<int> lst)
{
Node<int> p = lst;

Node<int> del = p.GetNext(); while (p != null)
{
if (p.GetValue() < p.GetNext().GetValue())
{
p.SetNext(del.GetNext());
del.SetNext(null);
}

p = p.GetNext();
}
return del;
}
 
  • #17
valenumr said:
Sure, but the algorithm is necessarily O(n^2) with a list. See how it runs with a million or so inputs.
Merge sort for linked lists has time complexity O(n log(n)). For Java, you'll need to implement your own linked list, as Java's native linked list provides no equivalent of C++ std::list::splice(), which is used to move nodes within a list or from one list to another.
 
  • Like
Likes   Reactions: valenumr
  • #18
phyzguy said:
How would you grade a homework problem where you asked the students to use linked lists, and someone returned a program using vectors? Would you give them full credit? I would not.
If the end result is a linked list, as specified in the problem, I see no problem.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K