Question

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 in the

                                                    // list

                void Insert(ItemType item); // if item is not in the list, insert it into the list

                void Delete(ItemType item); // delete item from the list

                void Print(); // Print all the items in the list on screen

                int Length();   // return the number of items in the list

                ~UList(); // we do not implement destructor: programmer should be responsible to deallocate the linked list

private:

                NodeType<ItemType> * listPtr;

};

Task 1: Implement the template class UList on the basis of the above skeleton. Compile your program.

Task 2: Write a simple driver that reads values from file float.dat, inserts them into an instance, x, of the list, prints the length of the final list, and prints the values of all the list elements.

Task 3: Add code to your driver to test the remaining member functions. Delete 2.0, 9.0, and 6.2 from x and print the list to be sure they are gone.

Task 4: Create another instance, y, through the copy constructor such that the content of y is the same as that of x, but a deep copy should be done. Print out the all the list elements of y.

Task 5: Declare an instance, z, and assign the content of x to z through the operation “=”. Print out the all the list elements of z.

Task 6: Test if 9.0 is in the lists x and y via the member function: IsThere(ItemType item) and print out the test results.

Task 7: What is the Big-O notation for the time complexity of IsThere( ) function?

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

Ans :

/C++ program

#include<iostream>
using namespace std;
template<class ItemType>

struct NodeType

{

ItemType item;

NodeType* next;

};

template<class ItemType>
class UList

{

public:

UList(){
listPtr = NULL;
}

UList(const UList &x){
NodeType<ItemType> * head = x.listPtr;
while(head){
this->Insert(head->item);
head = head->next;
}
}

bool IsThere(ItemType item) const{
NodeType<ItemType> * head = listPtr;
while(head){
if(head->item == item)return true;
head = head->next;
}
return false;

}
void Insert(ItemType item){
  
NodeType<ItemType> *head = listPtr;
NodeType<ItemType> *newNode = new NodeType<ItemType>;
newNode->item = item;
newNode->next = NULL;
if(listPtr==NULL){
listPtr = newNode;
return;
}
while(head->next){
if(head->item == item)return;
head = head->next;
}  
head->next = newNode;

}
void Delete(ItemType item){
NodeType<ItemType>* temp = listPtr, *prev;
if (temp != NULL && temp->item==item)
{
listPtr = temp->next;
delete(temp);
return;
}
while (temp != NULL && temp->item != item)
{
prev = temp;
temp = temp->next;
}

if (temp == NULL) return;
prev->next = temp->next;
  
delete(temp);

}

void Print(){
NodeType<ItemType> *head = listPtr;
while(head){
cout<<head->item<<" ";
head = head->next;
}
cout<<"\n\n";
}

int Length(){
NodeType<ItemType> *head = listPtr;
int count = 0;
while(head){
count++;
head = head->next;
}
return count;
}

~UList(){
NodeType<ItemType> *head = listPtr,*next;
  
while(head){
next = head->next;
delete(head);
head = next;
}
listPtr = NULL;
}

private:

NodeType<ItemType> * listPtr;

};

int main(){
UList<float>list;
list.Insert(5.0);
list.Insert(6.0);
list.Insert(5.0);
list.Insert(7.0);
list.Insert(2.0);
  
list.Print();
  
list.Delete(6.0);
  
list.Print();
  
return 0;
  
}

Thank you..

Add a comment
Know the answer?
Add Answer to:
c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...
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
  • 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...

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

    I want the full code for this part and Its needs to be done in C++ 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...

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

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

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

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

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

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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