Question

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 AaBbC

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

#include <iostream>

#define MAX 10

using namespace std;

class Deque

{

int front, rear, size;

int arr[MAX];

public:

Deque(int size)

{

this->size = size;

front = rear = -1;

}

bool isFull()

{

return ((front == 0 && rear == size-1)||front == rear+1);

}

bool isEmpty(){ return front == -1; }

void push(int data)

{

if(front == -1)

{

front = 0;

arr[++rear] = data;

}

else if(front != 0)

arr[--front] = data;

else

cout << "Overflow!\n\n";

}

void inject(int data)

{

if (isFull())

{

cout << "Overflow!\n\n";

return;

}

if (front == -1)

{

front++;

rear++;

}

else

rear = rear + 1;

arr[rear] = data;

}

int pop()

{

int res;

if(isEmpty())

{

cout << "Underflow!\n\n";

res = -1;

}

else

{

res = arr[front];

if(front == rear)

front = rear = -1;

else

front = front + 1;

}

return res;

}

int eject()

{

int res;

if(isEmpty())

{

cout << "Underflow!\n\n";

res = -1;

}

else

{

res = arr[rear];

if(front == rear)

front = rear = -1;

else

rear = rear - 1;

}

return res;

}

void show()

{

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

{

cout << arr[i] << " ";

}

cout << endl;

}

};

int main()

{

Deque deque(5);

deque.push(17);

deque.inject(10);

deque.inject(23);

deque.inject(16);

cout << "Initial contents:\n";

deque.show();

cout << "\nDeleting from front:\n";

cout << "Item deleted = " << deque.pop() << endl;

cout << "\nContents after deleting:\n";

deque.show();

cout << "\nDeleting from rear:\n";

cout << "Item deleted = " << deque.eject() << endl;

cout << "\nContents after deleting:\n";

deque.show();

cout << "\nPushing an element at the front:\n";

deque.push(21);

cout << "Contents after pushing to the front:\n";

deque.show();

return 0;

}

************************************************************ SCREENSHOT ****************************************************

CODE SNIPPET SCREENSHOTS:

main.cpp saved #include <iostream> #define MAX 10 using namespace std; class Deque int front, rear, size; int arr[MAX]; publi

main.cpp 5 saved if(front == -1) front = 0; arr[++rear] = data; } else if(front != 0) arr[--front] = data; else cout << Over

main.cpp 2 saved else rear = rear + 1; arr[rear] = data; int pop() int res; if(isEmpty()) cout << Underflow!\n\n; res = -1;

main.cpp 3 saved int eject() int res; if(isEmpty()) cout << Underflow!\n\n; res = -1; else res = arr[rear]; if(front == rea

107 108 main.cpp 2 saved 103 int main() 104 E{ 105 Deque deque (5); 106 deque.push(17); deque. inject(10); deque.inject(23);

CONSOLE OUTPUT:

clang version 7.0.0-3-ubuntu0.18.04.1 (tags/RELEASE 700/final), ? clang++-7 -pthread -o main main.cpp 3./main Initial content

Add a comment
Know the answer?
Add Answer to:
The language we are using in Data Structures is C++ right now. Assignment9-Apr 3 References A...
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
  • 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...

  • 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...

  • Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data...

    Spanish Vocabulary Helper For this assignment, we are going to work with adding and removing data from arrays, linear search, and File I/O. In addition, you will learn to work with parallel arrays This program will assist an English-speaking user to build their vocabulary in Spanish This program will read a file containing a list of words in English and another containing the translation of those words in Spanish. The words and corresponding translations should to be stored in two...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

  • For milestone #1, we will start the CARIT site with three static HTML pages and a...

    For milestone #1, we will start the CARIT site with three static HTML pages and a CSS file. Create a dedicated folder for this project. This folder should contain all related files in this project. The future milestones are cumulative and built directly on top of your prior work. Function/content requirements: A home page named “index.html”, which include these contents at least: Description of the center. You may reference the example sites. Latest news: use list tags; make up some...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or...

    RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or a fail. Its very imortant that this and the other questions posted are answered 100% Thank you. 13 - Template C++ Advance Please Only answer assignment if your code is 100%. When pasteing your code use text so I may copy and paste into visual studio. The code and questions must be answered 100% correct and works. Thank you. Programming Assignment Convert the int...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

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