Queue: A queue is a container of objects that are inserted and removed according to first-in-first-out(FIFO) principle.
[Problem]
Enqueue the given N (2 <=N <=100) numbers in order into a queue, then dequeue each and print on the screen.
[Input]
The first input line contains the number of test cases,T.
The next line contains the number of data input.
Then,integer data will be listed.
[Output]
For each given test case,print each queue.
Input OUTPUT
2 //Test case #1 1 2 3 4 5
5 //Data size #2 5 4 3 2 1
1 2 3 4 5
5
5 4 3 2 1
JAVA SOURCE:
C++ SOURCE:
[Problem]
Enqueue the given N (2 <=N <=100) numbers in order into a queue, then dequeue each and print on the screen.
[Input]
The first input line contains the number of test cases,T.
The next line contains the number of data input.
Then,integer data will be listed.
[Output]
For each given test case,print each queue.
Input OUTPUT
2 //Test case #1 1 2 3 4 5
5 //Data size #2 5 4 3 2 1
1 2 3 4 5
5
5 4 3 2 1
JAVA SOURCE:
package com.datastructure;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
public class Queue {
static private final String INPUT = System.getProperty("user.dir") + "\\src\\data\\queue.txt";
static final int MAX_N = 100;
static int front;
static int rear;
static int queue[] = new int[MAX_N];
public static void main(String[] args) {
FileInputStream instream = null;
try {
instream = new FileInputStream(INPUT);
System.setIn(instream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int tc=1;tc<=T;tc++)
{
int N = sc.nextInt();
initQueue();
for(int i=0;i<N;i++)
{
int value = sc.nextInt();
queueEnqueue(value);
}
System.out.println("#" + tc + " ");
while(!isQueueEmpty())
{
Integer value = queueDequeue();
if(value !=null)
{
System.out.println(value + " ");
}
}
System.out.println();
}
}
private static Integer queueDequeue() {
if(isQueueEmpty())
{
System.out.println("Queue is empty.");
return null;
}
int value = queue[rear];
rear++;
if(rear >=MAX_N)
{
rear = MAX_N;
}
return value;
}
private static int queueEnqueue(int value) {
if (isQueueFull()) {
System.out.println("Queue is full.");
return 0;
}
queue[front] = value;
front++;
if (front >= MAX_N) {
front = MAX_N;
}
return 1;
}
private static boolean isQueueFull() {
if ((front + 1) % MAX_N == rear) {
return true;
}
return false;
}
private static boolean isQueueEmpty() {
return (front == rear);
}
private static void initQueue() {
front = 0;
rear = 0;
}
}
C++ SOURCE:
#include<iostream>
#define MAX_N 100
using namespace std;
int front,rear;
int queue[MAX_N];
void initQueue();
bool isQueueIsEmpty();
bool isQueueISFull();
int queueEnqueue(int value);
int queueDequeue(int *value);
int main()
{
int T, tc, N;
freopen("Queue.txt", "r", stdin);
cin >> T;
for (tc = 1; tc <= T; tc++)
{
cin >> N;
initQueue();
for (int i = 0; i < N; i++)
{
int value;
cin >> value;
queueEnqueue(value);
}
cout << "#" << tc << " ";
while (!isQueueIsEmpty())
{
int value;
if (queueDequeue(&value) == 1)
{
cout << value << " ";
}
}
cout << endl;
}
return 0;
}
int queueDequeue(int *value)
{
if (isQueueIsEmpty())
{
cout << "Queue is empty." << endl;
return 0;
}
*value = queue[rear];
rear++;
if (rear == MAX_N)
{
rear = 0;
}
return 1;
}
int queueEnqueue(int value)
{
if (isQueueISFull())
{
cout << "Queue is full" << endl;
return 0;
}
queue[front] = value;
front++;
if (front == MAX_N)
{
front = 0;
}
return 1;
}
bool isQueueIsEmpty()
{
return (front == rear);
}
bool isQueueISFull()
{
if ((front + 1) % MAX_N == rear)
return true;
else return false;
}
void initQueue()
{
rear = 0;
front = 0;
}
No comments:
Post a Comment
Thanks for your comments