Need help with my c++ code Create a QueueLinkedList of todo items using the foll
ID: 3594173 • Letter: N
Question
Need help with my c++ code
Create a QueueLinkedList of todo items using the following struct
struct TodoItem
{
std::string todo;
};
So, you will be working on a stack array which stores todo items, for example: "Go to the grocery store, clean the house, etc..
• Do NOT add a main method to any of your submitted files.
• DO write your own test drivers to test your code, but do not submit them.
• Your code needs to be readable, efficient, and accomplish the task provided.
• Make sure you delete your dynamically allocated memory in the appropriate
methods!
• When working with array-based implementations, there is a max size
available (set to 5 for this assignment in the header files). Display an error
message if attempting to add to a full array:
“Stack full, cannot add new todo item.”
Or “Queue full, cannot add new todo item.”
Note – this does not apply to linked-list implementations.
• If the stack or queue is empty when you try to pop or peek it, display an error
message:
“Stack empty, cannot pop an item.”
“Stack empty, cannot peek.”
“Queue empty, cannot dequeue an item.”
“Queue empty, cannot peek.”
• Make sure your code is commented enough to describe what it is doing.
Include a comment block at the top of all .cpp files with your name,
assignment number, and course instructor, and anyone you worked with.
• You must implement the functions as specified. You do not need any
additional functions. Each .hpp file has one or more getter methods that are
defined in the header. You do not need to implement these.
• Use the following code for your header files, and name them as indicated. DO
NOT MODIFY.
Header file:
#ifndef HW4_TODO_QUEUELINKEDLIST
#define HW4_TODO_QUEUELINKEDLIST
#include <string>
struct TodoItem
{
std::string todo;
TodoItem *next;
};
class TodoQueueLinkedList
{
public:
TodoQueueLinkedList();
~TodoQueueLinkedList();
bool isEmpty();
void enqueue(std::string todoItem);
void dequeue();
TodoItem* peek();
TodoItem* getQueueFront() { return queueFront; }
TodoItem* getQueueEnd() { return queueEnd; }
private:
TodoItem* queueFront; // the item in the list that will be dequeued next
TodoItem* queueEnd; // the item in the list that was most recently enqueued
};
#endif
You must use the header file. Please add comments and explain what you are doing and why. If you could create a main function to test it that would be great. Will give thumbs up for correct functional code THAT FOLLOWS THE INSTRUCTIONS AND DO NOT COPY AND PASTE IRRELEVANT CODE PLEASE USE THE HEADER FILE PROVIDED AS WELL AS MY CODE.
Here is my code so far please correct any mistakes you see:
#include<iostream>
#include<string>
#include "HW4-Todo-QueueLinkedList.hpp"
TodoQueueLinkedList::TodoQueueLinkedList(){
queueFront = new TodoItem;
queueFront->next=NULL;
}
bool TodoQueueLinkedList::isEmpty()
{
return (queueFront == NULL);
}
TodoQueueLinkedList::~TodoQueueLinkedList(){
while(queueFront)
{
TodoItem *k=new TodoItem;
k=queueFront;
queueFront=k->next;
delete(k);
}
}
void TodoQueueLinkedList::enqueue(std::string todoItem)
{
TodoItem *newItem= new TodoItem;
newItem->todo=todoItem;
newItem->next=NULL;
if(queueFront->next!=NULL)
{
queueEnd->next=newItem;
}
queueEnd=newItem;
}
void TodoQueueLinkedList::dequeue()
{
if(TodoQueueLinkedList::isEmpty())
{
std::cout<<"Queue empty, cannot dequeue an item."<<std::endl;
}
else
{
TodoItem *p;
p=queueFront;
queueFront=queueFront->next;
delete p;
}
}
TodoItem* TodoQueueLinkedList::peek()
{
if(!TodoQueueLinkedList::isEmpty())
{
TodoItem *p;
p=queueFront;
queueFront=p->next;
return p;
}
else {
std::cout<<"Queue empty, cannot peek.";
return NULL;
}
}
Explanation / Answer
Save these three files, compile and run. main.cpp is for testing purpose, you can insert your own testcases.
Here is a sample output
/*******************************/
Queue empty, cannot dequeue an item.
Queue empty, cannot peek.
peak value Go
Eront value Go
End value Grocery
Eront value to
End value store
/************************/
//============================================================================
// Name : dynamicArray-main.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include "HW4TodoQueueLinkedList.hpp"
using namespace std;
int main() {
cout<<"Todo Queue program"<<endl;
TodoQueueLinkedList tdq; // creating object of class TodoQueueLinkedList
TodoItem *ptr; // creating pointer of struct TodoItem to hold peek value
tdq.dequeue();
tdq.peek();
tdq.enqueue("Go ");
tdq.enqueue("to ");
tdq.enqueue("the ");
tdq.enqueue("Grocery ");
ptr=tdq.peek();
cout<<"peak value "<<ptr->todo<<endl;
ptr=tdq.getQueueFront();
cout<<"Eront value "<<ptr->todo<<endl;
ptr=tdq.getQueueEnd();
cout<<"End value "<<ptr->todo<<endl;
tdq.dequeue();
tdq.enqueue("store ");
ptr=tdq.getQueueFront();
cout<<"Eront value "<<ptr->todo<<endl;
ptr=tdq.getQueueEnd();
cout<<"End value "<<ptr->todo<<endl;
return 0;
}
/**************************/
/*
* HW4TodoQueueLinkedList.hpp
*
* Created on: 17-Oct-2017
* Author:
*/
#ifndef HW4_TODO_QUEUELINKEDLIST
#define HW4_TODO_QUEUELINKEDLIST
#include <string>
#define MAX_SIZE 5
struct TodoItem
{
std::string todo;
TodoItem *next;
};
class TodoQueueLinkedList
{
public:
TodoQueueLinkedList();
~TodoQueueLinkedList();
bool isEmpty();
void enqueue(std::string todoItem);
void dequeue();
TodoItem* peek();
TodoItem* getQueueFront() { return queueFront; }
TodoItem* getQueueEnd() { return queueEnd; }
private:
TodoItem* queueFront; // the item in the list that will be dequeued next
TodoItem* queueEnd; // the item in the list that was most recently enqueued
int elementcount; //to count no of element in queue
};
#endif /* HW4TODOQUEUELINKEDLIST_HPP_ */
/***************************/
/*
* HW4TodoQueueLinkedList.cpp
*
* Created on: 17-Oct-2017
* Author:
*/
#include "HW4TodoQueueLinkedList.hpp"
#include<iostream>
#include<string>
using namespace std;
TodoQueueLinkedList::TodoQueueLinkedList(){
//creating empty queue by default with constructor call
queueFront = NULL;
queueEnd = NULL;
elementcount=0;
}
bool TodoQueueLinkedList::isEmpty()
{
return (queueFront == NULL);
}
TodoQueueLinkedList::~TodoQueueLinkedList(){
while(queueFront)
{
TodoItem *k=queueFront; //assigning temp pointer to remove the dynamic allocation
queueFront=queueFront->next; //point to next pointer
delete(k); //remove unused node
}
}
void TodoQueueLinkedList::enqueue(std::string todoItem)
{
if(elementcount<MAX_SIZE)
{
TodoItem *newItem= new TodoItem;
newItem->todo=todoItem;
newItem->next=NULL;
if(queueFront==NULL)
{
queueFront=newItem; //if queue is empty add the new item and make both queueFront, queueEnd same
queueEnd = queueFront;
}
else
{
queueEnd->next=newItem; //update queueEnd
queueEnd=queueEnd->next;
}
elementcount++; //increment element count
}
else
{
cout<<"Queue Full, cannot add new todo item."<<endl;
}
}
void TodoQueueLinkedList::dequeue()
{
if(isEmpty())
{
std::cout<<"Queue empty, cannot dequeue an item."<<std::endl;
}
else
{
TodoItem *p = queueFront;
queueFront=queueFront->next;
delete p;
}
elementcount--;
}
TodoItem* TodoQueueLinkedList::peek()
{
if(!isEmpty())
{
return queueFront; //peek returns the queuefront
}
else {
std::cout<<"Queue empty, cannot peek."<<endl;
return NULL;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.