How to implement linked list in C/C++ with out using
struct node{
int data;
struct node *next;
}
just using two arrays, one for data, other for next node
Sure you can. There is some discussion about the topic here: Is a Linked-List implementation without using pointers possible or not?
One implementation would be to create an array, and store the nodes in the array. Wikipedia has a good pseudo-code example: http://en.wikipedia.org/wiki/Linked_list (Linked lists using arrays of nodes section)
You define two arrays
1.Data Array
2.Index Array
every data array will contain data value and corresponding index in Index array will contain next node to be pointed by the value element
you need to keep track of the last value added because it gives the index of Index array where you can add an index of the next value pointed by that value node.
Adding a data value is also easy just add a new value in the data array and
fill the previous element value in Index array by the index of the data value in data element.
Data Array 2,5,3,1,2.
Index Array 1,2,3,4,0 (0 means it is not pointing to any data element)
add element: 2,5,3,1,2,77.
Index Array 1,2,3,4,5,0.
delete element: 2,5,0,1,2,77
Index Array 1,3,0,4,5,0.
Related
a=[1,2,3,4,5,6,7,8]
w=a.collect{|i| i%2==0}
p w
The result is coming to be
[false,true,false,true]
why?
When i am doing
w=a.collect{|i| i+2}
Result is an array like
[3,4,5,6,7,8,9,10,11]
Why? What am i doing wrong?
You don't need map / collect (they are aliases), you need
array.select(&:even?)
or
array.reject(&:odd?)
These methods filter original array, unlike map that performs actions with every element and returns new array of the same length
That's why you get boolean array same length instead of filtering
Please read more in docs:
Array#map, Array#select, Array#reject
I have the following situation:
I have an object
This object has an item that is an object again
The second object contains array of objects with ids
Can I filter only the parent objects that have subobjects that contain something in particular in the array on the bottom of the tree?
Try this:
$filter=nameOfObjectArrayProperty/any(o: contains(o/id, 'some-alue'))
or
$filter=nameOfObjectArrayProperty/any(o: o/id eq 'some-value')
It's also important to note that "/any" can be replaced with "/all" if you want all the subobjects to match your filter criteria.
So, any reason behind happen this things, because when i perform sort() function with let type at that time xcode give me a compile time error
i tried it in my playgound with only sort function and sort() function with other higher order function.
like so
let name = ["mehul","HeLLi","JeniFER","Ankul"]
name.sort()
error :
Cannot use mutating member on immutable value: 'name' is a 'let' constant
but with other higher order functions
let nameStartingWithBArray = name.filter {$0.first != "B"}.map {$0.uppercased()}.sorted()
now there is no error
Please consider the subtle but crucial difference between sort and sorted
sort has no return value. It sorts in place that means the calling object is mutated.
sorted ( as well as filter and map) returns a new object with the result of the operation.
sort() basically works with the list itself. It modifies the original list in place. The return value is None.
name.sort() sorts the list and save the sorted list.
sorted() works on any iterable that may include list, dict and so on. It returns another list and doesn't modify the original list.
sorted() returns a sorted copy of the list (a new sorted list), without changing the original list.
Keynote : sort() is faster than sorted()
The sort function sorts the collection in place - i.e. it modifies the array, name, on which you call sort.
filter on the other hand returns an array and does not modify the original array.
Compare sort with sorted which again returns a new array and does not modify the original array.
So, you can write
let sortedNames = name.sorted()
sortedNames will be in order, while name will be in the original, unsorted, order.
If you write
var name = ["mehul","HeLLi","JeniFER","Ankul"]
name.sort()
then name will be in order, and because it is declared as var rather than a constant with let the change is permitted.
The sort func you are using is the mutating func, that means sort function internally sort the array in the same variable.
Mutating Func : To modify the properties of your structure or enumeration within a particular method, you can opt in to mutating behavior for that method. The method can then mutate (that is, change) its properties from within the method, and any changes that it makes are written back to the original structure when the method ends.
So. let doesn't allow sort for doing mutating, so you need to make it var.
More ever, if you use sorted(by:) , it returns new array of sorted values. So here you don't get any error.
Is there an easy way to sort an array of custom objects (in this case Lists) into a dictionary based on a particular property of each list.
[List1, List2, List3, List4, List5];
For example, each List object has an NSString type property, which can be either "MyList","Sent","Received"
How would I create a dictionary based on these properties so that I have a dictionary like so:
"MyList" -> array of lists with MyList as their type property [List1, List5];
"Sent" -> array of lists with Sent as their type property "Received" [List3;
"Received" -> array of lists with Received as their type property [List2, List4];
I'd really rather not loopthrough my entire array of List objects if possible
You need to iterate the array and build your dictionary. If the key isn't already there, create the array and add it to the dictionary, then add the new item to it.
You could alternately use predicates to filter the array into sub-arrays and build the dictionary like that but it's a similar amount of code (for a few options) and doesn't support automatic future expansion when you have another value for the key you're organising on.
How to create an array in VTL and add contents to the array? Also how to retrieve the contents of the array by index?
According to Apache Velocity User Guide, right hand side of assignments can be of type
Variable reference
List item
String literal
Property reference
Method reference
Number literal
ArrayList
Map
You can create an empty list, which would satisfy all your needs for an array, in an Apache Velocity template with an expression like:
#set($foo = [])
or initialize values:
#set($foo = [42, "a string", 21, $myVar])
then, add elements using the Java add method:
$foo.add(53);
$foo.add("another string");
but beware, as the Java .add() method for the list type returns a boolean value, when you add an element to the list, Velocity will print, for instance, "true" or "false" based on the result of the "add" function.
A simple work around is assigning the result of the add function to a variable:
#set($bar = $foo.add(42))
You can access the elements of the list using index numbers:
<span>$foo[1]</span>
Expression above would show a span with the text "a string". However the safest way to access elements of a list is using foreach loops.
Creating an array is easy:
#set($array = [])
Putting an element into an array is also easy:
$array.add(23)
Getting an element from an array depends from your Velocity version.
In Velocity 1.6 you must use
$array.get($index)
Since Velocity 1.7 you can use the classic form:
$array[$index]
I haven't created an array in VTL but passed arrays to VTL context and used them. In VTL, you can not retrieve array contents by index, you only use foreach, as example this code is copied from my Dynamic SQL generation VTL Script:
#foreach( $col in $Columns ) SUM($col.DBColumn) AS ''$col.Name''#if($velocityCount!=$Columns.Count), #end #end
For this reason, we also can not have 2D arrays. When I needed an array to store 2 objects in a row, I used the workaround of defining a new class, and putting objects of that class in the single dimensional array.