![]() |
|
Popping the stack - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: Popping the stack (/Thread-Popping-the-stack) |
Popping the stack - Cosh - 12-04-2014 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);
}
}
}RE: Popping the stack - Adorapuff - 12-04-2014 Try posting on stackoverflow, not many here will be able to help with this. RE: Popping the stack - Oni - 12-04-2014 (12-04-2014, 04:19 AM)Adorapuff Wrote: Try posting on stackoverflow, not many here will be able to help with this. @"Tengu" might be able to assist. I know there are others too. RE: Popping the stack - dopamine - 12-04-2014 Code: #include <Windows.h>
class CIntList
{
private:
int* arr_iNumbers;
int m_iCount;
public:
void Add(int iNum);
int GetIndex(int iIndex);
};
void CIntList::Add(int iNum)
{
int* arr_iOldNumbers = arr_iNumbers;
m_iCount += 1;
arr_iNumbers = (int*)malloc(m_iCount * sizeof(int));
if (arr_iOldNumbers != NULL)
memcpy(arr_iNumbers, arr_iOldNumbers, (m_iCount - 1) * sizeof(int));
arr_iNumbers[m_iCount - 1] = iNum;
}
int CIntList::GetIndex(int iIndex)
{
if (iIndex < m_iCount)
return this->arr_iNumbers[iIndex];
else return -1;
}
int main()
{
CIntList* IntList = new CIntList();
IntList->Add(1);
IntList->Add(7);
IntList->Add(8);
int iNum = IntList->GetIndex(0);
int iNum2 = IntList->GetIndex(1);
int iNum3 = IntList->GetIndex(2);
return 0;
}I think this is what you wanted. Really simple stuff, read more about pointers. RE: Popping the stack - p0pn0pr0p - 12-11-2014 Is the head of your link list the top of the stack or the bottom? Your push function treats the head of the LL as the bottom of the stack, but your pop func treats it as the top. Have you considered putting in some debug output to check whether it's following the code path you think it is? (also why are you traversing the entire thing when you push?) This may be worth a read: http://c2.com/cgi/wiki?ShlemielThePainter |