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
Ive got an exercise due to tommorow and Ive got completely no idea how to do it, even though I know how to add to list, delete an element from list ( last or first ), or display a list, maybe its because I only know how to do it with one variable in struct.
sorry if my translation is not the best but Im translating the exercise from polish.
1.We have a structure
struct point {
double x, y;
};
Create a function that creats lists of consisting of n points
Then after it
Using the list created in previous exercise, create a function that prints on the screen coordinates of points lying inside the given circle. Pointer for the beginning of the list and defining values transfer as function parameters.
I was trying to do it alone but as of now Ive got completely no idea how to approach this. I think as for the first one, I should create a list, head and tail and next,then constructor that sets head and tail to NULL, then function that adds elements to list, then in main ask user for the 'n' value and create for loop with that function.
After that, Ive got completely no idea what to do next. I may have been wrong even until now.
Sorry if I waste your time reading this, I just hope someone can help me and explain me what to do.
Sorry if its also not the place to ask for that kind of help, Im kinda new to all of this.
You should separate the concept of data from the links.
struct Node
{
Point data;
Node * p_previous;
Node * p_next;
};
Later on you may want to make the list into a template so that you can pass any type for the data field.
To get the coordinates from a Node:
Point coordinate = p_node->data;
double x_ordinate = coordinate.x;
double y_ordinate = coordinate.y;
Off-topic: screen coordinates should be integers, not floating point. Usually, pixels are hole element, I haven't heard of real partial pixels.
Related
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 2 years ago.
Improve this question
Given the EleList class how do I go about writing the implementation of the Insert() function?
I've never really used pointers so this simple problem really has me stumped, I would appreciate a link to relevant learning material if possible.
class EleList
{
public:
EleList( char cData )
: m_cData(cData)
, m_Left(0)
, m_Right(0)
{
}
static void Insert( EleList* InsertEle,
EleList* InsertPos);
private:
char m_cData;
EleList* m_Left;
EleList* m_Right;
};
// Insert() function
void EleList::Insert( EleList* InsertEle,
EleList* InsertPos)
{
}
Pointers seem a bit tricky in the beginning. I strongly recommend you to draw in paper the algorithm you want to implement when it involves data structures and pointers, before doing any code. A linked list is a good way to start to learn them.
Into the implementation you can take several approaches, because you have to consider different cases:
Inserting at the beginning of a list or in an empty list: we create the node and swap the left pointer with the head of the list.
Inserting in the middle of a list: we iterate the list with an auxiliary pointer until the desired position, we create the new node and redirect the necessary pointers (the ones of the new node and the ones from the neighbour nodes).
Inserting at the end of the list: we iterate until the last element, create the new node and attach them to each other with the pointers.
You can try to implement one and from there try another. If you are learning, it doesn't have to be perfect, it has to make you understand how pointers and data structures work.
I'm sure you will find this link useful, it's very complete and has an extensive example in code.
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 4 years ago.
Improve this question
EDIT: apparently I asked this question wrong. Before voting to close, please allow me to know what the question is missing. I promise you, this is not an unanswerable question. You can always come back and vote to close it later.
I'm currently working in C++, but I think this question applies to most compiled languages.
In my project, I have an array of values which are calculated individually, one at a time, as late as possible based off a single variable. These values are not all calculated at once, they are calculated if and only if they need to be. As is normally the case when using "dirty", the objective is to label certain things as being in need of update, without updating it preemptively. These values are cycled through over and over, so I'd like to cache the computation if possible. Whenever the single variable changes, all the values should be marked dirty so the cycle knows to recalculate before storing and moving on.
I can think of a few ways of achieving this, but I'm not sure what is most efficient:
Have two arrays, one of booleans and one of values. Mark all booleans to false if dirty, and true when clean.
Have a clean start point. Consider everything dirty until passing that cycle point again. Has the drawback of not allowing skipping of cycle entries.
Brand new array. Just create a new array, if any of the items are unset, set them. This one seems like it would have tons of problems, but it's a thought.
Perhaps use some built in class meant for this stuff?
The above are just the first things that came to mind for me, but I'm kind of new to c++ and would like to have some idea of normal or special solutions to marking an array dirty.
How can I dirty an array efficiently?
In order to show an example of code, I will show js which I'm more used to:
const numbers = [];
const clean = [];
let length = 1000;
let variable;
const setVariable(num) => {
variable = num;
for (let i = 0; i < length; i++) { clean[i] = false; }
}
setVariable(42);
let pos = 0;
while (true) {
if (clean[pos] == false) {
clean[pos] = true;
numbers[pos] = someIntensiveMath(pos, variable);
}
doSomethingWithNumbers(numbers[pos]);
pos++;
if (pos >= length) pos = 0;
// wait a bit;
}
in js you could also do
const setVariable(num) => {
variable = num;
numbers = [];
}
const isDirt = numbers[pos] === undefined;
With js the latter would probably be faster due to the native implementation of the script, but I don't think that's the case with compiled languages. I think you guys do things differently.
I've found elsewhere that the typical way to label entries of an array "dirty" is by having a parallel array of booleans.
#stark mentioned in the comments the idea of using a map, and speed comparisons of the two appear to be pretty decent, but it was advised in the following answer to use an array for indexed items.
performance of array vs. map
Whether or not changes in modern coding have led to a new defacto way of labeling items or parts of an array (or linear collection of items) as "dirty" is unknown. But in the very least, as an answer, the most straight forward nooby way is to have a parallel array of booleans.
Further, depending on the way you are iterating through the "array" it may make sense to use vector or map. In the case of either of those, the form of dirtying would probably be best done by clearing the map or removing vector entries(??).
So, to give my best answer, it would seem one should first find the storage method that best fits their needs, and then use whichever method is most normal for that.
For arrays, as this question was specified towards, parallel arrays appears to be the answer.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to take the first node of my linked list to the end of it,
but so far i couldn't.
the idea that i had is to create a new node that contains the first node's info and then delete the first node, and then add the new node to the end of the list.
this is my code, but it doesn't work because i can't access the start pointer in my list since it's private:
Newnode-> = list.start->next->info;
list.RemoveFront(); //a function to remove the first node.
list.AddBack(Newnode); //a function to add the element to the end of the list.
Are you serious?
There are two cases:
Your linked list has a pointer to the last link (Very useful)
Or you don't.
If you don't have a pointer to the last node, traverse the list and create one.
Here are the operations:
Temp points to first node.
Change Head to point to 2nd Node.
Set the Temp node to point to nil or null.
Make the last node point to the temp node.
Make the Tail pointer point to the temp node.
Please draw this out. Linked lists are always easier to understand when you draw them.
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 9 years ago.
Improve this question
I want to do some operations on a graph. Before I start operations I have tried to construct the graph. But I'm not sure if I did correctly. Can you say if I'm in the right direction?
The input will be like this :
A
B
C
A B 1
A C 2
B C 3
I have put nodes into a char vector like this :
vector<char> nodes;
And I have put edges into a Edge vector like this :
class Edge
{
private :
int length;
char node1;
char node2;
public :
Edge(int l, char n1, char n2);
};
You seem to be on the way to a valid solution.
To my knowledge, there are two canonical approached to implementing graph data structures.
The first is with an edge list, which seems to be the approach you have chosen. The graph structure is represented by a list of edges with optional weights. Note, you may want to add more to this to make for a more efficient structure, depending on the uses for your graph. For example, if you need to find a list of all neighbors of a node quickly, it may be better to store an edge list for each incident node.
A second approach would be to represent the graph as a matrix-like structure where the entry (i,j) is either a boolean representing whether there is an edge from node i to node j.
It is not the efficient way you can implement a graph.
Vertexes and Edges are different entities, so they need to be represented by different data structures.
Like 2 classes : class Vertex and class Edge.
Also concept of cyclic dependency must be used. For further details, please check this site:
http://badboys.7p.com/notes/Graph%20-%20Linked%20Implementation%20(C++).htm
First you need a simple Node class. The node class usually contains a linked list (use a list!) of child nodes. In your case you are using a graph with edges so your nodes should contain linked lists of edges that connect to children. Also, I'm not sure why you are using char to reference your nodes, you should be using a Node class with Node references.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am having trouble figuring out how i can store new object into a vector, and be able to pull that information out.
What i am trying to do is, storing different data from files in a series of objects, then going through these objects and pulling out the information.
I am looking for something like this:
vector<myClass> list;
while( i < nFiles)
{
myClass *temp = new myClass;
list.push_back(temp);
temp->setSomething();
i++;
}
I want to have a different object for every nFile cycle, so i am able to later go through each object and pull the information out from each object.
I've tried pushing the temp to a vector but its giving me nothing but errors.
Is what i'm trying to do programatically correct? I can't get my head around this. Any sort of help would be much appreciated. Thank you.
First a bit of vocabulary: You don't want to store classes in the array (actually, vector), you want to store objects. Objects are instances of classes.
Second, you've got the syntax of the while loop wrong. Look it up in a C++ book. Better use a for loop.
Third, always write MyClass the same way. Don't change lower-/upper case.
And finally, learn about the difference between pointer to objects and objects. The element type you specify when you declare the vector doesn't match the things you put into it.
the syntax is while (...) not (while ...) AND you cant say i=1 in the while loop parameters. What you wanna do is:
either :
int i = 1;
while (i < nFiles){
//Do something
}
OR
for (int i = 1; i < nFiles; i++){
//Do something
}
Your vector should either be a vector of pointers to myClass, i.e.,
vector<myClass *> list;
Or your temp shouldn't be a pointer, i.e.,
myClass temp;
The latter means the whole temp object is copied when you do list.push_back (byte by byte).