Need help with my c++ code Create a StackLinkedList of todo items using the foll
ID: 3594176 • Letter: N
Question
Need help with my c++ code
Create a StackLinkedList 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_STACKLINKEDLIST
#define HW4_TODO_STACKLINKEDLIST
#include <string>
struct TodoItem
{
std::string todo;
TodoItem *next;
};
class TodoStackLinkedList
{
public:
TodoStackLinkedList();
~TodoStackLinkedList();
bool isEmpty();
void push(std::string todoItem);
void pop();
TodoItem* peek();
TodoItem* getStackHead() { return stackHead; }
private:
TodoItem* stackHead; // pointer to the TodoItem that will be popped next
};
#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<string>
#include<iostream>
#include "HW4-Todo-StackLinkedList.hpp"
TodoStackLinkedList::TodoStackLinkedList(){
stackHead= new TodoItem; //Assigning memory to staack head
stackHead->next=NULL; //the next item to an empty stacktop or stackHead is NULL
}
TodoStackLinkedList::~TodoStackLinkedList(){
delete(stackHead); //deleting the stackHead will leave the rest of the memory useless
}
void TodoStackLinkedList::push(std::string todoItem)
{
TodoItem *item=new TodoItem; //Assignng memory to the new element
item->next=stackHead; // pushing the new element to the top of the stack
item->todo=todoItem; // inserting the todoitem to the stacktop
}
void TodoStackLinkedList::pop()
{
if(TodoStackLinkedList::isEmpty())
{
std::cout<<"Stack empty, cannot pop an item.";
}
else
{
TodoItem *lastItem; // here lastitem is the stackHead which will be popped
lastItem=stackHead;
stackHead=lastItem->next; //assigning stackHead to the element after it
delete(lastItem);
}
}
bool TodoStackLinkedList::isEmpty()
{
if(stackHead==NULL)
{
return false;
}
else
{
return true;
}
}
TodoItem* TodoStackLinkedList::peek()
{
if(TodoStackLinkedList::isEmpty())
{
std::cout<<"Stack empty, cannot peek.";
}
else
{
TodoItem *lastItem; // here lastitem is the stackHead which will be popped
lastItem=stackHead;
stackHead=lastItem->next; //assigning stackHead to the element after it
delete(lastItem);
}
return 0;
}
Explanation / Answer
-------------------------- HW4-Todo-StackLinkedList.hpp -------------------
#ifndef HW4_TODO_STACKLINKEDLIST
#define HW4_TODO_STACKLINKEDLIST
#include<iostream>
#include <cstring>
#include<cstdlib>
using namespace std;
namespace myTodoStackLinkedList
{
// structure of a node of the linked list
struct TodoItem
{
// store the value of the node
string todo;
// point to the next node in the list
struct TodoItem *next;
};
class TodoStackLinkedList
{
public:
// constructor
TodoStackLinkedList();
// destructor
~TodoStackLinkedList();
// check if the list is empty
bool isEmpty();
// push an element into the list
void push(string todoItem);
// pop an element from the list
void pop();
// peek the top elemet
struct TodoItem* peek();
struct TodoItem* getStackHead() { return stackHead; }
private:
// head of the list
struct TodoItem* stackHead;
};
}
#endif
-------------------------- HW4-Todo-StackLinkedList.cpp -------------------
#include "HW4-Todo-StackLinkedList.hpp"
using namespace myTodoStackLinkedList;
// constructor
TodoStackLinkedList::TodoStackLinkedList()
{
// create an empty list
stackHead= NULL;
}
// destructor
TodoStackLinkedList::~TodoStackLinkedList()
{
// emptying the list
stackHead= NULL;
}
// push element into the stack
void TodoStackLinkedList::push(string todoItem)
{
//Assignng memory to the new node
struct TodoItem *item = new(struct TodoItem);
// pushing the new element to the top of the stack
item->next = stackHead;
// inserting the todoitem to the stacktop
item->todo = todoItem;
// set item as the head node
stackHead = item;
}
// pop element fom stack
void TodoStackLinkedList::pop()
{
// if stack is empty
if(isEmpty())
cout<<"Stack empty!";
else
{
TodoItem *temp = stackHead;
// set the element after stackHead as the new head of the list
stackHead = stackHead -> next;
// unallocate the mamory
delete(temp);
}
}
bool TodoStackLinkedList::isEmpty()
{
// then list is empty
if(stackHead == NULL)
return true;
return false;
}
// the top of the stack is returned
TodoItem* TodoStackLinkedList::peek()
{
// if list is empty
if(isEmpty())
{
cout<<"Stack empty!";
return NULL;
}
return stackHead;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.