IMPLEMENT FUNCTIONS C++ #include "node.h" #include "list.h" #include #include using namespace std; class DoublyLinkedList : public List {     node* head;     node* tail;     int index;     node* create_node(int num, node* predecessor, node* successor) {         node* n = (

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

IMPLEMENT FUNCTIONS C++

#include "node.h"
#include "list.h"
#include <cstdlib>
#include <iostream>
using namespace std;

class DoublyLinkedList : public List {
    node* head;
    node* tail;
    int index;

    node* create_node(int num, node* predecessor, node* successor) {
        node* n = (node*) malloc( sizeof(node) );
        n->element = num;
        n->next = successor;
        n->prev = predecessor;
        return n;
    }

    void add_between(int num, node* predecessor, node* successor) {
        node* newest = create_node(num, predecessor, successor);
        predecessor->next = newest;
        successor->prev = newest;
        index++;
    }

    public:
        DoublyLinkedList() {
            index = 0;
            head = create_node(0, NULL, NULL);
            tail = create_node(0, head, NULL);
            head->next = tail;
        }

        ~DoublyLinkedList() {
            clear();
        }

        void addHead(int num) {
            add_between(num, head, head->next);
        }

        void addTail(int num) {
            add_between(num, tail->prev, tail);
        }

        int removeHead() {


            return 0;
        }
        int removeTail() {


            return 0;
        }

        int add(int num) {


            return 0;
        }

        int remove(int num) {


            return 0;
        }

        int get(int pos) {
            node* currnode;
            int count;
            bool start_from_head = true;
            if (pos > index/2) {
                start_from_head = false;
            }

            if (start_from_head) {
                // START FROM HEAD
                currnode = head->next;
                count = 0;
                while (currnode != NULL) {
                    count++;
                    if (count == pos) {
                        return currnode->element;
                    } else {
                        currnode = currnode->next;
                    }
                }
            } else {
                // START FROM TAIL
                currnode = tail->prev;
                count = index + 1;
                while (currnode != NULL) {
                    count--;
                    if (count == pos) {
                        return currnode->element;
                    } else {
                        currnode = currnode->prev;
                    }
                }
            }
            return -1;
        }

        int size() {
            return index;
        }

        bool addAt(int num, int pos) {

     return false;
    }

        int removeAt(int pos) {


       return 0;
    }

        int removeAll(int num) {

      return 0;
      }

        int contains(int num) {


           return 0;
       }

        int count(int num) {

            
       return 0;
     }

        bool move(int pos1, int pos2) {

            
          return false;
     }


        bool isEmpty() {


        return false;
      }
       
        void clear() {


        }

        // WARNING! Do not modify this method below!
        // Doing so will nullify your score for this activity.
        void print() {
            node* currnode = head;
            cout << "HEAD: ";
            while (true) {
                cout << currnode->element;
                if (currnode == tail) {
                    cout << endl;
                    break;
                }
                cout << " -> ";
                currnode = currnode->next;
            }
            currnode = tail;
            cout << "TAIL: ";
            while (true) {
                cout << currnode->element;
                if (currnode == head) {
                    cout << endl;
                    break;
                }
                cout << " -> ";
                currnode = currnode->prev;
            }
        }
};

 

 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 14 images

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education