I know there's a lot of guys here that know c++ very well. I think I'm asking a simple question, but I can't get this done. How do I writte a constructor with "initializer_list" in my LinkedStack class? I just want to "push()" each element and point the "front" pointer to the first element.
Thank you in advance!
First pic is the method: I think I should iterate and add each element, also point to the first one.
This second pic is how the function should be called from the main()
please help, I know this must be very simple but I've never used this "initializer_list" thing ever
pls halp
pls guys
>>>/sqt/
>>>/stackexchange/
>>>/sage/
>>60065312
>initializer_list
Try writing
LinkedStack<int> stack {1, 2, 3, 4, 5, 6, 7, 8};
Instead
Also paste your code in here, IN FULL using
[ code ] (remove spaces)
Or fuck off. I know how to fix it but I won't tell you until you do so.
>>60065566
I've tried that. The problem is the code in the function, I don't know what to write (that "for" is not correct at all I'm sure).
>>60065581
Ok, I will do that now
[#ifndef NODE_H
#define NODE_H
using namespace std;
template <class T>
class Node {
public:
//constructor sense parametres, l'element sera l'enter 0
Node(){
//element= NULL; // NULL?????????????????????
next=nullptr;
}
//constructor amb parametre
Node(T elem) {
element=elem;
next= nullptr;
}
//retorna l'element que hi ha guardat al node
const T& getElement() const{
return element;
}
//retorna un punter al següent node o si no hi ha retorna nullpointer
Node<T> *getNext() const{
return next;
}
//modifica l'adreça de next per la del paràmetre
void setNext(Node<T> *nod){
next = nod;
}
private:
T element;
Node<T> *next;
};
#endif /* NODE_H */]
template <class T>
class LinkedStack {
public:
//constructor sense parametres
LinkedStack() {
numElem = 0;
front = nullptr;
}
LinkedStack(std::initializer_list<T> elements){
front = nullptr;
numElem = elements.size();
for(int i=0; i<elements.size(); i++){
push(elements[1]);
}
}
//LinkedStack(const LinkedStack& orig){}
//virtual ~LinkedStack(){}
//retorna el nombre d'element que hi ha en aquest moment
int size() const{
return numElem;
}
bool empty() const{
return (numElem==0);
}
const T& top() const{
return (*front).getElement();
}
void push(const T& elem) {
Node<T> *pNode; //l'hem de crear dinamicament perque es crei en temps d'execucio
pNode= new Node<T>(elem); //a pop() farem el "delete"
if (numElem > 0) {
(*pNode).setNext(front);
front = pNode;
}
else {
front = pNode;
}
numElem++;
}
//elimina l'element del top de la pila, si esta buida llença excepcio
void pop(){
if (!empty()){
Node<T> *nodeABorrar;
nodeABorrar=front;
front = (*front).getNext();
//borrem el node creat en memoria dinamica
delete nodeABorrar;
numElem--;
}
else{
string ex ="LinkedStack buida, no s'ha pogut extreure l'element.";
throw(ex);
}
}
//imprimeix per consola tots els elements de la llista
void print() const{
if (empty()){
cout<<"Pila buida."<<endl;
}
else{
Node<T> *pNode;
pNode=front;
cout<<"Pila: ";
for(int i=1;i<=numElem;i++){
cout<<(*pNode).getElement()<<" ";
pNode=(*pNode).getNext();
}
cout<<endl;
}
}
private:
Node<T> *front;
int numElem;
};
Please help. I just need to know how to make that constructor with "initializer_list"
>>60065581
Please man, I did what you said
>>60065798
I implore you
PLEASE
I hope you get fired and sent back to your designated shitting streets.
>>60065954
I'm a student, and not indian.
Please man, help me out. It's just that initializer_list thing
>>60065979
If you don't know the material, you don't deserve to pass your final.
Better luck next semester.
>>60065996
I know very well all that pointer stuff and every class. My problem is the initializer_list, no one has explained that to me, and the online examples I've found are unreadable
>>60066019
Do you really need to use initializer_list? Seems like yet another completely unnecessary feature. Use it somewhere and the next guy will be scratching his head for a day thinking what is going on here.
PLOS HALP
>>60066060
Yes, I have to for this asignment..
Do you know how it's used? Or at least where should I look to learn how to use it?
I'm lost af and I haven't found any usefull website
ok here's a hint
the bug is between 12 and 14
>>60066101
come on man, I'm just trying to learn, be good to me
>>60066120
What country?
>>60065701
>push(elements[1]);
>>60066143
Spain
>>60066157
I know that's wrong. How should it be done?
>>60066189
>Catalan
Gross
>>60066189
You don't really know it seems. What you want is push every element of the initializer list into your stack, so you have to use i as an index, that's the variable you increase every turn of the loop.
Using 1 just picks the second item of the list repeatedly, and crashes if there are none.
>>60065798