GID Forums C/C++ Forums
Code Submittal Form
Name/Brief Description:Stack & Queue class
Date of Original Submission:March 9, 2004
Submitted by:dsmith
License:None - nothing ground breaking here
Detailed Description:This is a small and simple implementation of both a stack and a queue. A stack is a filo (first in last out) structure and a queue is a fifo (first in first out) structure. They can be pretty handy for throwing items onto for later use. Here is a small sample file that shows there use.
#include "estack.h"
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
int main()
{
char string[MAX_SIZE];
estack a_stack;
eque a_que;
char* loc;
printf("This program will illustrate the difference between a queue and a stack.\n");
printf("Enter your strings (just enter to quit):\n\n");
do{
printf("String: ");
fgets(string,MAX_SIZE,stdin);
if(*string != '\n'){
*(string+(strlen(string)-1)) = 0; //Get rid of return char.
a_stack.push(string);
a_que.push(string);
}
}while(*string != '\n');
printf("\nOkay, here is your stack-->\n");
while(loc = a_stack.pop() )
printf("String: %s\n",loc);
printf("\n\nAnd here is your queue-->\n");
while(loc = a_que.pop() )
printf("String: %s\n",loc);
}
This will print your strings backwards in the case of the stack and forwards in the case of the queue. In order to run this program it will need to be linked with the estack.cpp file.
Problems/Limitations:- Written with C++ style syntax, so it won't port to strictly C compilers
- Poorly commented and confusing because it was originally meant as an error tracking implementation
- Only works with char* types
- Not tested rigorously - use at your own risk
As always, comments and questions are appreciated.