Makefile app.cpp st.cpp list.h The LinkedList class is specified in list.h. Write the code in the file list.cpp to implement the methods specified in the header file You can build the app using the make utility. Don’t modify any code in the app.cpp file To check memory leaks: Valgrind –leak-check-full executable-file leak-check-full executable-file
Expert Answer
#include “list.h”
LinkedList::LinkedList(){
head = NULL;
tail = NULL;
}
bool LinkedList::add(int val){
Node *temp,*ptr;
temp = new Node;
temp->data = val;
if (head == NULL){
head = temp;
tail = temp;
temp->next = NULL;
temp->prev = NULL;
}
else {
temp->prev = tail;
tail->next = temp;
temp->next = NULL;
tail = temp;
}
return true;
}
bool LinkedList::remove(int val){
Node *curr;
curr = head;
bool result = false;
if (curr->data == val){
if (head->next == NULL){
head = NULL;
tail = NULL;
}
else {
head = head->next;
head->prev = NULL;
}
return true;
}
while (curr->next != NULL && curr->next->data != val)
curr = curr->next;
if (curr->next != NULL){
if (tail == curr->next)
tail = curr;
result = true;
curr->next = curr->next->next;
if (curr->next != NULL){
curr->next->prev = curr;
}
}
return result;
}
void LinkedList::printAscending() const {
Node *curr;
curr = head;
while (curr !=NULL){
cout << curr->data << ” “;
curr = curr->next;
}
cout << endl;
}
void LinkedList::printDescending() const {
Node *curr;
curr = tail;
while (curr !=NULL){
cout << curr->data << ” “;
curr = curr->prev;
}
cout << endl;
}