Data Structures - Queue
Queue
Hello everyone ,
Today we will be learning about a data structure queue.
I dont think , I will be needed to explain what is a queue , but in simple terms it is just a data structure in which objects enter from the back and are removed from the front , just like any other irritating queue in this real world.
So , lets talk about its implementation :-
We create a structure or a class of the object we want to be the basis of the queue.
eg.
struct node
{
int key;
node *next;
}
You can add parameters like key you want to store in the queue .
next we create a point *front to refer to the front of the queue and a parameter *last referring to the last object in the queue.
Initially , we put the values of these two parameters as null;
Now we write the function enqueue
void enqueue
{
node *temp;
temp=new node;
if(front==null)
{
front=temp;
last=temp;
//Now ask values of parameters from the user
cin>>a;
last->key=a;
}
else
{
last->next=temp;
last=last->next;
//Now ask values of parameters from the user
cin>>a;
last->key=a;
}
}
Similarly we ca write the dequeue function as
node dequeue()
{
if(front==null)
{
return null;
}
else
{
node *temp=front;
front=front->next;
return temp;}
}
You can write any other operation now in the main or any other function.
Hope you liked this article , Please do subscribe and follow.
Comment for any doubts or mistakes in the article .
Comments
Post a Comment