Dynamic implementation of a Simple Queue with a suitable diagram
In a dynamic or linked Simple Queue, each node of the queue consists of two parts i.e. data
part and the link part. Each element of the queue points to its immediate next element in the
memory.
In the Dynamic Simple Queue, there are two pointers maintained in the memory i.e. front
pointer and rear pointer. The front pointer contains the address of the starting element of the
queue while the rear pointer contains the address of the last element of the queue.
Structure for creating a template of a Simple Queue.
Struct SimpleQ
{
int info;
struct SimpleQ *next;
}*FRONT,*REAR;
typedef SimpleQ queue;
int item;
Dynamically implement the Insert operation of Simple Queue
void EnqueueSQ( ) {
queue *insert;
printf(“\n Enter Element to insert into SQ:”);
scanf(“%d”, &item);
insert = (queue *) malloc(sizeof(queue));
if(insert==NULL) {
printf(“ !!! OVERFLOW !!!”);
}
else {
insert->info=item;
insert->next=NULL;
if(FRONT==NULL && REAR==NULL) {
FRONT=REAR=insert;
}
else {
REAR->next=insert;
REAR=insert;
}
printf(“\n %d inserted successfully in Simple Queue.”, REAR->info);
}
}
Dynamically implement the DequeueSQ( ) operation of Simple Queue
// Deletion Function
void DequeueSQ()
{
queue *temp;
if (FRONT == NULL && REAR==NULL)
{
printf("\nSQueue is empty \n");
}
else if( FRONT == REAR)
{
item = FRONT;
FRONT = REAR = NULL;
printf("\n%d deleted from Simple Queue", temp->info);
free(temp);
}
else
{
temp=FRONT;
FRONT=FRONT->next;
printf("\n%d deleted from Simple Queue", temp->info);
free(temp);
}
}
//Display function of Dynamic Simple Queue.
void displaySQ()
{
queue *temp;
temp = FRONT;
printf("\nThe elements in Simple Queue are:\n");
while (temp != NULL)
{
printf("%d\t", temp->info);
temp = temp->next;
}
}