print doubly linked list - no result - c++

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;
}

You need to set the head.
void appendToEnd(int val){
MagicSquaresNode *newNode = new MagicSquaresNode;
newNode->nodeIndex = val;
if(isEmpty()){
tail = newNode;
head = newNode;
} else {
tail->pright = newNode;
newNode->pleft = tail;
}
tail = newNode;
}

Just a few comments. First you want to use proper indents. For beginner it is important to learn to write a simple Makefile. In your case, I wrote one for you.
Makefile:
1 bin_PROGRAMS=doublelink
2 GCCLIBDIR= /usr/local/lib64
3 CXXFLAGS=-g -std=c++11
4 CC=g++
5 LDFLAGS=-L$(GCCLIBDIR)
6
7 all : $(bin_PROGRAMS)
8
9 doublelink : doublelink.o
10 $(CC) $(CXXFLAGS) -o $# $^ $(LDFLAGS)
For your source code I did simple editing and named your file: doublelink.cpp:
//#include "MagicSquare.hpp"
#include <iostream>
using namespace std;
class MagicSquaresList{
private:
struct MagicSquaresNode {
MagicSquaresNode(int ni) : nodeIndex(ni), pleft(0), pright(0), pup(0), pdown(0) { }
int nodeIndex;
MagicSquaresNode *pleft;
MagicSquaresNode *pright;
MagicSquaresNode *pup;
MagicSquaresNode *pdown;
};
MagicSquaresNode *head;
MagicSquaresNode *tail;
public:
MagicSquaresList () {
head = 0;
tail = 0;
}
int getListLength(){
MagicSquaresNode *temp = head;
if (temp == 0) {
return 0;
}
int length = 0;
while (temp != 0) {
++length;
temp = temp->pright;
}
return length;
}
bool isEmpty(){
return head == 0;
}
void appendToEnd(int val){
MagicSquaresNode *newNode = new MagicSquaresNode(val);
if (tail == 0) {
head = newNode;
}
else {
tail->pright = newNode;
newNode->pleft = tail;
}
tail = newNode;
}
void printListForward() {
MagicSquaresNode *ptr = head;
while (ptr != 0) {
//cout << ptr << endl;
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;
}
With both file in the directory, you type
make
a binary file doublelink will appear in your directory.
you run this program by typing its name:
$ doublelink
1 2 3 4 5 6 7 8 9
9
But with all these efforts. You should never need to implement double linked list. You should use the C++ standard library and customize the data type for your purpose. The std::list is implemented as double linked list. Please read the document at http://www.cplusplus.com/reference/list/list/. You should create your structure of interest by
list<MagicSquare> myfancySquareList;
myfancySquareList.push_back(MagicSquare(somevalue));
Your double linked list is also missing the destructor, and you are having memory leak. There are many other missing things from your implementation which is usually covered by a text book of several hundred pages. Hope this get you started. When you have trouble, you can run your program in debug mode: gdb doublelink. You can step through it and figure out where is your problem. Your initial problem is a segmentation fault. Try to run your original program and see where it terminate.

Related

Trying to initialize a linked list using array

I need to define a class of linked list,List, in a way such that object of class can be defined in two ways,
List obj1 = L1();//head=0
List obj2 = L2(given_arr[], size of array) // I would be given an array, whose elements are elements of list
so, I need to form a construter for both,
for obj1, Its easy.
List(){head=0};
But I am not abe to do so for second type of object.
I tried to form a program for this.
#include <iostream>
using namespace std;
class List {
class node {
public:
int val;
node* next;
};
public:
node* head;
int arr[];
List() { head = 0; }
List(int arr[], int size);
void addnode(int value) {
node* newnode = new node();
newnode->val = value;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
} else {
node* temp = head; // head is not NULL
while (temp->next != NULL) {
temp = temp->next; // go to end of list
}
temp->next = newnode; // linking to newnode
}
}
void display() {
if (head == NULL) {
cout << "List is empty!" << endl;
} else {
node* temp = head;
while (temp != NULL) {
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
}
}
};
List::List(int arr[], int size) {
int i;
head->val = arr[0];
for (i = 0; i < size; i++) addnode(arr[i]);
}
int main() {
int barr[4] = {9, 89, 0, 43};
List* M = new List();
List* L = new List(barr[4], 4);
L->display();
return 0;
}
This program doesn't work. Please suggest a way to do so.
Make these changes to your main().
int main() {
int barr[] = {9, 89, 0, 43}; // No need to specify size if you're initializing
// List* M = new List(); // unused
// Your array is barr, barr[4] makes no sense. You also don't allocate the List,
// the list allocates
List L = List(barr, sizeof(barr) / sizeof(barr[0]);
L.display(); // -> to .
return 0;
}
This now compiles, but immediately segfaults. Simply running the program in the debugger shows a simple error. The line head->val = arr[0]; attempts to dereference a null pointer. Which takes us to the next thing. Use nullptr, not NULL or 0.
Your array constructor was over-complicated, you just need this:
List::List(int arr[], int size) {
for (int i = 0; i < size; i++) addnode(arr[i]);
}
Your addnode() function already handled an empty list. Fixing that, your code should run. I made a couple other small changes, mostly trimming cruft out. Here's your complete code:
#include <iostream>
using namespace std;
class List {
class node {
public:
int val;
node* next;
};
public:
node* head = nullptr;
List() = default;
List(int arr[], int size);
void addnode(int value) {
node* newnode = new node();
newnode->val = value;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
} else {
node* temp = head; // head is not NULL
while (temp->next != NULL) {
temp = temp->next; // go to end of list
}
temp->next = newnode; // linking to newnode
}
}
void display() {
if (head == NULL) {
cout << "List is empty!" << endl;
} else {
node* temp = head;
while (temp != NULL) {
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
}
}
};
List::List(int arr[], int size) {
for (int i = 0; i < size; i++) addnode(arr[i]);
}
int main() {
int barr[] = {9, 89, 0, 43};
List L = List(barr, sizeof(barr) / sizeof(barr[0]));
L.display();
return 0;
}

Undefined Reference to Base Class Constructor

This is my code for an implementation of a doubly linked list that inherits previous code from a single linked list, I am currently having trouble with a linker error and surfed the web for the past hour looking for an answer to my problem and found nothing so far to help me. This is my last resor can anyone help?
Specifically the error i get when i try to use g++ to link my .o files is:
DoublyList.o:DoublyList.cpp:(.text+0xf): undefined reference to
`LinkedList::LinkedList()'
collect2.exe: error: ld returned 1 exit status
I have found very similar questions asked but none of the answers helped me or at least I do not know how to implement them in my code specifically, any help will be apprectiated.
My LinkedList class
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
using namespace std;
struct node
{
float value;
node *next;
};
class LinkedList
{
private:
node *first;
public:
LinkedList();
virtual void insert(float val);
virtual void del(float val);
virtual void read();
virtual int search(float val);
};
#endif
My LinkedList class definition
#include <iostream>
#include "LinkedList.h"
using namespace std;
LinkedList::LinkedList()
{
this->first = NULL;
}
void LinkedList::insert(float val)
{
if(this->first==NULL or this->first->value >= val)
{
node* a_node = new node();
a_node->value = val;
this->first = a_node;
return;
}
node* n = new node();
n = this->first;
node* new_node = new node();
new_node->value = val;
while(n->next != NULL and n->next->value < new_node->value)
{
n = n->next;
}
new_node->next = n->next;
n->next = new_node;
}
void LinkedList::del(float val)
{
node* n = this->first;
node* prev = new node();
prev = n;//in case if it is the first value
int i = this->search(val);
if(this->first->value == val)
{
this->first = this->first->next;
return;
}
if(i != -1)
{
for(int j = 0; j < i; j++)
{
prev = n;
n = n->next;
}
}
//one last check
if(n->value == val)
{
prev->next = n->next;
}
}
void LinkedList::read()
{
node* n = this->first;
int i = 1;
while(n != NULL)
{
cout << i << ". " << n->value << endl;
n = n->next;
i++;
}
}
int LinkedList::search(float val)
{
int i = 0;
node* n = this->first;
while(n != NULL)
{
if(n->value == val)
return i;
else
{
n = n->next;
i++;
}
}
return -1;
}
My doublylist class
#ifndef DOUBLYLIST_H
#define DOUBLYLIST_H
#include "LinkedList.h"
class DoublyList: public LinkedList
{
public:
struct node
{
float value;
node * next;
node * prev;
};
node * first;
DoublyList();
void insert(float val);
void del(float val);
void read();
int search(float val);
};
#endif
My Doubly List definiton
#include <cstddef>
#include "DoublyList.h"
#include "LinkedList.h"
using namespace std;
//constructor
DoublyList::DoublyList()
{
first = NULL;
}
//Insert a node into the correct position in the doubly linked list
void DoublyList::insert(float val)
{
//if linked list is empty or val <= the first node
if(this->first == NULL or this->first->value >= val)
{
node * a_node = new node();
a_node->value = val;//set node's value
//begin replacing and assigning pointers
a_node->next = this->first;
a_node->prev = NULL;
this->first = a_node;
return;
}
node * n = new node();
n = this->first;
node * new_node = new node();
new_node->value = val;
node * prev_node = new node();
while(n->next != NULL and n->next->value < new_node->value)
{
prev_node = n;
n = n->next;
}
prev_node->next = new_node;
new_node->next = n->next;
new_node->prev = prev_node;
n->next = new_node;
}
void DoublyList::del(float val)
{
node * n = this->first;
int i = this->search(val);
//if first node
if(this->first->value == val)
{
this->first = this->first->next;
this->first->prev = NULL;
return;
}
//if value found
if(i != -1)
{
for(int j = 0; j < i; j++)
{
n = n->next;
}
//if a middle node
if(n->value == val and n->next != NULL)
{
n->prev->next = n->next;
return;
}
//if last node
if(n->prev != NULL)
{
n->prev->next = n->next;
}
}
return;//value not found so return
}
void DoublyList::read() { }
int DoublyList::search(float val) { }
Edit: Forgot to mention this error specifically happens aruond line 8 of DoublyList.cpp, this was from previous trials to link the .o files.
The command I used to call the linker is
g++ -g main2.cpp DoublyList.o
Where main2.cpp is the code that contains my main function to test the code.
Thanks to xskxzr the solution was to also link LinkedList.o along with all the rest of the .o files. If anyone ever has the same problem this is the answer.

C++ Stack using a doubly linked list

I'm trying to implement a stack using a doubly linked list. I know that the functions for my stack class (push, pop) should contain calls to member functions of my doubly linked list class, but I'm having trouble actually implementing that.
dlist.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "dlist.hpp"
using namespace std;
void dlist::appendNodeFront(int shares, float pps){
Node *n = new Node(shares, pps);
if(front == NULL){
front = n;
back = n;
}
else {
front->prev = n;
n->next = front;
front = n;
}
}
void dlist::appendNodeBack(int shares, float pps){
Node *n = new Node(shares, pps);
if(back == NULL){
front = n;
back = n;
}
else {
back->next = n;
n->prev = back;
back = n;
}
}
void dlist::display(){
Node *temp = front;
cout << "List contents: ";
while(temp != NULL){
cout << temp->value << " ";
temp = temp->next;
}
cout << endl;
}
void dlist::display_reverse(){
Node *temp = back;
cout << "List contents in reverse: ";
while(temp != NULL){
cout << temp->value << " ";
temp = temp->prev;
}
cout << endl;
}
void dlist::destroyList(){
Node *T = back;
while(T != NULL){
Node *T2 = T;
T = T->prev;
delete T2;
}
front = NULL;
back = NULL;
}
stack.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include "stack.hpp"
using namespace std;
stack::stack(){
int i;
for(i = 0; i < 1500; i++){
shares[i] = 0;
pps[i] = 0;
}
first = 0;
}
void stack::push(int num, float price){
if(first ==(1500-1)){
cout << "Stack is full" << endl;
return;
}
first++;
shares[first] = num;
pps[first] = price;
return;
}
void stack::pop(int *num, float *price){
if(first == -1){
cout << "Stack is empty" << endl;
return;
}
num = &shares[first];
price = &pps[first];
cout << shares[first] << endl;
cout << pps[first] << endl;
shares[first] = 0;
pps[first] = 0;
first--;
return;
}
Should the push function in stack basically be a call to appendNodeFront() or appendNodeback()? Any help or advice is greatly appreciated!
You can create a stack class, then use linked list class as its container. In a linked list class there is virtually no limit to the number of items, so you add artificial limit to make it work like a stack. In a linked list, items can be added/removed anywhere in the list, you can limit add/remove the tail node only to make it work like stack. The example below demonstrate the usage.
Node that this is purely a programming exercise. Stack is relatively primitive compared to Doubly-linked list. Encapsulating a linked-list inside stack has no advantage. Also note, I declared all members as public for the sake of simplifying the problem, you may want to change some members to protected/private
#include <iostream>
#include <fstream>
#include <string>
using std::cout;
class Node
{
public:
Node *prev;
Node *next;
int shares;
float pps;
Node(int vshares, float vpps)
{
shares = vshares;
pps = vpps;
prev = next = nullptr;
}
};
class dlist
{
public:
Node *head;
Node *tail;
dlist()
{
head = tail = nullptr;
}
~dlist()
{
destroy();
}
void push_back(int shares, float pps)
{
Node *node = new Node(shares, pps);
if (head == NULL)
{
head = tail = node;
}
else
{
tail->next = node;
node->prev = tail;
tail = node;
}
}
void destroy()
{
Node *walk = head;
while (walk)
{
Node *node = walk;
walk = walk->next;
delete node;
}
head = tail = nullptr;
}
};
class stack
{
public:
int maxsize;
int count;
dlist list;
stack(int size)
{
count = 0;
maxsize = size;
}
void push(int num, float price)
{
if (count < maxsize)
{
list.push_back(num, price);
count++;
}
}
void pop()
{
Node *tail = list.tail;
if (!tail)
{
//already empty
return;
}
if (tail == list.head)
{
//only one element in the list
delete tail;
list.head = list.tail = nullptr;
count--;
}
else
{
Node *temp = list.tail->prev;
delete list.tail;
list.tail = temp;
list.tail->next = nullptr;
count--;
}
}
void display()
{
Node *walk = list.head;
while (walk)
{
cout << "(" << walk->shares << "," << walk->pps << ") ";
walk = walk->next;
}
cout << "\n";
}
};
int main()
{
stack s(3);
s.push(101, 0.25f);
s.push(102, 0.25f);
s.push(103, 0.25f);
s.push(104, 0.25f);
s.display();
s.pop();
s.display();
return 0;
}

Differences between VS2013 and Ubuntu c++ [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I wrote a c++ code in visual studio 2013 it's working great there.
I need it to work on Ubuntu to , but I get an error
"Undefinded referance Node::Node()"
Node.h
#pragma once
#include "string"
class Node
{
public:
double data;
std::string key;
Node *next;
Node *prev;
Node();
Node(double data , std::string key);
};
Node.cpp
#include <iostream>
#include "Node.h"
Node::Node(){
data = 0;
key = "";
next = NULL;
prev = NULL;
}
Node::Node(double data, std::string key){
this->data = data;
this->key = key;
}
MyLinkedList.h
#pragma once
#include "Node.h"
#include "string"
class MyLinkedList
{
public:
Node *head;
Node *tail;
int size;
MyLinkedList();
MyLinkedList(const MyLinkedList &l);
~MyLinkedList();
bool isEmpty();
void printList();
void add(const std::string key, const double val);
int remove(std::string s);
MyLinkedList& operator=( const MyLinkedList& l);
bool isInList(const std::string key, double &data);
};
MyLinkedList.cpp
#include "MyLinkedList.h"
#include <iostream>
MyLinkedList::MyLinkedList()
{
head = NULL;
tail = NULL;
size = 0;
}
void MyLinkedList::add(const std::string key, double val){
Node *n = new Node(val , key);
if (head == NULL){
head = n;
tail = head;
tail->next = NULL;
}
else{
n->prev = tail;
n->next = NULL;
tail->next = n;
tail = n;
}
++size;
}
MyLinkedList::MyLinkedList(const MyLinkedList &l){
Node *temp = l.head;
while (temp != NULL){
this->add(temp->key, temp->data);
temp = temp->next;
}
}
MyLinkedList::~MyLinkedList()
{
Node *temp = head;
Node *toDelete = temp;
while (temp != NULL)
{
temp = temp->next;
delete toDelete;
toDelete = temp;
}
}
bool MyLinkedList::isEmpty()
{
return head == NULL;
}
void MyLinkedList::printList(){
if (head == NULL){
std::cout << "Empty" << std::endl;
return;
}
Node *temp = head;
while (temp != NULL)
{
std::cout << temp->key <<","<< temp->data << std::endl;
temp = temp->next;
}
}
int MyLinkedList::remove(const std::string s){
Node *p = head;
Node *n = p->next;
int count=0;
while (size > 0 && !head->key.compare(s)){
head = head->next;
delete p;
p = head;
if (p!=NULL)
n = p->next;
--size;
++count;
}
while (size > 0 && n->next != NULL)
{
if (!s.compare(n->key)){
p->next = n->next;
n->next->prev = p;
delete n;
n = p->next;
--size;
++count;
}
else{
p = n;
n = n->next;
}
}
if (size > 0 && !n->key.compare(s)){
n->prev->next = NULL;
delete n;
++count;
--size;
}
return count;
}
MyLinkedList& MyLinkedList::operator = (const MyLinkedList& l){
if (this != &l) {
Node *temp = head;
Node *toDelete = temp;
while (temp != NULL)
{
temp = temp->next;
delete toDelete;
toDelete = temp;
}
temp = l.head;
while (temp != NULL){
this->add(temp->key, temp->data);
temp = temp->next;
}
}
return *this;
}
bool MyLinkedList::isInList(const std::string key, double &data){
Node *temp = head;
while (temp != NULL){
if (!temp->key.compare(key)){
data = temp->data;
return true;
}
}
return false;
}
Simple checks for the MyLinkedList implementation
#include "MyLinkedList.h"
#include <iostream>
#include <string>
int main()
{
MyLinkedList mylist;
std::string firstWord = "aa";
double firstVal = 1.5;
std::string secondWord = "bb";
double secVal = 2.2;
std::string thirdWord = "ab";
double thirdVal = 1.0;
mylist.printList();
std::cout << "Done print list" << std::endl << std::endl;
mylist.add(firstWord, firstVal);
mylist.add(secondWord, secVal);
mylist.add(firstWord, thirdVal);
mylist.add(thirdWord, firstVal);
mylist.printList();
std::cout << "Done print list" << std::endl << std::endl;
return 0;
}
This is all my code. If there is any more differences between Visual Studio and Ubuntu, I would like to know.
copiled with :
g++ -Wall -Werror -Wvla -g ListExample.cpp MyLinkedList.cpp -o ListExample
"Undefined reference" typically means you're missing a .o (object) file or library that contains one of the symbols that your program needs.
For example, if you do
g++ ListExample.cpp
then GCC will try to compile main.cpp straight to an executable and will require that it contain all of the symbols it references.
To compile object files then link multiple object files together, you should instead do something like
g++ -c ListExample.cpp
g++ -c Node.cpp
g++ -c MyLinkedList.cpp
g++ -o linked_list_test ListExample.o Node.o MyLinkedList.o
Writing a Makefile can streamline this for you.

C++ doubly linked list

I got a problem with my doubly linked list. How can i make the input unique ( i don`t want it to be repeated )
for example i can input 1 and then again 1 i will have a list of 1 and 1. I need to forbid this somehow :) so the list can contain only not repeating numbers.
#include <cstdlib>
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
node* prev;
};
class Node
{
public:
Node();
~Node();
void setKopa();
void printForward();
private:
node* head;
node* tail;
node* n;
};
Node::Node()
{
setKopa();
}
Node::~Node()
{
delete n;
}
void Node::setKopa()
{
int lenght;
do
{
cout << "Input list lenght (how many elements): ";
cin >> lenght;
if(lenght<2)
cout << "Error list has to have atleast 2 elements!" <<endl;
}
while(lenght<2);
int fill;
cout << "Input "<< lenght <<" elements: "<<endl;
for (int i=0; i<lenght; i++)
{
cin>>fill;
n = new node;
n->data = fill;
if (i==0)
{
n->prev = NULL;
head = n;
tail = n;
}
else if (i+1==lenght)
{
n->prev = tail;
tail->next = n;
tail = n;
tail->next = NULL;
}
else
{
n->prev = tail;
tail->next = n;
tail = n;
}
}
}
void Node::printForward()
{
node* temp = head;
while(temp != NULL)
{
cout << temp->data << " ";
temp = temp-> next;
}
cout << endl;
}
int main()
{
Node a;
a.printForward();
system("pause");
return 0;
}
When you read input, go through the list to see if the input is already there.
With that (simple) answer out of the way, I would like to address some other things regarding your code. The first is that you have a memory leak in that you never delete the list. The second is that you don't need the class member variable n, it might as well be a local variable inside the setKopa loop.
Your way of adding new nodes is also, well, weird. It should, in my opinion, be more general instead of using the loop counter to check what to do. What I suggest is that you make a member function to add new nodes, taking the integer data as argument. This way you can call this function to add nodes anywhere, and not just in the setKopa function. In fact, I think the list should not handle that input at all, instead it should be a free-standing function called from main and which calls the addNode function.
Also the node structure doesn't need to be in the global namespace, it could be a private structure in the Node class. And speaking of the Node class, shouldn't it really be called List instead?
So if I may suggest, you might want to do something like this:
#include <iostream>
class List
{
public:
List()
: head(nullptr), tail(nullptr)
{}
~List();
void addNode(const int data);
void printAll() const;
private:
struct node
{
node()
: next(nullptr), prev(nullptr)
{}
node* next;
node* prev;
int data;
};
node* head;
node* tail;
};
List::~List()
{
for (node* next, *cur = head; cur; cur = next)
{
next = cur->next;
delete cur;
}
}
void List::addNode(const int data)
{
node* n = new node;
n->data = data;
if (tail == nullptr)
{
// First node in list
head = tail = n;
}
else
{
n->prev = tail;
tail->next = n;
tail = n;
}
}
void List::printAll() const
{
std::cout << "{ ";
for (node* cur = head; cur != nullptr; cur = cur->next)
std::cout << cur->data << ' ';
std::cout << "}\n";
}
int main()
{
List list;
for (int i = 0; i < 10; ++i)
list.addNode(i);
list.printAll();
}
The above code should print
{ 0 1 2 3 4 5 6 7 8 9 }
Replace the node-adding loop with your own.