Consider a Linked List program with the following class: typedef int datatype; s
ID: 3815749 • Letter: C
Question
Consider a Linked List program with the following class:
typedef int datatype;
struct node
{
datatype data;
node *tail;
};
class LinkedList{
private:
node *head;
node *current;public: //constructors
LinkedList();
LinkedList(int i);
//destructor
~LinkedList();
bool start(); //sets list postion to header
bool nextNode(); //increments to next node in list
int getCurrent(); //returns data from current node
void insertNode(int i); //inserts node after current node
//then sets current node to new node
bool deleteNode();//deletes currentnode
void deleteAll(); //deletes all nodes
};
The main function has the following:
#include <string>
#include "LinkedList.h"
using namespace std;
.void test(LinkedList &ll);
void main()
{
LinkedList ll:
ll.insertNode(1);
ll.insertNode(4);
ll.insertNode(9);
ll.insertNode(2);
ll.insertNode(3);
ll.insertNode(22);
ll.insertNode(6);
ll.insertNode(7);
ll.insertNode(99);
ll.insertNode(8);
test(ll);
}
void test(LinkedList &ll)
{
//your code goes here.
}
Write a definition for the function "test" that will remove all the nodes with even linked list data and output the remaining list data.Make your function dynamic so that it can handle list other than the one presented in the problem. Output for this data list should be:
1
9
3
7
99
Note: minor syntax errors will not count against you.
Explanation / Answer
HI, I have implemented the required methods. This method will only print even data node.
Please let me know in case of any issue.
#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"
using namespace std;
void test(LinkedList &ll);
void main()
{
LinkedList ll:
ll.insertNode(1);
ll.insertNode(4);
ll.insertNode(9);
ll.insertNode(2);
ll.insertNode(3);
ll.insertNode(22);
ll.insertNode(6);
ll.insertNode(7);
ll.insertNode(99);
ll.insertNode(8);
test(ll);
}
void test(LinkedList &ll)
{
ll.start();// setting at start
while(ll.nextNode()){ // move till end
if(ll.getCurrent() % 2 == 1)
cout<<ll.getCurrent()<<" ";
}
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.