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 below, create recursive functions...
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...
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;...
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: 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). 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 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 <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 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()...