Question: Write a program using inheritance in C++ to insert a node in the middle of Doubly linked list .( using this code) but no need

Write a program using inheritance in C++ to insert a node in the middle of Doubly linked list .( using this code) but no need for deletion just the insert in the middle using inheritance.

#ifndef DLLIST #define DLLIST #include using namespace std; template class DLLNode { public: T info; DLLNode *next, *prev; DLLNode(){ next = prev = 0; } DLLNode(T t, DLLNode *n = 0, DLLNode *p = 0) { info = t; next = n; prev = p; } }; template class DLL{ protected: DLLNode *head=0, *tail=0; public: void insertAtHead(T t) { head = new DLLNode(t, head); if (tail != 0)// if at least two nodes; //if(head->nexttail!= 0) head->next->prev = head; else tail = head; } T deleteFromTail() { //if (head == 0) // in list is empty //return 0; // in most cases makes no sense; T t = tail->info; // (*tail).info tail = tail->prev; if (tail != 0) { delete tail->next; tail->next = 0; } else { delete head; head = 0; } return t; } bool isEmpty() { return head == 0; } template friend ostream& operator<<(ostream& out, const DLL& dll); }; template ostream& operator<<(ostream& out, const DLL& dll) { for (DLLNode *p = dll.head; p!= 0; p = p->next) out << p ->info << ' '; out << endl; return out; }

#include "stdafx.h" #include "DLL.h" #include using namespace std; void testDLL() { DLL dll; dll.insertAtHead(10); dll.insertAtHead(11); dll.insertAtHead(12); cout << dll; cout << dll.deleteFromTail() << endl; cout << dll; cout << dll.deleteFromTail() << endl; cout << dll; cout << dll.deleteFromTail() << endl; cout << dll; cout << dll.deleteFromTail() << endl; } void testSTLlist() { list lst; lst.push_back(10); lst.push_back(20); lst.push_back(30); cout << lst << endl; } template ostream& operator<<(ostream& out, list& lst) { for (list::iterator i = lst.begin(); i != lst.end(); i++)// overload out << *i << ' '; out << endl; return out; } int main() { testDLL(); testSTLlist() return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!