SlideShare a Scribd company logo
#include <iostream>
#include "d_node.h"
#include "d_nodel.h"
#include "d_random.h"
using namespace std;
// return a pointer to the maximum element in the linked list
template <typename T>
node<T> *getMax(node<T> *front);
// if target is in the linked list, remove its first
// occurrence; otherwise, do not modify the list
template <typename T>
void eraseValue(node<T> * & front, const T& target);
// returns the sum of all elements in a single linked list
template <typename T>
int sum(node<T> *front);
//// outputs the nodes of a single linked list in reverse order.
template <typename T>
void outputReverse(node<T> *front);
int main()
{
node<int> *front = NULL, *p;
randomNumber rnd;
int listCount, i;
cout << "Enter the size of the list: ";
cin >> listCount;
for (i = 0; i < listCount; i++)
front = new node<int>(rnd.random(100), front);
cout << "Original List of Values: ";
writeLinkedList(front, " ");
cout << endl;
cout << "The Sum of all elements are: "; //
<=============sum sld go here
cout << sum();
cout << endl;
//cout << "reverse is: "; // <============reverse should
go here
outputReverse();
//cout << endl;
cout << "Output in Descending Order: ";
while (front != NULL)
{
p = getMax(front);
cout << p->nodeValue << " ";
eraseValue(front, p->nodeValue);
}
cout << endl;
system("pause");
return 0;
}
template <typename T>
node<T> *getMax(node<T> *front)
{
node<T> *curr = front, *maxPtr = front;
while (curr != NULL)
{
if (maxPtr->nodeValue < curr->nodeValue)
maxPtr = curr;
curr = curr->next;
}
return maxPtr;
}
template <typename T>
void eraseValue(node<T> * & front, const T& target)
{
// curr moves through list, trailed by prev
node<T> *curr = front, *prev = NULL;
// becomes true if we locate target
bool foundItem = false;
// scan until locate item or come to end of list
while (curr != NULL && !foundItem)
{
if (curr->nodeValue == target) // have a match
{
if (prev == NULL) // remove the first node
front = front->next;
else
prev->next = curr->next; // erase intermediate node
delete curr;
foundItem = true;
}
else
{
// advance curr and prev
prev = curr;
curr = curr->next;
}
}
}
template <typename T> //
<============================ need help calling to
main
int sum(node<T> *front)
{
int s = 0;
node<T> *i;
if (front == NULL)
return 0;
for (i = front; i != NULL; i = i->next)
{
s = s + i->data;
}
return s;
}
template <typename T> //
<=============================== Need help calling
to main
void outputReverse(node<T> *front)
{
int arr[20];
node<T> *temp;
temp = front;
int i = 0, j;
while (temp != NULL)
{
arr[i] = temp->data;
temp = temp->next;
i++;
}
for (j = i - 1; j >= 0; j--)
{
cout << arr[j] << "<-";
}
}
Solution
#include <iostream>
#include "d_node.h"
#include "d_nodel.h"
#include "d_random.h"
using namespace std;
// return a pointer to the maximum element in the linked list
template <typename T>
node<T> *getMax(node<T> *front);
// if target is in the linked list, remove its first
// occurrence; otherwise, do not modify the list
template <typename T>
void eraseValue(node<T> * & front, const T& target);
// returns the sum of all elements in a single linked list
template <typename T>
int sum(node<T> *front);
//// outputs the nodes of a single linked list in reverse order.
template <typename T>
void outputReverse(node<T> *front);
int main()
{
node<int> *front = NULL, *p;
randomNumber rnd;
int listCount, i;
cout << "Enter the size of the list: ";
cin >> listCount;
for (i = 0; i < listCount; i++)
front = new node<int>(rnd.random(100), front);
cout << "Original List of Values: ";
writeLinkedList(front, " ");
cout << endl;
cout << "The Sum of all elements are: "; //
<=============sum sld go here
cout << sum();
cout << endl;
//cout << "reverse is: "; // <============reverse should
go here
outputReverse();
//cout << endl;
cout << "Output in Descending Order: ";
while (front != NULL)
{
p = getMax(front);
cout << p->nodeValue << " ";
eraseValue(front, p->nodeValue);
}
cout << endl;
system("pause");
return 0;
}
template <typename T>
node<T> *getMax(node<T> *front)
{
node<T> *curr = front, *maxPtr = front;
while (curr != NULL)
{
if (maxPtr->nodeValue < curr->nodeValue)
maxPtr = curr;
curr = curr->next;
}
return maxPtr;
}
template <typename T>
void eraseValue(node<T> * & front, const T& target)
{
// curr moves through list, trailed by prev
node<T> *curr = front, *prev = NULL;
// becomes true if we locate target
bool foundItem = false;
// scan until locate item or come to end of list
while (curr != NULL && !foundItem)
{
if (curr->nodeValue == target) // have a match
{
if (prev == NULL) // remove the first node
front = front->next;
else
prev->next = curr->next; // erase intermediate node
delete curr;
foundItem = true;
}
else
{
// advance curr and prev
prev = curr;
curr = curr->next;
}
}
}
template <typename T> //
<============================ need help calling to
main
int sum(node<T> *front)
{
int s = 0;
node<T> *i;
if (front == NULL)
return 0;
for (i = front; i != NULL; i = i->next)
{
s = s + i->data;
}
return s;
}
template <typename T> //
<=============================== Need help calling
to main
void outputReverse(node<T> *front)
{
int arr[20];
node<T> *temp;
temp = front;
int i = 0, j;
while (temp != NULL)
{
arr[i] = temp->data;
temp = temp->next;
i++;
}
for (j = i - 1; j >= 0; j--)
{
cout << arr[j] << "<-";
}
}

More Related Content

PDF
In the class we extensively discussed a node class called IntNode in.pdf
PDF
Change the driver file (the main .cpp) so that it asks the user to e.pdf
PDF
C++Write a method Node Nodereverse() which reverses a list..pdf
PDF
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
PDF
#includeiostream #includecstdio #includecstdlib using na.pdf
DOCX
Linked lists
PDF
This will need to be in a header file called LinkedList.hInser.pdf
PDF
for initializer_list include ltinitializer_listgt .pdf
In the class we extensively discussed a node class called IntNode in.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
Linked lists
This will need to be in a header file called LinkedList.hInser.pdf
for initializer_list include ltinitializer_listgt .pdf

Similar to #include iostream#include d_node.h #include d_nodel.h.docx (20)

PDF
I need to implment a function that can reverse a single linked list..pdf
PDF
This assignment and the next (#5) involve design and development of a.pdf
DOCX
Week 2 - Advanced C++list1.txt312220131197.docx
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
DOCX
C++ Please write the whole code that is needed for this assignment- wr.docx
PDF
In C++ I need help with this method that Im trying to write fillLi.pdf
PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
PDF
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
PDF
Write a program in C that does the followinga) Builds a simple li.pdf
PDF
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
PDF
Node file code below#ifndef NODE_H#define NODE_H#include .pdf
PDF
Please help solve this in C++ So the program is working fin.pdf
DOCX
Implementation File- -------------------------------------------------.docx
PDF
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PDF
include ltfunctionalgt include ltiteratorgt inclu.pdf
PDF
you will implement some sorting algorithms for arrays and linked lis.pdf
PDF
Template LinkedList;I am using templates to make some linkedLists.pdf
PDF
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
PDF
include ltfunctionalgt include ltiteratorgt inclu.pdf
I need to implment a function that can reverse a single linked list..pdf
This assignment and the next (#5) involve design and development of a.pdf
Week 2 - Advanced C++list1.txt312220131197.docx
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ Please write the whole code that is needed for this assignment- wr.docx
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Write a program in C that does the followinga) Builds a simple li.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Node file code below#ifndef NODE_H#define NODE_H#include .pdf
Please help solve this in C++ So the program is working fin.pdf
Implementation File- -------------------------------------------------.docx
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
Template LinkedList;I am using templates to make some linkedLists.pdf
–PLS write program in c++Recursive Linked List OperationsWrite a.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf

More from ajoy21 (20)

DOCX
Please complete the assignment listed below.Define and explain, us.docx
DOCX
Please cite sources for each question. Do not use the same sources f.docx
DOCX
Please choose one of the following questions to answer for this week.docx
DOCX
Please check the attachment for my paper.Please add citations to a.docx
DOCX
Please answer to this discussion post. No less than 150 words. Refer.docx
DOCX
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
DOCX
Please answer the question .There is no work count. PLEASE NUMBER .docx
DOCX
Please answer the following questions. Please cite your references..docx
DOCX
Please answer the following questions.1.      1.  Are you or.docx
DOCX
Please answer the following question with 200-300 words.Q. Discu.docx
DOCX
Please answer the following question Why do you think the US ha.docx
DOCX
Please answer the following questions. Define tunneling in the V.docx
DOCX
Please answer the following questions1. How can you stimulate the.docx
DOCX
Please answer the following questions very deeply and presicely .docx
DOCX
Please answer the following questions in an informal 1 ½ - 2-page es.docx
DOCX
Please answer the following questions in a response of 150 to 200 wo.docx
DOCX
Please answer these questions regarding the (TILA) Truth in Lending .docx
DOCX
Please answer the following question pertaining to psychology. Inc.docx
DOCX
Please answer the following questions in a response of 250 to 300 .docx
DOCX
Please answer the three questions completly. I have attached the que.docx
Please complete the assignment listed below.Define and explain, us.docx
Please cite sources for each question. Do not use the same sources f.docx
Please choose one of the following questions to answer for this week.docx
Please check the attachment for my paper.Please add citations to a.docx
Please answer to this discussion post. No less than 150 words. Refer.docx
Please attach Non-nursing theorist summaries.JigsawExecutive .docx
Please answer the question .There is no work count. PLEASE NUMBER .docx
Please answer the following questions. Please cite your references..docx
Please answer the following questions.1.      1.  Are you or.docx
Please answer the following question with 200-300 words.Q. Discu.docx
Please answer the following question Why do you think the US ha.docx
Please answer the following questions. Define tunneling in the V.docx
Please answer the following questions1. How can you stimulate the.docx
Please answer the following questions very deeply and presicely .docx
Please answer the following questions in an informal 1 ½ - 2-page es.docx
Please answer the following questions in a response of 150 to 200 wo.docx
Please answer these questions regarding the (TILA) Truth in Lending .docx
Please answer the following question pertaining to psychology. Inc.docx
Please answer the following questions in a response of 250 to 300 .docx
Please answer the three questions completly. I have attached the que.docx

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
Classroom Observation Tools for Teachers
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Basic Mud Logging Guide for educational purpose
PDF
Complications of Minimal Access Surgery at WLH
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Computing-Curriculum for Schools in Ghana
Classroom Observation Tools for Teachers
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Basic Mud Logging Guide for educational purpose
Complications of Minimal Access Surgery at WLH
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Institutional Correction lecture only . . .
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Supply Chain Operations Speaking Notes -ICLT Program
TR - Agricultural Crops Production NC III.pdf
Sports Quiz easy sports quiz sports quiz
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Anesthesia in Laparoscopic Surgery in India
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...

#include iostream#include d_node.h #include d_nodel.h.docx

  • 1. #include <iostream> #include "d_node.h" #include "d_nodel.h" #include "d_random.h" using namespace std; // return a pointer to the maximum element in the linked list template <typename T> node<T> *getMax(node<T> *front); // if target is in the linked list, remove its first // occurrence; otherwise, do not modify the list template <typename T> void eraseValue(node<T> * & front, const T& target); // returns the sum of all elements in a single linked list template <typename T> int sum(node<T> *front); //// outputs the nodes of a single linked list in reverse order. template <typename T> void outputReverse(node<T> *front); int main() { node<int> *front = NULL, *p; randomNumber rnd; int listCount, i; cout << "Enter the size of the list: "; cin >> listCount; for (i = 0; i < listCount; i++) front = new node<int>(rnd.random(100), front); cout << "Original List of Values: "; writeLinkedList(front, " "); cout << endl; cout << "The Sum of all elements are: "; // <=============sum sld go here cout << sum(); cout << endl; //cout << "reverse is: "; // <============reverse should
  • 2. go here outputReverse(); //cout << endl; cout << "Output in Descending Order: "; while (front != NULL) { p = getMax(front); cout << p->nodeValue << " "; eraseValue(front, p->nodeValue); } cout << endl; system("pause"); return 0; } template <typename T> node<T> *getMax(node<T> *front) { node<T> *curr = front, *maxPtr = front; while (curr != NULL) { if (maxPtr->nodeValue < curr->nodeValue) maxPtr = curr; curr = curr->next; } return maxPtr; } template <typename T> void eraseValue(node<T> * & front, const T& target) { // curr moves through list, trailed by prev node<T> *curr = front, *prev = NULL; // becomes true if we locate target bool foundItem = false; // scan until locate item or come to end of list while (curr != NULL && !foundItem) {
  • 3. if (curr->nodeValue == target) // have a match { if (prev == NULL) // remove the first node front = front->next; else prev->next = curr->next; // erase intermediate node delete curr; foundItem = true; } else { // advance curr and prev prev = curr; curr = curr->next; } } } template <typename T> // <============================ need help calling to main int sum(node<T> *front) { int s = 0; node<T> *i; if (front == NULL) return 0; for (i = front; i != NULL; i = i->next) { s = s + i->data; } return s; } template <typename T> // <=============================== Need help calling to main void outputReverse(node<T> *front)
  • 4. { int arr[20]; node<T> *temp; temp = front; int i = 0, j; while (temp != NULL) { arr[i] = temp->data; temp = temp->next; i++; } for (j = i - 1; j >= 0; j--) { cout << arr[j] << "<-"; } } Solution #include <iostream> #include "d_node.h" #include "d_nodel.h" #include "d_random.h" using namespace std; // return a pointer to the maximum element in the linked list template <typename T> node<T> *getMax(node<T> *front);
  • 5. // if target is in the linked list, remove its first // occurrence; otherwise, do not modify the list template <typename T> void eraseValue(node<T> * & front, const T& target); // returns the sum of all elements in a single linked list template <typename T> int sum(node<T> *front); //// outputs the nodes of a single linked list in reverse order. template <typename T> void outputReverse(node<T> *front); int main() { node<int> *front = NULL, *p; randomNumber rnd; int listCount, i; cout << "Enter the size of the list: "; cin >> listCount; for (i = 0; i < listCount; i++) front = new node<int>(rnd.random(100), front); cout << "Original List of Values: "; writeLinkedList(front, " "); cout << endl; cout << "The Sum of all elements are: "; // <=============sum sld go here cout << sum();
  • 6. cout << endl; //cout << "reverse is: "; // <============reverse should go here outputReverse(); //cout << endl; cout << "Output in Descending Order: "; while (front != NULL) { p = getMax(front); cout << p->nodeValue << " "; eraseValue(front, p->nodeValue); } cout << endl; system("pause"); return 0; } template <typename T> node<T> *getMax(node<T> *front) { node<T> *curr = front, *maxPtr = front; while (curr != NULL) { if (maxPtr->nodeValue < curr->nodeValue) maxPtr = curr; curr = curr->next;
  • 7. } return maxPtr; } template <typename T> void eraseValue(node<T> * & front, const T& target) { // curr moves through list, trailed by prev node<T> *curr = front, *prev = NULL; // becomes true if we locate target bool foundItem = false; // scan until locate item or come to end of list while (curr != NULL && !foundItem) { if (curr->nodeValue == target) // have a match { if (prev == NULL) // remove the first node front = front->next; else prev->next = curr->next; // erase intermediate node delete curr; foundItem = true; } else { // advance curr and prev
  • 8. prev = curr; curr = curr->next; } } } template <typename T> // <============================ need help calling to main int sum(node<T> *front) { int s = 0; node<T> *i; if (front == NULL) return 0; for (i = front; i != NULL; i = i->next) { s = s + i->data; } return s; } template <typename T> // <=============================== Need help calling to main void outputReverse(node<T> *front) {
  • 9. int arr[20]; node<T> *temp; temp = front; int i = 0, j; while (temp != NULL) { arr[i] = temp->data; temp = temp->next; i++; } for (j = i - 1; j >= 0; j--) { cout << arr[j] << "<-"; } }