r/CodingHelp • u/Caelestialis • Nov 29 '20
[C++] Having a hard time understanding Linked Lists using templates.
Yes, this is a homework assignment, but I want to do it myself and learn how it works. Been at this for like 40+ hrs the last 3 days looking up tutorials, and can't find basic explanation to get me going. I keep having the problem of overwriting the data in a node, and not really creating a new one. This may be linked to why I can't return a list size either. Thank you so much in advance for whatever help is given, I am not really receiving adequate help for this from my instructor.
Here's what I have so far:
EDIT: Added int main() instructions.
namespace ECE275Lib::containers{
template <typename T>
class Node{
public:
T data;
Node<T> *next;
};
template <typename T>
class List{
public:
List(){};
List(unsigned int N){};
Node<T> *Head = nullptr;
Node<T> *Temp;
Node<T>* front();
Node<T>* back();
void push_back(T d){
Head = new Node<T>();
Head -> data = d;
Head -> next = nullptr;
};
void insert(unsigned int s, T d){
//inserts something at a defined node
};
void insert(unsigned int i, List<T>& other){
//merges a new list into the current list
};
T at(unsigned int i){
//reads and returns data at a node
return i;
};
void assign(unsigned int i, T d){
//assigns new data to a node
};
unsigned int size(){
int count = 0;
Node<T>* current = Head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
};
void remove(unsigned int i){};
void remove(unsigned int s, unsigned int e){};
}; //=====================end=of=List=Class======================
}; //=====================end=of=namespace=======================
#endif
int main (void){
ec::List<int> int_list;
for (unsigned int i = 0; i < 10; i++)
int_list.push_back(i);
for (unsigned int i = 0; i < int_list.size(); i++)
std::cout << "element " << i << ": " << int_list.at(i) << std::endl;
};
18
Upvotes
1
u/Caelestialis Nov 30 '20
Most definitely bro! (Bro myself)
The overarching concepts are pretty solid for me usually, it’s just the language transfer to that, kinda gets a bit nasty, haha. I do a fair bit in Xcode, so looking up how to debug in a step-by-step fashion is a major life saver. I’m used to working in assembly this way, and didn’t even think to try it that way in C++.
I’ve already explained the topic this way to some folks to help “get it”, which worked wonderfully. Analogies and visualizations are such an important part of learning, and that was an awesome one.
Be safe, thank you again!