Algorithms: • push: The process for pushing to the stack: o Pass data pass only an int id and string pointer to the stac

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Algorithms: • push: The process for pushing to the stack: o Pass data pass only an int id and string pointer to the stac

Post by answerhappygod »

Algorithms Push The Process For Pushing To The Stack O Pass Data Pass Only An Int Id And String Pointer To The Stac 1
Algorithms Push The Process For Pushing To The Stack O Pass Data Pass Only An Int Id And String Pointer To The Stac 1 (65.66 KiB) Viewed 14 times
Algorithms Push The Process For Pushing To The Stack O Pass Data Pass Only An Int Id And String Pointer To The Stac 2
Algorithms Push The Process For Pushing To The Stack O Pass Data Pass Only An Int Id And String Pointer To The Stac 2 (87 KiB) Viewed 14 times
Algorithms Push The Process For Pushing To The Stack O Pass Data Pass Only An Int Id And String Pointer To The Stac 3
Algorithms Push The Process For Pushing To The Stack O Pass Data Pass Only An Int Id And String Pointer To The Stac 3 (24.13 KiB) Viewed 14 times
Algorithms: • push: The process for pushing to the stack: o Pass data pass only an int id and string pointer to the stack. Note that a string is an ADT itself so it must be passed by reference. The prototype for push should be one of the following. whichever notation and calls you prefer to use: o If there is room in the stack: ■ Test the validity of the data (positive int and non-empty string). Do not proceed to push if the data is invalid, return false. ■ ■ ■ bool push(int, string&); bool push(int, string"); If there is not room in the stack, return false. Dynamically create a struct Data to hold the data. Put the id and string in the struct Data. Increment the stack counter. Push the pointer for the struct onto the stack. Return true. 0 o Remember, only one actual return statement per function. Algorithms are not written like code, they are written to be logically consistent. The algorithm above looks like there are two or three returns, but in the actual code there should be only one return statement. • pop: The process for popping from the stack. o ■ ■ Pass an 'empty' struct Data to the stack (by reference). Note that this means main will have a struct declared and it is passed by reference to the stack so the stack can fill it. This avoids a costly return by value from pop. Make sure you understand this technique well, it is common in programming. o If the stack is not empty... W Get the data from the top of the stack and put it in the struct Data passed from the caller. Delete the allocated memory from the top of the stack. Decrement the stack counter. "Return" the data to the caller. Note that you will not actually "return" the data. The act of placing the data in the struct Data passed from the caller is the "return." Return true (notice this pop is different from the last assignment, now it returns a bool).
Fill the passed struct with -1 and (empty string). ■ ■ Return false (notice this pop is different from the last assignment, it returns a bool). • peek: The process for peek() is the same as pop() but do not deallocate or decrement the counter. Make your peek identical to pop() but simply don't decrement the counter. • Is Empty: The process for isEmpty() is to return true or false based on the top being-1 or not. This can be done in one very simple line of code. Think about it and see if you can do it in one line. You don't have to make it one line, but it's something to think about to improve your code. o If the stack is empty Remember ONE AND ONLY ONE return per function. If you need more than one, your code is not structured properly. Requirements: Follow the Assignment Specific Instructions using this GitHub assignment invite https://classroom.github.com/a/S5UKcNnm • You are given all the files you need for the assignment except.gitignore. Make a proper.gitignore first and commit it. ● odata.h: Do not modify this file but you should study it. o functions.cpp and functions.h: Modify these as needed. You shouldn't need to, but are free to use this for your own functions and/or to modify the function that is there. o main.h and main.cpp: Modify these as needed. Follow the comment instructions in main.cpp. stack.h: Modify this file as needed except for the attributes, do not modify or add to them. o o stack.cpp: This is essentially blank, it's up to you to fill this in with the correct stack code. README.md: Modify this file to describe your work. o • Place your comment headers on all files where indicated. • Write a complete and proper stack in stack.cpp/h conforming to the traditional stack definition given above in the algorithms section and as explained in class, following all best practices and loose coupling. • When your program runs, the user must pass in the size of the stack from the command line. For example, a.out 5 or a.exe 10. Your program (main) must check the program is called correctly with one and only one parameter which is an int. If the program is not called correctly, main() must exit gracefully by telling the user they must enter a single parameter at the command line which represents an int for the stack size. Your program must account for every possible user error and handle them accordingly (try everything!). Your stack must be able to be any size from 2 to n where n is any positive integer. Remember when you structure main() you may have only one return statement and may not use any functions that abruptly end the program (for example exit(); ). ● • Make sure you properly allocate and deallocate memory, including deallocating whatever is left in the stack when it exits (i.e. use your destructor to do final clean up). Write complete and exhaustive tests for your stack in main.cpp. o DO NOT use user interaction to test your code. Your main() should be an automated test program demonstrating your stack is robust and fully functional. O Do proper reporting back to the user demonstrating your stack is working and being tested. You must report back from main to the user proving your stack works. Because your stack can be any size, you must write automated tests that account for this. For example, if the user passes 3 for the stack size, you should be doing on the order of dozens of tests. If the user passes 10, then hundreds of tests. if the user passes 100, then thousands of tests. This means your testing must be dynamic and account for the stack size.
. Do not allocate memory for the structs outside the stack. The stack push() method accepts a positive int and a non-empty string, test those to make sure they are valid (positive and non-empty respectively), then creates the struct with that data and pushes the pointer to the struct to the stack. ● ● Do not deallocate memory for the struct outside the stack. The stack pop() method retrieves an "empty" struct, and "returns" the data, and then deallocates the stack's internally allocated memory. All submission and good practice guidelines apply.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply