Ok, first a heap. A heap is a region of free memory that your program can use with C's dynamic memory allocation functions. Such as malloc() and free(). This is memory NOT yet in use by any program, including windows, but set aside for use for the program in question that created the heap.
A queue is best represented with FIFO. First in First out. Best way to think of it as an array with two variables that will represent the position you are at. Basicaly one variable remembers how many you've put in and the other variable remembers how many you've taken out. Like this:
Code:
#define MAX 100
int queue[MAX];
int storePosition = 0;
int retrievePosition = 0;
void store(int num) {
if(storePosition >= MAX) {
printf("Full.\n");
}
else {
queue[storePosition] = num;
storePosition++;
}
}
int retrieve(void) {
int temp;
if(retrievePosition >= storePosition) {
printf("Empty.\n");
temp = 0;
}
else {
temp = queue[retrievePosition];
retrievePosition++;
}
return temp;
}
A stack on the other hand is best represented as FILO. First in Last Out. Typicaly a stack is thought to have two functions that will push() data into the stack and pop() data out of the stack. In this case only one variable will be needed to keep track of the position your in. Essentialy you put two, it incrememts position by two. When you retrieve one, it decrememts position by one.
Code:
#define MAX 100
int stack[MAX];
int topPosition;
void push(int num) {
if(topPosition >= MAX) {
printf("Full.\n");
}
else {
stack[topPosition] = num;
topPosition++;
}
}
int pop(void) {
int temp;
topPosition--;
if(topPosition < 0) {
printf("Empty.\n");
temp = 0;
}
else {
temp = stack[topPosition];
}
return temp;
}
Hope this helps.