I have to create a C++ program that takes in two linked lists and creates a new linked list of nodes from both, but with only one instance of every number. So the output of
list A = 1 --> 3 --> 1 --> 4 and list B = 4 --> 5 --> 12 would be
list C = 1 --> 3 --> 4 --> 5 --> 12. The order of numbers in the new linked list does not matter. What I am having trouble with is how to keep track of each number so that I only add one instance of that number to my new linked list. The only thing I can think is to iterate through the nodes and check if the number is already there. But is there a better way?
Sort each of the two input lists into ascending-order first, then you can just look at the first item at the head of each list, pop the smaller of the two off the head of its list, then append it to the tail of your new list — unless it is equal to the number currently at the tail of your new list, in which case just discard it. Repeat until both input lists are empty.
Related
I would like to find a maximum number of unique elements in path.
(path is from root to leaf).
for example, my tree is as below.
3
/ \
1 2
/\ \
1 3 5
Above tree, the answer will be 3. because there are three paths as below.
3-1-1
3-1-3
3-2-5.
and the unique elements of each path is as below.
3-1
3-1
3-2-5.
therefore the answer is 3.
My idea of how to get the number is as follows.
Firstly, I found all the paths from root to leaf. and when the pointer reached the leaf node, I printed the paths and calculated the unique elements. and iterated this procedure until all nodes were visited.
You can build a second similarly shaped tree that holds the number of unique elements in each subpath (from the root to any node, root and leaves included). That tree can be built from root to leaves like so : the root value is always 1 since the path from root to root contains one unique element, and the value of any other node is either its parent's value or one more.
Exemple with your tree :
3 1
/ \ / \
1 2 => 2 2
/ \ \ / \ \
1 3 5 2 2 3
The value of each leaf is the number of unique elements from the root to that leaf.
Although you could keep it for subsequent uses, you do not actually need to build the tree. You only need to perform a depth-first traversal while keeping track of the unique elements in the current subpath in a data structure, let's say a vector. Since you want the maximum number of unique elements, you need to keep track of the size of that vector when you hit a leaf.
The data structure can be something else than a vector, but it depends on what your elements are. You can probably use an ordered set, which would be equivalent to keeping a vector sorted. If you can hash your elements, you can use a "hashset" (std::unordered_set in C++11). If your elements are simple integers and their values are all within a relatively small range, you can use a vector of booleans instead of a hashset : initially, the vector holds N booleans to false, N being the size of the range your integers are in. Instead of adding and deleting elements, you toggle booleans at the corresponding indices.
I've got a problem and I don't really know how to do it in a proper Scala way.
I've got a list of objects, holding a date. I want to do something like this:
I want to make a selection using an acceptable time value, like 2 hours, between 2 successors in the list. The purpose is to keeps user trend comparing to a point (if he shows up 2 times here, or 1 or 15 !).
The algorithm I imagined:
Let's keep 2 points A and B. We calculate the time difference between the 2 points and then evaluate if it's acceptable or not (>2h, acceptable).
If it's not acceptable, we reject B and then new B is the next list element.
If acceptable, B becomes A and the new B is the next list element.
How to do it, with some filters or collects? Oh and if the algorithm doesn't sounds good for you, I'm open to criticism!
Edit: I'm not asking for the solution, but just the right functions to lookup!
Say I have a list of integers, and I want to step through them and only keep the ones which are more than 1 greater than the previous. I would use foldLeft to step through them, building a list of only the items which are acceptable:
val nums = List(1,2,4,5,7)
nums.foldLeft(List[Int]()){
case (List(), b) => List(b)
case (list, b) if b - list.head > 1 => list :+ b
case (list, b) => list
}
So I've been trying to implement this function in my module and so far I got this:
EXAMPLE 1.
[2,[3,[4,[5,[6,[7,[8,[]]]]]]]]
I am trying to figure out how I can make it look like a proper list, ie:
EXAMPLE 2.
[2,3,4,5,6,7,8].
I know I have to play with Heads and Tails but I am miserably failing at understanding it.
Any help would be appreciated.
Thanks!
Actually in the example 1 you show proper list. List that just consists of 2 elements - number and another list.
Improper list is different thing - for instance [1|2].
You can turn example 1 into example 2 by lists:flatten.
1> M = [2,[3,[4,[5,[6,[7,[8,[]]]]]]]].
[2,[3,[4,[5,[6,[7,[8,[]]]]]]]]
2> lists:flatten(M).
[2,3,4,5,6,7,8]
The root of the problem is how you have built your list. What you have here:
[2,[3,[4,[5,[6,[7,[8,[]]]]]]]]
is not one list but nested lists each of two elements. When you do [Element,List] this does NOT prepend Element to List but builds a new list with Element as the first element and List as the second element. Note that each list is a proper list but you have not built one list but nested lists.
To prepend Element to List you use the syntax [Element | List]. So:
[2|[3|[4|[5|[6|[7|[8|[]]]]]]]]
which builds the list [1,2,3,4,5,6,7,8].
So [Element | List] and [Element,List] are two very different things, the first prepends an element to the beginning of a list while the second builds a new list of two elements. There is no direct way of appending an element to a list without rebuilding the list.
Not as obvious as it looks at first, but this is a manual way of doing what lists:flatten/1 does (in this particular case, its more interesting otherwise):
proper(L) -> proper([], L).
proper(A, [H|[T]]) -> proper([H|A], T);
proper(A, []) -> lists:reverse(A).
As a part of the programming Assignment, I have to maintain a linked list in text file. I am pretty convenient with Linked List datastructure, but not so much with files in C++. Can some one give me an idea or overview of how to approach it. I should be able to add or delete the linked list, also able to add or delete the nodes in the linked list or else and should reuse the space that was deleted on one linked list. Each list has a number(integer), all nodes are of same size, contain integer.
My idea would be,
1) maintain a file with numbers(that contain linked List numbers)
0 - NULL
1 - head_offset for_linked_list_num 1
0 - NULL
1 - head_offset_for_linked_list_num 3
1 - head_offset_for_linked_list_num 3
1 - head_offset_for_linked_list_num 3
etc
where -1 is the termiator indication, 1 in a position indicates the ith location has an location associated with it
2) open another file and maintain the linked list like this
data next_offset
data next_offset
data NULL
By doing this, I can keep track of the linked list and efficiently add/delete/display an array.
For doing in C++ what are the functions I need to know and learn. I have very less time and I can code thinking it as an basic level of functions. Please advise. Thanks in advance
1 solution could be to have each list it's own file. The number would be the file name on disk. Inside of the file the first line is always the head node. Each line would be two peices of data. The node data followed by the line in the file for the next node. This would allow you to reuse empty spaces that have been deleted. (a -1 for the next node line would be the end of the list. For example:
1.txt:
(data) (next node lines number)
18 2
32 4
4 -1
5 3
So this link list would be 18 -> 32 -> 5 -> 4
For the what functions to use question, you might consult:
http://www.gnu.org/software/libc/manual/html_node/I_002fO-on-Streams.html#I_002fO-on-Streams
A read through this section may take 15 minutes and covers the basics.
Given a linked list of T size , select first 2n nodes and delete first n nodes from them; Then do it for the next 2n nodes and so on...
For example-
Let's consider a linked list of size 7:
`1->2->3->4->5->6->7`
If n = 2, the desired output is :
`1->2->5->6->7`
I didn't understand what this problem is actually indicating.Could somebody help me to understand the problem ?
EDIT : Adding C and C++ tags so that this may reach to more eye balls, and of-course those are only two languages allowed in the interview itself.
That actually looks like it should say:
Given a linked list of T size , select first 2n nodes and delete last n nodes from them; Then do it for the next 2n nodes and so on...
or:
Given a linked list of T size , select first 2n nodes and keep first n nodes from them; Then do it for the next 2n nodes and so on...
That would mean select 1,2,3,4 then delete 3,4 (or keep 1,2 which is the same thing). Then select 5,6,7,8, not possible so stop.
I think it's even simpler than #paxdiablo indicates ...
do
take n
skip n
until you run out of elements to take or skip