Question
Not sure if this is within the scope of this service... I'm hoping to get help implementing some simple functions. So I was given an
Not sure if this is within the scope of this service... I'm hoping to get help implementing some simple functions. So I was given an assignment with starter code to implement a linked list (which I have done with an unsorted doubly linked list) and in the starter code in the given header file there was a friend declaration that seems to have the goal of allowing me to print the linked list using the cout statement. This is the header file; note that I wrote everything in the private section.
#ifndef _LINKED_LIST_ #define _LINKED_LIST_ #includeclass LinkedList { public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); private: struct node { char data; node * next; node * prev; }; node * head, * tail; }; #endif // _LINKED_LIST_
In main, which was also part of the starter code, the teacher wrote cout << list; which leads me to believe the goal of the friend statement in the header file was to allow list to be printed to the console easily.
My question is, how can I implement friend std::ostream& operator<<(std::ostream& out, LinkedList& list) and how can I implement the add, find, and del functions. I know that the del function will read like ... bool LinkedList::del(char)
app.cpp is as follows
#include#include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) { if (list.find(ch)) cout << "found "; else cout << "did not find "; cout << ch << endl; } int main() { LinkedList list; list.add('x'); list.add('y'); list.add('z'); cout << list; find(list, 'y'); list.del('y'); cout << list; find(list, 'y'); list.del('x'); cout << list; find(list, 'y'); list.del('z'); cout << list; find(list, 'y'); 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