Question

Question 2: Breadth First Search algorithm, process scheduling, graph theory, and congestion handling in networks are some applications of queues. The two core operations in queues include enqueue ) for insertions on one end, and dequeue ( for deletions from the other end, size() returns the size of the queue, front) returns the first element in the queue, empty() determines if the queue is empty. Considering the lines give below, write a c program that implements enqueue and dequeue operations and print the resulting queue items after the specified operations below int que [N] que-enqueue ( 2 ) ; que.enqueue (3) que-enqueue () que-dequeue que enqueue (23) que enqueue () que enqueue (4) que.dequeue ) que.dequeue) q.enqueue (3) q.enqueue (23) que dequeue .enqueue (3)

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

#include <stdio.h>
#define MAX 100

//you can declare the basic variable like array, front, rear etc. even local
//but in that case you have to pass all the detail to every calling function
//so to make the program simple i am declaring different data global so that you
//can only focus on implementation

//below variable will be needed for queue implementation
int que[100]; //you can declare array of any size according to your need
int rear = - 1; //a variable which will point to rear element of queue and it will be -1 if queue is empty
int front = - 1; //a variable which will point to front element of queue and it will be -1 if queue is empty

// function definition are after main
main()
{
//que.enqueue() is a c++ style and you asked for c code
//so below code will serve the same purpose
enqueue(12);
enqueue(3);
enqueue(4);
deque();
enqueue(23);
enqueue(4);
enqueue(4);
deque();
deque();
enqueue(3);
enqueue(23);
deque();
enqueue(3);

//now lets display the content of our queue after above operation
display();
}

//below is the function for enqueue operation
void enqueue(int value)
{
if (rear == MAX - 1) //i.e if array to hold element is full then it is the case of overflow
printf("Queue Overflow \n"); //and do nothing
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0; //then we have to set front to 0 as initially it was -1 which show that queue was empty
rear = rear + 1; //making room for inserting new element
que[rear] = value; //inserting element at end/rear
printf("Element inserted in queue is : %d\n", que[rear]);
}
}

void deque()
{
if (front == - 1 || front > rear) //this must be checked whether there is element to delete or not
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", que[front]);
front = front + 1; //since element at front is deleted hence point to next available element in queue
}
}

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", que[i]);
printf("\n");
}
}

//sample output

Add a comment
Know the answer?
Add Answer to:
Breadth First Search algorithm, process scheduling, graph theory, and congestion handling in networks are some applications...
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
  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

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

  • (5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void...

    (5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void breadthFirstSearch (vertex w) for all vertices u num (u) 0 null edges i=1; num (w) i++ enqueue (w) while queue is not empty dequeue ( V= for all vertices u adjacent to v if num(u) is 0 num (u) = i++; enqueue (u) attach edge (vu) to edges; output edges; Now consider the following graph. Give the breadth-first traversal of the graph, starting from...

  • Balment a la medicul Quoc that speciala a circular que has the following private data members...

    Balment a la medicul Quoc that speciala a circular que has the following private data members and public member functions. The circular que simplemented using an atay. Your submission should consist of four separate files the three source code file header file.implementation file and main program or routine and the program otput. When making the submission, please do not submit it as a file Private data members: int the tray int current size oprema prinete the first met of the...

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

  • Introduction In this lab, you are supposed to implement a graph class with the data structure...

    Introduction In this lab, you are supposed to implement a graph class with the data structure implemented before like linked list and queue. graph The class graph contains three member variables: linkedList *adjacentVertices; //an array of linked list. For a vertice i, adjacentVertices[i] stores the linked list that contains all other vertices connected to vertice i. int numVertices; //The number of vertices in the graph. int maxNumVertices; //The maximum number of vertices the graph can hold. Following public methods are...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

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
Active Questions
ADVERTISEMENT