Question
C++ j. InsertInOrder function: insert a new node so the list reads in ascending order from the beginning to the end. *I need help on
C++
j. InsertInOrder function: insert a new node so the list reads in ascending order from the beginning to the end.
*I need help on building an InsertInOrder function and choice seven in int main that calls the InsertInOrder function and demonstrates it with user input.*
Here is the code I have so far
#include
class DNode { //Public Class public: int data; //Stores item DNode *next; // next DNode *prev; // previous
};
void print(DNode *head, DNode *tail){ //function }
class DLL{ //private members private: DNode *front; DNode *back; public: //Constructor DLL(){ front = NULL; }
//Destructor ~DLL() { delete front; delete back; }
int length() { if(front==NULL){ cout<<" List is empty:"; return 0; }
// Stores address of first node DNode *temp=front; int count=0;
while(temp!=NULL){ // Goes through loop temp=temp->next; count++; // counts elements in list } cout<<" "; return count; } void insertToFront(int item) { //creates object of DNode Class DNode *newnode=new DNode(); if(front==NULL){ newnode->data=item; newnode->prev=NULL; newnode->next=NULL; front=newnode; }
else{ newnode->data=item; newnode->next=front; newnode->prev=NULL; front->prev=newnode; front=newnode; } }
void deleteFromFront(){
if(front==NULL){ cout<<" List is empty: "; return ; } DNode *rmv=front; cout<<" Deleted data: "<
void printAll() {
if(front==NULL){ cout<<" List is empty:"; return; } cout<<" Values of doubly linked list:"; DNode *temp=front; while(temp!=NULL){ cout<
if(front==NULL ){ cout<<" List is empty."; return; } DNode *temp=front; if(temp->next==NULL){ // Backup: If list contains one element only delete temp; front=NULL; return; } // Case: If list contains more than one element for(int i=1;i
int main(){ DNode *head = NULL; DNode *tail = NULL; int choice, item,pos; DLL obj=DLL(); while(1){ cout<<" Enter 1 to insert to front side:"; cout<<" Enter 2 to delete from front side:"; cout<<" Enter 3 to delete last node:"; cout<<" Enter 4 to print all data:"; cout<<" Enter 5 to insert in middle:"; cout<<" Enter 6 to delete from middle:"; cout<<" Enter 7 to insert data in order:"; cout<<" Enter 8 to count the number of nodes:"; cout<<" Enter 9 to exit:"; cout<<" Enter your choice: "; cin>>choice;//user choice entered
if(choice==1){ cout<<" Enter any integer value:";
cin>>item;
obj.insertToFront(item);
}
else if(choice==2){
obj.deleteFromFront();
}
else if(choice==3){
obj.deleteLast();
}
else if(choice==4){
obj.printAll();
}
else if(choice==5){
cout<<" Enter the data:";
cin>>item;
cout<<" Enter the position at which data will store:";
cin>>pos;
obj.insertToMiddle(pos,item);
}
else if(choice==6){
cout<<" Enter the position of data to be deleted:";
cin>>pos;
obj.deleteMiddle(pos);
}
else if(choice==7){
//This is where I need help. It demonstrates the InsertInOrder function. Also, it will be similar to other choices
}
else if(choice==8){
cout<<" Number of elements in the list: "< } else if(choice==9){ exit(0); } else cout<<" Wrong choice!!"; } return 1; }
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