Question
In my C++ linked list header file, I am trying to create two functions. One function called divideMid , which will divide the middle of
In my C++ linked list header file, I am trying to create two functions. One function called divideMid, which will divide the middle of the list. And another function called divideAt, which will divide at a certain item within the list. Please use my header file code below and its same format to write the functions in C++:
struct node{ int data; node *prev; node *next; };
class linkType{ public: linkType(); void insertFront(int newData); void insertBack(int newData); void createList(int newData); void print(); void printBack(); void deleteFront(); void deleteBack(); private: node *first; node *last; };
linkType::linkType(){ first = NULL; last = NULL; }
void linkType::insertFront(int newData){ node *temp = new node; temp -> data = newData; temp -> next = first; temp -> prev = NULL; first = temp; }
void linkType::insertBack(int newData){ node *temp = new node; temp -> data = newData; if(last != NULL){ last -> next = temp; last = temp; } else{ first = last = temp; } }
void linkType::print(){ node *current; if(first != NULL){ current = first; while(current != NULL){ cout << current -> data << " "; current = current -> next; cout << endl; } } else{ cout << "List is empty! " << endl; } }
void linkType::printBack(){ node *current; if(first != NULL){ current = last; while(current != NULL){ cout << current -> data << " "; current = current -> prev; cout << endl; } } else{ cout << "List is empty! " << endl; } }
void linkType::deleteFront(){ node *current; if(first != NULL){ current = first; first = current -> next; delete current; } else{ cout <<"Can't delete from empty list!" << endl; } }
void linkType::deleteBack(){ node *trailCurrent; node *current; current = first; while(current -> next != NULL){ trailCurrent = current; current = current -> next; } trailCurrent -> next = NULL; delete current; }
void linkType::createList(int newData){ node *temp = new node; temp -> data = newData; if(first == NULL){ temp -> prev = NULL; first = temp; last = temp; } else{ temp -> prev = last; last -> next = temp; last = temp; } }
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