Question

Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
}

Add a comment
Know the answer?
Add Answer to:
Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node();...

    // Node.h #ifndef NODE_H #define NODE_H class Node { private: int m_entry; Node *m_next; public: Node(); Node(int entry); int getEntry() const; void setEntry(int entry); Node *getNext(); void setNext(Node *next); }; #endif // Node.cpp #include "Node.h" Node::Node() { m_entry = 0; m_next = nullptr; } Node::Node(int entry) { m_entry = entry; } int Node::getEntry() const { return m_entry; } void Node::setEntry(int entry) { m_entry = entry; } Node *Node::getNext() { return m_next; } void Node::setNext(Node *next) { m_next = next; }...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • C++ Assume that you have a structure Node as follows: struct Node { int value; Node*...

    C++ Assume that you have a structure Node as follows: struct Node { int value; Node* next; Node(int v, Node* n = nullptr) { // constructor value = v; next = n; } }; Question 1 (20 points) Write a function "count" that counts the number of elements in a given linked list. The prototype of this function is as follows: int count_elements(Node*); Question 2 (20 points) Write a function “average" that calculates the average of all the elements in...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect;...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • /* I'm trying to write this program here but my problem is getting the program to...

    /* I'm trying to write this program here but my problem is getting the program to save the user input to "int data" but cannot because it is part of the struct. What do I have to do in order to get this to work? (Language is C++) Write a program that prompts the user to enter numbers on the screen and keep adding those numbers to a linked list. The program stops when user enters -999. Hint: Use a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT