I am new to c++ and programming in general. I am trying to implement a doubly linked list. I think the list is created successfully, but I am having trouble printing the list out entirely. Can you please let me know what's wrong with my printListForward method below? My code is not complete yet. Would really appreciate any tips and suggestions as well.
#include "MagicSquare.hpp"
#include <iostream>
class MagicSquaresList{
private:
struct MagicSquaresNode{
int nodeIndex;
MagicSquaresNode *pleft;
MagicSquaresNode *pright;
MagicSquaresNode *pup;
MagicSquaresNode *pdown;
};
MagicSquaresNode *head;
MagicSquaresNode *tail;
public:
MagicSquaresList (){
head = NULL;
tail = NULL;
}
int getListLength(){
int length = 1;
MagicSquaresNode *temp = new MagicSquaresNode;
temp = head;
if(isEmpty()){
return 0;
}else{
while(temp != tail){
length++;
temp = temp->pright;
}
}
return length;
}
bool isEmpty(){
return head == NULL;
}
void appendToEnd(int val){
MagicSquaresNode *newNode = new MagicSquaresNode;
newNode->nodeIndex = val;
if(isEmpty()){
tail = newNode;
} else {
tail->pright = newNode;
newNode->pleft = tail;
}
tail = newNode;
}
void printListForward() {
MagicSquaresNode *ptr = head;
while(ptr != tail){
std::cout << ptr->nodeIndex << " ";
ptr = ptr->pright;
}
std::cout << std::endl;
}
};
int main(){
/*********** temporary *****************/
int matrixSize, listSize;
matrixSize = 3;
listSize = matrixSize * matrixSize;
/****************************************/
MagicSquaresList list1;
for (int i = 1; i <= listSize; i++){
list1.appendToEnd(i);
}
list1.printListForward();
std::cout << list1.getListLength() << std::endl;
return 0;
}