#include <iostream>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
int main()
{
//The first Stack
stack<string> stack1;
//Inserting into the first stack
stack1.push("1. Sam Williams 60");
stack1.push("2. John Phoenix 85");
stack1.push("3. Simon Johnson 75");
stack1.push("4. Sarah Khosa 81");
stack1.push("5. Mat Jackson 38");
stack1.push("6. Nick Roberts 26");
stack1.push("7. Isaac Wayne 74");
stack1.push("8. Anna Mishima 34");
stack1.push("9. Daniel Rose 64");
stack1.push("10. Aaron Black 83");
stack1.push("11. Jack Mohamed 27");
stack1.push("12. Kathrine Bruckner 42");
//Displaying the first stack
cout<<"The first stack before removing students whose surname starts with the alphabets R, J and M\n";
for (stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop())
cout << displayStack.top() << '\n';
cout << "(" << stack1.size() << " elements)\n";
//Second stack
stack<string>stack2;
stack<string>temp_stack1; //Creating another stack to store students whose surname don't starts with the alphabets R, J and M\n
// cout<<"The first stack after removing students whose surname starts with the alphabets R, J and M\n";
for(stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop()){
string item = displayStack.top() ;
if(item== "3. Simon Johnson 75" || item=="9. Daniel Rose 64" || item== "6. Nick Roberts 26" ||
item== "5. Mat Jackson 38" || item== "11. Jack Mohamed 27" || item== "8. Anna Mishima 34"
)
{
stack2.push(item);
// stack1.pop();
}
else{
temp_stack1.push(item);
}
stack1.pop();
}
stack1 = temp_stack1;
cout<<"The first stack after the removal of students whose surname starts with the alphabets R, J and M\n\n";
for (stack<string> displayStack = stack1; !displayStack.empty(); displayStack.pop())
cout << displayStack.top() << '\n';
cout << "(" << stack1.size() << " elements)\n";
cout<<"\n\nThe second stack containing students whose surname starts with the alphabets R, J and M\n";
for (stack<string> displayStack = stack2; !displayStack.empty(); displayStack.pop())
cout << displayStack.top() << '\n';
cout << "(" << stack2.size() << " elements)\n";
stack<string>stack3;
return 0;
}
Comments
Leave a comment