Fri. Apr 19th, 2024

Queue – algorithm (Sample 1)

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

Queue Representation

As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure −

As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.

Basic Operations

Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues −

  • enqueue() − add (store) an item to the queue.
  • dequeue() − remove (access) an item from the queue.

Few more functions are required to make the above-mentioned queue operation efficient. These are −

  • peek() − Gets the element at the front of the queue without removing it.
  • isfull() − Checks if the queue is full.
  • isempty() − Checks if the queue is empty.

In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer.

Let’s first learn about supportive functions of a queue −

peek()

This function helps to see the data at the front of the queue. The algorithm of peek() function is as follows −

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>

typedef struct nd{
       int cheie;
       struct nd*next;
       }nod;
nod *prim,*ultim;

void creare()
{
    nod*p;
    char c;
    prim = ultim = NULL;

    do
	{
      if((p=(nod*)malloc(sizeof(nod))) == NULL)
       {
		   printf("\n Eroare la alocare\n");
		   exit(0);
	  }

      printf("\n Dati informatia:");
      scanf("%d",&p->cheie);

      p->next = NULL;

      if(prim == NULL)
		  prim = ultim = p;
	  else{ultim->next = p;
		  ultim = p;
	}
      printf("\n Continuati?(d/n)");
      c = getch();
    }while((c!='n')&&(c!='N'));
}
void afisare()
{
	nod*p;
	if(prim==NULL)
		printf("\n Coada vida\n");
	else
	 {
	    p = prim;
	    printf("\n Continutul cozii este:\n");
	    while(p!=NULL)
		{
		   printf("\t %d",p->cheie);
		   p = p->next;
		 }
	 }
}

void adaugare()
{
     nod*p;
     if((p=(nod*)malloc(sizeof(nod)))==NULL)
     {
	    printf("\n Eroare la alocare\n");
		exit(0);
	 }

     printf("\n Dati informatia:");
     scanf("%d",&p->cheie);

     p->next=NULL;
     if(prim==NULL)
		 prim = ultim = p;
	 else
	  {
	    ultim->next = p;
		ultim = p;
	  }
}
void stergere()
{
	nod *p;
	if(prim == NULL)
		printf("\n Coada vida\n");
	else
	  {
		p = prim;
		printf("\n Informatia ce va fi stearsa este:%d",p->cheie);
		prim = prim->next;
		free(p);
	  }
}

void main()
{
	char ch;

	do
	{
		printf("\n");
		printf("----------------------------------------\n");
		printf("1.Creare\n");
		printf("2.Afisare\n");
		printf("3.Adaugare\n");
		printf("4.Stergere\n");
		printf("0.Exit\n");
		printf("----------------------------------------\n");

		ch = getch();

		switch(ch)
		{
			case '0' : return;
			case '1' : creare();break;
			case '2' : afisare();break;
			case '3' : adaugare();break;
			case '4' : stergere();break;
		}
	}while(1);
}

384,074 total views, 3 views today