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:
"The first node contains <value in first node>"
"The last node contains <value in last node>"
"The first node contains <value in first 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!
I need help and have to get this done asap: Using the following source codes provided...
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: Print the list in forward order Print the list in reverse order 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>" Print the sum of all the values...
Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() function. 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 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...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
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 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;...
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...
I'm having trouble getting this program to compile and run. It is in C++ Language and being run with CodeBlocks. Problem Statement: create and manage a linked list. Program will loop displaying a menu of user operations concerning the management of a linked list. Included will be: H create link at head R remove link at head T create link at tail K remove link at tail I remove link at ID S search...
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...