Question

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 if the list is full

            int Lengthls( );   // return length

            void RetrieveItem(ItemType &x, bool &found); // retrieve x from the list, the

// boolean result is stored in found

            void ResetList( ); // currentPos=-1

            void GetNextItem(ItemType &x);    // get the next element from the list with

// respect to the currentPos

            void printElement( ); // print out the values of all the list elements, separated by

                                                // a white space

};

You need to use linear search in InsertItem( ), DeleteItem( ) and RetrieveItem( ). You should create one instances of this class and read in data from a file: char.dat, which can be downloaded from cis 200 web site and are assumed to be in the root directory of your project. Data in char.dat contains chars, which should be inserted into the object of UnsortedList. Note that you do not have any prior knowledge about how data values in char.dat.

You also need to write a subroutine, int numCharElement(ifstream &x), to determine the number of char elements in char.dat.

The skeleton of main( ) should be:

Int main( )

{

           UnsortedList x;

           // open char.dat

           ……   // set up x based on the data in char.dat

           x.printElement( );

           cout << numCharElement( ) << endl;

           return 0;

}

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

temp.dat 1 Hello 2 I am a boy. How are 3 you?

----------------------

File content: Hello I am a boy. How are you? Length: 23

----------------------

Code

#include <bits/stdc++.h>

using namespace std;

#define MAX_ITEMS 1000

typedef char ItemType;

class UnsortedList

{

private:

    int length;

    ItemType values[MAX_ITEMS];

    int currentPos;

public:

    UnsortedList(){

        length=0;

        currentPos-1;

    }

    void MakeEmpty(){

        length=0;

    }

    void InsertItem(ItemType x){

        if(length<MAX_ITEMS){

            values[length++]=x;

        }

    }

    void DeleteItem(ItemType x){

        int pos=0;

        while(pos<length && values[pos]!=x)

            pos++;

        for(int i=pos;i<length-1;i++)

            values[i]=values[i+1];

        length--;

    }

    bool IsFull(){

        return length==MAX_ITEMS;

    }

    int Lengthls(){

        return length;

    }                 

    void RetrieveItem(ItemType &x, bool &found){

        for(int i=0;i<length;i++){

            if(x==values[i]){

                found=true;

                return;

            }

        }

    }

    void ResetList(){ currentPos=-1; }

    void GetNextItem(ItemType &x){ x= values[currentPos+1]; }

    void printElement(){

        for(int i=0;i<length;i++)

            cout<<values[i]<<" ";

        cout<<endl;

    }   

};

int main(){

    fstream file("temp.dat");

    UnsortedList list;

    char c;

    while(file>>c)

        list.InsertItem(c);

    cout<<"File content: \n";

    list.printElement();

    cout<<"Length: "<<list.Lengthls()<<endl;

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
MUST BE ANSWERED BY USING C++ Question 1 Unsorted List Implement a template class UnsortedList as...
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...

  • Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts...

    Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...

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

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

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

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

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

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

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