IN C++
Objectives: 1. Students will gain an experience to use doubly-linked list to solve a problem. Problem Descriptions: This project consists of two parts. The first part is to write a class that implements the ADT Bag using a doubly-linked list with a pointer to its first node. The second part of the project specifies how a program will use the class. Part I In a doubly linked list, each node can point to the previous node as well as to the next node. Figure 4-10 shows a doubly linked list and its head pointer. Define a class DoublyLinkedBag that implements the ADT bag by using a doubly linked list as shown in Figure 4-10. Able Baker ones | Smuth |-|wison - ·DI Wilson. headPtr .... Requirements 1. First define a class to represent a node in a doubly linked list by modifying the node class we used for linked base implementation of ADT Bag. Test it before you use it for the class DoublyLinkedBag. This node class should have the following data members: • A pointer to the next node A pointer to the previous node An item 2. Define and implement class DoublyLinkedBag following the example of the LinkedBag we discussed in the class. 3. Use the following program to test your ADT Bag implemented by doubly linked list
// This program tests the ADT Bag class which is implemented by a doubly // linked list. #include #include > #include "DLinkedBag.h" //doubly linked list bag using namespace std; void displayBag(const DLinkedBag& bag) { cout << "The bag contains " << bag.getCurrentSize() << "items:" << endl; CSCI 301 Computer Science II vector bagItems = bag. toVector(); int numberOfEntries = (int) bagItems.size(); for (int i = 0; i < numberOfEntries; i++) { cout << bagItems << " "; } // end for cout << endl<< endl; } // end displayBag void bagTester (DLinkedBag& bag) { cout << "isEmpty: returns " << bag.isEmpty() <<" should be 1 (true)" << endl; displayBag (bag); Summer 2022 string items[] = {"one", "two", "three", "four", "five", "one"}; cout << "Add 6 items to the bag: << endl; for (int i = 0; i < 6; i++) {
string items[] = {"one", "two", "three", "four", "five", "one"}; cout << "Add 6 items to the bag: << endl; for (int i = 0; i < 6; i++) { bag.add(items); } // end for displayBag (bag); cout << "isEmpty: returns << bag.isEmpty() <<"; should be 0 (false)" << endl; cout << "getCurrentSize: returns << bag.getCurrentSize() <<<"; should be 6" << endl; cout << "Try to add another entry: add(\"extra\") returns " << bag.add("extra") << endl; cout << "contains (\"three\"): returns << bag.contains ("three") <<"; should be 1 (true)" << endl; cout << "contains (\"ten\"): returns << bag.contains("ten") <<"; should be 0 (false)" << endl; cout << "getFrequency of (\"one\"): returns " << bag.getFrequencyOf("one") <<<< returns " cout << "remove(\"one\"): returns <<"; should be 1 (true)" << endl; cout << "getFrequency of (\"one\"): << bag.getFrequencyof("one") <<< should be 1" << endl; cout << "remove(\"one\"); returns " << bag.remove("one") <<"; should be 1 (true)" << endl; cout << "remove(\"one\"); returns <<"; should be 0 (false)" << endl; cout << endl; << bag.remove("one") should be 2" << endl; < bag; cout << "Testing the Link-Based Bag:" << endl; cout << "The initial bag is empty." << endl; bagTester (bag); cout << "All done!" << endl; return 0; } // end main
Part II Consider spell checking problem: a program reads a word and checks if it is spelled correctly. You can use a bag to serve as a dictionary that contains a collection of correctly spelled words. To check if a word is spelled correctly, you check whether it is contained in the Bag that serves as a dictionary. Use this scheme to create a spell checker for the words in an external file. To simplify your task, restrict your dictionary to a manageable size. Requirements Use DoublyLinkedBag in Part I. • Create the dictionary using a list of correctly spelled words in a file. Input • The name of an external file that contains words to check. Output • A list of incorrectly spelled words in the input file. An Example of Test Runs The output of your program might look like this: Enter the name of the file that contains words to check: myreport.txt The following words in the file "myreport.txt" are not spelled correctly: Stude Reseach Resul Outpt Thanks for using the spell checker system.
IN C++
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am