Question

Create a QUEUE ADT. QUEUE should be implemented using the linked list. b) Enter ten random...

Create a QUEUE ADT. QUEUE should be implemented using the linked

list.

b) Enter ten random integer numbers between 0 to 50 in the QUEUE.

c) After adding each element, print the content of the QUEUE.

c) Delete five numbers from the QUEUE.

d) Print the content of the QUEUE.

d) Finally, destroy the QUEUE.

(in c/c++)

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

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

class Node{

private:

int data;

Node* nextNodePtr;

Node* prevNodePtr;

public:

Node(){}

void setData(int d){

data = d;

}

int getData(){

return data;

}

void setNextNodePtr(Node* nodePtr){

nextNodePtr = nodePtr;

}

Node* getNextNodePtr(){

return nextNodePtr;

}

void setPrevNodePtr(Node* nodePtr){

prevNodePtr = nodePtr;

}

Node* getPrevNodePtr(){

return prevNodePtr;

}

};

class Queue{

private:

Node* headPtr;

Node* tailPtr;

public:

Queue(){

headPtr = new Node();

tailPtr = new Node();

headPtr->setNextNodePtr(0);

tailPtr->setPrevNodePtr(0);

}

Node* getHeadPtr(){

return headPtr;

}

Node* getTailPtr(){

return tailPtr;

}

bool isEmpty(){

if (headPtr->getNextNodePtr() == 0)

return true;

return false;

}

void enqueue(int data){

Node* newNodePtr = new Node();

newNodePtr->setData(data);

newNodePtr->setNextNodePtr(0);

Node* lastNodePtr = tailPtr->getPrevNodePtr();

if (lastNodePtr == 0){

headPtr->setNextNodePtr(newNodePtr);

newNodePtr->setPrevNodePtr(0);

}

else{

lastNodePtr->setNextNodePtr(newNodePtr);

newNodePtr->setPrevNodePtr(lastNodePtr);

}

tailPtr->setPrevNodePtr(newNodePtr);

}

int dequeue(){

Node* firstNodePtr = headPtr->getNextNodePtr();

Node* nextNodePtr = 0;

int poppedData = -100000; //empty queue

if (firstNodePtr != 0){

nextNodePtr = firstNodePtr->getNextNodePtr();

poppedData = firstNodePtr->getData();

}

else

return poppedData;

if (nextNodePtr != 0){

nextNodePtr->setPrevNodePtr(0);

headPtr->setNextNodePtr(nextNodePtr);

}

else{

headPtr->setNextNodePtr(0);

tailPtr->setPrevNodePtr(0);

}

return poppedData;

}


void printQueue(){

Node* temp = headPtr->getNextNodePtr();

while( temp != NULL){

cout <<temp->getData() <<" ";

temp = temp->getNextNodePtr();

}

cout<<endl;

}

int peek(){

Node* firstNodePtr = headPtr->getNextNodePtr();

if (firstNodePtr != 0)

return firstNodePtr->getData();

else

return -100000; //empty queue

}

void destroy(){

Node* temp = headPtr->getNextNodePtr();

while( temp != NULL){

Node* prev = temp;

temp = temp->getNextNodePtr();

delete prev;

}

}

};

int main()

{

Queue *q = new Queue();

srand(time(NULL));

for(int i = 1;i<=10; ++i){

q->enqueue(rand()%50);

q->printQueue();

}

int i = 1;

cout << "Deque 5 elements: "<<endl;

while(!q->isEmpty() && i<=5){

cout<<q->dequeue()<<" ";

i = i + 1;

}

cout << "\nQueue contents after deleting 5 elements is: "<<endl;

q->printQueue();


q->destroy();

cout<<"Queue destroyed. "<<endl;

}

==================================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. PLEASE UPVOTE, PLEASE COMMENT

Add a comment
Know the answer?
Add Answer to:
Create a QUEUE ADT. QUEUE should be implemented using the linked list. b) Enter ten random...
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
  • In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the...

    In class, we discussed the priority queue (PQ) ADT implemented using min-heap. In a min-heap, the element of the heap with the smallest key is the root of the binary tree. On the other hand, a max-heap has as root the element with the biggest key, and the relationship between the keys of a node and its parent is reversed of that of a min-heap. We also discussed an array-based implementation of heaps. In this assignment, your task is to...

  • You will design a program to keep track of a restaurants waitlist using a queue implemented...

    You will design a program to keep track of a restaurants waitlist using a queue implemented with a linked list. Create a class named waitList that can store a name and number of guests. Use constructors to automatically initialize the member variables. Add the following operations to your program: Return the first person in the queue Return the last person in the queue Add a person to the queue Delete a person from the queue Create a main program to...

  • // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. //...

    // Java Queue LinkedList Assignment // A queue is implemented using a singly linked list. // the variable: back "points" at the first node in the linked list // new elements ( enqueued) are added at the back // the variable: front "points" at the last node in the linked list. // elements are removed (dequeued) from the front // // Several queue instance methods are provided for you; do not change these // Other instance methods are left for...

  • » 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...

  • Onl. (a) i. What is a linear list? ii. Distinguish between linear list and circular linked list. (b) With the aid of...

    Onl. (a) i. What is a linear list? ii. Distinguish between linear list and circular linked list. (b) With the aid of diagrams i. Write an algorithm to delete the Kth element in the list. Vt--︶ 11. write an algorithm to insert an element Y immediately after the Kth element. (c) Using the Stack and the Queue structures explain the LIFO and FIFO principle in data management. (d). Explain the statement "the time taken by an algorithm grows with the...

  • solving using C. Use a singly linked list to implement a priority queue with two operations:...

    solving using C. Use a singly linked list to implement a priority queue with two operations: enqueue and dequeue. Each node contains an integer value, a priority, and a pointer to the next node. The priority is a value between 1- 10 (where 10 is the highest priority). When a value is added to the queue, it is added with a value and priority. When a value is removed from the priority queue, the first element with the highest priority...

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

  • a. Using C++, define a node structure of the linked list (e.g. value is an integer,...

    a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list.

  • 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...

  • ( implemented in C) Please , Make The .c   and   makefile • The   executable • A...

    ( implemented in C) Please , Make The .c   and   makefile • The   executable • A pdf document explaining your approach on how your solved the problem and   a    screenshot of   a   test   run   of   your   program QUESTION: Implement a   doubly   linked   list   and   the   following   methods:   a. Insert b. Delete c. Add first   d. Add last   e. Get f.   Deep   copy   g. Size The main should allow the user to enter three values to insert, a position to delete,  ...

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