I'm new to C programming and I kinda need some help in the frontBackSplitLinkedList part,
For example, assume that given linked list is: 2 6 7 8 9
The resulting front and back are:
front: 2 6 7
back: 8 9
I've searched through some websites but most of the codings are using nodes instead of linked list.
Any idea how to do this? Thank you!
You do not actually have to count elements in list, as far as i see it, you store the number of elements in _linkedlist. You can always use that value to split however you like. I would strongly suggest to first simlify your List, so it can add elements only to the back or the front of the List, it will be easier to work with it, but won't really affect the principle of split function.
If I understand your question correcly then you want to split the list in the middle, where un uneven list will be split a frontlist that is one larger than the backlist.
First of all you must have a count of the list length. I propose that you walk the list, counting the number of list nodes.
Then you divide that number as follows:
count = (count+1)/2;
Now we need to know if you must create two new lists (so you have to make copies of all the list nodes), or whether the old list may be reused. Assume it may be reused, then you assign the old list to frontlist, walk count listnodes, asign that one to backlist and set the next member of the one before it to NULL.
You are done now, except that the caller may no longer use the original list (it has become equivalent, and actually is, frontlist).
What I am mainly trying to do is to create a variable in which I can assign, within a stratum of my sample (defined by an 'id' variable, for instance), a name that is associated with the highest frequency (in the stratum) of this same name in another (string) variable. If tabulate* would work the way I need it to work, my code would run like this:
gen new_class_within_id=""
forvarlues i=1/80 {
tab class_var, matcell(x) if id==`i'
svmat x
sum x2
local name =x1 if x2==r(max)
replace new_class_within_id=`name' if id==`i'
}
That would be the general idea if tabulate would permit storing the unique observation names in a matrix -- the code might have some unintended errors too, of course. But while it does not seem to be possible using the above code, I thought that I could use mkmat if I would be able to store, in the loop, the unique observations inside a vector with some additional coding. Would that be possible? Also, is there an easier way to perform what I want to do?
*Firstly, I thought that using tabulate and extracting the results into a matrix would do the work that I need, but tabulate does not allow me to extract the names of the observations, just the frequencies. tabulate seemed nice because in its output it shows the unique observations of a variable in a column, but I could not find a way to extract those observations the way the output shows.
I think I understand your question, but maybe I don't. Some code:
clear
set more off
input ///
id str1 anothvar
1 a
1 a
1 a
1 b
1 m
2 c
2 c
2 m
2 a
2 z
end
list, sepby(id)
*-----
bysort id anothvar : gen count = _N
bysort id (count): gen newvar = anothvar[_N]
list, sepby(id)
More work needs to be done if you have missings and/or ties.
This does not seem possible, so my guess is I am thinking of how to handle my data incorrectly, not simply that I can't find a way to handle it the way I envision.
I wanted to create a 3-dimensional array; I am thinking of this like a relational database. All dimensions would have the same number of rows, but they have different number of columns.
For example,
dimension 1 = 1 column, 20 unique location identifiers (rows)
dimension 2 = 3 columns of abiotic variables associated with the 20 unique locations (rows)
dimension 3 = 2 columns of biotic variables associated with the 20 unique locations (rows)
I was thinking this would be the best way to set up my data so that I could go through each column easily and combine within abiotic or biotic easily just using subscripting (e.g., I could apply a function across all varibles (columns) in the 2nd (abiotic) dimension (e.g., dat[,,2] = log(dat[,,2]), which might be different than something I want to do to all biotic variables.
This is a not a treatment group type of analysis, so I don't want stacked data (where I repeat the unique locations 5 times with multiple treatment options for each location - that makes no sense to me for these data, e.g.,
loc type var
1 ab 1
1 ab 2
1 ab 3
1 bi 1
1 bi 2
I am frozen in the way I am thinking about these data. In the past I've always used arrays and the subscripting is so intuitive to me. Can you indeed make an array that has different number of columns in each dimension? If not, what am I missing about how to handle these data?
Thanks for any thoughts.
Yes. You can do as you are describing with
# build a 20 x 3 x 2 array
dat <- array(data, c(20,3,2))
# example of applying functions on a given dimension
log(dat[,,2])
There is no requirement that the dimensions of the array be equally sized.
I need a solution where I have one "master" list/array that has a number of sequentially ordered linked children, each one representing a sub-segment of the parent list. It resembles the "unrolled linked list" pattern, but here the segment-list size should be dynamic.
Here I will try to explain further. I would like to find out if there's an established term for this kind of data structure/pattern (in the same manner of "graph", "binary tree" etc.) that would be to my help when further investigating this, trying to find the best implementation.
Let's say we have a "master" list with a size of ten items, 0-9, and with three children a, b and c representing sub-segments of the master in the following way:
"master" -------------------
0-9 0 1 2 3 4 5 6 7 8 9
===== ========= ===
"children" a b c
0-2 3-7 8-9
Ideally, the solution should allow
the master to create and adjust sub-segment size of it's children (depending on rules connected to the master list data items content)
the children to change their sub-segment size, causing the linked siblings to adjust their size/positions accordingly
handle under- and overflow of total children sizes compared to master size
Any blogs, articles, code snippets etc that tackles something like this would be to great help! (My solutions will be created in php and as3, but language doesn't matter here).
Thanx!
If I understand correctly what you want. Build your data structure the other way around. List (or vector) of children nodes and each keeps a list to master node with backlink. Changing sub-segment size in such case would be simple moving of master nodes from one list to another.
1 -> 2 -> 3 -> 4 -> 5
\ / / \ /
a----- b
I totally understand the purpose of arrays, yet I do not feel I have "mastered" them. Does anyone have some really good problems or readings involving arrays. I program in PHP and C++ so if there are examples with those languages that would be preferable but is not necessary.
Draw everything out on graph paper.
Memory is just little boxes, it's a lot clearer to see on paper with a few arrows than in some complex markup language
Define 'mastered'.
Does anyone have some really good problems or readings involving arrays.
Try array based
Linked-list implementation.
Implement stacks, queues, etc and then use stack(s) to emulate a queue etc
Heaps
A lot of people seem to struggle with the concept of arrays at first, particularly arrays of >2 dimensions.
It's a little too abstract. However, getting past that initial block just requires a concrete exploration of the mechanics. So, here's an example I've used a lot that shows the basic mechanics in a nerd-friendly way:
Concrete Example (in psuedocode)
Let's say you're building a role playing game and you want to keep track of your character's stats. You could use an array of integers like this:
Stats(0) could be strength
Stats(1) could be dexterity
Stats(2) could be intelligence
...And so on.
Now let's add a level of complexity. Maybe we want to introduce a potion of strength that increases strength by 5 for 10 turns. We could represent the stat side of that by making this into a 2-dimensional array:
Stats(0, 0) - this is my current strength.
Stats(0, 1) - this is my normal strength.
Stats(0, 2) - this is the number of turns until the strength potion wears off.
Stats(1, 0) - this is my current dexterity.
...And so on. You get the idea.
So I've added a 2nd dimension to hold details about the 1st dimension. What if I wanted our Stats array to handle statistics for more than one character? I could represent that by making this into a 3-dimensional array:
Stats(0, 0, 0) - character 0's current strength.
Stats(1, 1, 0) - character 1's current dexterity.
It would be even better to create some constants or enums to eliminate magic numbers from the code:
Const Strength = 0
Const Dexterity = 1
Const Intelligence = 2
Const CurrentValue = 0
Const NormalValue = 1
Const PotionTurns = 2
Then I could do:
Stats(1, Dexterity, NormalValue) = 5 'For character 1, set the normalvalue of dex to 5.
A few more thoughts about arrays... At least in the .Net world where I live, most of us don't have to use them in our day-to-day lives too often because they're slowly being relegated to underpinnings for more elaborate data structures like collections.
In fact, if I were to implement character stats, realistically I would not use arrays.
However, it's still important to get your head around them because they are rocket-fast and there are definitely cases where they're invaluable.
Try manually (no built in methods) sorting with arrays (bubblesort is a good one to get yourself going)
An array is a contiguous block of memory devoted to N items of the same type, where N is a fixed number indicating the number of items. In order to expand an array (as they are fixed in size), you can use the C function realloc(..). In C++, you can manually re-allocate an array by creating a new, larger array and copying the contents of the old array into the new one (an expensive operation).
Modern languages have options that supplant arrays (so as to overcome the inherent limitations of arrays). In C++, you can use the Standard Template Library (STL) for this purpose; the STL "vector" data type is a typical replacement for standard arrays in C++. Platform-dependent APIs like MFC have built-in constructs like the CArrayList for a richer array usage experience. ;-)
I'm going to try to explain the best i can arrays in their fundamental form.
Ok, so an array is basically a way to store data. For instance if you want a list of shopping items, we would use a one dimensional array:
[0] - "Bread"
[1] - "Milk"
[2] - "Eggs"
[3] - "Butter"
.
.
.
[n] - "Candy"
Each index 0,1,2,3,...n holds a specific data. The data as you can see are represented as Strings. Now i can't use something like :
[n+1] = 1000
because this will put an integer as index n+1, the compiler will tell you that it's no good and you need to fix that issue.
Moving on to matrices or 2-dimensional arrays. Take a piece of squared paper like the ones you use for math and draw a Cartesian system and some dots. Put the coordinates on different piece of paper and next to them put a 1. For example:
[0,0] = 1
[0,1] = 1
[2,3] = 1
What this means is that at index [0,0],[0,1],[2,3] i have 1. A representation would be like so:
Cartesian System in form of a matrices :
1) 2) 3)
1) 1 1 0
2) 0 0 1
3) 0 0 0
I used just simple arrays to illustrate what are they, for example if you want to go 3D the data structure for that would be a array of matrices aka a list which holds each Cartesian location at a specific height.
If we had 10 as height and the same dots as above it would be something like:
[10,0,0] - 1
[10,0,1] - 1
[10,2,3] - 1
If you want a way to master: grab a simple list of problems and try to implement them using arrays of any kind. There is no quick way to do it, just have patience and practice.
Alright, so a one-dimensional array is just a grouping of variables. Useful if you've got a lot of something, and you like to save time and space. Functionally,
element1:=5;
element2:=6;
element3:=7;
...
is the same as saying
element[1]:=5;
element[2]:=6;
element[3]:=7;
...
except now the computer knows what you're talking about and you can write something like:
for i:=1 to n do
element[i]:=element[i]+1;
Moving on, a two dimensional array is somewhat more complicated, but can be thought of as an array of arrays. So we could have something like this:
type
arrayA=array[1..50] of integer;
arrayB=array[1..50] of arrayA;
arrayB=array[1..50,1..50] of integer; //an equivalent declaration in Pascal to the above two
More specifically, a two-dimensional array is a table. So for example, if arrayA contains the grades of a student in 50 classes, then arrayB represents the grades of a bunch of students. So, arrayB[3,5] would be the grade of the third student on class number 3.
It's easy to expand the same logic to add another dimension to the array:
arrayC=array[1..50] of arrayB;
We could say that C represents a school, so arrayC[2,4,6] is the grade of the second schools fourth student in class 6.
Now, what are arrays used for? Storing groups of similar information, or information which'll need to be processed in bulk. In practice, though, you'll mostly be using a one-, at most two-, dimensional array. If you can imagine your data as a table, you'll almost definitely want an two-dimensional array, for example. How else would you represent a chess board, if you had to?
Three-dimensional arrays tend to be used less, but what if you had to save some sort of state for each of your table elements? Something like:
Table=array[1..50, 1..50, 0..1] of integer;
Then the third value is 1 if some condition is true for a given element, and 0 otherwise. Of course, a trivial example, but it's another way of understand three-dimensional arrays more easily.