Question

I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...

I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises:

  1. Print the list in forward order
  2. Print the list in reverse order
  3. Print the following three lines (in this order):

    "The first node contains <value in first node>"

    "The last node contains <value in last node>"

    "The first node contains <value in first node>"

  4. Print the sum of all the values in the list.
  5. Returns the sum of all the values in the list.
  6. Returns the sum of all the values in the list (again, but done differently), sum them up going backwards through the list.
  7. Returns the average of all the values in the list.
  8. Return the value in the middle node.
  9. Move the data from the first node to the last node.
  10. Move the data from the first node to the last node, and the data from the last node to the first node.
  11. Make the first node the last node, and the last node the first node (don’t just move the data, move the entire node).
  12. Remove every even node (consider the first node, the one pointed to by head, to be node 1, an odd node).

Code:

intList.h

#ifndef _INTLIST_H
#define _INTLIST_H
#include<iostream>
using namespace std;

class Node
{
public:
Node* next;
int data;
};

class intList
{
public:

Node* head;

intList();
~intList();
void insert(int);
bool remove(int);
int count();
int sum();
double average();
int find(int);
void removeAll();
void print();
};
#endif

IntList.cpp

#include"IntList.h"

intList::intList()
{
this->head = NULL;
}
void intList::insert(int data)
{
Node* node = new Node();
node->data = data;
node->next=NULL;
if(head==NULL)
{
head=node;
}
else
{
Node* tempHead = this->head;
while(tempHead->next!=NULL)
{
tempHead = tempHead->next;
}
tempHead->next=node;
}
}
void intList::print()
{
Node* head = this->head;
int i = 1;
if(head==NULL)
{
cout<<"List is empty";
return ;
}
while(head->next!=NULL){
cout<<head->data<<",";
head = head->next;
}
cout<<head->data<<endl;
}
int intList::count()
{
int count=0;
Node* tempHead = this->head;
while(tempHead)
{
count++;
tempHead = tempHead->next;
}
return count;
}
int intList::sum()
{
int sum=0;
Node* tempHead = this->head;
while(tempHead)
{
sum+=tempHead->data;
tempHead = tempHead->next;
}
return sum;
}
double intList::average()
{
double avg=sum()/(double)count();
return avg;
}
int intList :: find(int item)
{
int index=0;
Node* tempHead = this->head;
while(tempHead)
{
if(item==tempHead->data)
return index+1;
tempHead = tempHead->next;
index++;
}
return 0;
}
bool intList::remove(int item)
{
if(head->data==item)
{
head=head->next;
return true;
}
Node* prevNode = this->head;
Node* nextNode = this->head;
while(nextNode)
{
if(nextNode->data==item)
break;
prevNode=nextNode;
nextNode=nextNode->next;
}
prevNode->next=nextNode->next;
return true;
}
void intList::removeAll()
{
Node* nextNode = this->head;
while(nextNode)
{
remove(nextNode->data);
nextNode=nextNode->next;
}
}

IntListTest.cpp

#include"IntList.h"

int main()
{
intList* list = new intList();
list->insert(10);
list->insert(20);
list->insert(30);
list->insert(5);
cout<<"Count is : "<<list->count()<<endl;
cout<<"Elements in the list are: ";
list->print();

list->remove(5);
cout<<"\n\nAfter removing 5 from the list:"<<endl;
cout<<"Count is : "<<list->count()<<endl;
cout<<"Elements in the list are: ";
list->print();

list->remove(20);
list->remove(30);
cout<<"\n\nAfter removing 20 and 30 from the list:"<<endl;
cout<<"Count is : "<<list->count()<<endl;
cout<<"Elements in the list are: ";
list->print();

list->remove(10);
cout<<"\n\nAfter removing 10 from the list:"<<endl;
cout<<"Count is : "<<list->count()<<endl;
cout<<"Elements in the list are: ";
list->print();

cout<<endl;
return 1;
}

Please complete all, thank you!

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

Add allolowi nyour Intlist.p un cho intList .pit ThrteLines -) heo d while (temp-next 岰 tevP-.ne4 emp cs canne CamScannér CSSum Reverse ( Noce ohead e a urm Nods head this → while. (.head → next value head-, daka Camscannerhongetisst Las Node heads while (6etemp --)-next 一6.) |Beid Int List-Reverse List this →-head . No de 꾸 head od Curr -r nextthead ..ead → next- head → next→ next head -:- Read → next -te 名ead have beem made. cs CS Camscanner

Add a comment
Know the answer?
Add Answer to:
I need help and have to get this done asap: Using the following source codes provided below, create recursive functions...
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
  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • Hello, can someone help me solve the remove function since when I try to remove it...

    Hello, can someone help me solve the remove function since when I try to remove it doesn't work, please and thank you. #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public:    typedef int data_t;    node *next;    data_t data;    node(data_t d) { next = NULL; data = d; } }; class linked_list { private:    node *head; public:    linked_list()    {        head = NULL;    }    // Get the...

  • I have a problem with merging the two linked lists together. In the function AscendMerge, I...

    I have a problem with merging the two linked lists together. In the function AscendMerge, I am trying to compare the values of each node in the two lists and output them into one list in ascended order. Everything works except for the one function. Can someone tell me what im doing wrong, when I run it it skips over values. #include <iostream> #include <cassert> using namespace std; struct nodeType {    int info;    nodeType *link;    nodeType *current;...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

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

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

  • Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual...

    Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node {    int data;    node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() {    struct node*head=NULL;    char commands;    int value;   ...

  • Hello, I have this code but its not running like it should: #include <iostream> #include &l...

    Hello, I have this code but its not running like it should: #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public: typedef int data_t; node *next; data_t data; node(data_t d) { next = NULL; data = d; } }; class linked_list { private: node *head; public: linked_list() { head = NULL; } int size() { node *tptr = head; int c = 0; while (tptr) { c++; tptr = tptr->next; } return c; } void add(int...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...

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