Question

Write an (efficient) pseudocode for the implementation of each of the following function prototypes (proper C...

Write an (efficient) pseudocode for the implementation of each of the following function prototypes (proper C code will also be accepted)

- void print(Q) – prints the entire queue Q from front to back.

- int enqueue(Q,x) – enqueues the element x into queue Q (if unsuccessful, return -1)

- int* dequeue(Q) – dequeues the next integer from front of the queue (if unsuccessful, return null)

- int isEmpty(Q) – returns 1 (true) or 0 (false) depending on emptiness of the queue Q

- unsigned int size(Q) – returns the number of items currently in the queue

In your final solution, you may add any extra attributes that you feel is needed for this question but you must use as few extra attributes as possible and your algorithm must be as efficient as possible (in terms of big-O analysis).

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

Dear Learner,

I'm providing you the pseudocode of the functions asked

We will be implementing queue using array data structure.

There will be attributes of the queue viz., front, rear and capacity, indicating the front element,last element and the maxmum capacity of the queue, respectively.

1. void print(Q) //time complexity = O(N)

if (front = = rear) //to check if the queue is empty

print("EMPTY QUEUE")

return

else //when the queue is not empty

for i = front to i <= rear

print(Q[i])

2. int enqueue(Q,x) //time complexity = O(1)

if (capacity == rear) //if the queue is full

printf("Queue is full")

           return (-1)

else

queue[rear] = data

           rear = rear + 1

3. int* dequeue(Q) //time complexity = O(1)

if (front == rear) //if queue is empty

printf("Queue is empty")

return null

else

for i=0 to i<rear

queue[i] = queue[i+1]

rear = rear-1

4. int isEmpty(Q) //time complexity = O(1)

if (front = = rear) //when the queue IS empty

return (1)

else // when the queue IS NOT empty

return (0)

5. NOTE : There are basically 2 ways in which you can count the number of elements in queue.

1. Where you have a rear pointer pointing towards the last element in the queue. In this case returning the value of rear will give us the number of elements in the queue.

2. Traversing the entire queue and counting the elements. This method is much less efficient as we have to go through the entire queue each time we want to count the number of elements in it.

However, we'll discuss both the methods here,

METHOD 1. time complexity = O(1)

unsigned int size(Q)

return rear

METHOD 2. time complexity = O(N)

unsigned int size(Q)

int count = 0

for i = front to i<=rear

count = count + 1

return rear

I hope I have explained the question the way you were expecting. In case if you still have any doubts or further queries, feel free to ask us in the comment section.

Please leave a like if this was helpful.

Thanks,

Happy Studying.

Add a comment
Know the answer?
Add Answer to:
Write an (efficient) pseudocode for the implementation of each of the following function prototypes (proper C...
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
  • A limited-sized Queue ADT is an abstract data type that has a limit on the length...

    A limited-sized Queue ADT is an abstract data type that has a limit on the length of the queue. It can be created with a static array of size N, where N is the maximum length of the array. In C, this structure can be defined as follows: typedef struct {int * data;//array of the data on the queue//you may add other attributes here but use as few as possible} queue_t; Write an (efficient) pseudocode for the implementation of each...

  • How do I pass values to this function? class DynIntQueue { struct QueueNode { int value;...

    How do I pass values to this function? class DynIntQueue { struct QueueNode { int value; QueueNode *next; QueueNode(int value1, QueueNode *next1 = nullptr) { value = value1; next = next1; } }; // These track the front and rear of the queue QueueNode *front; QueueNode *rear; public: // Constructor and Destructor DynIntQueue(); ~DynIntQueue(); // Member functions void enqueue(int); void dequeue(int &); bool isEmpty() const; void clear(); }; main #include <iostream> #include "DynIntQueue.h" using namespace std; int main() {DynIntQueue list;...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • Create a Java code that includes all the methods from the Lecture slides following the ADTs...

    Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....

  • how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 }...

    how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...

  • Design and implement a class Q that uses Q.java as a code base. The queue ADT...

    Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

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