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();
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();