Advertisement
AbsolutelyS

Untitled

May 11th, 2024
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include "stl.h"
  2. // Returns a NEW stack of students containing only the students who live in
  3. // the given parameter home town. The order of the students in the new stack
  4. // should be preserved. That is students in the new stack should remain below
  5. // students who were higher up in the original stack. See tests for examples.
  6. stack<Student> homeTownStack(stack<Student> &students, string homeTown)
  7. {
  8. stack<Student> main = students;
  9. stack<Student> result;
  10. stack<Student> temporary;
  11.  
  12. while(!main.empty()){
  13. if(main.top().getHomeTown() == homeTown){ // con top() buscamos el 1ero
  14. temporary.push(main.top()); // si es el que queremos se agrega a temp
  15. main.pop(); // pero en vez de ABC se agrega como CBA
  16. }
  17. else {
  18. main.pop();
  19. }
  20. }
  21.  
  22. while(!temporary.empty()){ // para arreglar lo de CBA se pone en otro loop
  23. result.push(temporary.top()); // de CBA se vuelve ABC
  24. temporary.pop();
  25. }
  26.  
  27.  
  28. return result; // Dummy return. Please replace.
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement