Inserting elements into a linked list C++ [closed] - c++

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.

Related

Can someone help me with an exercise? C++ Lists [closed]

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.

Remove from the beginning of std::vector [closed]

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 6 years ago.
Improve this question
I have a vector of the following data structure
struct Rule {
int m_id = -1;
std::wstring name;
double angle;
};
std::vector<Rule>& topPriorityRules;
and I am erasing the first element of the vector using
topPriorityRules.erase(topPriorityRules.begin());
Is there any other good alternative for removing elements from the front of a std::vecor?
Given
std::vector<Rule>& topPriorityRules;
The correct way to remove the first element of the referenced vector is
topPriorityRules.erase(topPriorityRules.begin());
which is exactly what you suggested.
Looks like i need to do iterator overloading.
There is no need to overload an iterator in order to erase first element of std::vector.
P.S. Vector (dynamic array) is probably a wrong choice of data structure if you intend to erase from the front.
Three suggestions:
Use std::deque instead of std::vector for better performance in your specific case and use the method std::deque::pop_front().
Rethink (I mean: delete) the & in std::vector<ScanRule>& topPriorityRules;
Use std::vector::erase() (see Caleth's comment below).

How to move the first node to the end of the list in linked list c++ [closed]

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.

The fastest dynamic data structure in C++ [closed]

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
I've got a task which mainly consist in adding or removing elements from an array in C++. Since arrays ain't dynamic but operations on them are very fast, I've been looking for a dynamic data structure which is nearly as fast to operate. I've been thinking about std::vector but since it is predefined and quite massive construct I'm afraid about time of the operations which is crucial for me. Could Anybody provide me with some information about Your point of view? I'd be very glad for any help from You!
edited:
I'm really sorry I haven't included all important point in my question; below I'd try to add more info:
I'll be traversing elements of the structure many times and access them in a random manner so operation on elements on every possible positions are possible
I think that there will be (depending on tests provided) many operations on elements in the middle of the data structure as well as near its "brims".
I believe that will help my post to be more clear, specific and, thus, more useful for others.
Thank You for all the answers!
Refer to Mikael Persson's "container choice" diagram:
http://www.cim.mcgill.ca/~mpersson/pics/STLcontainerChoices.png
The different data structures were implemented in the STL to be used for different reasons. Therefore the structures differ when it comes to insertion/deletion speeds at the start, the middle or the end of the structures or even when it comes to the random access of the structure elements.
A nice short comparison of STL containers:
http://john-ahlgren.blogspot.com/2013/10/stl-container-performance.html
If it's possible for you to use an associative array, maps at least guarantee an insertion/look-up time of O(log n) which is a good bit faster for large amounts of data/lots of insertions and deletes than vector's guarantee of O(n) for non-back insertions.
Not sure if they will work here or not, this link also shows some graphs of benchmarks using random insert/removes/searches/fills/sorts, etc. on several different containers:
http://www.baptiste-wicht.com/2012/12/cpp-benchmark-vector-list-deque/
Lastly, a flow chart from SO that could help you decide on a container:
In which scenario do I use a particular STL container?
While not perfect, it still might turn out that a vector is your best bet.
Will a linked list implemented using an array meet your needs?
class AList
{
public:
AList()
{
for (int = 0; i != 256; ++i )
{
nodes[i].prev = (i-1+256)%256;
nodes[i].next = (i+1)%256;
}
}
int const& operator[](int index)
{
// Deal with the case where nodes[index].isSet == false
return nodes[index].data;
}
// Not sure what the requirements are for adding
// and removing items from the list.
//
// add();
// remove();
private:
struct Node
{
Node() : data(0), prev(0), next(0), isSet(false) {}
int data;
unsigned char prev;
unsigned char next;
bool isSet;
};
Node nodes[256];
};

Graph implementation in C++ [closed]

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.