Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TO DO: include needed libraries
- #include<iostream>
- #include"stack.h"
- using namespace std;
- //---------------------------------------------------------------------------------------------------
- // initStack
- //---------------------------------------------------------------------------------------------------
- // Given a stack, initializes it to empty
- void initStack(stack & s) {
- s.head = NULL;
- s.tail = NULL;
- } // initStack()
- //---------------------------------------------------------------------------------------------------
- // printStack
- //---------------------------------------------------------------------------------------------------
- // Given a stack: prints the keys of all nodes on the stack from top to bottom, left to right on 1 line
- // Ex: STACK: 22 87 39 45 (where 22 is the top and 45 is the bottom)
- void printStack(stack & s) {
- node * w = s.head;
- cout << "STACK: ";
- if (w == NULL) {
- cout << "Empty";
- }
- else {
- while (w != NULL) {
- cout << w->key << " ";
- w = w->next;
- }
- }
- cout << "\n";
- } // printStack()
- //---------------------------------------------------------------------------------------------------
- // pop
- //---------------------------------------------------------------------------------------------------
- // removes the top element of the stack, deallocates it, and returns its key
- int pop(stack & s) {
- if (s.head == NULL) {
- cout << "";
- return 0;
- }
- else {
- int n = s.head->key;
- s.head = s.head->next;
- return n;
- }
- } // pop()
- //---------------------------------------------------------------------------------------------------
- // push
- //---------------------------------------------------------------------------------------------------
- // Given a stack and the key of a new node; creates a new node and populates it with the given key;
- // pushes the new node on the stack.
- void push(stack & s, int key) {
- node * m = new node;
- node * e = new node;
- m->key = key;
- m->next = NULL;
- m->next = s.head;
- s.head = m;
- } // push()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement