Question

(Deque) A deque is a data structure consisting of a list of items on which the following operations are possible: a. push (x)

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

ANS.......................................................................................

DEQUEUE.CPP:
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;

struct node
{
int data;
node *next;
node *prev;
}*front = NULL,*rear = NULL,*temp1 = NULL,*temp2 = NULL;

void push(int x)
{
temp1 = new node;
temp1->data = x;
  
if(front == NULL)
{


front = rear = temp1;
rear->next = NULL;
rear->prev = NULL;
}
else
{
temp1->next = front;
front->prev = temp1;
temp1->prev = NULL;
front = temp1;
}
}

void inject(int x)
{
temp1 = new node;
temp1->data = x;
temp1->next = NULL;
if(front == NULL)
{
front = rear = temp1;
rear->next = NULL;
rear->prev = NULL;
}
else
{
temp1->prev = rear;
rear->next = temp1;
rear = temp1;
}
}

int pop()
{
int x;
if(front == NULL)
{
cout<<"Queue is empty!! ";
}
else
{
temp2 = front;
x = temp2->data;
front = front->next;
if(front==NULL){
rear = NULL;
}else{
front->prev = NULL;
}
delete(temp2);
return(x);
}
}

int eject()
{
int x;
if(front == NULL)
{
cout<<"Queue is empty!! ";
}
else
{
temp2 = rear;
x = temp2->data;
rear = rear->prev;
if(rear==NULL){
front = NULL;
}else{
rear->next = NULL;
}
delete(temp2);
return(x);
}
}

MAIN.CPP:

In the main function we would be calling each of these functions n number of times, n being the number of values the user needs to push. PFB the a2q3.cpp file:

#include<iostream>
#include<stdio.h>
#include<conio.h>

#include "Deque.h"
using namespace std;

int main()
{
int n1,c1 = 0,x1;
cout<<"Enter the number of values to be pushed into queue ";
cin>>n1;
while (c1 < n1)
{
cout<<"Push the values in the Deque ";
cin>>x1;
push(x1);
c1++;
}

while(c1>0)
{
cout<<" The front element popped out is "<<pop()<<endl;
c1--;
}

int n2,c2 = 0,x2;
cout<<"Enter the number of values to be pushed into queue ";
cin>>n2;
while (c2 < n2)
{
cout<<"Inject the values in the Deque ";
cin>>x2;
inject(x2);
c2++;
}
  
while(c2>0)
{
  
cout<<" The rear element ejected out is "<<eject()<<endl;
c2--;
}
getch();
}

Add a comment
Know the answer?
Add Answer to:
(Deque) A deque is a data structure consisting of a list of items on which the...
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
  • The language we are using in Data Structures is C++ right now. Assignment9-Apr 3 References A...

    The language we are using in Data Structures is C++ right now. Assignment9-Apr 3 References A A A X? A. Mailings Review A E E. A. View . :. 2+ . 1 . Albebote albebete Nere AutoCeDdte AaBbCcDc AabCeDdE Heading 1 Heading 2 Student Name: Student ID (Last 4 digits): Due Date: 4/3/2020 Note: For any program questions of the assignment, Make sure you complete programs. You may copy & paste the code to this word file, and submit them...

  • Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deq...

    Template Deque Class (C++) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Recommended Steps testDeque.cpp : // C++ implementation of doubly linked list Deque doubly linked list #include <bits/stdc++.h> using namespace std; class Timer { // To replace with the full timer class definition // inside this folder: LearnCpp9_18_timeSortArray.cpp }; // Node of a doubly linked list template<class T> class...

  • Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1"...

    Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1" << endl; dq1.insertFront(42); dq1.insertBack(216); cout << dq1.peekFront() << " - 42" << endl; cout << dq1.peekBack() << " - 216" << endl; cout << dq1.size() << " - 2" << endl; Deque dq2(dq1); Deque dq3; dq3 = dq1; cout << dq1.removeFront() << " - 42" << endl; cout << dq1.removeBack() << " - 216" << endl; cout << dq2.peekFront() << " - 42" <<...

  • using python file and screenshot the file that show up is correct Your input data for...

    using python file and screenshot the file that show up is correct Your input data for each application is:             7   1   8   9   4   2   5   10   6   3 Treat this data as integer data type. Push onto and pop off of each of the input items onto a stack implemented in Python.   Identify the item 4 when it is popped off of your stack. 2. Feed this same input data into a Queue implemented in Python.   Identify the...

  • please explain all the steps. I need it ASAP. i will give u good ratings. Do...

    please explain all the steps. I need it ASAP. i will give u good ratings. Do in java file Queues are often used to simulate the flow of people, cars, airplanes, transactions, and so on. Write a program that models checkout lines at a supermarket, using the Queue class from the queue.java program (Listing 4.4). Several lines of customers should be displayed; you can use the display, insertſ), and remove() method. You can add a new customer by pressing a...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template ...

    Template Dequeue Class (C++ ) In this assignment, we will use a given test menu for the template Deque Class (a Linked List based Double Ended Queue, Deque) that you have put together. Starter testDeque.cpp which contains: Timer class holder (you need to go through the LearnCpp Ch15 and import it in) Node class Deque class specification (you need to fill out the definition) here is the testDeque.cpp code: // C++ implementation of doubly linked list Deque doubly linked list...

  • CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class....

    CS 373 Home Work project 11 Create a queue class by priviate inherting the unorderedArrayListType class. A queue class is a First-In-First-Out data structure. To understand a queue, think of a checkout line at a grocery store: the person at the front is served first (removed), and people are added to the line from the back end. class queue : private unorderedArrayListType { public: bool isEmpty() const; // test whether queue is empty // Post: returns true if queue is...

  • Using C++, create a doubly linked list data structure that stores strings. At a minimum, you...

    Using C++, create a doubly linked list data structure that stores strings. At a minimum, you must have a List class that contains the list functionality (including an insert function) and a linkable object ("link node") class. For convenience, you may include the data directly or the data may be external to the link node, connected with a reference. You may use an inner class for the LinkNode and/or include the LinkNode class with the List class file if you...

  • In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is...

    In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is to complete the implementation of the classes specified as below. Y 1 Your Task You are given a class “Item” that contains one integer value, and two pointers. You are going to build a doubly linked list class DLinkedList. I describe the tasks below. Task 1: Implement the constructors (default and copy) of DLinkedList. You need to make sure that the copy constructor makes...

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
ADVERTISEMENT