Boquiron, Reymark E - QUEUE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Rizal Technological University

College of Engineering, Architecture and Technology

Activity 3
Queue Implementation using array
and linked
list

Subject: Data Structures and Algorithms


Name: Boquiron, Reymark E.
Section: PCEAT-03-301P
Instructor: Engr. Ezekiel Nequit
Date Submitted: October 26, 2020
Queue Implementation using array
Activity 3
Sample Code:
#include <iostream>
using namespace std;
int queue[500], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow ";
return ;
} else {
cout<<"Element deleted from queue is : "<< queue[front]
<<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
string choice;
int r;
cout<<"-------------------------------------------\n";
cout<<" Queue Data Structure \n";
cout<<"-------------------------------------------\n";
cout<<" [1] Insert element to queue \n";
cout<<" [2] Delete element from queue \n";
cout<<" [3] Display all the elements of queue\n";
cout<<" [4] Exit \n";
cout<<"-------------------------------------------\n";
do {
cout<<"\nEnter your choice : "<<endl;
cin>>choice;
{
if(choice=="1"){
Insert();

}
else if(choice=="2"){
Delete();

}
else if(choice=="3"){
Display();

}
else if(choice=="4"){

cout<<"Exit"<<endl;
return 0;

}
else {

cout<<"Invalid choice"<<endl;}
}
} while(r!=4);
return 0;
}

Output
Queue Implementation using linked list
Activity 3

Sample code:
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
struct Node
{
int data, free;

Node *link;
};

Node *front = NULL;


Node *rear = NULL;

bool isempty()
{
if(front == NULL && rear == NULL)
return true;
else
return false;
}

void enqueue ( int value )


{
Node *ptr = new Node();
ptr->data= value;
ptr->link = NULL;

if( front == NULL )


{
front = ptr;
rear = ptr;
}
else
{
rear ->link = ptr;
rear = ptr;
}
}

void dequeue ( )
{
if( isempty() )
cout<<"Queue is empty\n";
else

if( front == rear)


{
Node *free(front);
front = rear = NULL;
}
else
{
Node *ptr = front;
front = front->link;
Node *free(ptr);
}
}

void showfront( )
{
if( isempty())
cout<<"Queue is empty\n";
else
cout<<"element at front is:"<<front->data;
}

void displayQueue()
{
if (isempty())
cout<<"Queue is empty\n";
else
{
Node *ptr = front;
while( ptr !=NULL)
{
cout<<ptr->data<<" ";
ptr= ptr->link;
}
}
}

int main()
{

string choice;
int r, f=1, value;
while( f == 1)

cout<<"\n-------------------------------------------\n";
cout<<" \nQueue Data Structure \n";
cout<<"-------------------------------------------\n";
cout<<" [1] Enqueue \n";
cout<<" [2] Dequeue \n";
cout<<" [3] Showfront\n";
cout<<" [4] Display Queue \n";
cout<<" [5] exit \n";
cout<<"-------------------------------------------\n";
{
cout<<"Pick your choice:";
cin>>choice;

if(choice=="1"){

cout<<"Enter Value:\n";
cin>>value;
enqueue(value);

else if (choice=="2") {
dequeue();
}
else if (choice=="3"){
showfront();

}
else if (choice=="4") {
displayQueue();

}
else {
f = 0;

}
}
}

return 0;
}

Sample output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy