Question
using c++ i have the .cpp file as single linked list and need it to implent each funciton as double linked list please and add
using c++ i have the .cpp file as single linked list and need it to implent each funciton as double linked list please and add main file for the program to see if it work please need help
#include"ListLinked.h" #include #include
using namespace std;
template List::ListNode::ListNode(const DataType& nodeData, ListNode* nextPtr) { dataItem= nodeData; next =nextPtr; } template List::List(int ignored) { cursor = NULL; head = NULL; } template List::List(const List& other) //copy constructor { ListNode *tmp = NULL;// to travel in other list ListNode *cursorPstn = NULL; // to travel in list cursor = NULL; head = NULL;
if (other.isEmpty()) {
} else { tmp = other.head; do { insert(tmp->dataItem); if (tmp == other.cursor) { cursorPstn = cursor; } tmp = tmp->next; } while (tmp != NULL); cursor = cursorPstn; }
return *this; } template List& List::operator= (const List& other) //overload { ListNode *tmp2 = NULL;// to travel in other list ListNode *cursorPstn = NULL; // to travel in list cursor = NULL; head = NULL;
if (other.isEmpty()) {
} else { tmp2 = other.head; do { insert(tmp2->dataItem); if (tmp2 == other.cursor) { cursorPstn = cursor; } tmp2 = tmp2->next; } while (tmp2 != NULL); cursor = cursorPstn; } }
template List::~List() //destructor ?? { while (head != NULL) { //newPtr = temp; ListNode *temp = head; head = head->next; //move head to next node temp->next = NULL;//this will point to zero //size--; delete temp; } }
template void List::insert(const DataType& newDataItem) throw (logic_error) { ListNode *N = NULL; if (isFull()) { return; } else { N = new ListNode(newDataItem, NULL); //insert new dataItem as first and move cursor to new data item if (isEmpty()) { head = N; cursor = N; } else { if (cursor->next != NULL) { N->next = cursor->next; } cursor->next = N; cursor = N; } } } template void List::remove() throw (logic_error) { ListNode *deleting_Cursor = cursor; if (isEmpty()) { return; } else
{ //if not empty move cursor to the data item follow delet item if ((cursor == head) && (cursor->next) == NULL) { cursor = NULL; head = NULL; } else if (cursor == head) { gotoNext(); head = cursor; } else { gotoPrior(); cursor->next = deleting_Cursor->next; if (deleting_Cursor->next != NULL) { gotoNext(); } else { gotoBeginning(); } } deleting_Cursor; } } template void List::replace(const DataType& newDataItem) throw (logic_error) { if (isEmpty()) { return; } else { cursor->dataItem = newDataItem; // replace data item with newDataItem } } template void List::clear() { while (head != NULL) { // newPtr = temp; ListNode *temp = head; head = head->next; //move head to next node temp->next = NULL;//this will point to zero delete temp; //size--; } } template bool List::isEmpty() const { if (head == NULL) { return true; } else { return false; } } template bool List::isFull() const { if (head != NULL) { return true; } else { return false; }
} template void List::gotoBeginning() throw (logic_error) { cursor->next = head; } template void List::gotoEnd() throw (logic_error) { while (cursor->next != NULL) { cursor = cursor->next; } } template bool List::gotoNext() throw (logic_error) { if (cursor != NULL){ if (cursor->next != NULL) { cursor = cursor->next; return true; } else { return false; } } else { return false; } } template bool List::gotoPrior() throw (logic_error) { ListNode *tmpCursor = head; // creat new temp if (isEmpty()) { if (cursor == head) { return false; } else { while (tmpCursor->next != cursor) { tmpCursor = tmpCursor->next; } cursor = tmpCursor; return true; }
} else { return false; } } template DataType List::getCursor() const throw (logic_error) { if (cursor != NULL) { return cursor->dataItem; } else { throw logic_error("data will not convert to int"); } } //-------------------------------------------------------------------- // show5.cpp: includes implementation of showStructure //--------------------------------------------------------------------
template void List::showStructure() const
// Outputs the items in a list. If the list is empty, outputs // "Empty list". This operation is intended for testing and // debugging purposes only.
{ if (isEmpty()) { cout << "Empty list" << endl; } else { for (ListNode* temp = head; temp != 0; temp = temp->next) { if (temp == cursor) { cout << "["; }
// Assumes that dataItem can be printed via << because // is is either primitive or operator<< is overloaded. cout << temp->dataItem;
if (temp == cursor) { cout << "]"; } cout << " "; } cout << endl; } } template void List::moveToBeginning() throw (logic_error) { ListNode *temp = NULL; //insert at the begining if (isEmpty()) { return; } else { if (cursor == head) { return; } else { temp = new ListNode(getCursor(), head); //remove cursor to beginging head = temp; remove(); cursor = head; } } } template void List::insertBefore(const DataType& newDataItem) throw (logic_error) { ListNode* newNode = NULL; if (isEmpty()) {
newNode = new ListNode(newDataItem, NULL); //insert new data item as first cursor = newNode; head = newNode; }
else {
newNode = new ListNode(getCursor(), NULL); //insert new data before cursor newNode->next = cursor->next; replace(newDataItem); cursor->next = newNode;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started