Question

C++ Data Structures Build a program with data structure deque. Hint: Implementation of Deque using circular...

C++ Data Structures

Build a program with data structure deque.

Hint: Implementation of Deque using circular array (from geeksforgeeks)

Homework Answers

Answer #1

code:

#include<iostream>
using namespace std;
  
// Maximum size of array or Dequeue
#define MAX 100
  
// A structure to represent a Deque
class Deque
{
int arr[MAX];
int front;
int rear;
int size;
public :
Deque(int size)
{
front = -1;
rear = 0;
this->size = size;
}
  
// Operations on Deque:
void insertfront(int key);
void insertrear(int key);
void deletefront();
void deleterear();
bool isFull();
bool isEmpty();
int getFront();
int getRear();
};
  
// Checks whether Deque is full or not.
bool Deque::isFull()
{
return ((front == 0 && rear == size-1)||
front == rear+1);
}
  
// Checks whether Deque is empty or not.
bool Deque::isEmpty ()
{
return (front == -1);
}
  
// Inserts an element at front
void Deque::insertfront(int key)
{
// check whether Deque if full or not
if (isFull())
{
cout << "Overflow\n" << endl;
return;
}
  
// If queue is initially empty
if (front == -1)
{
front = 0;
rear = 0;
}
  
// front is at first position of queue
else if (front == 0)
front = size - 1 ;
  
else // decrement front end by '1'
front = front-1;
  
// insert current element into Deque
arr[front] = key ;
}
  
// function to inset element at rear end
// of Deque.
void Deque ::insertrear(int key)
{
if (isFull())
{
cout << " Overflow\n " << endl;
return;
}
  
// If queue is initially empty
if (front == -1)
{
front = 0;
rear = 0;
}
  
// rear is at last position of queue
else if (rear == size-1)
rear = 0;
  
// increment rear end by '1'
else
rear = rear+1;
  
// insert current element into Deque
arr[rear] = key ;
}
  
// Deletes element at front end of Deque
void Deque ::deletefront()
{
// check whether Deque is empty or not
if (isEmpty())
{
cout << "Queue Underflow\n" << endl;
return ;
}
  
// Deque has only one element
if (front == rear)
{
front = -1;
rear = -1;
}
else
// back to initial position
if (front == size -1)
front = 0;
  
else // increment front by '1' to remove current
// front value from Deque
front = front+1;
}
  
// Delete element at rear end of Deque
void Deque::deleterear()
{
if (isEmpty())
{
cout << " Underflow\n" << endl ;
return ;
}
  
// Deque has only one element
if (front == rear)
{
front = -1;
rear = -1;
}
else if (rear == 0)
rear = size-1;
else
rear = rear-1;
}
  
// Returns front element of Deque
int Deque::getFront()
{
// check whether Deque is empty or not
if (isEmpty())
{
cout << " Underflow\n" << endl;
return -1 ;
}
return arr[front];
}
  
// function return rear element of Deque
int Deque::getRear()
{
// check whether Deque is empty or not
if(isEmpty() || rear < 0)
{
cout << " Underflow\n" << endl;
return -1 ;
}
return arr[rear];
}
  
// Driver program to test above function
int main()
{
Deque dq(5);
cout << "Insert element at rear end : 5 \n";
dq.insertrear(5);
  
cout << "insert element at rear end : 10 \n";
dq.insertrear(10);
  
cout << "get rear element " << " "
<< dq.getRear() << endl;
  
dq.deleterear();
cout << "After delete rear element new rear"
<< " become " << dq.getRear() << endl;
  
cout << "inserting element at front end \n";
dq.insertfront(15);
  
cout << "get front element " << " "
<< dq.getFront() << endl;
  
dq.deletefront();
  
cout << "After delete front element new "
<< "front become " << dq.getFront() << endl;
return 0;
}

output:

Insert element at rear end  : 5                                                                                                

insert element at rear end : 10                                                                                                

get rear element  10                                                                                                           

After delete rear element new rear become 5                                                                                    

inserting element at front end  

get front element  15                                                                                                          

After delete front element new front become 5

Know the answer?
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
Similar Questions
visual studio VB.Net write a vb.net program to create an array of 15 structures, each structure...
visual studio VB.Net write a vb.net program to create an array of 15 structures, each structure contains the following fields: user name, number of posts, then write a sub to read from the keyboard values into the structures fields and write a function to find and return the average of number of posts for all users and write a sub to print the user name that has zero number of posts.[ number of posts could be from 0 or more]
Using C++ 1. Create a program that asks the user to create 5 triangles, asking for...
Using C++ 1. Create a program that asks the user to create 5 triangles, asking for 5 bases and 5 heights (you can use an array), have a function to print the triangle bases, heights, and areas 2. Create a structure for the Triangle that holds the base and height of the triangles. Ask the user for 5 triangles, and print them. 3. Create an array of 5 structures - ask the user for 5 triangles, and then print the...
Describe a software application that would require a graph data structure in its implementation. Explain what...
Describe a software application that would require a graph data structure in its implementation. Explain what the vertices and edges of the graph would represent. Would the graph be undirected or directed? Would it be weighted or unweighted? Decide which type of representation would be best for this application, an adjacency matrix, an adjacency list using an array of linked lists or a fully linked representation. Explain your reasoning.
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to...
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to generate and populate Parallel-Array Data Structure: const int NG = 4; //Number of Grades string names[] = {"Amy Adams", "Bob Barr", "Carla Carr", "Dan Dobbs", "Elena Evans" }; int exams[][NG]= { {98,87,93,88}, {78,86,82,91}, {66,71,85,94}, {72,63,77,69}, {91,83,76,60} };    --------------------------------------------------------------------------------- 1 A) Create and Populate a Parallel-Array Data Structure using the code described in "StudentDataStructure.txt". B) Define a Record Data Structure for student records. It...
The Heapsort algorithm is based on the heap data structure. The algorithm works by: repeatedly removing...
The Heapsort algorithm is based on the heap data structure. The algorithm works by: repeatedly removing the maximum value, i.e. the element at position 0, from the heap; and then restoring the heap property, until the heap is empty. Given the array [73, 57, 0, 47, 8, 9], first turn it into a Max-Heap, and then into a sorted array in ascending order, using the Heapsort algorithm. You have to show the array content of the heap, as well as...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
C program question: Write a small C program connect.c that: 1. Initializes an array id of...
C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to...
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
Write the following program using the if/else decision structure using C programming. This problem calculates the...
Write the following program using the if/else decision structure using C programming. This problem calculates the cost of a movie ticket. If a person is 5 years old or younger, the movie ticket is free. If the person is over 65, then the movie ticket costs $5.00. Everyone else pays $10.00. Your program should ask for the person’s age. It should also print output which looks like this: You are (put their age her) year old, so your movie ticket...
What is the basic structure of a C++ program?
What is the basic structure of a C++ program?