Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int head = -1 , tail = -1 , array[4] , size = 4; //declare head and tail as -1 and took a array with size 3
- //ISFULL
- bool isEmpty(){
- if(head ==-1 || head > tail ){
- return true;
- } //check the condition is it empty??
- return false;
- }
- //ISEMPTY
- bool isFull(){
- if(tail == size - 1){
- return true;
- }//check the condition is it full??
- return false;
- }
- //ENQUEUE
- void enqueue(int val){
- if(isFull() == false){
- head = 0;
- tail++;
- array[tail] = val; //store the parameter value into the tail index
- }else{
- cout << "Array is full" << endl;
- //exit(EXIT_FAILURE);
- }//check the condition and add a value into the tail section
- }
- //DEQUEUE
- void dequeue(){
- if(isEmpty()){
- return;
- }else{
- //int x;
- if(tail == 0 && head == 0){
- head++;//when we use only one data.
- }else{
- array[head];
- head++;
- }
- }//check the condition and remove the head data or first data from the queue
- }
- //PEEK
- int peek(){
- if (isEmpty())
- {
- cout << "Empty\n";
- }
- return array[head];
- //return -1 if the array is full.
- }
- //Main function
- int main(){
- ///peek();
- //cout << peek()<<endl;
- cout<< peek();
- dequeue();
- cout<< peek();
- enqueue(5);
- enqueue(15);
- cout<< peek() <<endl;
- dequeue();
- cout<< peek();
- }
Add Comment
Please, Sign In to add comment