Question

1. You have to use a .h file for class definitions and two (as specified below) cpp file for the C++ implementation. 2. You have to generate the list from scratch and check for overflow 3. I will be giving extra points (up to 2 points this time) to students that exceed the requirements posed in the assignments. For example, extensive testing, checking for exceptions, and usable user interface. 4. Please submit enough screenshots showing compiling and execution under Linux Assignment Instructions: Write a main program that uses the list based queue class (defined below) to check if an input string is of the form abnan, meaning n times the character ‘a, followed by n times the charter b, followed by n times the character a For example, consider: ‘aaaabbbbaaaa vs. ‘aaabbbbaa or abaab bab You should modularize your program using: AnBnAn.cpp, LQ.cpp and LQ.h. The program should use the following algorithm: 1. Scan the string from left to right until reaching the NULL terminator. 2. If you reach any character that is different than a or b then the string is invalid 3. If the string does not start with an a then the string is invalid 4. Next, for every occurrence of the character a insert the character b to the queue. 5. Once you reach the first bcharacter in the string do: for each b in the string remove a b from the queue and insert an a to the queue. Finally, once you reach the first a character in the string remove an a character from the queue for every a character encountered. If (5) cannot be followed (e.g., an a is encountered within a b characters the string is considered invalid. The string is valid if upon reaching the NULL character the queue is empty and no overflow or underflow has accrued. 6. The Queue Class Write a class LLQ that implements a Queue in linked a list of characters. The class should include the following utilities: 1. 2. 3. 4. A constructor and a destructor At least one inline An insert function that gets a character value and inserts it at the end of the queue A remove function that gets the value of the first queue element and stores it in a character variable A Boolean function is full that returns true if the queue is full (otherwise it returns false). This function should be called before inserting items to the queue. getter and one inline setter for private members 5. 6. A Boolean function is empty that returms true if the queue is empty (otherwise it returns false). This function should be called before removing items from the queue

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

AnBnAn.cpp
---------------------------------------------------------
#include "LQ.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{

    LLQ queue;

    int userOption = -1;
    string test = "aaaabbbbaaaa";
    int stringLength = test.length();

    for (int i = 0; i < stringLength; i++) //placing the string character into a linked list
    {
        queue.createList(test[i]);
    }


    while (userOption != NULL)
    {
        cout << "1. View list" << endl;
        cout << "2. Insert to list" << endl;
        cout << "3. Remove from list" << endl;
        cout << "4. Inspect list " << endl;
        cout << "0. Exit Program" << endl << endl;
        cout << "Enter a option: ";

        cin >> userOption;

        switch (userOption)
        {
            case 1:
                cout << "\ncase 1 test, view list\n" << endl;

                queue.viewList();


                break;

            case 2:
                cout << "\ncase 2 test" << endl;

                queue.insert('g');
                break;

            case 3:
                cout << "\ncase 3 test" << endl;
                if (!queue.is_empty())
                    queue.remove();
                else
                    cout << "\nERROR Queue is empty" << endl;
                break;

            case 4:
                cout << "\ncase 3 test" << endl;
                if (!queue.is_empty())
                {
                    if (queue.inspectList())
                        cout << "\nlist is valid\n" << endl;
                    else
                        cout << "\nlist is not valid does not follow rules" << endl;
                }
                else
                    cout << "\nERROR Queue is empty" << endl;

                break;
        }
        cout << endl;
    }

    return 0;
}
-----------------------------------------------------------
LQ.cpp
-----------------------------------
#include "LQ.h"
#include <iostream>


using namespace std;

bool LLQ::is_full() const
{
    return false;
}

bool LLQ::is_empty() const
{
    return (head==NULL);
}

void LLQ::remove()
{
    //removing the first element in the list
    node *nodePtr;

    nodePtr = head;

    head = nodePtr->next;

}

void LLQ::insert(char insertChar)
{
    node *newNode;
    node *nodePtr;

    //placing a value into newNode and assigning and pointing the next to null
    newNode = new node;
    newNode->character = insertChar;
    newNode->next = NULL;
    if (!head)
    { //if list is empty
        head = newNode;
        nodePtr = head;
        newNode->next = NULL;
    }
    else
    {//if list not empty
        nodePtr = head;

        while (nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }

}

void LLQ::createList(char insert)
{

    node *newNode;
    node *nodePtr;

    newNode = new node;
    newNode->character = insert;
    newNode->next = NULL;

    if (!head)
    { //if list is empty
        head = newNode;
        nodePtr = head;
        newNode->next = NULL;
    }
    else
    {//if list not empty
        nodePtr = head;

        while (nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
}

void LLQ::viewList() const
{
    struct node *ptr;

    if (head == NULL)
        cout << "\nThe Queue is empty" << endl;
    else
    {
        ptr = head;
        cout << "\nCurrent Queue:\n" << endl;

        while (ptr != NULL)
        {
            cout << ptr->character;
            ptr = ptr->next;
        }
    }
    cout << endl;

}

bool LLQ::inspectList()
{
    bool isValid = true;

    node *nodePtr;

    nodePtr = head;

    if (nodePtr->character != 'a') //checkin if first element is not 'a'
        isValid = false;
    else
    {
        while (nodePtr->next && isValid == true) //checking for characters other than 'a' or 'b'
        {

            if (nodePtr->character != 'a' || nodePtr->character != 'b')
                isValid = false;
            nodePtr = nodePtr->next;

        }
    }


    return isValid;
}

LLQ::~LLQ()
{
    node *nodePtr; //to traverse the list
    node *nextNode; //to point to the next node

    nodePtr = head;

    while (nodePtr != NULL)
    {
        //save a pointer to the next node
        nextNode = nodePtr->next;

        //delete the current node
        delete nodePtr;

        //position nodePtr at the next node
        nodePtr = nextNode;
    }
}
---------------------------------------------------------------------------
LQ.h
---------------------------------
#ifndef LQ_H
#define LQ_H
#include <iostream>

class LLQ {

private :

    struct node
    {
        char character;
        node *next;
    };

    node *head;

public:

    //constructor
    LLQ()
    {head = NULL;}

    //destructor
    ~LLQ();

    //functions
    bool is_full() const;
    bool is_empty() const;
    void remove();
    void insert(char);
    void viewList() const;
    void createList(char);
    bool inspectList();


};
#endif

iNCLionProjectsLQ] - .AnBnAn.cpp File Edit Yew Navigate Code Beractor Run ļools VCS Window Help CLion 2017.2 LQAnBnAn.cpp a P

Add a comment
Know the answer?
Add Answer to:
You have to use a h file for class definitions and two (as specified below) cpp...
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
  • Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do...

    Purpose: Once you have completed and tested the functionality of your MyStringBuilder class, you will do a rough comparative test to see how long some of the operations take relative to the predefined StringBuilder class and the predefined String class. Details: You will complete a simple simulation to compare the times required for three operations: append(char c) , insert(int loc, char c) and delete(int start, int stop) These methods are defined in the StringBuilder and MyStringBuilder classes, but you will...

  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity...

    JAVA Implement a MyQueue class which implements a queue using two stacks. private int maxCapacity = 4; private Stack stack1; private Stack stack2; Note: You can use library Stack but you are not allowed to use library Queue and any of its methods Your Queue should not accept null or empty String or space as an input You need to implement the following methods using two stacks (stack1 & stack2) and also you can add more methods as well: public...

  • DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to...

    DIRECTIONS FOR THE WHOLE PROJECT BELOW BigInt class The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide.   A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to...

  • Using C++, create a doubly linked list data structure that stores strings. At a minimum, you...

    Using C++, create a doubly linked list data structure that stores strings. At a minimum, you must have a List class that contains the list functionality (including an insert function) and a linkable object ("link node") class. For convenience, you may include the data directly or the data may be external to the link node, connected with a reference. You may use an inner class for the LinkNode and/or include the LinkNode class with the List class file if you...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • This lab will give you a practice with both queue and stack ADTs. In this work,...

    This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on. Sample Output: Please enter a string of characters: abba The given string is a palindrome. Want to examine another string? (y/n): y Please enter a string of...

  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

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