Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a struct type node:
struct node
{
int data;
node *next;
};
I have a linked list using this node structure. I can sort this linked list using another linked list and copying node values there, but I want to implement a selection sort in the linked list. I have gone through some questions and articles but it is not clear to me whether this is possible. Do I need an index to implement selection sort? If it is possible then how can I proceed?
Selection sort is meant for arrays, vectors, things that have random access iterators and no links like next pointers, (the links create an overhead when swapping elements). For a simple list sort, start with an empty list that will become the sorted list, say this is struct node *sorted = NULL; . Remove nodes one at at time from the original list and insert them into the sorted list (that starts off as an empty list) in sorted order.
Write some code and update your answer with what you've tried so far.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
what would be a better way to find an item in a list rather visiting each node and comparing with the key item.
example:
1->2->3->4->5->6->7->8->9->10->null.
In this list, is there an better way to reach item 6 without visiting each node form zero to 6.
No - with a linked list there is no better option.
There are data structures similar to linked lists which can find the nth item in O(log n), such as skip lists and binary trees. However, in most cases it's a better idea to replace the linked list with a dynamic array.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make a binary search tree in MATLAB just like how you would do it in java or C++, and fill the nodes with characters. I understand that the nodes can be made with a struct, but I can't understand how to connect them so that it becomes a binary tree.
My struct has the following fields: left (for the node with the lower value), value, and right (for the node with a higher value).
You should use an array of your struct, and use the array index instead of a pointer. So with your struct:
node = struct('left',[],'right',[],'value',0);
you can add a node like so:
node(2).value = 1;
node(1).left = 2; % the index of the new node
creating a tree where node(2) is the left child of node(1).
isempty(node(i).left) means that node i has no left child.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Why are Linked Lists Reversed?
I have been listening to a lot of Software Engineers that talk about reversing Linked Lists. What is the use of reversing a linked list? What are the advantages of doing so rather than traversing backwards?
Why are they used in technical interviews?
What is the use besides technical interviews?
As far as I know there are linked Lists aswell as double linked lists. When you are usind double linked lists, every node has a pointer to the next and the previous node so that it is easy to traverse backwards. When the list is "only linked" the nodes only have a "next-reference" so you won't be able to traverse backwards.
I hope that answered the question properly
Reversing a linked list comes up every now and then in real life.
Most recently I did that while implementing a lock-free multi-producer single-consumer work queue. Stacks are the simplest lock-free data structure to implement, and the implementation is a singly-linked list, so my multiple producers would push new work onto a lock free stack.
The single consumer can then get the whole stack at once, but then has to reverse it to make a queue of tasks in the correct order.
That said... you're probably asking the wrong question. The task of reversing a linked list is one of the easiest data structure manipulations you can do. If you're being taught to do it, then it's just an early lesson on the long road to really understanding programming. If you're being asked about it in an interview, then the interviewer wants to know if you might be programmer who can design, implement, and maintain data structures, or if you can just copy and modify answers from StackOverflow, and glue together things you don't understand.
It is very common to reverse lists that are the result of a recursive function. Consider a map function
fn map (fun, lst) =
local fn mp (in, out) =
if null(in)
then out
else mp(tail(in), fun(head(in)) :: out)
return reverse(mp(lst, nil))
It reverses the result of the inner function to restore the result order to the order of the input list.
There are more efficient ways to do this if your language supports mutation, i.e., by keeping a pointer to the last pair in the list and appending a pair on every call to the inner function.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying out the bottom up dynamic programming method and I have a few problems with it.
I have learnt to arrive at the required solution by storing previously computed values in either a 1D or 2D array and referring to them when necessary. The problem is I am not able to backtrack using the values stored in my array.
For example, if the problem is the classic 'Longest Subsequence' problem, I can arrive at the value of the longest subsequence, but I am not able to backtrack through the stored values and find what letters/digits appear in the subsequence.
I have gone through a lot of university course tutorials and youtube tutorials, but nobody seems to explain how a person can 'word' the subproblem correctly.
Does anybody have tips on how to craft the subproblems and maintain array values so that backtracking possible and easy?
A simple solution is to keep a second array with the same dimensions as the first, and call it your index array, and use it to track the location of the element that contributed to your choice.
So in a 2d example:
LetA be the standard dynamic programming array
Let I be the index array
If the value A[x,y] is decided by A[x0,y0], then I[x,y]=(x0,y0).
When trying to backtrack from A[i,j], access I[i,j] to find the next element in the backtrack chain.
You can use default values for the array I so you know when you have reached the end of the chain.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We have an algorithm that compare ruby objects coming from MongoDB. The majority of the time spent, it taking the results (~1000), assigning a weight to them, and comparing them to a base object. This process takes ~2 sec for 1000 objects. Afterwards, we order the objects by the weight, and take the top 10.
Given that the number of initial matches will continue to grow, I'm looking for more efficient ways to compare and sort matches in Ruby.
I know this is kind of vague, but let's assume they are User objects that have arrays of data about the person and we're comparing them to a single user to find the best match for that user.
Have you considered storing/caching the weight? This works well if the weight depends only on the attributes of each user and not on values external to that user.
Also, how complex is the calculation involving the weight associated with a user and the "base" user? If it's complex you may want to consider using a graph database, which can store data that is specific to the relation between 2 nodes/objects.