Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write C++ code for the following questions: a. Implement the member function pus

ID: 3771180 • Letter: W

Question

Write C++ code for the following questions:
a. Implement the member function push_back(const int &). The function adds a new entry at the back of a deque. The total number of nodes should be increased by one. The deque might be empty.

b. Implement the member function pop back(). The functiion removes the node at the back of a deque and returns the data of the node. The total number of nodes should be decreased by one. The deque might be empty.

c. Implement the member function back(). The function returns the data value at the back of a deque. The deque might be empty.

this is a c++ question.

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

class Deque;
class DequeNode {
   friend class Deque;
private:
   int data;
   DequeNode *prev;
   DequeNode *next;


};
class Deque {

private:
   DequeNode *head;
   int number;
   DequeNode *tail;

public:
   Deque() {
       head = NULL;
       tail = NULL;
   }
   void push_back(const int &ele) {
       DequeNode *node = new DequeNode();
       node->data = ele;
       node->prev = NULL;
       node->next = NULL;
       if(tail == NULL) {
           head = node;
           tail = node;
       } else {
           //insert at tail
           node->prev = tail;
           tail->next = node;
           tail = node;
       }
   }

   int pop_back() {
       if(tail == NULL)
           return NULL;
       else {
           DequeNode *prev = tail->prev;
           prev->next = NULL;
           tail->prev = NULL;
           //delete tail
           tail = prev;

       }

   }

   int back() {
       if(tail == NULL)
           return NULL;
       else
           return tail->data;
   }

   void display() {
       DequeNode *temp = head;
       while(temp != NULL) {
           cout << temp->data << " ";
           temp = temp->next;
       }

       cout <<endl;
   }
};


int main() {

   Deque deq = Deque();
   deq.push_back(1);
   deq.display();
   deq.push_back(2);
   deq.push_back(3);
   deq.push_back(4);
   deq.display();
   cout << " back(): " << deq.back() <<endl;
   deq.pop_back();
   deq.pop_back();
   deq.display();


}

---output--

1
1 2 3 4
back(): 4
1 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote