SlideShare a Scribd company logo
C++ Please write the whole code that is needed for this assignment, write the completed code
together with the provided one, don't copy anyone else's code, label in what file each code
belongs, and please don't forget to share your code output after finishing writing the code, if
these requirements aren't met, I will nor upvote the answer, everything must be clear and
complete, everything is given below. Thank you.
ListNode.h
#include<memory>
#include <string>
template <class T>
//template <class T>
class ListNode{
public: T data;
public: ListNode * next;
// post: constructs a node with data 0 and null link
public: ListNode();
public: ListNode(T data);
public: ListNode(T idata, ListNode<T> * newNext);
public:std::string toString();
public: ListNode<T> * getNext();
public: void setNext(ListNode<T> * newNext);
public: T getData();
public: void setData(T newData);
};
ListNodecpp.h
#include "ListNode.h"
template <class T>
// post: constructs a node with data 0 and null link
//GIVEN
ListNode<T>:: ListNode() {
//std::cout<<" IN constructor"<<std::endl;
T t ;
next=nullptr;
}
//GIVEN
template <class T>
ListNode<T>:: ListNode(T idata) {
data=idata;
next=nullptr;
}
//TODO
template <class T>
ListNode<T>:: ListNode(T idata, ListNode<T>* inext) {
//TODO - assign data and next pointer
}
//GIVEN
template <class T>
std::string ListNode<T>::toString(){
return data.toString();
}
//GIVEN
template <class T>
ListNode<T> * ListNode<T>::getNext(){
return next;
}
//TODO
template <class T>
void ListNode<T>::setNext(ListNode<T> * newNext){
//TODO set the next pointer to incoming value
}
//GIVEN
template <class T>
T ListNode<T>::getData(){
return data;
}
//TODO
template <class T>
void ListNode<T>::setData(T newData){
//TODO set the data to incoming value
}
main.cpp
#include <iostream>
#include "ListNodecpp.h"
//Requires: integer value for searching, address of front
//Effects: traverses the list node chain starting from front until the end comparing search value
with listnode getData. Returns the original search value if found, if not adds +1 to indicate not
found
//Modifies: Nothing
int search(ListNode<int> * front, int value);
//Requires: integer value for inserting, address of front
//Effects: creates a new ListNode with value and inserts in proper position (increasing order)in
the chain. If chain is empty, adds to the beginning
//Modifies: front, if node is added at the beginning.
//Also changes the next pointer of the previous node to point to the newly inserted list node. the
next pointer of the newly inserted pointer points to what was the next of the previous node. This
way both previous and current links are adjusted
//******** NOTE the use of & in passing pointer to front as parameter - Why do you think this
is needed ?**********
void insert(ListNode<int> * &front,int value);
//Requires: integer value for adding, address of front
//Effects:creates a new ListNode with value and inserts at the beginning
//Modifies:front, if node is added at the beginning.
void addNode(ListNode<int> * &front,int value);
//Requires: integer value for removing, address of front
//Effects: removes a node, if list is empty or node not found, removes nothing.
//Modifies: If the first node is removed, front is adjusted.
// if removal is at the end or middle, makes sure all nececssary links are updated.
void remove(ListNode<int>* & front, int value);
//Requires: address of front
//Effects: prints data of each node, by traversing the chain starting from the front using next
pointers.
//Modifies: nothing
void printList(ListNode<int> * front);
//GIVEN
int main() {
// Add 3 Nodes to the chain of ListNodes
//note AddNode method appends to the end so this will be out of order
// the order of the nodes is 1,2 , 4
//Create a daisy chain aka Linked List
//
ListNode<int> * front = nullptr;
printList(front);
std::cout<<"**********************n";
addNode(front,1);
printList(front);
std::cout<<"**********************n";
addNode(front,2);
printList(front);
std::cout<<"**********************n";
addNode(front,4);
printList(front);
std::cout<<"**********************n";
// the insert method inserts node with value 3 in place
insert(front,3);
printList(front);
std::cout<<"**********************n";
// remove the first, so front needs to point to second
remove(front, 1);
printList(front);
std::cout<<"**********************n";
// insert it back
insert(front,1);
printList(front);
std::cout<<"**********************n";
//remove from the middle
remove(front, 3);
printList(front);
std::cout<<"**********************n";
// remove at the end
remove(front, 4);
printList(front);
std::cout<<"**********************n";
//remove a non existent node
remove(front, 5);
printList(front);
std::cout<<"**********************n";
// remove all nodes one by one leaving only front pointing to null pointer
remove(front, 2);
printList(front);
std::cout<<"**********************n";
remove(front, 1);
printList(front);
std::cout<<"**********************n";
// insert at the beginning of the empty list a larger value
insert(front, 4);
printList(front);
std::cout<<"**********************n";
// insert the smaller value at correct position in the front of the chain and change front
insert(front, 1);
printList(front);
std::cout<<"**********************n";
}
//GIVEN
void printList(ListNode<int> * front){
ListNode<int> * current = front;
while (current!=nullptr){
std::cout<<current->getData()<<"n";
current=current->getNext();
}
if (front ==nullptr)
std::cout<<"EMPTY LIST n";
}
//GIVEN
int search(ListNode<int> * front,int value){
ListNode<int> * current = front;
while (current!=nullptr&& current->getData()!=value){
// std::cout<<current->getData()<<"n";
current=current->getNext();
}
if (current!= nullptr) return current->getData();
return value+1 ; // to indicate not found. The calling program checks if return value is the same
as search value to know if its found; I was using *-1 but if search value is 0, then that does not
work;
}
//GIVEN
void addNode(ListNode<int> * & front ,int value){
ListNode<int> * current = front;
ListNode<int> * temp = new ListNode<int>(value);
if (front ==nullptr)
front=temp;
else {
while (current->getNext()!=nullptr){
// std::cout<<current->getData()<<"n";
current=current->getNext();
}
//ListNode<int> * temp = new ListNode<int>(value);
current->setNext(temp);
}
}
//TODO
void remove(ListNode<int> * &front,int value){
//TODO
}
//TODO
void insert(ListNode<int> * &front,int value){
//TODO
}
output.txt
EMPTY LIST
**********************
1
**********************
1
2
**********************
1
2
4
**********************
1
2
3
4
**********************
2
3
4
**********************
1
2
3
4
**********************
1
2
4
**********************
1
2
**********************
1
2
**********************
1
**********************
EMPTY LIST
**********************
4
**********************
1
4
**********************
This assignment and the next (#5) involve design and development of a sequential non
contiguous and dynamic datastructure called LinkedList. A linked list object is a container
consisting of connected ListNode objects. As before, we are not going to use pre-fabricated
classes from the c++ library, but construct the LinkedList ADT from scratch. The first step is
construction and testing of the ListNode class. A ListNode object contains a data field and a
pointer to the next ListNode object (note the recursive definition). #This assignment requires
you to 1. Read the Assignment 4 Notes 2. Watch the Assignment 4 Support video 3. Implement
the following methods of the ListNode class -custom constructor -setters for next pointer and
data 4. Implement the insert and remove method in the main program 5. Scan the given template
to find the above //TODO and implement the code needed //TODO in ListNodecpp. h file public:
ListNode(T idata, ListNode < T > newNext); public: void setNext(ListNode < T > newNext);
public: void setData(T newData); # The driver is given ListNodeMain.cpp is given to you that
does the following tests 1. Declares a pointer called front to point to a ListNode of datatype
integer 2. Constructs four ListNodes with data 1,2,4 and adds them to form a linkeo list. 3.
Inserts ListNode with data 3 to the list 4. Removes node 1 and adds it back to test removing and
adding the first element 5. Removes node 3 to test removing a middle node 6. Removes node 4 to
test removing the last node 7. Attempt to remove a non existent node 8. Remove all existing
nodes to empty the list 9. Insert node 4 and then node 1 to test if insertions preserve order 10.
Print the list

More Related Content

PDF
This assignment and the next (#5) involve design and development of a.pdf
PDF
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
PDF
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
PDF
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
PDF
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
PDF
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
PDF
How do you stop infinite loop Because I believe that it is making a.pdf
PDF
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf
This assignment and the next (#5) involve design and development of a.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
Need done for Date Structures please! 4-18 LAB- Sorted number list imp.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
In C++ please, do not alter node.hStep 1 Inspect the Node.h file.pdf

Similar to C++ Please write the whole code that is needed for this assignment- wr.docx (20)

PDF
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
PPTX
C Exam Help
PDF
Please help solve this in C++ So the program is working fin.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PPTX
C Homework Help
PDF
In the class we extensively discussed a node class called IntNode in.pdf
PDF
Write a program to implement below operations with both singly and d.pdf
PPTX
DSA(1).pptx
PPT
computer notes - Data Structures - 3
DOCX
C++ please put everthing after you answer it- thanks Complete the stub.docx
PDF
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
PDF
Write an algorithm that reads a list of integers from the keyboard, .pdf
PDF
How to do insertion sort on a singly linked list with no header usin.pdf
PDF
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
DOCX
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
PDF
could you implement this function please, im having issues with it..pdf
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
PDF
Write a program that accepts an arithmetic expression of unsigned in.pdf
PDF
I need help completing this C++ code with these requirements.instr.pdf
PDF
Sorted number list implementation with linked listsStep 1 Inspec.pdf
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
C Exam Help
Please help solve this in C++ So the program is working fin.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
C Homework Help
In the class we extensively discussed a node class called IntNode in.pdf
Write a program to implement below operations with both singly and d.pdf
DSA(1).pptx
computer notes - Data Structures - 3
C++ please put everthing after you answer it- thanks Complete the stub.docx
C++ CodeConsider the LinkedList class and the Node class that we s.pdf
Write an algorithm that reads a list of integers from the keyboard, .pdf
How to do insertion sort on a singly linked list with no header usin.pdf
Need Help!! C++ #include-iostream- #include-linkedlist-h- using namesp.pdf
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
could you implement this function please, im having issues with it..pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
I need help completing this C++ code with these requirements.instr.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
Ad

More from BrianGHiNewmanv (20)

DOCX
Cai Corporation uses a job-order costing system and has provided the f.docx
DOCX
Cabana Cruise Line uses a combination of debt and equity to fund opera.docx
DOCX
C-{2-8-10}.docx
DOCX
C++ Programming! Make sure to divide the program into 3 files the head.docx
DOCX
C++ 10-25 LAB- Artwork label (classes-constructors) Given main()- comp.docx
DOCX
By researching online find information about 2 malware samples that we.docx
DOCX
By which of the following processes are bacteria known to produce-acqu.docx
DOCX
BONUS PROBLEM ( 3 points) Ed owns 1-200 shares of ABC Corp- The compan.docx
DOCX
Business ethics- 12- Introduce the abuse of official position concept.docx
DOCX
Building a strong and ethical IT policy requires the cooperation of al.docx
DOCX
Building a Project WBS and Schedule in MS Project Define Toy Requireme.docx
DOCX
Briefly compare-contrast right cerebral hemisphere versus left cerebra.docx
DOCX
Briefly compare-contrast resting potential versus action potential- In.docx
DOCX
Brite Toothbrushes has gathered the following information to complete.docx
DOCX
Bridgeport Corporation engaged in the following cash transactions duri.docx
DOCX
BONUS- If you removed calcium (Ca2+) from a tissue's extracellular env.docx
DOCX
BONUS- If you rmoved calcium (Ca2+) from a tissue's extracellular envi.docx
DOCX
BONUS- If you removed calcium (Ca2+) from a tissue's extracellular env (1).docx
DOCX
Bob paid $100 for a utility bill- Which of the following accounts will.docx
DOCX
Blood Circulation Across 1- supply blood to the upper limbs Down 3- ca.docx
Cai Corporation uses a job-order costing system and has provided the f.docx
Cabana Cruise Line uses a combination of debt and equity to fund opera.docx
C-{2-8-10}.docx
C++ Programming! Make sure to divide the program into 3 files the head.docx
C++ 10-25 LAB- Artwork label (classes-constructors) Given main()- comp.docx
By researching online find information about 2 malware samples that we.docx
By which of the following processes are bacteria known to produce-acqu.docx
BONUS PROBLEM ( 3 points) Ed owns 1-200 shares of ABC Corp- The compan.docx
Business ethics- 12- Introduce the abuse of official position concept.docx
Building a strong and ethical IT policy requires the cooperation of al.docx
Building a Project WBS and Schedule in MS Project Define Toy Requireme.docx
Briefly compare-contrast right cerebral hemisphere versus left cerebra.docx
Briefly compare-contrast resting potential versus action potential- In.docx
Brite Toothbrushes has gathered the following information to complete.docx
Bridgeport Corporation engaged in the following cash transactions duri.docx
BONUS- If you removed calcium (Ca2+) from a tissue's extracellular env.docx
BONUS- If you rmoved calcium (Ca2+) from a tissue's extracellular envi.docx
BONUS- If you removed calcium (Ca2+) from a tissue's extracellular env (1).docx
Bob paid $100 for a utility bill- Which of the following accounts will.docx
Blood Circulation Across 1- supply blood to the upper limbs Down 3- ca.docx
Ad

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
GDM (1) (1).pptx small presentation for students
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PDF
Classroom Observation Tools for Teachers
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
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 Đ...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Pre independence Education in Inndia.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPH.pptx obstetrics and gynecology in nursing
GDM (1) (1).pptx small presentation for students
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Classroom Observation Tools for Teachers
Sports Quiz easy sports quiz sports quiz
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O5-L3 Freight Transport Ops (International) V1.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 Đ...
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Insiders guide to clinical Medicine.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pre independence Education in Inndia.pdf

C++ Please write the whole code that is needed for this assignment- wr.docx

  • 1. C++ Please write the whole code that is needed for this assignment, write the completed code together with the provided one, don't copy anyone else's code, label in what file each code belongs, and please don't forget to share your code output after finishing writing the code, if these requirements aren't met, I will nor upvote the answer, everything must be clear and complete, everything is given below. Thank you. ListNode.h #include<memory> #include <string> template <class T> //template <class T> class ListNode{ public: T data; public: ListNode * next; // post: constructs a node with data 0 and null link public: ListNode(); public: ListNode(T data); public: ListNode(T idata, ListNode<T> * newNext); public:std::string toString(); public: ListNode<T> * getNext(); public: void setNext(ListNode<T> * newNext); public: T getData(); public: void setData(T newData); }; ListNodecpp.h #include "ListNode.h" template <class T> // post: constructs a node with data 0 and null link //GIVEN ListNode<T>:: ListNode() { //std::cout<<" IN constructor"<<std::endl; T t ; next=nullptr; } //GIVEN template <class T> ListNode<T>:: ListNode(T idata) { data=idata; next=nullptr; }
  • 2. //TODO template <class T> ListNode<T>:: ListNode(T idata, ListNode<T>* inext) { //TODO - assign data and next pointer } //GIVEN template <class T> std::string ListNode<T>::toString(){ return data.toString(); } //GIVEN template <class T> ListNode<T> * ListNode<T>::getNext(){ return next; } //TODO template <class T> void ListNode<T>::setNext(ListNode<T> * newNext){ //TODO set the next pointer to incoming value } //GIVEN template <class T> T ListNode<T>::getData(){ return data; } //TODO template <class T> void ListNode<T>::setData(T newData){ //TODO set the data to incoming value } main.cpp #include <iostream> #include "ListNodecpp.h" //Requires: integer value for searching, address of front //Effects: traverses the list node chain starting from front until the end comparing search value with listnode getData. Returns the original search value if found, if not adds +1 to indicate not found //Modifies: Nothing int search(ListNode<int> * front, int value); //Requires: integer value for inserting, address of front //Effects: creates a new ListNode with value and inserts in proper position (increasing order)in the chain. If chain is empty, adds to the beginning //Modifies: front, if node is added at the beginning.
  • 3. //Also changes the next pointer of the previous node to point to the newly inserted list node. the next pointer of the newly inserted pointer points to what was the next of the previous node. This way both previous and current links are adjusted //******** NOTE the use of & in passing pointer to front as parameter - Why do you think this is needed ?********** void insert(ListNode<int> * &front,int value); //Requires: integer value for adding, address of front //Effects:creates a new ListNode with value and inserts at the beginning //Modifies:front, if node is added at the beginning. void addNode(ListNode<int> * &front,int value); //Requires: integer value for removing, address of front //Effects: removes a node, if list is empty or node not found, removes nothing. //Modifies: If the first node is removed, front is adjusted. // if removal is at the end or middle, makes sure all nececssary links are updated. void remove(ListNode<int>* & front, int value); //Requires: address of front //Effects: prints data of each node, by traversing the chain starting from the front using next pointers. //Modifies: nothing void printList(ListNode<int> * front); //GIVEN int main() { // Add 3 Nodes to the chain of ListNodes //note AddNode method appends to the end so this will be out of order // the order of the nodes is 1,2 , 4 //Create a daisy chain aka Linked List // ListNode<int> * front = nullptr; printList(front); std::cout<<"**********************n"; addNode(front,1); printList(front); std::cout<<"**********************n"; addNode(front,2); printList(front); std::cout<<"**********************n"; addNode(front,4); printList(front); std::cout<<"**********************n";
  • 4. // the insert method inserts node with value 3 in place insert(front,3); printList(front); std::cout<<"**********************n"; // remove the first, so front needs to point to second remove(front, 1); printList(front); std::cout<<"**********************n"; // insert it back insert(front,1); printList(front); std::cout<<"**********************n"; //remove from the middle remove(front, 3); printList(front); std::cout<<"**********************n"; // remove at the end remove(front, 4); printList(front); std::cout<<"**********************n"; //remove a non existent node remove(front, 5); printList(front); std::cout<<"**********************n"; // remove all nodes one by one leaving only front pointing to null pointer remove(front, 2); printList(front); std::cout<<"**********************n"; remove(front, 1); printList(front); std::cout<<"**********************n"; // insert at the beginning of the empty list a larger value insert(front, 4); printList(front); std::cout<<"**********************n"; // insert the smaller value at correct position in the front of the chain and change front insert(front, 1); printList(front); std::cout<<"**********************n"; } //GIVEN void printList(ListNode<int> * front){ ListNode<int> * current = front;
  • 5. while (current!=nullptr){ std::cout<<current->getData()<<"n"; current=current->getNext(); } if (front ==nullptr) std::cout<<"EMPTY LIST n"; } //GIVEN int search(ListNode<int> * front,int value){ ListNode<int> * current = front; while (current!=nullptr&& current->getData()!=value){ // std::cout<<current->getData()<<"n"; current=current->getNext(); } if (current!= nullptr) return current->getData(); return value+1 ; // to indicate not found. The calling program checks if return value is the same as search value to know if its found; I was using *-1 but if search value is 0, then that does not work; } //GIVEN void addNode(ListNode<int> * & front ,int value){ ListNode<int> * current = front; ListNode<int> * temp = new ListNode<int>(value); if (front ==nullptr) front=temp; else { while (current->getNext()!=nullptr){ // std::cout<<current->getData()<<"n"; current=current->getNext(); } //ListNode<int> * temp = new ListNode<int>(value); current->setNext(temp); } } //TODO void remove(ListNode<int> * &front,int value){ //TODO } //TODO void insert(ListNode<int> * &front,int value){
  • 7. ********************** 1 4 ********************** This assignment and the next (#5) involve design and development of a sequential non contiguous and dynamic datastructure called LinkedList. A linked list object is a container consisting of connected ListNode objects. As before, we are not going to use pre-fabricated classes from the c++ library, but construct the LinkedList ADT from scratch. The first step is construction and testing of the ListNode class. A ListNode object contains a data field and a pointer to the next ListNode object (note the recursive definition). #This assignment requires you to 1. Read the Assignment 4 Notes 2. Watch the Assignment 4 Support video 3. Implement the following methods of the ListNode class -custom constructor -setters for next pointer and data 4. Implement the insert and remove method in the main program 5. Scan the given template to find the above //TODO and implement the code needed //TODO in ListNodecpp. h file public: ListNode(T idata, ListNode < T > newNext); public: void setNext(ListNode < T > newNext); public: void setData(T newData); # The driver is given ListNodeMain.cpp is given to you that does the following tests 1. Declares a pointer called front to point to a ListNode of datatype integer 2. Constructs four ListNodes with data 1,2,4 and adds them to form a linkeo list. 3. Inserts ListNode with data 3 to the list 4. Removes node 1 and adds it back to test removing and adding the first element 5. Removes node 3 to test removing a middle node 6. Removes node 4 to test removing the last node 7. Attempt to remove a non existent node 8. Remove all existing nodes to empty the list 9. Insert node 4 and then node 1 to test if insertions preserve order 10. Print the list