Assignment is: \"Page 349-350 #4 and #5 Use the \"Linked List lab\" you have bee
ID: 3792119 • Letter: A
Question
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) 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 #4 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 <iostream> #include <cstdlib> 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."<<endl; } }
system("PAUSE"); return 0; }
void createList(nodeType*& first, nodeType*& last) { int number; nodeType *newNode;
first = NULL; last = NULL;
cout<<"Enter an integer (-999 to stop): "; cin>>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... "<<endl; nodeType *current; current = new nodeType; current = first; while (current != NULL) { cout << current->info << " "; current = current->link; } cout<<endl; } "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) 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 #4 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 <iostream> #include <cstdlib> 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."<<endl; } }
system("PAUSE"); return 0; }
void createList(nodeType*& first, nodeType*& last) { int number; nodeType *newNode;
first = NULL; last = NULL;
cout<<"Enter an integer (-999 to stop): "; cin>>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... "<<endl; nodeType *current; current = new nodeType; current = first; while (current != NULL) { cout << current->info << " "; current = current->link; } cout<<endl; }
unor or the (Use either the class your function.) class to t st Dividing a linked list into two sublists of almost equal sizes) a Add the operation divideMid to the class as fol- lows: void divideMid &sublist;); This operation divides the given list into two sublists //of (almost) equal sizes. points to first points to the first node and last the last sublist. sublist first points to the first node sublist.last points to the last node of the second sublist. Consider the following statements unorderedLinkedListkint> my List; unorderedLinkedListkint subList; Suppose myList points to the list with elements 34 65 27 89 12 lin this order). The statement: myList divide Mid (subList) divides myList into two sublists: myList points to the list with the elements 34 65 27, and ist points to the sublist with the elements 89 12. b. Write the definition of the function template to implement the o tion divideMid. Also write a program to test your function.
Explanation / Answer
//following code will help you to solve your problem
/*make sure that you incorporate this into your piece of code along with necessary change like adding the option for this function as well into your options list of tasks*/
template <class Type>
void linkedListType<Type>:divideMid(linkedListType<Type> &sublist)
{
int myListItems, subListItems;
if ((count%2)!=0) myListItems = (count/2 + 1);
else myListItems = (count/2);
subListItems = (count - myListItems);
nodeType<Type> *current;
current = first;
sublist.last = last;
for (int i=0; i<myListItems; i++)
{
last = current; //sets the last node of the first list
current = current -> link; //traverses the list until it gets to where it must divde.
}
last->link=NULL; //cuts off myList in the middle
sublist.first = current; //assigns the next node to sublist.first.
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.