Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++PROGRAM WITH SINGLE LINK LIST create main.h and main.cpp source code files that contains the C++ source code for a program that Accepts an input

image text in transcribed

image text in transcribedimage text in transcribed

image text in transcribed

image text in transcribedimage text in transcribedimage text in transcribed

C++PROGRAM WITH SINGLE LINK LIST

create main.h and main.cpp source code files that contains the C++ source code for a program that

  1. Accepts an input file name from the user
  2. If the input file can not be opened, display an appropriate message and end the program.
  3. Create a list of the words that appear in the file, along with the number of times each word appears. Each list element must be an object of the Element class, and the list must be an object of the List class. All list operations must be done using the standard class operators, accessors, mutators, and constructors. Your list should not have any duplicate words. There is no need to display the list in any particular order (no need to sort the list).
Generic linked list element. element.h s/ #pragma once // System Libraries #include string 10stream stream regex> , // Data typedef typedef std::string Data; // Used instead of a custom Data class // Class definition class Ss Element public: Element; Element (Data) Element(const Element &);// Copy Constructor Element(Element &&); Element); Element & operator(const Element &) Element & operator = (Element 66); // Default constructor Parm constructor / Move constructor // Destructor // Copy assignment // Move assignment // Accessors and mutators Element get next) const; Element getprev) const; Data get datal) const; void // Return next pointer // Return prey, pointer // Return data pointer // Set next pointer // Set prey, pointer // Set data pointer set next (Element ) set prey(Element ) set dtlData ; // Operators friend std: :ostream & operator (const Element &, const Element &); friend bool operator = (const Element &, const Element &); private: Element next Element prev; Data data // Next pointer // Prev pointer // Data pointer Implementation for Element Class element.cpp s/ #include "element" h" Element:: Element) // Default constructor next -nullptr; prevnullptr; data = nullptr; Element: :Element(Data pam) // Parm constructor next -nullptr; prevnullptr; data - parm; Element:: Element(const Element & other) // Copy constructor next other.next; rey.-othec.prey: at-new Dta(*other.data); Element: Element (Element && other) // Move constructor next = other, next ; rev = other ev; ata - other.data; other.nextnullptr; other.prevnullptr other.dtanullptr; Element:Element) // Destructor delete data; Element:: Element(const Element & other) // Copy constructor next = other, next ; ata new Data(#0ther,data); Element: Element (Element && other) // Move constructor next other.next; ata-other.dat other.nextnullptr; other.prev nullptr; other.dtanullptr; Element:Element) // Destructor delete data; Element & Element: : operator = (const Element & other) // Copy assignment if (data-nullptr) delete data; data - new Data(other.data); return this; Element & Element: : operator (ELement 66 other) // Move assignment if (data != nullptr) delete data; data othe r. data other.data- nullptr; next other, next other.nextnullptr; prey-other prey: other.preynutLptr Peturn2ithis.nutptri ElementElement::get_next) const return next; Element Element::get pre) const return prey: Data Element::get dtt) const return dat void oid voidElement::set data(Data parm) 1 data-parm: Element:: set next Element * parm) E Lement::set_prev(ELement parm) next parn; prevparm std: :ostream & operator > (std::istream & in, Element & parm,) if (parn..data : nullpt r) parm..data: new Data; 1n >> parm.data; return in; bool operator (const Element & left, const Element & right) { return *left.data *right . data;} bool operator const Element & left, const Element & right) f return left.data > right.data; bool operator != (const Element & left, const Element & right) { return *left,data != right . data;} bool operator = (const Element & left, const Element & right) { return ,left,data >zk right,data * Linked List Class list.h #p ragna once // System Libraries #include #include string> #include :cmath #include iostream #include #include sstrean #include ionanp> #include // Time utilities // Limits // Limits // Limits // Assertions // Exceptions // Char type data // C++ String class / Math routines / Standard streams / File streams // String streams // Formatting // Regular expressions // Project libraries #include "element" h" class List public: List); / Default constructor // Parm, constructor / Copy Constructor / Move constructor / Destructor ist (Element) ist(const List&: List(List && List) List & operator = (const List &); List & operator = (List 66); / Copy assignment /I Move assignment // Operators friend std: :ostream & operator > (std::istream &, List &); friend bool operator(const List &, const List &); friend bool operator (const List &, const List &); friend bool operator != (const List &, const List &); friend bool operator > (const List &, const List &); friend bool operator (const List &, const List &); friend bool operator >= (const List &, const List &); // List Operations Element insert (Element *,int); Element remove(Element *,int); // Insert element at position // Remove by position OR value private: Element Element size t sizet head; tail; max_elements; used_elements // List head pointer // List tail pointer // Maximum capacity // Number of elements in the list Implementation for List class List.cpp #include "list.h" List : : List () / Default constructor head = nullptr; tail nullptr; max elements ; used elements ; List::List(Elementparm)List) // Parm constructor head parm tail = parm; used, elements = 1; List::List(const List & other): List) Element while (Surr!= nullptr){ this-insert(new Element(curr),-1): ur urr-get_next) List::List(List && otherList max elements other.max elenents; used elements other.used elements; head other. head tail = other. tail; other.max-elements = 0; other.used elements 0 other, head = nullptr; other. tail nullptr; List::-List) i while (head != nullptr) { tail head-get next) delete head; head tail; head nuuptr; tail nullptr; max elenents 0; used, elements = 0; List & List:; operator = (const List & other) return *this; List & List: : operator (List 66 other) return *this; std: :ostream& operator get-next(); return out: std:istream& operator > (std::istream & in, List & parm) f whitein.peek) !- EOF) Element * butter new Element; in buffer->get data)): if (in.good) true) parm.1nsetbutfer,-1): delete buffer: return in b001 operator {const List & left, const List & right) { return lett. used, elements right. used, elements; b001 operator != {const List & lett, const List & right) { return lett.used-elements != rightused-elements; bool operator (const List &left, const List &right) return left.used_elements > right.used_elements bool operator (const List &left, const List &right) return left.used_elements right.used_elements bool operator (const List &left, const List &right) return left.used_elementsget-next() ; 5ave ptr to removed element Point to new list head else 1t (posused_elements 1 pos8) //Case B2: Remove last elmeent (trim) Element *surr. head: Element * Prev= nulup while curr get _next)nullptr) Traversal Pointer Fina tast element Traverse nter save result curr Pointer to last element in l1st Set new last element in list Case B3: Remove from middle of list 1; Posit1on counter Traversal pointer Traversal tracker Find element at position Increment counter int counter ement * Element * prevnuttptr: while cucr. get nextnullptr&& counter get nexto) Unlink from the list Case C: Remove by value Use for traversal Element prey.nutiptr: while(resultnullptr) Used to track traversal Find (or not) target Track traversal it {*{target->get-datal)) =:result->get-datal))) Fesult resuit->get_next): break;// Break loop 1t target found Traverse if (result - nullptr) prev- set next result->get nexto) Did we find the target? Unlink from 11st f (result nullptr) used elements- return result; / Decrement 1ist size 1t we delete anything Return the result Element List::insert Element *target, int pos) Case 1: Empty List? / Case 2: Non-empty 1st eadtarget: Yes: now 1 element list Case 2a: push? target->set next(head) Yes: Set element next pointer Set head pointer ese t (pos get-next( ) ; Find end of the ist curr->set next target): curr->set next target) etse /Case 3b: Somewhere in the middle I... Yes: Set up traversal pointer Element curr: head: lementrenultptr: size-t counter = o; white curr>get_nextnullptr& counter set next surr prey->set nextitarget): Insert before next Link used elements++ return target: Generic linked list element. element.h s/ #pragma once // System Libraries #include string 10stream stream regex> , // Data typedef typedef std::string Data; // Used instead of a custom Data class // Class definition class Ss Element public: Element; Element (Data) Element(const Element &);// Copy Constructor Element(Element &&); Element); Element & operator(const Element &) Element & operator = (Element 66); // Default constructor Parm constructor / Move constructor // Destructor // Copy assignment // Move assignment // Accessors and mutators Element get next) const; Element getprev) const; Data get datal) const; void // Return next pointer // Return prey, pointer // Return data pointer // Set next pointer // Set prey, pointer // Set data pointer set next (Element ) set prey(Element ) set dtlData ; // Operators friend std: :ostream & operator (const Element &, const Element &); friend bool operator = (const Element &, const Element &); private: Element next Element prev; Data data // Next pointer // Prev pointer // Data pointer Implementation for Element Class element.cpp s/ #include "element" h" Element:: Element) // Default constructor next -nullptr; prevnullptr; data = nullptr; Element: :Element(Data pam) // Parm constructor next -nullptr; prevnullptr; data - parm; Element:: Element(const Element & other) // Copy constructor next other.next; rey.-othec.prey: at-new Dta(*other.data); Element: Element (Element && other) // Move constructor next = other, next ; rev = other ev; ata - other.data; other.nextnullptr; other.prevnullptr other.dtanullptr; Element:Element) // Destructor delete data; Element:: Element(const Element & other) // Copy constructor next = other, next ; ata new Data(#0ther,data); Element: Element (Element && other) // Move constructor next other.next; ata-other.dat other.nextnullptr; other.prev nullptr; other.dtanullptr; Element:Element) // Destructor delete data; Element & Element: : operator = (const Element & other) // Copy assignment if (data-nullptr) delete data; data - new Data(other.data); return this; Element & Element: : operator (ELement 66 other) // Move assignment if (data != nullptr) delete data; data othe r. data other.data- nullptr; next other, next other.nextnullptr; prey-other prey: other.preynutLptr Peturn2ithis.nutptri ElementElement::get_next) const return next; Element Element::get pre) const return prey: Data Element::get dtt) const return dat void oid voidElement::set data(Data parm) 1 data-parm: Element:: set next Element * parm) E Lement::set_prev(ELement parm) next parn; prevparm std: :ostream & operator > (std::istream & in, Element & parm,) if (parn..data : nullpt r) parm..data: new Data; 1n >> parm.data; return in; bool operator (const Element & left, const Element & right) { return *left.data *right . data;} bool operator const Element & left, const Element & right) f return left.data > right.data; bool operator != (const Element & left, const Element & right) { return *left,data != right . data;} bool operator = (const Element & left, const Element & right) { return ,left,data >zk right,data * Linked List Class list.h #p ragna once // System Libraries #include #include string> #include :cmath #include iostream #include #include sstrean #include ionanp> #include // Time utilities // Limits // Limits // Limits // Assertions // Exceptions // Char type data // C++ String class / Math routines / Standard streams / File streams // String streams // Formatting // Regular expressions // Project libraries #include "element" h" class List public: List); / Default constructor // Parm, constructor / Copy Constructor / Move constructor / Destructor ist (Element) ist(const List&: List(List && List) List & operator = (const List &); List & operator = (List 66); / Copy assignment /I Move assignment // Operators friend std: :ostream & operator > (std::istream &, List &); friend bool operator(const List &, const List &); friend bool operator (const List &, const List &); friend bool operator != (const List &, const List &); friend bool operator > (const List &, const List &); friend bool operator (const List &, const List &); friend bool operator >= (const List &, const List &); // List Operations Element insert (Element *,int); Element remove(Element *,int); // Insert element at position // Remove by position OR value private: Element Element size t sizet head; tail; max_elements; used_elements // List head pointer // List tail pointer // Maximum capacity // Number of elements in the list Implementation for List class List.cpp #include "list.h" List : : List () / Default constructor head = nullptr; tail nullptr; max elements ; used elements ; List::List(Elementparm)List) // Parm constructor head parm tail = parm; used, elements = 1; List::List(const List & other): List) Element while (Surr!= nullptr){ this-insert(new Element(curr),-1): ur urr-get_next) List::List(List && otherList max elements other.max elenents; used elements other.used elements; head other. head tail = other. tail; other.max-elements = 0; other.used elements 0 other, head = nullptr; other. tail nullptr; List::-List) i while (head != nullptr) { tail head-get next) delete head; head tail; head nuuptr; tail nullptr; max elenents 0; used, elements = 0; List & List:; operator = (const List & other) return *this; List & List: : operator (List 66 other) return *this; std: :ostream& operator get-next(); return out: std:istream& operator > (std::istream & in, List & parm) f whitein.peek) !- EOF) Element * butter new Element; in buffer->get data)): if (in.good) true) parm.1nsetbutfer,-1): delete buffer: return in b001 operator {const List & left, const List & right) { return lett. used, elements right. used, elements; b001 operator != {const List & lett, const List & right) { return lett.used-elements != rightused-elements; bool operator (const List &left, const List &right) return left.used_elements > right.used_elements bool operator (const List &left, const List &right) return left.used_elements right.used_elements bool operator (const List &left, const List &right) return left.used_elementsget-next() ; 5ave ptr to removed element Point to new list head else 1t (posused_elements 1 pos8) //Case B2: Remove last elmeent (trim) Element *surr. head: Element * Prev= nulup while curr get _next)nullptr) Traversal Pointer Fina tast element Traverse nter save result curr Pointer to last element in l1st Set new last element in list Case B3: Remove from middle of list 1; Posit1on counter Traversal pointer Traversal tracker Find element at position Increment counter int counter ement * Element * prevnuttptr: while cucr. get nextnullptr&& counter get nexto) Unlink from the list Case C: Remove by value Use for traversal Element prey.nutiptr: while(resultnullptr) Used to track traversal Find (or not) target Track traversal it {*{target->get-datal)) =:result->get-datal))) Fesult resuit->get_next): break;// Break loop 1t target found Traverse if (result - nullptr) prev- set next result->get nexto) Did we find the target? Unlink from 11st f (result nullptr) used elements- return result; / Decrement 1ist size 1t we delete anything Return the result Element List::insert Element *target, int pos) Case 1: Empty List? / Case 2: Non-empty 1st eadtarget: Yes: now 1 element list Case 2a: push? target->set next(head) Yes: Set element next pointer Set head pointer ese t (pos get-next( ) ; Find end of the ist curr->set next target): curr->set next target) etse /Case 3b: Somewhere in the middle I... Yes: Set up traversal pointer Element curr: head: lementrenultptr: size-t counter = o; white curr>get_nextnullptr& counter set next surr prey->set nextitarget): Insert before next Link used elements++ return target

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions