Question

In: Computer Science

Done in C language using mobaxterm if you can but use basic C This program is...

Done in C language using mobaxterm if you can but use basic C

This program is broken up into three different functions of insert, delete, and main. This program implements a queue of individual characters in a circular list using only a single pointer to the circular list; separate pointers to the front and rear elements of the queue are not used.

  1. The linked list must be circular.  
      
  2. The insert and remove operations must both be O(1)
      
  3. You may use only one pointer into the queue from the outside; you may not have separate, named pointers for the front and the rear.

Test Cases:

  1. Try to remove – thus making sure that the queue was initialized properly (i.e., empty)
  2. Insert 'A', then do a remove, then another remove – thus making sure that you can empty the queue
  3. Insert 'B', then 'F', do a remove, then insert 'D', then 'C', then do four removes (the last one should fail, of course). This test case will make sure your queue is FIFO – your last four removes should come out 'F', 'D', 'C', and then "cannot remove from an empty queue"

Solutions

Expert Solution

#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("Queue Underflown");
return ;
}
printf("Element deleted from queue is : %dn",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is emptyn");
return;
}
printf("Queue elements :n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos])
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}
printf("n");
}
int main()
{
int choice,item;
do
{
printf("1.Insertn");
printf("2.Deleten");
printf("3.Displayn");
printf("4.Quitn");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Input the element for insertion in queue : ");
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choicen");
}
}while(choice!=4);
return 0;
}

Another way :-

// C or C++ program for insertion and

// deletion in Circular Queue

#include<bits/stdc++.h>

using namespace std;

struct Queue

{

    // Initialize front and rear

    int rear, front;

    // Circular Queue

    int size;

    int *arr;

    Queue(int s)

    {

       front = rear = -1;

       size = s;

       arr = new int[s];

    }

    void enQueue(int value);

    int deQueue();

    void displayQueue();

};

/* Function to create Circular queue */

void Queue::enQueue(int value)

{

    if ((front == 0 && rear == size-1) ||

            (rear == (front-1)%(size-1)))

    {

        printf("\nQueue is Full");

        return;

    }

    else if (front == -1) /* Insert First Element */

    {

        front = rear = 0;

        arr[rear] = value;

    }

    else if (rear == size-1 && front != 0)

    {

        rear = 0;

        arr[rear] = value;

    }

    else

    {

        rear++;

        arr[rear] = value;

    }

}

// Function to delete element from Circular Queue

int Queue::deQueue()

{

    if (front == -1)

    {

        printf("\nQueue is Empty");

        return INT_MIN;

    }

    int data = arr[front];

    arr[front] = -1;

    if (front == rear)

    {

        front = -1;

        rear = -1;

    }

    else if (front == size-1)

        front = 0;

    else

        front++;

    return data;

}

// Function displaying the elements

// of Circular Queue

void Queue::displayQueue()

{

    if (front == -1)

    {

        printf("\nQueue is Empty");

        return;

    }

    printf("\nElements in Circular Queue are: ");

    if (rear >= front)

    {

        for (int i = front; i <= rear; i++)

            printf("%d ",arr[i]);

    }

    else

    {

        for (int i = front; i < size; i++)

            printf("%d ", arr[i]);

        for (int i = 0; i <= rear; i++)

            printf("%d ", arr[i]);

    }

}

/* Driver of the program */

int main()

{

    Queue q(5);

    // Inserting elements in Circular Queue

    q.enQueue(A);

  q.enQueue(B);

    q.enQueue(F);

    q.enQueue(D);

    q.enQueue(C);

    // Display elements present in Circular Queue

    q.displayQueue();

    // Deleting elements from Circular Queue

    printf("\nDeleted value = %d", q.deQueue());

    printf("\nDeleted value = %d", q.deQueue());

    q.displayQueue();

    q.enQueue(E);

    q.enQueue(D);

    q.enQueue(C);

    q.displayQueue();

    q.enQueue(20);

    return 0;

}


Related Solutions

this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Use Visual Basic Language In this assignment you will need to create a program that will...
Use Visual Basic Language In this assignment you will need to create a program that will have both a “for statement” and an “if statement”. Your program will read 2 numbers from the input screen and it will determine which is the larger of the 2 numbers. It will do this 10 times. It will also keep track of both the largest and smallest numbers throughout the entire 10 times through the loop. An example of the program would be...
You are using ONLY Programming Language C for this: In this program you will calculate the...
You are using ONLY Programming Language C for this: In this program you will calculate the average of x students’ grades (grades will be stored in an array). Here are some guidelines to follow to help you out: 1. In your program, be sure to ask the user for the number of students that are in the class. The number will help in declaring your array. 2. Use the function to scan the grades of the array. To say another...
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
(MUST BE DONE IN C (NOT C++)) For this program, remember to use feet and inches....
(MUST BE DONE IN C (NOT C++)) For this program, remember to use feet and inches. First, ask the user for the name of students they have in their class. Then, using a loop, you will ask for each student’s height. However, you will have to use two separate variables, one for feet and one for inches. Then, you will have to call two functions. The first function will check if the values entered are valid (check if number of...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
Please use C language and use link list to do this program. This program should ask...
Please use C language and use link list to do this program. This program should ask user to enter two fraction polynomials. Then user chocie if he want add it or multiple it. I need you please to test it to see if it work with these two fraction polynomials 1-  Left Poly Pointer: 1/1x2 + 3/4x + 5/12 2-Right Poly Pointer: 1/1x4 – 3/7x2 + 4/9x + 2/11 AND AFTER ADDING 3- Resulting Poly Pointer: 1/1x4 + 4/7x2 + 43/36x...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT