Here is some C code that might help you get started.
// A linear list of data accessed first in, first out (FIFO) is called a QUEUE.
// Pelles C (vegaseat)
#include <stdio.h> // in/out function header
#include <string.h> // string function header
#include <stdlib.h> // malloc()
void enter(void);
void list(void);
void q_store(char *ptr);
void q_delete(void);
int store = 0; // next storage position in queue[]
int retrieve = 0; // retrieve position in queue[]
char *queue[100]; // this array forms the queue
void main(void)
{
enter(); // enter some data in the queue and list the data
puts("\n\nThis is all the data in the fifo queue:");
list();
q_delete(); // delete first entry in queue and list again
puts("\n\nThis the data after one deletion (first in, first out):");
list();
getchar();
}
//
// prompt for data entry and store in queue via q_store()
//
void enter(void)
{
static char str[100], *ptr;
do {
printf("Enter name (ENTER only will exit) : ");
gets(str);
ptr = (char *) malloc(strlen(str)); // starting address of memory
strcpy(ptr,str);
if (*str)
q_store(ptr); // store in queue if string has info
} while (*str); // until no entry
}
void list(void) // list the contents of the queue
{
int k;
for (k = retrieve; k < store; k++)
printf("\n%d) %s",k+1,queue[k]);
}
//
// store data items in the queue
//
void q_store(char *ptr)
{
if (store == 100) {
puts("\nList is full!");
return;
}
queue[store] = ptr;
store++; // point to next available storage position in queue[]
}
//
// delete data at retrieve position
//
void q_delete(void)
{
if (store == retrieve) {
puts("\nQueue is empty!");
return;
}
retrieve++; // move retrieve position to next data item
} There could be more modern code for the puts() and gets() functions.
vegaseat
DaniWeb's Hypocrite
Moderator
5,988 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417