GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Mar-2004, 20:43
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light

[Library] Stack and Queue Class


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.

CPP / C++ / C Code:
#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.
Attached Files
File Type: zip stack.zip (1.1 KB, 373 views)
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.