Fill the code in list.cpp
app.cpp:
#include "list.hpp"
int main()
{
LinkedList aList;
bool success = false;
aList.evensFrontOddsEnd(3);
aList.evensFrontOddsEnd(10);
aList.evensFrontOddsEnd(6);
aList.evensFrontOddsEnd(9);
aList.evensFrontOddsEnd(17);
aList.evensFrontOddsEnd(12);
aList.printForward(); // output should be: 12 6 10 3 9 17
aList.printBackward(); // output should be: 17 9 3 10 6 12
success = aList.remove(17);
if(success)
cout << "17 Successfully Removed." << endl;
success = aList.remove(10);
if(success)
cout << "10 Successfully Removed." << endl;
success = aList.remove(7); // 7 is not in the list, so success should be false.
if(!success)
cout << "7 is not in the list." << endl;
success = aList.remove(12);
if(success)
cout << "12 Successfully Removed." << endl;
aList.printForward(); // output should be: 6 3 9.
aList.printBackward(); // output should be: 9 3 6.
aList.remove(3);
aList.remove(6);
aList.remove(9);
aList.printForward(); // The list is now empty, so your program should not crash.
aList.printBackward();
return 0;
}
list.cpp:
#include "list.hpp"
// Implementation file for lab 6, CS162.
//put your implementation of LinkedList class here
// LinkedList constructor. Set head and tail to nullptr.
LinkedList::LinkedList() {
head = nullptr;
tail = nullptr;
}
// evensFrontOddsEnd method. You must add nodes with numbers that are even,
// (2, 4, 12, etc.) to the head and nodes with odd numbers to 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. To add to the head,
// make head->prev point to temp, temp->next point to head, and head = temp.
// To 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::evensFrontOddsEnd(int val) {
}
// printForward method. 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::printForward() const {
}
// printBackward method. 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::printBackward() const {
}
// Place the remove method below. If the val 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) {
}
list.hpp:
#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 evensFrontOddsEnd(int val); // Add nodes with numbers even numbers to the front
// of the list. Add odds to the end of the list.
bool remove(int val);
void printForward() const; // print the list in forward order.
void printBackward() const; // print the list in reverse order.
};
#endif

#######################################
list.cpp
#######################################
#include "list.hpp"
// Implementation file for lab 6, CS162.
//put your implementation of LinkedList class here
// LinkedList constructor. Set head and tail to nullptr.
LinkedList::LinkedList() {
head = nullptr;
tail = nullptr;
}
// evensFrontOddsEnd method. You must add nodes with numbers that are even,
// (2, 4, 12, etc.) to the head and nodes with odd numbers to 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. To add to the head,
// make head->prev point to temp, temp->next point to head, and head = temp.
// To 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::evensFrontOddsEnd(int val) {
Node *temp = new Node;
temp->data = val;
temp->next = nullptr;
temp->prev = nullptr;
if(head == nullptr) {
head = temp;
tail = temp;
} else if(val % 2 == 0) {
temp->next = head;
head->prev = temp;
head = temp;
} else {
temp->prev = tail;
tail->next = temp;
tail = temp;
}
}
// printForward method. 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::printForward() const {
Node *tmp = head;
while(tmp != nullptr) {
cout << tmp->data << " ";
tmp = tmp->next;
}
cout << endl;
}
// printBackward method. 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::printBackward() const {
Node *tmp = tail;
while(tmp != nullptr) {
cout << tmp->data << " ";
tmp = tmp->prev;
}
cout << endl;
}
// Place the remove method below. If the val 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) {
return false;
}
Node *cur = head;
if(cur->data == val) {
head = head->next;
if(head != nullptr) {
head->prev = nullptr;
} else {
tail = nullptr;
}
delete cur;
return true;
} else if(tail->data == val) {
cur = tail;
tail = tail->prev;
if(tail != nullptr) {
tail->next = nullptr;
} else {
head = nullptr;
}
delete cur;
return true;
} else {
Node *prev = cur;
cur = cur->next;
while(cur != nullptr && cur->data != val) {
prev = cur;
cur = cur->next;
}
if(cur != nullptr) {
prev->next = cur->next;
cur->next->prev = prev;
delete cur;
return true;
}
}
return false;
}
#######################################
list.hpp
#######################################
#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 evensFrontOddsEnd(int val); // Add nodes with numbers even numbers to the front
// of the list. Add odds to the end of the list.
bool remove(int val);
void printForward() const; // print the list in forward order.
void printBackward() const; // print the list in reverse order.
};
#endif
#######################################
main.cpp
#######################################
#include "list.hpp"
int main()
{
LinkedList aList;
bool success = false;
aList.evensFrontOddsEnd(3);
aList.evensFrontOddsEnd(10);
aList.evensFrontOddsEnd(6);
aList.evensFrontOddsEnd(9);
aList.evensFrontOddsEnd(17);
aList.evensFrontOddsEnd(12);
aList.printForward(); // output should be: 12 6 10 3 9 17
aList.printBackward(); // output should be: 17 9 3 10 6 12
success = aList.remove(17);
if(success)
cout << "17 Successfully Removed." << endl;
success = aList.remove(10);
if(success)
cout << "10 Successfully Removed." << endl;
success = aList.remove(7); // 7 is not in the list, so success should be false.
if(!success)
cout << "7 is not in the list." << endl;
success = aList.remove(12);
if(success)
cout << "12 Successfully Removed." << endl;
aList.printForward(); // output should be: 6 3 9.
aList.printBackward(); // output should be: 9 3 6.
aList.remove(3);
aList.remove(6);
aList.remove(9);
aList.printForward(); // The list is now empty, so your program should not crash.
aList.printBackward();
return 0;
}
Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!
Fill the code in list.cpp app.cpp: #include "list.hpp" int main() { LinkedList aList; bool success =...
For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
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...
What is the specific answer for 1. and 2. Thanks! Add a new method, find, to class SinglyLinkedList (defined here) that takes as input a “data” value and returns a pointer to a node. If the input data is present in the linked list, the returned pointer should point to that node; if not, the returned pointer is nullptr. Write the (single line) method declaration/specification. Write the method definition/implementation. Test by running the main() function below and capture the console...
I need help with understanding dummy nodes in doubly-linked lists. Here is the code that I have right now. ************city.h**************** #ifndef city_h #define city_h #include <string> using namespace std; class City{ public: City () { name = "N/A"; population = 0; } City (string nm, unsigned int pop){ name = nm; population = pop; } void setName (string name) { this -> name = name; } void setPopulation (unsigned int population){ this -> population = population; } string getName() const...
Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() function. Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...
C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private: Node *head; // You may add whatever private data members or private member functions you want to this class. void printReverseRecursiveHelper(Node *temp) const; public: // default constructor LinkedList() : head(nullptr) { } // copy constructor LinkedList(const LinkedList& rhs); // Destroys all the dynamically allocated memory //...
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...
C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...
I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be: H create link at head R remove link at head T create link at tail K remove link at tail I remove link at ID S search...