Consider a Linked List program with the following class: typedef int datatype; s
ID: 3815978 • 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
Explanation / Answer
HI, Friend Please find my implementation.
You have not posted complete program so I can not test.
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()<<" ";
else
ll.deleteNode(); // deleting even data node
}
cout<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.