Question

In: Computer Science

C language Write a program in C to implement Queue and its operation (like create, insert,...

C language

Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.

Solutions

Expert Solution

Write a program in C to implement Queue and its operation (like create, insert, delete, search) using array data structure.

Code :-

#include <stdio.h>
 #include<stdlib.h>
#define MAX 50
 
void insert();
void delete();
void display();
void Search();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
    int choice;
    while (1)
    {
        printf("1.Insert element to queue \n");
        printf("2.Delete element from queue \n");
        printf("3.Display all elements of queue \n");
        printf("4.search element in queue \n");
        printf("5.Quit \n");
        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
            insert();
            break;
            case 2:
            delete();
            break;
            case 3:
            display();
            break;
            case 4:
            Search();
            break;
            case 5:
            printf(" you quit from the program");
            exit(1);
            default:
            printf("Wrong choice \n");
        } 
    } 
} 
 
void insert()
{
    int add_item;
    if (rear == MAX - 1)
    printf("Queue Overflow \n");
    else
    {
        if (front == - 1)
        /*If queue is initially empty */
        front = 0;
        printf("Inset the element in queue : ");
        scanf("%d", &add_item);
        rear = rear + 1;
        queue_array[rear] = add_item;
    }
} 
 
void delete()
{
    if (front == - 1 || front > rear)
    {
        printf("Queue Underflow \n");
        return ;
    }
    else
    {
        printf("Element deleted from queue is : %d\n", queue_array[front]);
        front = front + 1;
    }
} 
 
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 ", queue_array[i]);
        printf("\n");
    }
}
void Search()
{

int found = 0,i=0,item; 
printf("\nEnter element to search: ");
    scanf("%d", &item);
   for(i=front;i<=rear;i++)
   {
        
        if(queue_array[i] == item)
        {
            found = 1;
            break;
        }
    }

    if(found == 1)
    {
        printf("\n %d is found in the array\n ", item);
    }
    else
    {
        printf("\n%d is not found in the array\n", item);
    }

}

Output:-

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue
5.Quit
Enter your choice : 1
Inset the element in queue : 12
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue  
5.Quit
Enter your choice : 1
Inset the element in queue : 18
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue
5.Quit
Enter your choice : 1
Inset the element in queue : 8
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue
5.Quit
Enter your choice : 1
Inset the element in queue : 25
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue
5.Quit
Enter your choice : 3
Queue is :
12 18 8 25
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue
5.Quit
Enter your choice : 2
Element deleted from queue is : 12
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue
5.Quit
Enter your choice : 4
Enter element to search: 8
8 is found in the array
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.search element in queue
5.Quit
Enter your choice : 5
you quit from the program.


Related Solutions

write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a...
Language C++ Implement a Priority Queue with a Binary HEAP. Use a Max Heap. Create a class called Node: Have a Name and Priority.Data set - 10 is the highest priority, 1 is lowest priority. Enqueue and dequeue in the following order. Function  Name, Priority Enqueue  Joe, 3 Enqueue  Fred,1 Enqueue Tuyet,9 Enqueue  Jose, 6 Dequeue Enqueue  Jing, 2 Enqueue  Xi, 5 Enqueue  Moe, 3 DequeueEnqueue  Miko, 7 Enqueue Vlady, 8 Enqueue Frank, 9 Enqueue  Anny, 3 DequeueEnqueue  Xi, 2 Enqueue  Wali, 2 Enqueue  xChe, 6 Enqueue  xVerra, 8 Dequeue Dequeue Dequeue Dequeue...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked...
Write a C program to implement a queue (FIFO) of characters in a one-way, circular, linked list. One way means that each node has only one pointer, Q, (not both a rear and a front pointer), so the program must use pointers-to-pointers. Include functions to insert(), remove(), for use in main() where the user is prompted "Enter "i" to insert a new element, "r" to remove an element, "q" to quit:"
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file)
Write a c++program using queue to find the minimum value in a queue. Use the above...
Write a c++program using queue to find the minimum value in a queue. Use the above program for implementing queue. You must use dequeue function to read values from queue.  
How do I create this program? Using C++ language! Write a program that reads data from...
How do I create this program? Using C++ language! Write a program that reads data from a text file. Include in this program functions that calculate the mean and the standard deviation. Make sure that the only global varibles are the mean, standard deviation, and the number of data entered. All other varibles must be local to the function. At the top of the program make sure you use functional prototypes instead of writing each function before the main function....ALL...
((by C++ ))Write a program that will reverse the content of a Queue using the following...
((by C++ ))Write a program that will reverse the content of a Queue using the following standard queue operations. enqueue(x) : Add an item x to rear of queue. dequeue() : Remove an item from front of queue. empty() : Checks if a queue is empty or not. For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a funcntion that takes in a binary tree and heapafies it. Meaning it takes in a pointer to a binary tree and converts it into a heap. (You may choose max or min heap).
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT