Question

C++ We are to implement 8 small additional functions and 2 helper functions to a singly...

C++ We are to implement 8 small additional functions and 2 helper functions to a singly linked list.

1. removeSecondNode340 deletes the second node in the Linked Bag.

2. addEnd340 inserts the new node at the end of the Linked Bag.

3. getCurrentSize340Iterative counts the number of nodes in the Linked Bag iteratively.

4. getCurrentSize340Recursive counts the number of nodes in the Linked Bag recursively. Use 1 helper function: getCurrentSize340RecursiveHelper.  

5. IMMEDIATE RECURSION: getCurrentSize340RecursiveNoHelper counts the number of nodes in the Linked Bag recursively. This recursive function does not use any helper functions.

6. getFrequencyOf340Recursive recursively counts the number of times an entry appears in the Linked Bag. Use 1 helper function: getFrequencyOf340RecursiveHelper.

7. IMMEDIATE RECURSION: getFrequencyOf340RecursiveNoHelper recursively counts the number of times an entry appears in the Linked Bag. This recursive function does not use any helper functions.  

8. removeRandom340 removes a random entry from the Linked Bag.

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

#include<bits/stdc++.h>

int rand();

using namespace std;

struct node{

int data;

struct node *next;

};

void removeSecondNode340(struct node *root)

{

if(root==NULL||root->next==NULL)

{cout<<"List contain less than two element\n";

return;

}

struct node *temp;

temp=root->next->next;

root->next->next=NULL;

root->next=temp;

}

void addEnd340(struct node **root)

{

int item;

cout<<"Enter item to add:";

cin>>item;

struct node *temp=(struct node *)malloc(sizeof(struct node));

temp->data=item;

temp->next=NULL;

if(*root==NULL)

{

*root=temp;

return;

}

struct node *curr=*root;

while(curr->next!=NULL)

curr=curr->next;

curr->next=temp;

}

int getCurrentSize340Iterative(struct node *root)

{

int c=0;

struct node *temp=root;

while(temp!=NULL)

{

c++;

temp=temp->next;

}

return c;

}

void print(struct node *root)

{

struct node *temp=root;

while(temp!=NULL)

{

cout<<temp->data<<" ";

temp=temp->next;

}

cout<<endl;

}

int getCurrentSize340RecursiveNoHelper(struct node *root)

{

if(root==NULL)

return 0;

return (1+getCurrentSize340RecursiveNoHelper(root->next));

}

int getFrequencyOf340RecursiveHelper(int c,int entry,int key)

{

if(key==entry)

c++;

return c;

}

int getFrequencyOf340Recursive(struct node *root,int entry)

{

static int c=0;

if(root==NULL)

return c;

c=getFrequencyOf340RecursiveHelper(c,entry,root->data);

return getFrequencyOf340Recursive(root->next,entry);

}

int getFrequencyOf340RecursiveNoHelper(struct node *root,int entry)

{

if(root==NULL)

return 0;

if(root->data==entry)

{

return (1+getFrequencyOf340RecursiveNoHelper(root->next,entry));

}

else

{

return getFrequencyOf340RecursiveNoHelper(root->next,entry);

}

}

int removeRandom340(struct node **root)

{

int count=getCurrentSize340Iterative(*root);

if(count==0)

return -1;

int index=rand()%count+1;

struct node *temp=*root,*temp2;

if(index==1)

{

*root=temp->next;

temp->next=NULL;

return temp->data;

}

int counter=1;

temp2=temp;

while(counter<index)

{

temp2=temp;

temp=temp->next;

counter++;

}

temp2->next=temp->next;

temp->next=NULL;

return temp->data;

}

int getCurrentSize340RecursiveHelper(int c)

{

c++;

return c;

}

int getCurrentSize340Recursive(struct node *root)

{

static int c=0;

if(root==NULL)

return c;

c=getCurrentSize340RecursiveHelper(c);

return getCurrentSize340Recursive(root->next);

}

int main()

{

struct node *root=NULL;

int choice;

int c;

int entry;

do

{

cout<<"Enter choice";

cin>>choice;

switch(choice)

{

case 1:removeSecondNode340(root);

break;

case 2:addEnd340(&root);

break;

case 3:c=getCurrentSize340Iterative(root);

cout<<"Count = "<<c<<endl;

break;

case 4:c=getCurrentSize340Recursive(root);

cout<<"Size of the list "<<c<<endl;

break;

case 5:c=getCurrentSize340RecursiveNoHelper(root);

cout<<"Size of the list "<<c<<endl;

break;

case 6:

cout<<"Enter entry to see frequency:";

cin>>entry;

c=getFrequencyOf340Recursive(root,entry);

cout<<"Frequency of "<<entry <<" in list is "<<c<<endl;

break;

case 7:

cout<<"Enter entry to see frequency:";

cin>>entry;

c=getFrequencyOf340RecursiveNoHelper(root,entry);

cout<<"Frequency of "<<entry <<" in list is "<<c<<endl;

break;

case 8:c=removeRandom340(&root);

if(c==-1)

cout<<"there are no element in the list\n";

else

cout<<c <<" is removed from the list\n";

break;

case 10:print(root);

break;

default:choice=0;

}

}while(!choice==0);

return 0;

}

Add a comment
Know the answer?
Add Answer to:
C++ We are to implement 8 small additional functions and 2 helper functions to a singly...
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
  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define...

    Need to implement this function, mostly confused withcopy constructor , operator overloading and deleteNode#ifndef TREE_H #define TREE_H #include <iostream> #include <cstdlib> // necessary in order to use NULL class TreeNode { public: TreeNode() : left(NULL), right(NULL) {}    TreeNode* left; TreeNode* right; int value; }; class Tree { public: // Default constructor Tree();    // Copy constructor Tree(const Tree& other);    //Destructor ~Tree();    // overloaded Assignment Operator Tree& operator=(const Tree& other);    // Similar to insert function we discussed...

  • 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...

  • Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes....

    Programing C Just with #include <stdio.h> We will create a singly linked list of 7 nodes. Then, the user will tell us whether to print the “odd-placed” nodes or the “even-placed” nodes. For example, if I had a list of 5 nodes like below, and the user specifies for the odd nodes to be printed, my program would print the values in nodes nl and n3. If the user instead indicated for the even nodes to be printed, my program...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

  • ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled...

    ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...

  • (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class...

    (C++) You are tasked with implementing a recursive function void distanceFrom(int key) on the IntList class (provided). The function will first search through the list for the provided key, and then, recursively, change all previous values in the list to instead be their distance from the node containing the key value. Do not update the node containing the key value or any nodes after it. If the key does not exist in the list, each node should contain its distance...

  • Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic...

    Part 1: Implement a singly linked list -------------------------------------- (a) Your job is to implement a generic singly linked list that can hold any data type. The interface has been specified and provided to you in a header file called mylist.h. So your job is to write mylist.c that implements each function whose prototype is included in mylist.h. Specifically, you are asked to write the following functions: struct Node *addFront(struct List *list, void *data) void traverseList(struct List *list, void (*f)(void *))...

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
Active Questions
ADVERTISEMENT