Popping the stack 12-04-2014, 01:13 AM
#1
So below is my final programming assignment for class. It's pretty basic, but for some reason my stack won't pop and I can't figure out what I should do to fix it. Every Any help would be greatly appreciated!!!
Code:
/*
Purpose of a program is to write a stack.cpp that will create a dynamic stack of int data type.
*/
#include<iostream>
using namespace std;
struct StackNode
{
int value;
StackNode * next;
};
bool isEmpty(StackNode * top);
void displayStack(StackNode * top);
void push(StackNode *& top, int num);
void pop(StackNode *& top, int &item);
int main()
{
StackNode * top = NULL;
bool status;
int item;
// Push the values 5, 10, and 15
// onto the stack.
cout << "Pushing 5 \n";
push(top, 5);
cout << "Pushing 10 \n";
push(top, 10);
cout << "Pushing 15 \n";
push(top, 15);
cout << endl;
cout << "Display the numbers in the stack (from top to bottom): ";
displayStack(top);
cout << endl;
cout << "Popping... \n";
pop(top, item);
cout << item << endl;
pop(top, item);
cout << item << endl;
pop(top, item);
cout << item << endl;
// The stack is empty, but try to
// pop another value.
cout << "\nAttempting to pop again... ";
pop(top, item);
system("pause");
return 0;
}
bool isEmpty(StackNode * top) // Checks to see if the whole stack is empty
{
if (!top)
{
return false;
}
}
void displayStack(StackNode * top) // Displays the stack
{
if(top)
{
displayStack(top->next);
cout << top->value << " ";
}
}
void push(StackNode *& top, int num) // Pushes a number onto the stack
{
if (!top)
{
top = new StackNode;
top->value = num;
top->next = NULL;
}
else
{
push(top->next, num);
}
}
void pop(StackNode *& top, int &item) // clears the top number of the stack
{
if (top)
{
if (top->value)
{
item = top->value;
pop(top->next, item);
}
}
}
![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)




























![[Image: 7ajmN5P.jpg]](https://i.imgur.com/7ajmN5P.jpg)