Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. //TO DO: include needed libraries
  2. #include<iostream>
  3. #include"stack.h"
  4. using namespace std;
  5.  
  6. //---------------------------------------------------------------------------------------------------
  7. // initStack
  8. //---------------------------------------------------------------------------------------------------
  9. // Given a stack, initializes it to empty
  10. void initStack(stack & s) {
  11. s.head = NULL;
  12. s.tail = NULL;
  13. } // initStack()
  14.  
  15. //---------------------------------------------------------------------------------------------------
  16. // printStack
  17. //---------------------------------------------------------------------------------------------------
  18. // Given a stack: prints the keys of all nodes on the stack from top to bottom, left to right on 1 line
  19. // Ex: STACK: 22 87 39 45 (where 22 is the top and 45 is the bottom)
  20. void printStack(stack & s) {
  21. node * w = s.head;
  22. cout << "STACK: ";
  23. if (w == NULL) {
  24. cout << "Empty";
  25. }
  26. else {
  27. while (w != NULL) {
  28. cout << w->key << " ";
  29. w = w->next;
  30. }
  31. }
  32. cout << "\n";
  33. } // printStack()
  34.  
  35. //---------------------------------------------------------------------------------------------------
  36. // pop
  37. //---------------------------------------------------------------------------------------------------
  38. // removes the top element of the stack, deallocates it, and returns its key
  39. int pop(stack & s) {
  40. if (s.head == NULL) {
  41. cout << "";
  42. return 0;
  43. }
  44. else {
  45. int n = s.head->key;
  46. s.head = s.head->next;
  47. return n;
  48. }
  49. } // pop()
  50.  
  51. //---------------------------------------------------------------------------------------------------
  52. // push
  53. //---------------------------------------------------------------------------------------------------
  54. // Given a stack and the key of a new node; creates a new node and populates it with the given key;
  55. // pushes the new node on the stack.
  56. void push(stack & s, int key) {
  57. node * m = new node;
  58. node * e = new node;
  59. m->key = key;
  60. m->next = NULL;
  61.  
  62. m->next = s.head;
  63. s.head = m;
  64.  
  65. } // push()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement