Write the code in the file list.cpp to implement the methods specified in the he
ID: 3902483 • Letter: W
Question
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 or list.hpp, (unless I made a mistake that you must correct… let me know if that happens). This means that you should only modify list.cpp.To check memory leaks:
valgrind --leak-check=full executable-file
// ************ main function file, driver ********************
#include "list.hpp"
int main()
{
LinkedList aList;
bool success = false;
aList.add(3);
aList.add(10);
aList.add(1);
aList.add(7);
aList.add(9);
aList.add(12);
aList.printAscending();
aList.printDescending();
success = aList.remove(3);
if(success)
cout << "3 Successfully Removed." << endl;
success = aList.remove(1);
if(success)
cout << "1 Successfully Removed." << endl;
success = aList.remove(5); // 5 is not in the list, so success should be false.
if(!success)
cout << "5 is not in the list." << endl;
success = aList.remove(12);
if(success)
cout << "12 Successfully Removed." << endl;
aList.printAscending(); // Should produce: 10, 7, 9, and/or: 9, 7, 10.
aList.printDescending();
aList.remove(10);
aList.remove(7);
aList.remove(9);
aList.remove(1); // The list is now empty, so your program should not crash.
aList.printAscending();
return 0;
}
// *************** Header File ******************
// Doubly Linked List
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
struct Node
{
int data;
Node * next;
Node * prev;
};
class LinkedList
{
private:
Node * head, * tail;
public:
LinkedList();
void add(int val); // You may add nodes to the head or tail.
bool remove(int val);
void printAscending() const; // print the list in forward order.
void printDescending() const; // print the list in reverse order.
};
#endif
// *************list.cpp******************
//This where the changes should be only.
#include "list.hpp"
// Implementation file
//put your implementation of LinkedList class here
// LinkedList constructor. Set head and tail to nullptr.
LinkedList::LinkedList() {
head = nullptr;
tail = nullptr;
}
// Add method. You may add nodes to the head or the tail. Suppose you use temp as a pointer
// and create a new node using temp. If head = nullptr, set head = tail = temp.
// Temp will be the first node. Otherwise if you add to the head, make head->prev point to temp,
// temp->next point to head, and head = temp.
// If you add to the tail, make tail->next point to temp, temp->prev point to tail,
// and tail = temp. If you add to the head, make temp->prev = nullptr.
// If you add to the tail, then temp->next = nullptr.
void LinkedList::add(int val) {
}
// Print the list in forward order. If the list is empty, return immediately.
// Otherwise, create a temporary pointer that starts at head,
// and then moves down the linked list: cur = head, while cur != nullptr, cur = cur->next.
void LinkedList::printAscending() const {
}
// Print the list in reverse order. If the list is empty, return immediately.
// Otherwise create a temporary pointer that starts at tail,
// and then moves up the linked list: cur = tail, while cur != nullptr, cur = cur->prev.
void LinkedList::printDescending() const {
}
// Place the remove method here. You must create the header, along with the definition.
// If the number argument is not found in the list, or if the list is empty, return false.
// Otherwise, create a temporary pointer, perhaps called cur.
// If the number to remove is the head, set cur = head, set head->next->prev = nullptr,
// head = head->next, then delete cur. You must check if head->next is not equal to nullptr first.
// If the number to remove is the tail, set cur = tail, set tail->prev->next = nullptr,
// tail = tail->prev, then delete cur. Check to see if tail->prev is not equal to nullptr first.
// Lastly, if the node is somewhere in the middle, then cur->prev->next = cur->next,
// cur->next->prev = cur->prev, and then delete cur.
// If you removed a node, return true.
Explanation / Answer
Below is your code.Please do rate this answer positive, If i was able to help you. Let me know if you have any issues in comments
list.hpp
// Doubly Linked List
#ifndef LIST_H
#define LIST_H
#include <iostream>
using namespace std;
struct Node
{
int data;
Node * next;
Node * prev;
};
class LinkedList
{
private:
Node * head, * tail;
public:
LinkedList();
void add(int val); // You may add nodes to the head or tail.
bool remove(int val);
void printAscending() const; // print the list in forward order.
void printDescending() const; // print the list in reverse order.
};
#endif
list.cpp
//This where the changes should be only.
#include "list.hpp"
// Implementation file
//put your implementation of LinkedList class here
// LinkedList constructor. Set head and tail to nullptr.
LinkedList::LinkedList() {
head = nullptr;
tail = nullptr;
}
// Add method. You may add nodes to the head or the tail. Suppose you use temp as a pointer
// and create a new node using temp. If head = nullptr, set head = tail = temp.
// Temp will be the first node. Otherwise if you add to the head, make head->prev point to temp,
// temp->next point to head, and head = temp.
// If you add to the tail, make tail->next point to temp, temp->prev point to tail,
// and tail = temp. If you add to the head, make temp->prev = nullptr.
// If you add to the tail, then temp->next = nullptr.
void LinkedList::add(int val) {
Node *newNode = new Node;
newNode->data = val;
newNode->next = nullptr;
newNode->prev = nullptr;
// if list is empty
if(head == nullptr) {
head = newNode;
tail = head;
}else if(head->data >= val) {
newNode->next = head;
head->prev = newNode;
head= newNode;
}else if(tail->data <= val) {
tail->next= newNode;
newNode->prev= tail;
tail= newNode;
}else{
Node *curr = head;
while(curr->next->data < val)
curr = curr->next;
newNode->next = curr->next;
curr->next->prev = newNode;
newNode->prev= curr;
curr->next = newNode;
}
}
// Print the list in forward order. If the list is empty, return immediately.
// Otherwise, create a temporary pointer that starts at head,
// and then moves down the linked list: cur = head, while cur != nullptr, cur = cur->next.
void LinkedList::printAscending() const {
Node *curr = head;
while(curr != nullptr){
cout<<curr->data<<" ";
curr = curr->next;
}
cout<<endl;
}
// Print the list in reverse order. If the list is empty, return immediately.
// Otherwise create a temporary pointer that starts at tail,
// and then moves up the linked list: cur = tail, while cur != nullptr, cur = cur->prev.
void LinkedList::printDescending() const {
Node *curr = tail;
while(curr != nullptr){
cout<<curr->data<<" ";
curr = curr->prev;
}
cout<<endl;
}
// Place the remove method here. You must create the header, along with the definition.
// If the number argument is not found in the list, or if the list is empty, return false.
// Otherwise, create a temporary pointer, perhaps called cur.
// If the number to remove is the head, set cur = head, set head->next->prev = nullptr,
// head = head->next, then delete cur. You must check if head->next is not equal to nullptr first.
// If the number to remove is the tail, set cur = tail, set tail->prev->next = nullptr,
// tail = tail->prev, then delete cur. Check to see if tail->prev is not equal to nullptr first.
// Lastly, if the node is somewhere in the middle, then cur->prev->next = cur->next,
// cur->next->prev = cur->prev, and then delete cur.
// If you removed a node, return true.
bool LinkedList::remove(int val) {
if(head == nullptr || head->data > val || tail->data < val) {
return false;
}
if(head->data == val){
Node *t = head;
head = head->next;
// there were more than one node
if(head != nullptr)
head->prev = nullptr;
else// there were only one node
tail = nullptr;
delete t;
return true;
}
if(tail->data == val) {
Node *t = tail;
tail = tail->prev;
tail->next = nullptr;
delete t;
return true;
}
Node *curr = head->next;
while(curr != nullptr && curr->data != val) {
curr = curr->next;
}
// val is not in list
if(curr == nullptr)
return false;
else {
Node *t = curr;
curr->next->prev = curr->prev;
curr->prev->next = t->next;
delete t;
return true;
}
}
main.cpp
#include "list.hpp"
int main()
{
LinkedList aList;
bool success = false;
aList.add(3);
aList.add(10);
aList.add(1);
aList.add(7);
aList.add(9);
aList.add(12);
aList.printAscending();
aList.printDescending();
success = aList.remove(3);
if(success)
cout << "3 Successfully Removed." << endl;
success = aList.remove(1);
if(success)
cout << "1 Successfully Removed." << endl;
success = aList.remove(5); // 5 is not in the list, so success should be false.
if(!success)
cout << "5 is not in the list." << endl;
success = aList.remove(12);
if(success)
cout << "12 Successfully Removed." << endl;
aList.printAscending(); // Should produce: 10, 7, 9, and/or: 9, 7, 10.
aList.printDescending();
aList.remove(10);
aList.remove(7);
aList.remove(9);
aList.remove(1); // The list is now empty, so your program should not crash.
aList.printAscending();
return 0;
}
Output
1 3 7 9 10 12
12 10 9 7 3 1
3 Successfully Removed.
1 Successfully Removed.
5 is not in the list.
12 Successfully Removed.
7 9 10
10 9 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.