Question

Use a linked list to implement the following skeleton of an unsorted list typedef int ItemType: struct NodeType ItemType item; NodeType next; class List List(); // default constructor List( const List &x); I copy constructor: deep copy is required List & operator = (const List &x); // assignment operator: deep copy. 1s required bool IsThere(ItemType x); identify if x is in the list void Insert(ItemType x); insert an item x void delete(ItemType x); // delete all the items x from the list void print print out all the items in the list void printaddress(); // print out the memory addresses of all the list elements int Length); //return the length of the list -List); lI destructor: should deallocate the linked list

I want the full code for this part and Its needs to be done in C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to implements an unsorted linked list

#include <iostream>

using namespace std;

typedef int ItemType;

struct NodeType

{

               ItemType item;

               NodeType *next;

};

class List{

private:

               NodeType *head;

               int num_elements;

public:

               List();//default constructor

               List(const List &x); // copy constructor : deep copy required

               List& operator=(const List &x); //assignment operator : deep copy required

               bool isThere(ItemType x); //identify x is in the list

               void Insert(ItemType x); //insert an item x

               void deleteItem(ItemType x); //delete all the items x from the list

               void print(); //print out all the items in the list

               void printaddress(); // print out the memory addresses of all elements in the list

               int Length(); // returns the length of the list

               ~List() ;// destructor : should deallocate the linked list

};

List::List()

{

               head = nullptr;

               num_elements =0;

}

List::List(const List &x)

{

               head = nullptr;

               num_elements = 0;

               NodeType *curr = x.head;

               while(curr != nullptr)

               {

                              Insert(curr->item);

                              curr = curr->next;

               }

}

List& List::operator =(const List&x)

{

               if (this != &x) ///avoid self-copy

               {

                              NodeType *curr;

                              while(head != nullptr)

                              {

                                             curr = head;

                                             head = head->next;

                                             delete curr;

                              }

                              head = nullptr;

                              num_elements = 0;

                              curr = x.head;

                              while(curr != nullptr)

                              {

                                             Insert(curr->item);

                                             curr = curr->next;

                              }

               }

               return *this;

}

bool List::isThere(ItemType x)

{

               NodeType *curr = head;

               while(curr != nullptr)

               {

                              if(curr->item == x)

                                             return true;

                              curr = curr->next;

               }

               return false;

}

void List::Insert(ItemType x)

{

               NodeType *node = new NodeType;

               node->item = x;

               node->next = nullptr;

               if(head == nullptr)

               {

                              head = node;

               }else{

                              NodeType *curr = head;

                              while(curr->next != nullptr)

                                             curr = curr->next;

                              curr->next = node;

               }

               num_elements++;

}

void List::deleteItem(ItemType x)

{

               NodeType *curr;

               while(head != nullptr && head->item == x)

               {

                              curr = head;

                              head = head->next;

                              delete curr;

                              num_elements--;

               }

               if(head != nullptr)

               {

                              curr = head->next;

                              NodeType *preCurr = head;

                              while(curr != nullptr)

                              {

                                             if(curr->item == x)

                                             {

                                                            preCurr->next = curr->next;

                                                            curr = curr->next;

                                                            num_elements--;

                                             }else

                                             {

                                                            preCurr = curr;

                                                            curr = curr->next;

                                             }

                              }

               }

}

void List::print()

{

               if(head == nullptr)

                              cout<<" Empty list"<<endl;

               else{

                              cout<<" List : ";

                              NodeType *curr = head;

                              while(curr !=nullptr)

                              {

                                             cout<<curr->item<<" ";

                                             curr = curr->next;

                              }

                              cout<<endl;

               }

}

void List::printaddress()

{

               if(head == nullptr)

                              cout<<" Empty List"<<endl;

               else{

                              cout<<" List address : ";

                              NodeType *curr = head;

                              while(curr != nullptr)

                              {

                                             cout<<curr<<" ";

                                             curr = curr->next;

                              }

                              cout<<endl;

               }

}

int List::Length()

{

               return num_elements;

}

List::~List()

{

               NodeType *curr;

               while(head != nullptr)

               {

                              curr = head;

                              head = head->next;

                              delete curr;

               }

               num_elements =0;

}

int main() {

               // test the List class

               List l1,l2;

               for(ItemType i=0;i<5;i++)

                              l1.Insert(i);

               cout<<" L1 : "<<endl;

               l1.print();

               l2 = l1;

               cout<<" L2 : "<<endl;

               l2.print();

               cout<<" 4 is in L1 : "<<l1.isThere(4)<<endl;

               cout<<" 10 is in L1 : "<<l1.isThere(10)<<endl;

               List l3(l2);

               cout<<" L3 : "<<endl;

               l3.print();

               l3.Insert(2);

               l3.deleteItem(2);

               cout<<" L3 address : "<<endl;

               l3.printaddress();

               cout<<" L3 : "<<endl;

               l3.print();

               cout<<" Length of l1 : "<<l1.Length()<<endl;

               return 0;

}

//end of program

Output:

L1: List: 0 1 2 3 4 L2 : List: 0 1 2 3 4 4 is in L1: 1 10 is in L1: 0 L3 List: 0 1 2 3 4 L3 address : List address: 0x600069710 0x600069730 0x600069770 0x600069790 L3 List: 0 1 3 4 Length of 11: 5

Add a comment
Know the answer?
Add Answer to:
I want the full code for this part and Its needs to be done in C++...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given...

    MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given the following skeleton of an unsorted list class that uses an unsorted linked list: template < class ItemType > struct NodeType {                 ItemType item;                 NodeType* next; }; template < class ItemType > class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign...

  • 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 va...

    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    //...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as...

    MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as defined by the following skeleton: #define MAX_ITEMS 10 typedef char ItemType; class UnsortedList {        private:             int length; ItemType values[MAX_ITEMS]; int currentPos;        public:             SortedList( ); // default constructor: lenght=0, currentPos=-1             void MakeEmpty;    // let length=0             void InsertItem(ItemType x);   // insert x into the list                 void DeleteItem(ItemType x); // delete x from the list bool IsFull( );   // test...

  • Please give a answer to both tasks with a screenshot of the running file. Thanks Consider...

    Please give a answer to both tasks with a screenshot of the running file. Thanks Consider that individual nodes in an unsorted linked list have the following definition class Vector public: Vector(int s = 0)( // makes Size //allocates s space, // makes all entries s, kes all entries e Vector (const Vector & rhs) // copy constructor // makes self a deep copy of rhs Vector operator (const Vector & rhs)(// makes self a deep copy of rhs nVector...

  • I'm just not sure how to tackle all the template class this header wants me to...

    I'm just not sure how to tackle all the template class this header wants me to write. I got this far into making the template for public and private and would like help on the template functions. Thank you! This is in C++ by the way. #include <iostream> #include <cassert> using namespace std; #ifndef ARRAYLIST_H #define ARRAYLIST_H template<typename T> class arrayList { public:    arrayList(); //Constructor with default parameter. //Sets maxSize = 100 and length = 0 if no parameter...

  • please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class...

    please write the code in C++ 2 Base class File 3 Derived class PDF 3.1 Class declaration • The class PDF inherits from File and is a non-abstract class 1. Hence objects of the class PDF can be instantiated. 2. To do so, we must override the pure virtual function clone) in the base class • The class declaration is given below. • The complete class declaration is given below, copy and paste it into your file. . It is...

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • C++ Implement a templated class list and listnode. You may add methods/functions as you see fit....

    C++ Implement a templated class list and listnode. You may add methods/functions as you see fit. Test these classes. I have left all of the implementation as an exercise for you. template< class NODETYPE > class List;  // forward declaration template<class NODETYPE> class ListNode {    friend class List< NODETYPE >; // make List a friend public:    ListNode( const NODETYPE &newData);  // copy constructor    NODETYPE getData() const;      // return data in the node private:    NODETYPE data;                 // data    ListNode< NODETYPE > *nextPtr; // next node...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT