C Queue structure
What is Queue
A queue is a list with insertions at one end and deletions at the other end. A queue exhibits the FIFO (first in first out) property.
Example
A queue based on char array
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
// w w w . j a va 2s . co m
#define MAX 100
char *p[MAX], *pop(void);
int spos = 0;
int rpos = 0;
void add(void), push(char *q), print(void), remove(void);
void add(void)
{
char s[256], *p;
do {
printf("spos %d: ", spos+1);
gets(s);
if(*s==0) {
break;
}
p = (char *) malloc(strlen(s)+1);
if(!p) {
printf("Out of memory.\n");
return;
}
strcpy(p, s);
if(*s) {
push(p);
}
} while(*s);
}
void print(void)
{
int t;
for(t=rpos; t < spos; ++t)
printf("%d. %s\n", t+1, p[t]);
}
void remove(void)
{
char *p;
if((p=pop())==NULL) {
return;
}
printf("%s\n", p);
}
void push(char *q)
{
if(spos==MAX) {
printf("List Full\n");
return;
}
p[spos] = q;
spos++;
}
char *pop(void)
{
if(rpos==spos) {
printf("No more.\n");
return NULL;
}
rpos++;
return p[rpos-1];
}
int main(void)
{
char s[80];
register int t;
for(t=0; t < MAX; ++t) {
p[t] = NULL;
}
while(1) {
printf("Add(A), Print(P), Remove(R), Quit(Q): ");
gets(s);
*s = toupper(*s);
switch(*s) {
case 'A':
add();
break;
case 'P':
print();
break;
case 'R':
remove();
break;
case 'Q':
exit(0);
}
}
return 0;
}
Example 2
Array Implementation of a int Queue
#include <stdio.h>
#include <stdlib.h>
// w ww .ja v a2s . c o m
#define MAX 10
void insert(int queue[], int *rear, int value)
{
if(*rear < MAX-1)
{
*rear= *rear +1;
queue[*rear] = value;
}
else
{
printf("The queue is full can not insert a value\n");
exit(0);
}
}
void delete(int queue[], int *front, int rear, int * value)
{
if(*front == rear)
{
printf("The queue is empty can not delete a value\n");
exit(0);
}
*front = *front + 1;
*value = queue[*front];
}
void main()
{
int queue[MAX];
int front,rear;
int n,value;
front = rear = -1;
insert(queue,&rear,1);
insert(queue,&rear,2);
delete(queue,&front,rear,&value);
printf("The value deleted is %d\n",value);
}
The code above generates the following result.
Example 3
A queue based on the linked list.
#include <stdio.h>
#include <stdlib.h>
struct node/* ww w. j a v a 2 s . co m*/
{
int data;
struct node *link;
};
void insert(struct node **front, struct node **rear, int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("No Memory available Error\n");
exit(0);
}
temp->data = value;
temp->link=NULL;
if(*rear == NULL)
{
*rear = temp;
*front = *rear;
}
else
{
(*rear)->link = temp;
*rear = temp;
}
}
void delete(struct node **front, struct node **rear, int *value)
{
struct node *temp;
if((*front == *rear) && (*rear == NULL))
{
printf(" The queue is empty cannot delete Error\n");
exit(0);
}
*value = (*front)->data;
temp = *front;
*front = (*front)->link;
if(*rear == temp)
*rear = (*rear)->link;
free(temp);
}
void main()
{
struct node *front=NULL,*rear = NULL;
int n,value;
insert(&front,&rear,1);
insert(&front,&rear,2);
insert(&front,&rear,3);
insert(&front,&rear,4);
delete(&front,&rear,&value);
printf("The value deleted is %d\n",value);
delete(&front,&rear,&value);
printf("The value deleted is %d\n",value);
delete(&front,&rear,&value);
printf("The value deleted is %d\n",value);
}
The code above generates the following result.