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?
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..
c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...
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++
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 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. 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...
This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...
(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...
Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header file QueueLinked.h. // QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include <iostream> using namespace std; template <typename T> class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void...
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)...
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...
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...