Data Structure and Algorithms based on the Object-oriented programming (OOP) where some are the base of OOP for instant inheritance, polymorphism, Encapsulation and Abstraction etc. Now we will start learning data structures in Programming languages such as Linked list, arrays, Structs, Classes, Queues, Stacks etc.

Today’s Concept:

Queue Data Structure

 

Prerequisite of this Lab is all those students who have basic concepts on C++ Programming such as Syntax of Language, concept of functions, pointers, datatypes etc. Visual Studio 2012 and onward version will be used for implementation and programming tasks assigned in the Lab.

 

Queue Data Structure can be implemented using C++ or any other programming languages. This is also known as a linear data structure because they use to store data in contiguous memory blocks just like single data structure Arrays stored their data. Queue works in FIFO (first in first out ) principle by this it means any values which enters first will print first for example if we enter 1,2,3,4 in the queue then we will get 1 then 2 and so on you can take example of passengers who waits for the bus or a line in banks to deposit amount any person who comes first serves first.

peepgrams.com


Operations in Queue:

There are different operations in Queue such as:

  • EnQueue use to Enter value in Queue
  • DeQueue use to remove value from Queue
  • IsFull() check the full  status of Queue
  • IsEmpty() use to check the empty status of queue
Details about implementation of Queue & Code using C++:

for implementation we use Pointer because we have to decide size of queue on run time that's why we take pointer as a data member and we will take f variable to track all removals for example whenever Dequeue calls f variable incremented one time on the other hands for Enqueue we will use R variable R get incremented every time value inserted into the Queue. isFull() function will always call whenever we try to insert value in queue and isEmpty() will always call when we try to remove value form queue.

Visuals Of Insertion and removals:
peepgrams



After insertion or Enqueue 10 into Queue 
Peepgrams



After Dequeue inserted value
Peepgrams.com




Queue Code Implementation Using C++:

queue.h

#include<iostream>

using namespace std;

class queue

{

int *q,maxsize,rear,fornt;

public:

queue(int size)

{

this->q=new int(size);

maxsize=size;

rear=-1;

fornt=-1;

}

bool isfull()

{

if(this->rear==maxsize-1)

{

return true;

}

else

{

return false;

}

}

bool isEmpty()

{

if(this->rear==-1)

{

return true;

}

else

{

return false;

}


}


void enqueu(int value)

{

if(this->isfull())

{

cout<<"Queue is full"<<endl;

}

else

{

++rear;

if(rear <= maxsize-1)

{

this->q[rear]=value;

}

else

{

cout<<"no more enteries possible"<<endl;

}



}

}



void deqeue()

{

if(isEmpty())

{

cout<<"Queue is empty"<<endl;

}

else

{

++fornt;

if(this->fornt > rear)

{      

cout<<"trying to delete more values than entered"<<endl;

}

else

{

cout<<"Vlaues at front possiton"<< q[this->fornt]<<endl;

}

            } 



}

};

main.cpp

#include"queue.h"

int main()

{

       queue obj(5);

       obj.enqueu(10);

       obj.enqueu(20);

       obj.enqueu(30);

       obj.enqueu(40);

       obj.enqueu(50);

       obj.enqueu(50);

 

       obj.deqeue();

       obj.deqeue();

       obj.deqeue();

       obj.deqeue();

       obj.deqeue();

       obj.deqeue();

       system("pause");

       return 0;

}

 

Lab Task Activity Queue:


  1. Write your understanding on the word document about linked list using C++ please read the manual and just write what you understand don't try to copy and past from other sources.
  2. Create C++ program to run same code given as Example and take screenshot for submission.
  3. You have learnt linked list single and doubly you are instructed to implement FIFO using Linked list of your choice single or double.

Click for Submission:










Post a Comment

Previous Post Next Post