Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please i want the code to be written using C++ 7/Lab 4 tasks Homeworks cout Task4.1: swapFirstLast(); cout Task4.2: countEvenOddNodes(); } DER SER Welcome to

please i want the code to be written using C++
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
7/Lab 4 tasks Homeworks cout Task4.1: swapFirstLast()"; cout Task4.2: countEvenOddNodes()"; } DER SER Welcome to GOB Online. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, ************ // LL2.cpp: This file contains the main function. Program execution begins and ends there. Finclude data-data: next NULL) > //Getters int getData() return data) > Node. get.Next() return next 3 //Setters void setDatatint data) this->data-data: > void setentNode. next Mode) this next next Mode > CLARA Links elass Linkedtist { private Node. head; publier // Constructor Linkedlist) head - NULL: > 1. Below are MANY nethods that are used on Linked Lists../ ----- istnpty method ----- bool istmpty) return head = NULL; --- search method ----/ bool search int data) Node helpPtr - head; while (helpper - NULL) { 1 helper->getData() * data) return true > helppur - helpper->get.Next();// Step one node over > return false; 7 findiNode method / Node. findNodeint data) Node. helpptr - head; while cheippte - NULL) { 11 (helptr->getData) - data) return helppers > belpur - helpper->get.Next();// Step one node over return NULL) printAll Nodes method / void printAll Modes Nodehelpptr - head; while (helpPLE I NULL) cout getData() get.Next(); cout getData() > data) { DeNode->set.Next(head) head - newlode return head; // ELSE, we have a list. Insert the new node at the correct location // We need to traverse to the correct insertion location...so we need a help ptr Node helpPtr - head; while helpptc->getNext()!= NULL 46 helpPtr->getNext)->getData() get.Next() // Now make the new node. Set its next to point to the successor node. 11 And then make the predecessor node point to the new node newlode->set.Next(helpper->get.Next()); helpper-Set Next(nevilode): return heads 1/add all the taske methods here 1/Task 3.1 int findtargento Node helpper - head: Ant largest - head->getData(); while (helpptr - NULL) if (helper->getData) > largest largest - helpptr->getData(); > helppte helper->get.Next(); // Step one node over return largest 1/Task 3.2 int data2ndNode return head->get.Next()->getData(); > // Task 3.3 int data2ndLastNode() Node helppur head; while (helper->get.Next()->getNext() - NULL) helppte helpPtr->get.Next(); return helpper->getData(); > //Task 3.4 void delete astNode) Ifhead WULL) cout The list is empty": elset Node helptr - head, Node. Node - new Node() while helpper->get.Next()->get.Next() != NULL) helpptr - helpper-get.Next(); dNode - helpPtr->getNext(): helper.Next(NULL) fredede > 1/Task 3.5 void oddavenodd Mode helpper - head) while (helppur - NULL) 17(helper->getData(120) int nun - helper->getData()-1) helper->Dasanum) > elsel int num-helper->getData(); helpt Datanun) helpt - helpper->get.Next(); } > >://end Linkedlist class int main // Make a new Linked List called wyList LinkedList* myList - new LinkedList(); int value int nanode-01 cout How many values do you want to insert: cin >> nunode: //inset nodes for (int i -01 value > value; if (myList->search value)) cout * * The list is empty": > else coute" mylint->printAll Model cout Tank). 11 findtargete): ccnyl.ist->findtargest 18 (myList->search value)) 1 couto ocvaluece already exists in the list. Duplicates are not allowed." i- > else 1 myList-insert Node(value) cout * cout Printing All Nodes: if (myList->isEmpty) cout The list is empty": cout ayList->print All.Nodes(); cout cout findLargest(); cout data2ndNodes cout Task). 31 datandlastNode) "data2ndLastNode) cout Task3.4: deleteLastNode) mylist-deleteLastNode(); myList->printAll Node () cout Task3.51 oddEven2odd(): ELt-Poid2Eva20dd : myliat->printAll Nodest); 7/Lab 4 tasks Homeworks cout Task4.1: swappiratLast" cout Taskt.2t count EvenoddNodes)" > 1. Create a new project called Lab4_ Link List Project2. 2. Create a Node class & a LinkedList class, and program the tasks in the main method. 3. Taskl: Write and test a method named swapFirstLast() which swaps the data of first and last nodes in the linked list. Note: If list is empty or contains only one node, your code should display error message accordingly. Hint: Use following method header: void swapFirstLast() { } 4. Task2: Write and test a method named countEvenOddNodes() which counts and displays number of even nodes as well as number of an odd nodes present in the linked list. Note: If list is empty, your code should display error message accordingly. Hint: Use following method header: void count EvenoddNodes () { int evenNodes = 0; int oddNodes = 0; cout Task4: deleteNodeAfter () "; //you will complete code here 8. When you run this program, it will display the following screen: Sample run 1: If the list is empty. How many values do you want to insert: 0 Printing A11 Nodes: The list is empty countNodes() Error: cannot count nodes (the list is empty) printEvenkodes) Error: cannot print even nodes (the list is empty) Taski: swapFirstLaat() Error: cannot swap the list has less than two nodes) VVVV Task2: countEvenOddNodes() The list is empty Number of Even nodes in the list is o Number of Odd nodes in the 11st 13 0 AA Taxk3: addNodeAfter () Error: cannot add node after the list is empty) AA Task4: deleteNodeAfter() Error: cannot delete node after the list is empty) Samples run 2: * If the list has one element. 1) Printing all Nodes: 12, countNodes) Number of nodes in the linked list is 1 VY printEvenNodes) Printing Even Nodes. 12, V V Taski: swapFirstLast Error: cannot swap(the list has less than two nodes) AAA Task2: countEvenOddNodest) Number of Even nodes in the list is 1 Number of Odd nodes in the list is 0 VVV Task3: addNodeAftert) Please Enter the node data After which you want to add a new node: 30 Error: There is no node in the List with 30 AAA Task4: deleteNodeafter) Please Enter the node data After which you want to delete a node:30 Error: There is no node in the List with 30 How many values do you want to insert: 1 value 1: 12 12 was successfully inserted into the list. Printing all Nodes: 12, AA countNodes () Number of nodes in the linked list is 1 printEvenNodes) Printing Even Nodes. 12, Taski: swapFirstLast Error: Cannot swap(the list has less than two nodes) VVV Task2: countEvenoddNodest) Number of Even nodes in the list is 1 Number of Odd nodes in the list is o > Tasks: addNodeAfter) Please Enter the node data after which you want to add a new node: 12 New node have been added successfully! 12, 24, AAAA Task4: deleteNodeAfter() Please Enter the node data after which you want to delete a node: 12 Node have been deleted successfully! 12. How many values do you want to insert: 1 value 1: 12 12 was successfully inserted into the list. Printing All Nodes: 12. countNodest) Number of nodes in the linked list is 1 printEvenNodes) Printing Even Nodes. 12, Taski: swapFirstLast() Error: cannot swap(the list has less than two nodes) Tank2: countEvenOddNodes) Number of Even nodes in the list is 1 Number of Odd nodes in the list is o VVVV Task3: addNodeAfter() Please enter the node data after which you want to add a new node: 30 Error: There is no node in the List with 30 12, Task 4: deleteNodeAfter) Please Enter the node data after which you want to delete a node: 12 No node can be deleted after this node as it is the last node in the list! 12, How many values do you want to insert: 2 value 1: 10 10 was successfully inserted into the list. value 2: 7 7 was successfully inserted into the list. Printing A11 Nodes: 7, 10, countNodes) Number of nodes in the linked list is 2 printEvenNodes) Printing Even Nodes. 10. Taski: wapFirst Last First and last element had been swaped successfully! 10, 7, VY Task2: countEvenOddNodes () Number of Even nodes in the list is 1 Numbez of Odd nodes in the list is 1 VVVV Task3: adidNodeAfter) Please enter the node data after which you want to add a new node:7 New node have been added successfully! 10. 7. 14, AAAA Tasku: deleteNodeAtter) Please Enter the node data after which you want to delete a node:10 Node have been deleted successfully! 10, 14, How many values do you want to insert: E value 1: 12 12 was successfully inserted into the 1.ss. value 2: 17 17 was successfully inserted into the 113. value 3: 10 10 was ruccessfully inserted into the list. value 4: 50 50 was successfully inserted into the list. value 5: 10 10 already exists in the liss. Duplicates are not allowed. value 5: 20 20 was successfully inserted into the list. value 6: 30 30 was successfully inserted into the list. Printing All Nodes: 10, 12, 17, 20, 30, 50, countNodes) Number of nodes in the linked list 1:6 printEvenNodes) Printing Even Nodes. 10, 12, 20, 30, 50, Taski: swapFirstlast) First and last element had been waped successfully! 50, 12, 17, 20, 30, 10, Tank2: countEvenOddNoden) Number of Even nodes in the list is S Number of Odd nodes in the list is 1 Tasks: addNodeAfter (3) Please enter the node date after which you want to add new node:20 New node have been added successfully! So, 12, 17, 20, 40, 30, 10, Taak: deleteNodeAfter Please enter the node data after which you want to delete a node: 12 Bode have been deleted successfully! 50, 12, 20, 40, 30, 10

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_2

Step: 3

blur-text-image_3

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

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

What is a CDO squared? How about a CDO cubed? AppendixLO1

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago