Question

Using C++ language, Design and implement a class representing a doubly linked list. The class must...

Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements:

  1. The linked list and the nodes must be implemented as a C++ templates
  1. The list must be generic – it should not implement arithmetic/logic functions. (template class)
  1. It must include a destructor and a copy constructor
  2. It must include methods to insert at the front and at the back of the list
  3. It must include a method to return the length of the list
  4. It must provide an iterator-based interface for the user from front to back
  5. It must include an iterator-based interface for the user from back to front
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>
using namespace std;

template<class T>
struct node {
   node<T>* next;
   node<T>* prev;
   T data;
};

template<class T>
class DDL {
   private:
       node<T> *next;
       node<T> *prev;
       node<T> *first;
       node<T> *last;
       node<T> *current;
       int size = 0;
   public:
       DDL<T>()
       {
           next = NULL;
           prev = NULL;
           first = NULL;
           last = NULL;
           current = NULL;
       }
       DDL<T>(const DDL<T> &D)
       {
           first = D.first;
           last = D.last;
           next = D.next;
           prev = D.prev;
           current = D.current;
           size = D.size;
       }
       void push_back(T d)
       {
           size++;
           if(!first) {
           // The list is empty
           first = new node<T>;
           first->data = d;
           first->next = NULL;
           first->prev = NULL;
           last = first;
           current = first;
       } else {
           // The list isn't empty
           if(last == first) {
               // The list has one element
               last = new node<T>;
               last->data = d;
               last->next = NULL;
               last->prev = first;
               first->next = last;
               current = last;
           } else {
               // The list has more than one element
               node<T>* insdata = new node<T>;
               insdata->data = d;
               insdata->next = NULL;
               last->next = insdata;
               insdata->prev = last;
               last = insdata;
               current = last;
           }
       }
       }
      
       void push_front(T d)
       {
           size++;
           if(!last) {
           // The list is empty
           last = new node<T>;
           last->data = d;
           last->next = NULL;
           last->prev = NULL;
           first = last;
           current = first;
       } else {
           // The list isn't empty
           if(last == first) {
               // The list has one element
               first = new node<T>;
               first->data = d;
               first->prev = NULL;
               first->next = last;
               first->prev = first;
               current = first;
           } else {
               // The list has more than one element
               node<T>* insdata = new node<T>;
               insdata->data = d;
               insdata->prev = NULL;
               last->next = insdata;
               insdata->next = first;
               first = insdata;
               current = first;
           }
       }
       }
      
       T get()
       {
           return this->current.data;
       }
      
       T get(int index) {
           if(index == 0) {
               // Get the first element
               return this->first->data;
           } else {
               // Get the index'th element
               node<T>* curr = this->first;
               for(int i = 0; i < index; ++i) {
                   curr = curr->next;
               }
               return curr->data;
           }
       }
      
       int length()
       {
           return size;
       }
      
       T operator[](int index) {
           return get(index);
       }
       ~DDL() { }
};

int main()
{
   DDL<int> p;
   for(int i=0;i<10;i++)
       p.push_front(i+1);
   DDL<int> head = p;
   for(int i=0;i<head.length();i++)
       cout<<head[i]<<endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using C++ language, Design and implement a class representing a doubly linked list. The class must...
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
  • PART 1: Design and implement a class representing a doubly linked list. The class must have...

    PART 1: Design and implement a class representing a doubly linked list. The class must have the following requirements: 1)The linked list and the nodes must be implemented as C++ templates 2)The list must be generic

  • In c++ language Design and implement a Queue data structure using linked list. Support the following...

    In c++ language Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.

  • Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted...

    Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable

  • You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow...

    You need to implement a queue based on the doubly linked list. (In C++) **PLEASE follow the format** **Don't worry about the readme.txt** THANK YOU SO SO MUCH! You have to implement the doubly linked list in DList.h and DList.cpp and the queue in the LinkedQueue.h and LinkedQueue.cpp; all as template classes. You have to provide the main.cpp file as we discussed in class to allow the user to interact with your queue using enqueue, dequeue, front, back, size, empty,...

  • This class implements a doubly linked list in which the nodes are of the class DLNode....

    This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked...

    C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to create a queue of user-specified capacity (c) enqueue (d) dequeue (e) is_full (f) is_empty display (h) destructor that deallocates all nodes (i) copy constructor (j) overloaded assignment operator Demonstrate using a main function.

  • Using C++ • Implement the following in both string linked list and in the template version...

    Using C++ • Implement the following in both string linked list and in the template version of the class: • Add to the back and remove from back of the list : - void addBack(….); - string removeBack(); E removeBack(…); • Insert elements in a sorted list i.e., find the position where an element fits and add it to the list. Note you must call the addBack and addFront functions if you insert in the front or rare of the...

  • Write a C++implementation of a doubly linked list class using a template class representation for a...

    Write a C++implementation of a doubly linked list class using a template class representation for a node and using pointers.In its public API provide functions to insert,find, delete, get size and get position of an element. Write C++ code to show those functions work as expected. (MUST COMPILE AND RUN)

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

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