Question
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an
Hi, I hope I can get some help with the following exercise in C++( CPP):
1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list).
/* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments */ Node(void) : pNext(NULL) { } /* Constructors with a given value */ Node(int val) : value(val), pNext(NULL) { } /* Constructors with a given value and a link of the next node */ Node(int val, Node* next) : value(val), pNext(next) {} /* Getters */ int getValue(void) { return value; } Node* getNext(void) { return pNext; } };
/* definition of the linked list class */ class LinkedList { private: /* pointer of head node */ Node *pHead; /* pointer of tail node */ Node *pTail; public: /* Constructors with No Arguments */ LinkedList(void); /* Constructors with a given value of a list node */ LinkedList(int val); /* Destructor */ ~LinkedList(void); /* Traversing the list and printing the value of each node */ void traverse_and_print(); void push_back(int val); };
LinkedList::LinkedList() { /* Initialize the head and tail node */ pHead = pTail = NULL; }
LinkedList::LinkedList(int val) { /* Create a new node, acting as both the head and tail node */ pHead = new Node(val); pTail = pHead; }
LinkedList::~LinkedList() { }
void LinkedList::traverse_and_print() { Node *p = pHead; /* The list is empty? */ if (pHead == NULL) { cout << "The list is empty" << endl; return; } cout << "LinkedList: "; /* A basic way of traversing a linked list */ while (p != NULL) { /* while there are some more nodes left */ /* output the value */ cout << p->value; /* The pointer moves along to the next one */ p = p->pNext; } cout << endl; }
void LinkedList::push_pack(int val){ /*Your code here*/ }
int main(int argc, const char * argv[]) { /* Create an empty list */ LinkedList list1; cout << "Created an empty list named list1." << endl; /* output the result */ cout << "list1:" << endl; list1.traverse_and_print(); /* Create a list with only one node */ LinkedList list2(10); cout << "Created a list named list2 with only one node." << endl; /* output the result */ cout << "list2:" << endl; list2.traverse_and_print(); /*your testing code here*/ for (int i = 0 ; i < 10; i++){ list2.push_back(i); } list2.traverse_and_print(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started