SlideShare a Scribd company logo
Assignment is :
"Page 349-350 #4 and #5 Use the "Linked List lab" you have been working on in class and add
the two functions the questions are asking you to develop: divideMid and divideAt. Be sure to
include comments Use meaningful identifier names (constants where appropriate) Do not work
together; no two people should have identical work!?!? Turn in .cpp file AND Turn in a "print-
screen' of your output (press "print-screen' on keyboard, then 'paste' in MS-Word)"
How do you solve QUESTION #5 in the book data structures using c++ by D.S. Malik in Visiual
Studios using the linked list below with what is being asked? Please need help
Linked list :
#include
#include
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
void insertFront(nodeType*& first);
void insertBack(nodeType*& last);
void deleteFirst(nodeType*& first);
void deleteLast(nodeType*& last, nodeType* first);
int main()
{
nodeType *first, *last;
int num;
createList(first, last);
int choice;
while(true)
{
cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Exit.
";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: insertFront(first); break;
case 2: insertBack(last); break;
case 3: deleteFirst(first); break;
case 4: deleteLast(last, first); break;
case 5: printList(first); break;
case 6: return 0;
default: cout<<"Invalid menu option. Try again."<>number;
while (number != -999)
{
newNode = new nodeType; // create new node
newNode->info = number;
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
} // end of while-loop
} // end of build list function
void deleteFirst(nodeType*& first)
{
nodeType *temp;
temp= first;
first= temp->link;
delete temp;
return;
}
void deleteLast(nodeType*& last, nodeType* current)
{
nodeType *temp;
while(current->link != NULL)
{
temp=current;
current=current->link;
}
temp=last;
current->link=NULL;
delete temp;
last = current;
return;
}
void insertFront(nodeType*& front)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= front;
front= newNode;
return;
}
void insertBack(nodeType*& last)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= NULL;
last->link= newNode;
last = newNode;
return;
}
void printList(nodeType*& first)
{
cout<<"Inside printList...printing linked list... "<info << " ";
current = current->link;
}
cout<
#include
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
void insertFront(nodeType*& first);
void insertBack(nodeType*& last);
void deleteFirst(nodeType*& first);
void deleteLast(nodeType*& last, nodeType* first);
int main()
{
nodeType *first, *last;
int num;
createList(first, last);
int choice;
while(true)
{
cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Exit.
";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: insertFront(first); break;
case 2: insertBack(last); break;
case 3: deleteFirst(first); break;
case 4: deleteLast(last, first); break;
case 5: printList(first); break;
case 6: return 0;
default: cout<<"Invalid menu option. Try again."<>number;
while (number != -999)
{
newNode = new nodeType; // create new node
newNode->info = number;
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
} // end of while-loop
} // end of build list function
void deleteFirst(nodeType*& first)
{
nodeType *temp;
temp= first;
first= temp->link;
delete temp;
return;
}
void deleteLast(nodeType*& last, nodeType* current)
{
nodeType *temp;
while(current->link != NULL)
{
temp=current;
current=current->link;
}
temp=last;
current->link=NULL;
delete temp;
last = current;
return;
}
void insertFront(nodeType*& front)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= front;
front= newNode;
return;
}
void insertBack(nodeType*& last)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= NULL;
last->link= newNode;
last = newNode;
return;
}
void printList(nodeType*& first)
{
cout<<"Inside printList...printing linked list... "<info << " ";
current = current->link;
}
cout< otherList; Suppose myList points to the list with the elements: 34 65 18 39 27 89 12 (in
this order). The statement: myList divideAt (otherList, 18) divides myList into two sublists:
myList points to the list with the elements 34 65, and otherList points to the sublist with the
elements 39 27 89 12. 6. b. Write the definition of the function to implement the tion divide
template a. Add o write a program to test your function. the following operation to the class
dered inkedli st opera-
Solution
Explanation :-
----------------------------
1.Take
nodeType *first1, *last1;
First and last for the divided list
2. Add Choice "6" for Divide
3.When user enter choice
Ask index where to break the list
When user enters the index , then break the list there
3) After User enters the index of item where to break the list
First find the item at the index
4)Check the method
/**
* first---> start of main list
* last ----> last of main list
* first1---> start of new list after list
* last1 ----> last of new listafter list
* item ---> item at which we have to split
*/
void breakList(nodeType*& first, nodeType*& last,nodeType*& first1, nodeType*&
last1,nodeType*item);
5)To break the list at middle , just give the middle index
Programme :-
----------------------------
#include
#include
using namespace std;
struct nodeType
{
int info;
nodeType *link;
};
void createList(nodeType*& first, nodeType*& last);
void printList(nodeType*& first);
void insertFront(nodeType*& first);
void insertBack(nodeType*& last);
void deleteFirst(nodeType*& first);
void deleteLast(nodeType*& last, nodeType* first);
//method to split the list
/**
* first---> start of main list
* last ----> last of main list
* first1---> start of new list after list
* last1 ----> last of new listafter list
* item ---> item at which we have to split
*/
void breakList(nodeType*& first, nodeType*& last,nodeType*& first1, nodeType*&
last1,nodeType*item);
int main()
{
nodeType *first, *last;
//First,Last for 2nd list after divide
nodeType *first1, *last1;
int num;
createList(first, last);
int choice;
while(true)
{
cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Divide
7. Exit. ";
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: insertFront(first); break;
case 2: insertBack(last); break;
case 3: deleteFirst(first); break;
case 4: deleteLast(last, first); break;
case 5: printList(first); break;
//case 6 , Divide
case 6:{
int index;
//Ask user item at which we need to divide
cout<<"Enter your index: ";
cin>>index;
//Go to item at given index , and then split
int i = 0;
nodeType *current = NULL;
current = first;
while (current != NULL)
{
current = current->link;
i++;
if(i == index){
break;
}
}
breakList(first,last,first1,last1,current);
}
break;
case 7: return 0;
default: cout<<"Invalid menu option. Try again."<>number;
while (number != -999)
{
newNode = new nodeType; // create new node
newNode->info = number;
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
cout<<"Enter an integer (-999 to stop): ";
cin>>number;
} // end of while-loop
} // end of build list function
void deleteFirst(nodeType*& first)
{
nodeType *temp;
temp= first;
first= temp->link;
delete temp;
return;
}
void deleteLast(nodeType*& last, nodeType* current)
{
nodeType *temp;
while(current->link != NULL)
{
temp=current;
current=current->link;
}
temp=last;
current->link=NULL;
delete temp;
last = current;
return;
}
void insertFront(nodeType*& front)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= front;
front= newNode;
return;
}
void insertBack(nodeType*& last)
{
int num;
cout<<" Enter the number to insert: ";
cin>>num;
nodeType *newNode = new nodeType;
newNode->info=num;
newNode->link= NULL;
last->link= newNode;
last = newNode;
return;
}
void printList(nodeType*& first)
{
cout<<"Inside printList...printing linked list... "<info << " ";
current = current->link;
}
cout<link == item){
found = true;
break;
}
current = current->link;
}
//If item found
if(found){
//set first of new list
first1 = current->link;
//set last of new list
last1= last;
//set last of new list
last = current;
//close old list after current item
last->link = NULL;
}
}

More Related Content

Similar to Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf (20)

PDF
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
PDF
maincpp Build and procees a sorted linked list of Patie.pdf
adityastores21
 
PDF
Please need help on following program using c++ language. Please inc.pdf
nitinarora01
 
PDF
TutorialII_Updated____niceupdateprogram.pdf
BappyAgarwal
 
PDF
C++ Background Circular Linked List A circular linked list.pdf
saradashata
 
PDF
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
stopgolook
 
PDF
Background Circular Linked List A circular linked list is .pdf
aaseletronics2013
 
PDF
C++ Please test your program before you submit the answer.pdf
aashisha5
 
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
PDF
Implement the unsorted single linked list as we did in the class and .pdf
arihantstoneart
 
PDF
Write a program to implement below operations with both singly and d.pdf
thangarajarivukadal
 
PPTX
Link List Programming Linked List in Cpp
Anil Yadav
 
PDF
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
DOCX
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
PDF
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
PDF
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
PDF
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
DOCX
Write the definition of the linkedListKeepLast function- (Please write.docx
delicecogupdyke
 
PPTX
Programming Assignment Help
Programming Homework Help
 
PDF
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
maincpp Build and procees a sorted linked list of Patie.pdf
adityastores21
 
Please need help on following program using c++ language. Please inc.pdf
nitinarora01
 
TutorialII_Updated____niceupdateprogram.pdf
BappyAgarwal
 
C++ Background Circular Linked List A circular linked list.pdf
saradashata
 
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
stopgolook
 
Background Circular Linked List A circular linked list is .pdf
aaseletronics2013
 
C++ Please test your program before you submit the answer.pdf
aashisha5
 
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
Implement the unsorted single linked list as we did in the class and .pdf
arihantstoneart
 
Write a program to implement below operations with both singly and d.pdf
thangarajarivukadal
 
Link List Programming Linked List in Cpp
Anil Yadav
 
File LinkedList.java Defines a doubly-l.pdf
Conint29
 
C++ please put everthing after you answer it- thanks Complete the stub.docx
MatthPYNashd
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
Write the definition of the linkedListKeepLast function- (Please write.docx
delicecogupdyke
 
Programming Assignment Help
Programming Homework Help
 
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 

More from formicreation (20)

PDF
Many people believe that, because the emergence of life was very impr.pdf
formicreation
 
PDF
Know the different types of viruses which have a dsRNA genome A type .pdf
formicreation
 
PDF
Issued a $4000 Note Receivable for 3 months 10 interest. Calculate .pdf
formicreation
 
PDF
IPv4 provided the primary addressing scheme in TCPIP. However after.pdf
formicreation
 
PDF
Implement the member function void insertAfterHead(int d) of the Lin.pdf
formicreation
 
PDF
I am trying to create a program that will create Objects of differen.pdf
formicreation
 
PDF
How does Ansoffs matrix impact organizations missionsSoluti.pdf
formicreation
 
PDF
How is a chorioallantoic placenta different from a choriovitelline.pdf
formicreation
 
PDF
How can I tell when it is going to be a Archimedean Spiral. What is .pdf
formicreation
 
PDF
flow chart or step by step process of the key steps in forming a uni.pdf
formicreation
 
PDF
Description For this part of the assignment, you will create a Grid .pdf
formicreation
 
PDF
Discuss one (1) traditional and two (2) new strategies that special i.pdf
formicreation
 
PDF
Define health systems and healthcare delivery systems and discuss th.pdf
formicreation
 
PDF
B. You do an experiment in which you divide 8 cell stage urch.pdf
formicreation
 
PDF
a. project aid. b. emergency aid. c. development aid. d. bilateral ai.pdf
formicreation
 
PDF
An electron is moving in a circular motion clockwise. A uniform magne.pdf
formicreation
 
PDF
After reading Thomas Malthus population theory and the ideas of Ju.pdf
formicreation
 
PDF
Any ideas on an IT topic I could use for this case studyFor this .pdf
formicreation
 
PDF
C++ Write a function that takes a number as a parameter and display.pdf
formicreation
 
PDF
Why is it important for marketers to understand the product life cyc.pdf
formicreation
 
Many people believe that, because the emergence of life was very impr.pdf
formicreation
 
Know the different types of viruses which have a dsRNA genome A type .pdf
formicreation
 
Issued a $4000 Note Receivable for 3 months 10 interest. Calculate .pdf
formicreation
 
IPv4 provided the primary addressing scheme in TCPIP. However after.pdf
formicreation
 
Implement the member function void insertAfterHead(int d) of the Lin.pdf
formicreation
 
I am trying to create a program that will create Objects of differen.pdf
formicreation
 
How does Ansoffs matrix impact organizations missionsSoluti.pdf
formicreation
 
How is a chorioallantoic placenta different from a choriovitelline.pdf
formicreation
 
How can I tell when it is going to be a Archimedean Spiral. What is .pdf
formicreation
 
flow chart or step by step process of the key steps in forming a uni.pdf
formicreation
 
Description For this part of the assignment, you will create a Grid .pdf
formicreation
 
Discuss one (1) traditional and two (2) new strategies that special i.pdf
formicreation
 
Define health systems and healthcare delivery systems and discuss th.pdf
formicreation
 
B. You do an experiment in which you divide 8 cell stage urch.pdf
formicreation
 
a. project aid. b. emergency aid. c. development aid. d. bilateral ai.pdf
formicreation
 
An electron is moving in a circular motion clockwise. A uniform magne.pdf
formicreation
 
After reading Thomas Malthus population theory and the ideas of Ju.pdf
formicreation
 
Any ideas on an IT topic I could use for this case studyFor this .pdf
formicreation
 
C++ Write a function that takes a number as a parameter and display.pdf
formicreation
 
Why is it important for marketers to understand the product life cyc.pdf
formicreation
 
Ad

Recently uploaded (20)

PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
Difference between write and update in odoo 18
Celine George
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Workbook de Inglés Completo - English Path.pdf
shityouenglishpath
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Difference between write and update in odoo 18
Celine George
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Ad

Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf

  • 1. Assignment is : "Page 349-350 #4 and #5 Use the "Linked List lab" you have been working on in class and add the two functions the questions are asking you to develop: divideMid and divideAt. Be sure to include comments Use meaningful identifier names (constants where appropriate) Do not work together; no two people should have identical work!?!? Turn in .cpp file AND Turn in a "print- screen' of your output (press "print-screen' on keyboard, then 'paste' in MS-Word)" How do you solve QUESTION #5 in the book data structures using c++ by D.S. Malik in Visiual Studios using the linked list below with what is being asked? Please need help Linked list : #include #include using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first);
  • 2. void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType *first, *last; int num; createList(first, last); int choice; while(true) { cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Exit. "; cout<<"Enter your choice: "; cin>>choice; switch(choice) { case 1: insertFront(first); break; case 2: insertBack(last); break; case 3: deleteFirst(first); break; case 4: deleteLast(last, first); break; case 5: printList(first); break; case 6: return 0; default: cout<<"Invalid menu option. Try again."<>number; while (number != -999) { newNode = new nodeType; // create new node newNode->info = number;
  • 3. newNode->link = NULL; if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } cout<<"Enter an integer (-999 to stop): "; cin>>number; } // end of while-loop } // end of build list function void deleteFirst(nodeType*& first) { nodeType *temp;
  • 4. temp= first; first= temp->link; delete temp; return; } void deleteLast(nodeType*& last, nodeType* current) { nodeType *temp; while(current->link != NULL) { temp=current; current=current->link; } temp=last; current->link=NULL; delete temp; last = current; return; } void insertFront(nodeType*& front) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= front; front= newNode; return; }
  • 5. void insertBack(nodeType*& last) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= NULL; last->link= newNode; last = newNode; return; } void printList(nodeType*& first) { cout<<"Inside printList...printing linked list... "<info << " "; current = current->link; } cout< #include using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first);
  • 6. void deleteLast(nodeType*& last, nodeType* first); int main() { nodeType *first, *last; int num; createList(first, last); int choice; while(true) { cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Exit. "; cout<<"Enter your choice: "; cin>>choice; switch(choice) { case 1: insertFront(first); break; case 2: insertBack(last); break; case 3: deleteFirst(first); break; case 4: deleteLast(last, first); break; case 5: printList(first); break; case 6: return 0; default: cout<<"Invalid menu option. Try again."<>number; while (number != -999) { newNode = new nodeType; // create new node newNode->info = number; newNode->link = NULL;
  • 7. if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } cout<<"Enter an integer (-999 to stop): "; cin>>number; } // end of while-loop } // end of build list function void deleteFirst(nodeType*& first) { nodeType *temp; temp= first; first= temp->link;
  • 8. delete temp; return; } void deleteLast(nodeType*& last, nodeType* current) { nodeType *temp; while(current->link != NULL) { temp=current; current=current->link; } temp=last; current->link=NULL; delete temp; last = current; return; } void insertFront(nodeType*& front) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= front; front= newNode; return; } void insertBack(nodeType*& last) {
  • 9. int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= NULL; last->link= newNode; last = newNode; return; } void printList(nodeType*& first) { cout<<"Inside printList...printing linked list... "<info << " "; current = current->link; } cout< otherList; Suppose myList points to the list with the elements: 34 65 18 39 27 89 12 (in this order). The statement: myList divideAt (otherList, 18) divides myList into two sublists: myList points to the list with the elements 34 65, and otherList points to the sublist with the elements 39 27 89 12. 6. b. Write the definition of the function to implement the tion divide template a. Add o write a program to test your function. the following operation to the class dered inkedli st opera- Solution Explanation :- ---------------------------- 1.Take nodeType *first1, *last1; First and last for the divided list 2. Add Choice "6" for Divide 3.When user enter choice Ask index where to break the list
  • 10. When user enters the index , then break the list there 3) After User enters the index of item where to break the list First find the item at the index 4)Check the method /** * first---> start of main list * last ----> last of main list * first1---> start of new list after list * last1 ----> last of new listafter list * item ---> item at which we have to split */ void breakList(nodeType*& first, nodeType*& last,nodeType*& first1, nodeType*& last1,nodeType*item); 5)To break the list at middle , just give the middle index Programme :- ---------------------------- #include #include using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void insertFront(nodeType*& first); void insertBack(nodeType*& last); void deleteFirst(nodeType*& first); void deleteLast(nodeType*& last, nodeType* first); //method to split the list /** * first---> start of main list * last ----> last of main list * first1---> start of new list after list * last1 ----> last of new listafter list
  • 11. * item ---> item at which we have to split */ void breakList(nodeType*& first, nodeType*& last,nodeType*& first1, nodeType*& last1,nodeType*item); int main() { nodeType *first, *last; //First,Last for 2nd list after divide nodeType *first1, *last1; int num; createList(first, last); int choice; while(true) { cout<<"1. Insert Front. 2. Insert Last. 3. Delete Front. 4. Delete Last. 5. Print List. 6. Divide 7. Exit. "; cout<<"Enter your choice: "; cin>>choice; switch(choice) { case 1: insertFront(first); break; case 2: insertBack(last); break; case 3: deleteFirst(first); break; case 4: deleteLast(last, first); break; case 5: printList(first); break; //case 6 , Divide case 6:{ int index; //Ask user item at which we need to divide cout<<"Enter your index: "; cin>>index; //Go to item at given index , and then split int i = 0; nodeType *current = NULL; current = first; while (current != NULL)
  • 12. { current = current->link; i++; if(i == index){ break; } } breakList(first,last,first1,last1,current); } break; case 7: return 0; default: cout<<"Invalid menu option. Try again."<>number; while (number != -999) { newNode = new nodeType; // create new node newNode->info = number; newNode->link = NULL; if (first == NULL) { first = newNode; last = newNode; } else { last->link = newNode; last = newNode; } cout<<"Enter an integer (-999 to stop): "; cin>>number; } // end of while-loop } // end of build list function void deleteFirst(nodeType*& first) { nodeType *temp; temp= first; first= temp->link;
  • 13. delete temp; return; } void deleteLast(nodeType*& last, nodeType* current) { nodeType *temp; while(current->link != NULL) { temp=current; current=current->link; } temp=last; current->link=NULL; delete temp; last = current; return; } void insertFront(nodeType*& front) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= front; front= newNode; return; } void insertBack(nodeType*& last) { int num; cout<<" Enter the number to insert: "; cin>>num; nodeType *newNode = new nodeType; newNode->info=num; newNode->link= NULL;
  • 14. last->link= newNode; last = newNode; return; } void printList(nodeType*& first) { cout<<"Inside printList...printing linked list... "<info << " "; current = current->link; } cout<link == item){ found = true; break; } current = current->link; } //If item found if(found){ //set first of new list first1 = current->link; //set last of new list last1= last; //set last of new list last = current; //close old list after current item last->link = NULL; } }