Question

C++ I need help to create a program that executes a simple queue data type. You...

C++

I need help to create a program that executes a simple queue data type. You must define the queue data type and use the enqueue(), dequeue(), and displayQueue() functions. (Dont use C++ STL to execute queue logic) You may use a linked list , or just use a regular C array of integers. The input file (testfile.txt) will be the list of operations to carry out on a queue.

Example input file:

enqueue 6
enqueue 8
dequeue
enqueue 4
enqueue 9
enqueue 8
dequeue
dequeue
dequeue

Sample output should look similar to this:

Enqueuing item: 6
Head: 6 :Tail
Enqueuing item: 8
Head: 6 8 :Tail
and so on.....


0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Node {
   int data;
   Node *next;
};

class Queue {
   private:
       Node *head;
       Node *tail;
   public:
       Queue() {
           head = NULL;
           tail = NULL;
       }
       void enqueue(int data) {
           Node *n = new Node;
           n->data = data;
           n->next = NULL;
           if(head == NULL) {
               head = n;
               tail = n;
           } else {
               tail->next = n;
               tail = n;
           }
       }
       int dequeue() {
           int data = head->data;
           head = head->next;
           if(head == NULL) {
               tail = NULL;
           }
           return data;
       }
       void print() {
           Node *temp = head;
           cout << "Head: ";
           while(temp != NULL) {
               cout << temp->data << " ";
               temp = temp->next;
           }
           cout << ":Tail" << endl;
       }
};

int main() {
   string filename;
   cout << "Enter file name: ";
   cin >> filename;
   ifstream in(filename.c_str());
   if(in.is_open()) {
       string op;
       int data;
       Queue q;
       while(in >> op) {
           if(op == "enqueue") {
               in >> data;
               q.enqueue(data);
               cout << "Enqueuing item: " << data << endl;
           } else {
               cout << "Dequeuing item: " << q.dequeue() << endl;
           }
           q.print();
       }
       in.close();
   }
   else {
       cout << "can not open " << filename << " to read" << endl;
   }
   return 0;
}

Enter file name: input.txt Enqueuing item: 6 Head 6 Tail Enqueuing item: 8 Head 6 8 :Tail Dequeuing item: 6 Head 8 :Tail Enqu

Add a comment
Know the answer?
Add Answer to:
C++ I need help to create a program that executes a simple queue data type. You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Write a C++ program to implement a queue using linked lists. You can use the queue...

    Write a C++ program to implement a queue using linked lists. You can use the queue data structure from the Standard Template Library (STL). The program should provide the following functionality: Enqueue data into queue Dequeue data from queue Print data at the front Print data at the back Print the entire queue Check if the queue is empty Print the number of elements in the queue Test your program using at least the following test cases (considering the queue...

  • 3. Some circular queue implementations use the mod operator % in enqueue and dequeue operations. Explain...

    3. Some circular queue implementations use the mod operator % in enqueue and dequeue operations. Explain why this is inefficient. 4. If a queue is implemented using a singly linked list with a head and tail pointer, you should always insert at the tail and remove from the head. Explain why this is so. 5. What is a Priority Queue, and how does it differ from a standard queue? 6. Priority Queues are almost always implemented with an ordered data...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • Suppose the following operations are performed on a queue containing integers. Create an empty queue Push...

    Suppose the following operations are performed on a queue containing integers. Create an empty queue Push 1 Push 2 Push 3 Pop Push 4 Push 5 Pop Now do the following: a. Draw a sketch of a doubly linked-list based queue after steps 1-4. b. Draw a sketch of a doubly linked-list based queue after all steps 1-8. c. Draw a sketch of a circular array-based queue with capacity 6 after steps 1-4. Specify f, t, and size after each...

  • I need help to do this code

    Create a queue using a linked list as your container and use a class. The system must have enqueue and dequeue operations and functions such as, isfull() and isempty(). Make sure it has an input and 10 data

  • You are going to create a Queue. (alternately you can create a list and simply implement...

    You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • Your assignment is to create and test a class for a queue of objects. You may...

    Your assignment is to create and test a class for a queue of objects. You may use any object class of your choice as the data for the queue.   The instances of the class should have at least one field that distinguishes each instance from other instances of the class (key property, also called a key field). You should complete the software to implement and test your queue and submit the software along with a project report. Your queue class...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT