Stack implementation.
Posted: Wed Jan 11, 2012 11:35 pm
I've been lurking on the forums for a long time now and I will happily admit that I'm a super-noob (uber-noob?) in the OS development arena. I've only been programming for about 5-6 years now, and only pushing myself at learning more difficult topics for about 7 months now. So, I apologize if I'm completely wrong. Now, to the meat-and-potato's of this post:
I recently tried making a stack implementation just for fun, learning, and the likes. The question was that when I read a paper on it (http://www.cs.bu.edu/teaching/c/stack/array/) it varied completely from the way I did it, though I'm pretty sure mine works in a similar way. In my implementation I have an array simulating the stack. When pushing to the stack, all I do is add to the array like you normally would. Where-as when popping from the stack, I just remove the most recently added element. I don't do anything like he did in that paper (though we both followed a similar principle from what I could tell). Maybe I just need to beef up my programming skills more than I already thought I did, but I'm just wondering why he did his differently.
Here's my 50% C, 50% C++ code (trying to transition from C++ to C):
Are these the same (besides the fact that mine's more noobish)? If not, what advantages does his code have over mine?
Sorry if this is obvious,
~Cent
I recently tried making a stack implementation just for fun, learning, and the likes. The question was that when I read a paper on it (http://www.cs.bu.edu/teaching/c/stack/array/) it varied completely from the way I did it, though I'm pretty sure mine works in a similar way. In my implementation I have an array simulating the stack. When pushing to the stack, all I do is add to the array like you normally would. Where-as when popping from the stack, I just remove the most recently added element. I don't do anything like he did in that paper (though we both followed a similar principle from what I could tell). Maybe I just need to beef up my programming skills more than I already thought I did, but I'm just wondering why he did his differently.
Here's my 50% C, 50% C++ code (trying to transition from C++ to C):
Code: Select all
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int i = 0;
int arraySize;
char * stack[100];
string stackPopped;
int stackPoppedI;
void stackPush(char * pushed)
{
if(i == 100)
{
cout << "ERROR: Stack overflow!";
free(stack);
exit(EXIT_FAILURE);
}
else
{
int length;
length = strlen(pushed);
stack[i] = (char*)malloc(sizeof(i));
strcpy(stack[i], pushed);
i++;
cout << "Stack currently contains;" << endl;
for(int x; x < i; x++)
{
cout << stack[x] << endl;
}
}
}
void stackPop(int arrayNumber)
{
if(i == 0)
{
cout << "Stack underflow!" << endl;
exit(EXIT_FAILURE);
}
int j;
if((j = atoi(stack[arrayNumber])) > 0)
{
stackPoppedI = atoi(stack[arrayNumber]);
stack[arrayNumber] = 0;
--i;
cout << "stackPoppedI currently contains your number." << endl;
cout << stackPoppedI;
free(stack[arrayNumber]);
}
else
{
char stackPoppedc[256]; // (Stack Popped Copy) So that I can copy the string from the stack, into my string
strcpy(stackPoppedc, stack[arrayNumber]);
stackPopped = stackPoppedc;
stack[arrayNumber] = 0;
--i;
cout << "stackPopped currently contains your string." << endl;
cout << stackPopped << endl;
free(stack[arrayNumber]);
}
}
Sorry if this is obvious,
~Cent