Question

In: Computer Science

Implement a generic queue.  Use the code below for main(). main() { int i, x; float y;...

Implement a generic queue.  Use the code below for main().

main()

{

int i, x;

float y;

char z;

Queue<int> A;

Queue<float> B;

Queue<char> C;

ifstream in;

in.open("int.txt");

for (i = 0; i < 100; i++){

        in >> x;

        A.enqueue(x);

    }

cout << A.dequeue() << endl;;

for (i = 0; i < 12; i++)

        A.enqueue(i);

cout << A.dequeue() << endl;

cout << A.dequeue() << endl;

cout << "Dequeueing: "<< A.dequeue()<< endl;

while (!A.isEmpty())

     cout  << A.dequeue() << "  ";

if (A.isEmpty())

      cout << "Integer Queue Is Empty "  << endl;

in.close();

cout << endl << "Now for the floating point numbers...." << endl;

in.open("float.txt");

for (i = 0; i < 100; i++){

        in >> y;

        B.enqueue(y);

    }

cout << "Here are the first ten:" << endl;

for (i = 0; i < 10; i++){

    cout << B.dequeue() << endl;

    }

in.close();

in.open("char.txt");

for (i = 0; i < 100; i++){

        in >> z;

        C.enqueue(z);

    }

cout << "Here are the characters" << endl;

while (!C.isEmpty()){

    cout << C.dequeue();

    }

in.close();

}

Solutions

Expert Solution

GENERIC QUEUE

Below is the implementation of a Generic Queue using Linked List :

#include<iostream>

#include <fstream>
using namespace std;


template <typename T>
class Node {

public :
T data;
Node<T> *next;
  
Node(T data) {
this -> data = data;
next = NULL;
}
};

template<typename T>
class Queue {


Node <T>*head;
Node <T>*tail;
int size;
  
public :
  
  
Queue() {
size=0;
head=NULL;
tail=NULL;
}
  
void enqueue(T data) {
if(head==NULL)
{
Node<T> *n=new Node<T>(data);
head=n;
tail=n;
size++;

}
else
{
Node<T> *n=new Node<T>(data);
tail->next=n;
tail=n;
size++;
}
}
  
int getSize() {
return size;
  
}
  
bool isEmpty() {
  
return size==0;   
}
  
T dequeue() {
// Return 0 if queue is empty
if(size==0)
return 0;
else
{
Node <T> *n=new Node <T> (head->data);
head=head->next;
size--;
return n->data;
  
}
}
  
T front() {
// Return 0 if queue is empty
if(size==0)
return 0;
else
return head->data;
}
};


Related Solutions

I need a Flowgorithm chart of this #include using namespace std; int main() {    float...
I need a Flowgorithm chart of this #include using namespace std; int main() {    float CISBook[] = {55.5,35.8,67.5};    float MATHBook[] = {25.5, 54.5};    float MECBook[] = {65.0,75.5,86.8};    float sumCIS=0, sumMATH = 0, sumMEC = 0;              for (int i=0; i<4; i++)    {sumCIS += CISBook[i];}       for (int i=0; i<3; i++)    {sumMATH += MATHBook[i];}       for (int i=0; i<4; i++)    {sumMEC += MECBook[i];}       cout<<"\nTotal for CIS Books:...
int main() { float A[4] = { 0.51, 1.23, 7.4, 10.88}; float B[4] = { -11.1,...
int main() { float A[4] = { 0.51, 1.23, 7.4, 10.88}; float B[4] = { -11.1, 78.044, 12.009, -9.99}; float s = 0.0; for (int i=0; i<4; i++) { s = s + A[i]*B[i]; } } change this c languange to arm assembly languange extension of vfp registor
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or y is 0, then the return value and x*y will be zero. }else if(y<0&&x<0){ x=-x;//Changing the sign of x y=-y;//Changing the sign of y }else if(x>=1){ return (y+product(x-1,y)); } return (x+product(x,y-1)); } find the space complexity and the time complexity of the above algorithm.
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0;...
#include <stdio.h> int main(void) { float funds = 1.0, cost = 0.1; int candies = 0; while(cost <= funds) { candies += 1; funds -= cost; cost += 0.1; } printf("%d candies with $%.2f left over.\n",candies,funds); return 0; } When you compile and run this code you get 3 candies with $0.40 left over. Without knowing how floating point numbers work in a computer, would this result be expected? Explain why or why not. Explain why this result, in fact,...
Convert the Queue to Generic implementation: public class MyQueueImpl implements MyQueue {    private int capacity;...
Convert the Queue to Generic implementation: public class MyQueueImpl implements MyQueue {    private int capacity;    private int front;    private int rear;    private int[] arr;    public MyQueueImpl(int capacity){        this.capacity = capacity;        this.front = 0;        this.rear = -1;        this.arr = new int[this.capacity];    }    @Override    public boolean enQueue(int v) {                  if(this.rear == this.capacity - 1) {                //Perform shift   ...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;   ...
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;    }else{    i+=4;    fork();    i+=5;    }    printf("%d\n",i); } how many processes does this code create including the parent
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;   ...
int main(){    int i = 3;    if(fork()){    i++;    fork();    i+=3;    }else{    i+=4;    fork();    i+=5;    }    printf("%d\n",i); } what is the output of the code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT