COS2611 2024 Assignment 1 memo
Question 1 The difference between a linear representation of a stack and a
linked implementation of a stack is that IN A LINKED IMPLEMENTATION OF
STACKS:
1. Elements are stored as nodes with references/pointers to the next element.
2. Elements are typically stored in a fixed-size array or a dynamic array.
3. The stack operations are performed based on the indices of the elements within the array.
4. None of the options given.
Question 2 Consider the provided code. Its objective is to establish a stack,
display the stack's elements, and report the stack's size.
THE USER EXPECTED THE FOLLOWING OUTPUT:
FredMaryPeterJohnThe size of the class: 4
However, the output does not align with the user's expectations.
How can the code be modified to generate the anticipated output?"
//the code
#include <stack>
#include <string>
int main()
{ std::stack<std::string> stackClass;
stackClass.push("John");
stackClass.push("Peter");
stackClass.push("Mary");
stackClass.push("Fred");
//....code to be modified
while (!stackClass.empty())
{
std::cout << stackClass.top();
stackClass.pop();
}
int size = stackClass.size();
,std::cout << "the size of the class: " << size;
//... end code to be modified return 0;}
a. while (!stackClass.empty())
{ std::cout << stackClass.top();
stackClass.pop();
}
int size = stackClass.size();
std::cout << "The size of the class: " << size << "\n";
b. while (!stackClass.empty())
{
std::cout << stackClass.top() << "\n";
stackClass.pop();
}
int size = stackClass.size();
std::cout << "The size of the class: " << size;
c. while (!stackClass.empty())
{ stackClass.pop();
std::cout << stackClass.top() << "\n";
}
int size = stackClass.size();
std::cout << "The size of the class: " << size;
d. int size = stackClass.size();
while (!stackClass.empty())
{ stackClass.pop();
std::cout << stackClass.top() << "\n";
}
std::cout << "The size of the class: " << size;
, e. None of the provided options.
Question 3 Consider the following code. In this code the function
displayClass(stackClass) is called.
Which of the following code (i) represents the correct implementation of the DISPLAYCLASS
function?
#include <stack>
#include <string>//...(i)... missing code for the function.
int main()
{
std::stack<std::string> stackClass;
stackClass.push("John");
stackClass.push("Peter");
stackClass.push("Mary");
DISPLAYCLASS(stackClass);
return 0;
}
a. void displayClass(std::stack<std::string> myStack)
{
while (!myStack.empty())
{
std::cout << myStack.top() << "\n";
myStack.pop();
}
}
b. void displayClass(std::stack<std::string> myStack)
{
while (!myStack.empty())
{
myStack.pop();
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through EFT, credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying this summary from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller foxNotes. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy this summary for R144,33. You're not tied to anything after your purchase.