Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using namespace std;

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: "<data; front=front->next; front->prev=NULL; delete rmv; }

void printAll() {

if(front==NULL){ cout<<" List is empty:"; return; } cout<<" Values of doubly linked list:"; DNode *temp=front; while(temp!=NULL){ cout<data<<" "; temp=temp->next; } cout<<" "; } void deleteLast(){ DNode *temp=front; // Stores front address if(front==NULL){ cout<<" List is empty:"; } else if(front->next==NULL){ cout<<" Deleted node: "<data; delete temp; front=NULL; } else { while(temp->next!=NULL){ //Goes through loop temp=temp->next; } // Delink last node from second last node cout<<" Deleted data: "<data; temp->prev->next=NULL; temp->prev=NULL; delete temp; // Deleting last node } } void insertToMiddle(int position, int item){ if(front==NULL ){ cout<<" List is empty.You cant store data in middle."; return; } DNode *temp=front; DNode *newnode=new DNode(); newnode->data=item; for(int i=1;inext; } if(temp==NULL ){ cout<<" Not enough data in list:"; return; } newnode->prev=temp; newnode->next=temp->next; newnode->next->prev=newnode; temp->next=newnode; } void deleteMiddle(int position) {

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;inext; } DNode *rmv=temp->next; temp->next=temp->next->next; temp->next->prev=temp; delete rmv; } void InsertInOrder(DNode *head, DNode *tail, int key) { //This is where I need help to build InsertInOrder } } ;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions

Question

Write short notes on departmentation.

Answered: 1 week ago

Question

What are the factors affecting organisation structure?

Answered: 1 week ago

Question

What are the features of Management?

Answered: 1 week ago

Question

Briefly explain the advantages of 'Management by Objectives'

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago