Question

In: Computer Science

In C++: Provide a linked implementation of a deque and name it LinkedDeque (use singly linked...

In C++: Provide a linked implementation of a deque and name it LinkedDeque (use singly linked list). It can be a template/generic class or you can set it up with a certain data type. Use a test driver to try out your LinkedDeque by adding and removing values from both ends.

Solutions

Expert Solution


#######################################
           Deque.cpp
#######################################
#include "Deque.h"

using namespace std;

Deque::Deque() {
        head = NULL;
        tail = NULL;
}

void Deque::createNode(node* &x, int value) {
        x = new node;
        x->data = value;
        x->next = NULL;
}

void Deque::push(int value) {
        // create a new node
        node *x;
        createNode(x, value);
        x->next = head;
        
        // if list is originally empty
        if(head == NULL) {
                tail = x;
        }
        head = x;
}

void Deque::inject(int value) {
        // create a new node
        node *x;
        createNode(x, value);
        
        // if list is originally empty
        if(head == NULL) {
                head = x;
                tail = x;
        } else {
                tail->next = x;
                tail = x;
        }
}

int Deque::pop(){
        int data = -1;
        // check if list is not empty
        if(head != NULL) {
                node *x = head;

                // update head pointer
                head = head->next;
                if(head == NULL) {
                        tail = NULL;
                }
                data = x->data;
                delete x;
        }
        return data;
}

int Deque::eject() {
        int data = -1;
        // check if list is not empty
        if(head != NULL) {
                node *x = head;

                // check if list has only one node
                if(x == tail) {
                        data = x->data;
                        delete x;
                        head = tail = NULL;                     
                } else {
                        // move to second last node
                        while(x->next != tail) {
                                x = x->next;
                        }
                        data = tail->data;
                        delete tail;
                        tail = x;
                        tail->next = NULL;
                }
        }
        return data;
}

void Deque::printQueue() {
        node *x = head;

        // till all nodes are not traversed
        while(x != NULL) {
                cout << x->data << " ";
                x = x->next;
        }
        cout << endl;
}



#######################################
             Deque.h
#######################################
#pragma once
#include <iostream>

struct node {
        int data;
        node *next;
};

class Deque {
        private:
        node *head;
        node *tail;
        void createNode(node* &x, int value);

        public:
        Deque();
        void push(int value);
        void inject(int value);
        int pop();
        int eject();
        void printQueue();
};



#######################################
            main.cpp
#######################################
#include <iostream>
#include "Deque.h"

using namespace std;

int main() {
        Deque d;

        d.push(1);
        d.push(2);
        d.push(3);
        d.push(4);
        d.push(5);

        cout << "After pushing values from 1 to 5: ";
        d.printQueue();

        d.inject(1);
        d.inject(2);
        d.inject(3);
        d.inject(4);
        d.inject(5);

        cout << "After injecting values from 1 to 5: ";
        d.printQueue();

        cout << "The front element popped out: " << d.pop() << endl;
        cout << "The front element ejected out: " << d.eject() << endl;
        cout << "The front element popped out: " << d.pop() << endl;
        cout << "The front element ejected out: " << d.eject() << endl;
}



**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some...
You are provided with a partial implementation of a templated singly-linked list in LinkedList.h, with some missing functionality. It contains a templated Node class and a templated LinkedList class. Do not modify the class definitions. The linked list class contains the following methods: • LinkedList – Constructor of a linked list with a head pointing to null (implemented). • ~LinkedList – Destructor of a linked list. • deleteFromHead – Removes and returns content of the first node of the list....
Give a complete implementation of the queue ADT using a singly linked list that includes a...
Give a complete implementation of the queue ADT using a singly linked list that includes a header sentinel (in Python).
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Q1) In the implementation of Singly linked list we had an integer variable called size that...
Q1) In the implementation of Singly linked list we had an integer variable called size that keeps track of how many elements in the list. Also, we have a reference “tail” that points to the last node in the list. You are asked to re-implement the concept of singly linked list without using variable size, and without using reference “tail”. a) What are the methods of the main operation of singly linked list that need to be changed? Rewrite them...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
Write a template class that implements an extended queue (use singly Linked List) in c++ please...
Write a template class that implements an extended queue (use singly Linked List) in c++ please create 3 classes please create 3 classes please create 3 classes please create 3 classes please create 3 classes Ex: ExtendedQueue int_queue; ExtendedQueue double_queue; ExtendedQueue char_queue; –Write a program to test this template class. you have to use inheritance so you will create 3 classes : so you will create 3 classes : so you will create 3 classes : so you will create...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The school has multiple classes. Each class has a different number of students. class student { Long ID; string Name; string Address; float grades[3]; student *below; }; class Node // the node represents a class in school { int ID; int NoOfStudents; int NoOfQuizes; student *t;// a linked list of students is allocated dynamically Node *Next; }; class school { string Name; Node *Head; int...
towers of hanoi c++ program using stacks and singly linked lists.
towers of hanoi c++ program using stacks and singly linked lists.
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
Using C++, Create a singly Linked List of patients list so that you can sort and...
Using C++, Create a singly Linked List of patients list so that you can sort and search the list by last 4 digits of the patient's Social Security number. Implement Node insertion, deletion, update and display functionality in the Linked List. Each patient's record or node should have the following data: Patient Name: Age: Last 4 digits of Social Security Number: The program should first display a menu that gives the user the option to: 1) Add a Patient's record...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT