0% found this document useful (0 votes)
185 views6 pages

Queue Laboratory Exercise PDF

The document describes a queue data structure and provides 3 programs to demonstrate queue operations. Program 1 inserts integers and strings into queues and outputs the size and elements. Program 2 inputs integers into a queue and outputs the elements. Program 3 implements a queue as an array, adding and removing elements and displaying the queue. The summary provides space to simulate each program line and observe the outputs. Finally, a supplementary problem is proposed to create a banking customer queue program.

Uploaded by

Edmark Aldea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
185 views6 pages

Queue Laboratory Exercise PDF

The document describes a queue data structure and provides 3 programs to demonstrate queue operations. Program 1 inserts integers and strings into queues and outputs the size and elements. Program 2 inputs integers into a queue and outputs the elements. Program 3 implements a queue as an array, adding and removing elements and displaying the queue. The summary provides space to simulate each program line and observe the outputs. Finally, a supplementary problem is proposed to create a banking customer queue program.

Uploaded by

Edmark Aldea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DATA STRUCTURE AND ALGORITHM ANALYSIS

Activity: Queue

I. Objectives:
1. To create a program using queue.
2. To be able to know how to use a queue syntax.
3. To determine the theory behind the algorithm of Queuing.

II. Exercise/s
Encode the following program and compile them. Illustrate the
output.

Program No. 1

#include <list>
#include <iostream>
#include <queue>
#include <deque>
#include <conio.h>

using namespace std ;

// Using queue with list


typedef list<int > INTLIST;
typedef queue<int> INTQUEUE;

// Using queue with deque


typedef deque<char*> CHARDEQUE;
typedef queue<char*> CHARQUEUE;

int main()
{
size_t size_q;
INTQUEUE q;
CHARQUEUE p;

// Insert items in the queue(uses list)


q.push(42);
q.push(100);
q.push(49);
q.push(201);
// Output the size of queue
size_q = q.size();
cout << "size of q is:" << size_q << endl;

// Output items in queue using front()


// and use pop() to get to next item until
// queue is empty
while (!q.empty())
{
cout << q.front() << endl;
q.pop();

// Insert items in the queue(uses deque)


p.push("cat");
p.push("ape");
p.push("dog");
p.push("mouse");
p.push("horse");

// Output the item inserted last using back()


//cout << p.back() << endl;
// Output the size of queue
size_q = p.size();
cout << "size of p is:" << size_q << endl;

// Output items in queue using front()


// and use pop() to get to next item until
// queue is empty
while (!p.empty())
{
cout << p.front() << endl;
p.pop();

}
getch();
return 0;
}
Program No. 2

#include <iostream>
#include <queue>
#include <conio.h>

using namespace std;

int main ()
{
queue<int> myqueue;
int myint;

cout << "Please enter some integers (enter 0 to end):\n";

do {
cin >> myint;
myqueue.push (myint);
} while (myint);

cout << "myqueue contains: ";


while (!myqueue.empty())
{
cout << " " << myqueue.front();
myqueue.pop();
}
getch ();
return 0;
}

Program No. 2

#include<iostream>
#include<conio.h>
#define SIZE 5

using namespace std;

int q[SIZE],front=0,rear=0;

int main()
{
void enqueue();
void dequeue();
void display();
while(1)
{
int ch;
cout<<"\n [1] Add Element";
cout<<"\n [2] Remove Element";
cout<<"\n [3] Display List";
cout<<"\n [4] Program Exit";
cout<<"\n enter your choice:";
cin>>ch;

switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
cout<<"\n invalid choice";
} } }

int enqueue()
{
int no;
if (rear==SIZE && front==0)
cout<<"queue is full";
else
{
cout<<"enter the num:";
cin>>no;
q[rear]=no;
}
rear++;
}

int dequeue()
{
int no,i;
if (front==rear)
cout<<"queue is empty";
else
{
no=q[front];
front++;
cout<<"\n"<<no<<" -removed from the queue\n";
} }

int display()
{
int i,temp=front;
if (front==rear)
cout<<"the queue is empty";
else
{
cout<<"\n element in the queue:";
for(i=temp;i<rear;i++)
{
cout<<q[i]<<" ";
} } }
III. Summary of Program Outputs/Observation

Direction: Simulate each line of the program. Demonstrate the


corresponding output for each of the given program. Give your
observation and analysis.

1. ________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
2. ________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
_______________________________________________________
3. ________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
________________________________________________________
_______________________________________________________

IV. Supplementary Problem


1. Make a program for queuing for customer in bank.

You might also like