Looking at the First In First Out (FIFO) data situation. Just like a line at the grocery store check-out. Its counterpart FILO would be like a stack of dinner plates.
Conquer the Queue
// A linear list of data accessed first in, first out (FIFO) is called a QUEUE.
// compiled with free PellesC: http://smorgasbordet.com/pellesc/index.htm
#include <stdio.h> // in/out function header
#include <string.h> // string function header
#include <stdlib.h> // malloc()
void q_enter(void);
void q_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
int main()
{
q_enter(); // enter some data in the queue and list the data
printf("\n\nThis is all the data in the fifo queue:");
q_list();
q_delete(); // delete first entry in queue and list again
printf("\n\nThis is data after one deletion (first in, first out):");
q_list();
getchar(); // wait
return 0;
}
//
// prompt for data entry and store in queue via q_store()
// gets() allows string input only, kind of poor,
// could stand a more bulletproof entry here
//
void q_enter(void)
{
static char str[100], *ptr;
do {
printf("Enter name (ENTER only will exit) : ");
scanf("%s",str); // 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
}
//
// list the contents of the queue
//
void q_list(void)
{
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) {
printf("\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) {
printf("\nQueue is empty!");
return;
}
retrieve++; // move retrieve position to next data item
}
jephthah 1,888 Posting Maven
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.