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...
Question 1: Write a C program that stores student information in structure and display it. Question...
Question 1: Write a C program that stores student information in structure and display it. Question 2: In addition to the first question, you are expected to create one more structure for courses. Then, structures must be filled by user inputs. After filling your structures, you will let user to get any student enroll to any course from the course list. Hint: To achieve these steps, you must use pointer in your student structure to link it to course structure.
Movie Structure File Write a program that will read in a CSV file with information regarding...
Movie Structure File Write a program that will read in a CSV file with information regarding movies. The file will contain a movie name, MPAA coding (G, PG, PG-13, R), number of minutes, and ratings (1-10) from the three top critics. Your program will read in the data into an array of structures (No more than 20 movies) and send the array to a function that will produce a professional report file (MovieReport.txt) which includes all of the information, an...
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.
When doing the File IO_struct program, did you notice that the design of the data structure...
When doing the File IO_struct program, did you notice that the design of the data structure can be improved? The problem is that some data are redundant in the struct/array structure. For example, the word “Amy” appears three times in the data structure. In real industry systems, no redundant data is allowed, since data in this kind of systems cannot be easily updated, deleted, added or saved. It is extremely error-prone. Thus in practice this type of designs should be...
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...
c++ Create a program that saves data beyond the life time of the program. Utilize the...
c++ Create a program that saves data beyond the life time of the program. Utilize the following struct: struct PetRock { string name; int age; }; When the program runs, it first checks to see if a file contains data (PetRocks). If the file contains data then the data will be read from the file into an array of size 3 and will be printed (assume there will be no more than 3 objects saved in the data file). If...
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...
#data structures Give the appropriate execution time efficiency of the following array algorithms using Big-O notation....
#data structures Give the appropriate execution time efficiency of the following array algorithms using Big-O notation. Assume the size of the array is n. a)   Linear Search (average case)   ______________________    b)   Binary Search (worst case)       ______________________    c)   Insertion Sort (best case)       ______________________    d)   Insertion Sort (average case)           ______________________    e)   Quick Sort (average case)           ______________________
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT