SlideShare a Scribd company logo
1 of 15
Download to read offline
Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them,
reverses them, and prints them again. DO NOT USE THE revprint2 function that is in the code!
LinkListImp.cpp
//----- List.cpp -----
#include
using namespace std;
#include "linkedlist.h"
//-- Definition of the class constructor
List::List()
: first(0), mySize(0)
{ }
//-- Definition of the copy constructor
List::List(const List & origList)
{
mySize = origList.mySize;
first = 0;
if (mySize == 0) return;
List::NodePointer origPtr, lastPtr;
first = new Node(origList.first->data); // copy first node
lastPtr = first;
origPtr = origList.first->next;
while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}
//-- Definition of the destructor
inline List::~List()
{
List::NodePointer prev = first,
ptr;
while (prev != 0)
{
ptr = prev->next;
delete prev;
prev = ptr;
}
}
// Definition of empty()
bool List::empty()
{
return mySize == 0;
}
//-- Definition of the assignment operator
const List & List::operator=(const List & rightSide)
{
mySize = rightSide.mySize;
first = 0;
if (mySize == 0) return *this;
if (this != &rightSide)
{
this->~List();
List::NodePointer origPtr, lastPtr;
first = new Node(rightSide.first->data); // copy first node
lastPtr = first;
origPtr = rightSide.first->next;
while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}
return *this;
}
//-- Definition of insert()
void List::insert(ElementType dataVal, int index)
{
if (index < 0 || index > mySize)
{
cerr << "Illegal location to insert -- " << index << endl;
return;
}
mySize++;
List::NodePointer newPtr = new Node(dataVal),
predPtr = first;
if (index == 0)
{
newPtr->next = first;
first = newPtr;
}
else
{
for (int i = 1; i < index; i++)
predPtr = predPtr->next;
newPtr->next = predPtr->next;
predPtr->next = newPtr;
}
}
//-- Definition of erase()
void List::erase(int index)
{
if (index < 0 || index >= mySize)
{
cerr << "Illegal location to delete -- " << index << endl;
return;
}
mySize--;
List::NodePointer ptr,
predPtr = first;
if (index == 0)
{
ptr = first;
first = ptr->next;
delete ptr;
}
else
{
for (int i = 1; i < index; i++)
predPtr = predPtr->next;
ptr = predPtr->next;
predPtr->next = ptr->next;
delete ptr;
}
}
//-- Definition of search()
int List::search(ElementType dataVal)
{
int loc;
List::NodePointer tempP = first;
for (loc = 0; loc < mySize; loc++)
if (tempP->data == dataVal)
return loc;
else
tempP = tempP->next;
return -1;
}
//-- Definition of display()
void List::display(ostream & out) const
{
NodePointer ptr = first;
while (ptr != 0)
{
out << ptr->data << " ";
ptr = ptr->next;
}
}
void List::revPrint(ostream & out)
{
reverse();
display(out);
reverse();
}
void List::revPrint2(ostream &out, NodePointer ptr)
{
out << "Inside revPrint2 ";
static List::NodePointer point = first;
out << point->data << endl;
if (point != NULL)
{
out << "Inside if statement ";
revPrint2(out, point->next);
out << point->data;
}
}
//-- Definition of the output operator
ostream & operator<<(ostream & out, const List & aList)
{
aList.display(out);
return out;
}
//-- Definition of nodeCount()
int List::nodeCount()
{ // or simply, { return mySize; }
int count = 0;
List::NodePointer ptr = first;
while (ptr != 0)
{
count++;
ptr = ptr->next;
}
return count;
}
//-- Definition of reverse()
void List::reverse()
{
}
//-- Definition of ascendingOrder()
bool List::ascendingOrder()
{
if (mySize <= 1)
//empty or one element list
return true;
//else
NodePointer prevP = first,
tempP = first->next;
while (tempP != 0 && prevP->data <= tempP->data)
{
prevP = tempP;
tempP = tempP->next;
}
if (tempP != 0)
return false;
// else
return true;
}
linkedlist.cpp
#include
using namespace std;
#include "linkedlist.h"
int main()
{
List mylist;
for (int i = 0; i < 6; i++)
mylist.insert(i * 2, i);
mylist.display(cout);
cout << endl;
mylist.revPrint(cout);
cout << endl;
cout << mylist;
}
linkedlist.h
//----- List.h -----
#ifndef LINKEDLIST
#define LINKEDLIST
#include
using namespace std;
typedef int ElementType;
class List
{
private:
class Node
{
public:
ElementType data;
Node * next;
Node()
: next(0)
{ }
Node(ElementType dataValue)
: data(dataValue), next(0)
{ }
}; //--- end of Node class
typedef Node * NodePointer;
public:
//------ List OPERATIONS
List();
List(const List & origList);
~List();
const List & operator=(const List & rightSide);
bool List::empty();
void insert(ElementType dataVal, int index);
void erase(int index);
int search(ElementType dataVal);
void display(ostream & out) const;
void revPrint(ostream & out);
void revPrint2(ostream &out, NodePointer ptr = NULL);
int nodeCount();
void reverse();
bool ascendingOrder();
private:
//------ DATA MEMBERS
NodePointer first ;
int mySize;
}; //--- end of List class
ostream & operator<<(ostream & out, const List & aList);
//istream & operator>>(istream & in, List & aList);
#endif
Solution
////----- List.cpp -----
//added reverse functionality
#include
using namespace std;
#include "linkedlist.h"
//-- Definition of the class constructor
List::List()
: first(0), mySize(0)
{ }
//-- Definition of the copy constructor
List::List(const List & origList)
{
mySize = origList.mySize;
first = 0;
if (mySize == 0) return;
List::NodePointer origPtr, lastPtr;
first = new Node(origList.first->data); // copy first node
lastPtr = first;
origPtr = origList.first->next;
while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}
//-- Definition of the destructor
inline List::~List()
{
List::NodePointer prev = first,
ptr;
while (prev != 0)
{
ptr = prev->next;
delete prev;
prev = ptr;
}
}
// Definition of empty()
bool List::empty()
{
return mySize == 0;
}
//-- Definition of the assignment operator
const List & List::operator=(const List & rightSide)
{
mySize = rightSide.mySize;
first = 0;
if (mySize == 0) return *this;
if (this != &rightSide)
{
this->~List();
List::NodePointer origPtr, lastPtr;
first = new Node(rightSide.first->data); // copy first node
lastPtr = first;
origPtr = rightSide.first->next;
while (origPtr != 0)
{
lastPtr->next = new Node(origPtr->data);
origPtr = origPtr->next;
lastPtr = lastPtr->next;
}
}
return *this;
}
//-- Definition of insert()
void List::insert(ElementType dataVal, int index)
{
if (index < 0 || index > mySize)
{
cerr << "Illegal location to insert -- " << index << endl;
return;
}
mySize++;
List::NodePointer newPtr = new Node(dataVal),
predPtr = first;
if (index == 0)
{
newPtr->next = first;
first = newPtr;
}
else
{
for (int i = 1; i < index; i++)
predPtr = predPtr->next;
newPtr->next = predPtr->next;
predPtr->next = newPtr;
}
}
//-- Definition of erase()
void List::erase(int index)
{
if (index < 0 || index >= mySize)
{
cerr << "Illegal location to delete -- " << index << endl;
return;
}
mySize--;
List::NodePointer ptr,
predPtr = first;
if (index == 0)
{
ptr = first;
first = ptr->next;
delete ptr;
}
else
{
for (int i = 1; i < index; i++)
predPtr = predPtr->next;
ptr = predPtr->next;
predPtr->next = ptr->next;
delete ptr;
}
}
//-- Definition of search()
int List::search(ElementType dataVal)
{
int loc;
List::NodePointer tempP = first;
for (loc = 0; loc < mySize; loc++)
if (tempP->data == dataVal)
return loc;
else
tempP = tempP->next;
return -1;
}
//-- Definition of display()
void List::display(ostream & out) const
{
NodePointer ptr = first;
while (ptr != 0)
{
out << ptr->data << " ";
ptr = ptr->next;
}
}
void List::revPrint(ostream & out)
{
reverse();
display(out);
reverse();
}
void List::revPrint2(ostream &out, NodePointer ptr)
{
out << "Inside revPrint2 ";
static List::NodePointer point = first;
out << point->data << endl;
if (point != NULL)
{
out << "Inside if statement ";
revPrint2(out, point->next);
out << point->data;
}
}
//-- Definition of the output operator
ostream & operator<<(ostream & out, const List & aList)
{
aList.display(out);
return out;
}
//-- Definition of nodeCount()
int List::nodeCount()
{ // or simply, { return mySize; }
int count = 0;
List::NodePointer ptr = first;
while (ptr != 0)
{
count++;
ptr = ptr->next;
}
return count;
}
//-- Definition of reverse()
//added by chegg EA
void List::reverse()
{
static List::NodePointer current,next,prev;
prev = NULL;
current = first;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
first= prev;
}
//-- Definition of ascendingOrder()
bool List::ascendingOrder()
{
if (mySize <= 1)
//empty or one element list
return true;
//else
NodePointer prevP = first,
tempP = first->next;
while (tempP != 0 && prevP->data <= tempP->data)
{
prevP = tempP;
tempP = tempP->next;
}
if (tempP != 0)
return false;
// else
return true;
}
------------------------------------------------------------------------
// List.h
this file unchanged
---------------------------------------------------------------
//main.cpp
#include
using namespace std;
#include "linkedlist.h"
int main()
{
List mylist;
int num;
for (int i = 0; i < 6; i++)
{
//mylist.insert(i * 2, i);
//added by chegg EA,ask for user input
cin >> num;
mylist.insert(num, i);
}
cout << "mylist: ";
mylist.display(cout);
cout << endl;
//mylist.revPrint(cout);
//added by chegg EA,reverse the list
mylist.reverse();
//added by chegg EA, print the reversed list
cout << endl;
cout << "After reversing the list: "<

More Related Content

Similar to Change the driver file (the main .cpp) so that it asks the user to e.pdf

C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
callawaycorb73779
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
angelsfashion1
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
anwarsadath111
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
sales98
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
LeonardN9WWelchw
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
naslin841216
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
I have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdfI have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdf
jibinsh
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
forladies
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
Malikireddy Bramhananda Reddy
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
FOREVERPRODUCTCHD
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdf
maheshkumar12354
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
vishalateen
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
fantoosh1
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
feelinggift
 

Similar to Change the driver file (the main .cpp) so that it asks the user to e.pdf (20)

C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docxGIVEN CODE template -typename T- class DList { private- struct Node {.docx
GIVEN CODE template -typename T- class DList { private- struct Node {.docx
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
include ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdfinclude ltfunctionalgt include ltiteratorgt inclu.pdf
include ltfunctionalgt include ltiteratorgt inclu.pdf
 
DS - Application of List
DS - Application of ListDS - Application of List
DS - Application of List
 
CS8391-Data Structures Unit 1
CS8391-Data Structures Unit 1CS8391-Data Structures Unit 1
CS8391-Data Structures Unit 1
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
I have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdfI have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdf
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
 
Im having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdfIm having difficulty with the directives i figured out a duplicatio.pdf
Im having difficulty with the directives i figured out a duplicatio.pdf
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
 
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdfC++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
 

More from fatoryoutlets

The right and left sternocleidomastoid muscles of humans originate o.pdf
The right and left sternocleidomastoid muscles of humans originate o.pdfThe right and left sternocleidomastoid muscles of humans originate o.pdf
The right and left sternocleidomastoid muscles of humans originate o.pdf
fatoryoutlets
 
Suppose the rate of plant growth on Isle Royale supported an equilib.pdf
Suppose the rate of plant growth on Isle Royale supported an equilib.pdfSuppose the rate of plant growth on Isle Royale supported an equilib.pdf
Suppose the rate of plant growth on Isle Royale supported an equilib.pdf
fatoryoutlets
 
Note Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdfNote Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdf
fatoryoutlets
 
Negotiations are not usually faster in Deal focused cultures than re.pdf
Negotiations are not usually faster in Deal focused cultures than re.pdfNegotiations are not usually faster in Deal focused cultures than re.pdf
Negotiations are not usually faster in Deal focused cultures than re.pdf
fatoryoutlets
 
9. The tort of assault and the tort of battery A) can occur independe.pdf
9. The tort of assault and the tort of battery A) can occur independe.pdf9. The tort of assault and the tort of battery A) can occur independe.pdf
9. The tort of assault and the tort of battery A) can occur independe.pdf
fatoryoutlets
 
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdfJj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf
fatoryoutlets
 

More from fatoryoutlets (20)

Write an informal paper that is exactly 3 pages long not counting th.pdf
Write an informal paper that is exactly 3 pages long not counting th.pdfWrite an informal paper that is exactly 3 pages long not counting th.pdf
Write an informal paper that is exactly 3 pages long not counting th.pdf
 
Write a C program to find factorial of an integer n, where the user .pdf
Write a C program to find factorial of an integer n, where the user .pdfWrite a C program to find factorial of an integer n, where the user .pdf
Write a C program to find factorial of an integer n, where the user .pdf
 
When was the black body mutation in drosophila melanogaster discover.pdf
When was the black body mutation in drosophila melanogaster discover.pdfWhen was the black body mutation in drosophila melanogaster discover.pdf
When was the black body mutation in drosophila melanogaster discover.pdf
 
What is it that consumer researchers try to find among varying cultu.pdf
What is it that consumer researchers try to find among varying cultu.pdfWhat is it that consumer researchers try to find among varying cultu.pdf
What is it that consumer researchers try to find among varying cultu.pdf
 
What are pros and cons of Symantec endpoint security softwareSo.pdf
What are pros and cons of Symantec endpoint security softwareSo.pdfWhat are pros and cons of Symantec endpoint security softwareSo.pdf
What are pros and cons of Symantec endpoint security softwareSo.pdf
 
All of the following describe the International Accounting Standard .pdf
All of the following describe the International Accounting Standard .pdfAll of the following describe the International Accounting Standard .pdf
All of the following describe the International Accounting Standard .pdf
 
The right and left sternocleidomastoid muscles of humans originate o.pdf
The right and left sternocleidomastoid muscles of humans originate o.pdfThe right and left sternocleidomastoid muscles of humans originate o.pdf
The right and left sternocleidomastoid muscles of humans originate o.pdf
 
The code in image3.cpp has the error as shown above, could you help .pdf
The code in image3.cpp has the error as shown above, could you help .pdfThe code in image3.cpp has the error as shown above, could you help .pdf
The code in image3.cpp has the error as shown above, could you help .pdf
 
the ability of the cardiac muscle cells to fire on their own is .pdf
the ability of the cardiac muscle cells to fire on their own is .pdfthe ability of the cardiac muscle cells to fire on their own is .pdf
the ability of the cardiac muscle cells to fire on their own is .pdf
 
Template LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdfTemplate LinkedList;I am using templates to make some linkedLists.pdf
Template LinkedList;I am using templates to make some linkedLists.pdf
 
Surface water entering the North Atlantic may have a temperature of .pdf
Surface water entering the North Atlantic may have a temperature of .pdfSurface water entering the North Atlantic may have a temperature of .pdf
Surface water entering the North Atlantic may have a temperature of .pdf
 
Suppose the rate of plant growth on Isle Royale supported an equilib.pdf
Suppose the rate of plant growth on Isle Royale supported an equilib.pdfSuppose the rate of plant growth on Isle Royale supported an equilib.pdf
Suppose the rate of plant growth on Isle Royale supported an equilib.pdf
 
Q1. public void operationZ() throws StackUnderflowException {   .pdf
Q1. public void operationZ() throws StackUnderflowException {   .pdfQ1. public void operationZ() throws StackUnderflowException {   .pdf
Q1. public void operationZ() throws StackUnderflowException {   .pdf
 
Print Calculator Question 18 of 20 Sapling Learning Map do ment of Lu.pdf
Print Calculator Question 18 of 20 Sapling Learning Map do ment of Lu.pdfPrint Calculator Question 18 of 20 Sapling Learning Map do ment of Lu.pdf
Print Calculator Question 18 of 20 Sapling Learning Map do ment of Lu.pdf
 
Part A 1. Solid product forms during the addition of the bleach solu.pdf
Part A 1. Solid product forms during the addition of the bleach solu.pdfPart A 1. Solid product forms during the addition of the bleach solu.pdf
Part A 1. Solid product forms during the addition of the bleach solu.pdf
 
Only 15 of the carbon-14 in a wooden bowl remains. How old is the b.pdf
Only 15 of the carbon-14 in a wooden bowl remains. How old is the b.pdfOnly 15 of the carbon-14 in a wooden bowl remains. How old is the b.pdf
Only 15 of the carbon-14 in a wooden bowl remains. How old is the b.pdf
 
Note Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdfNote Use Java Write a web server that is capable of processing only.pdf
Note Use Java Write a web server that is capable of processing only.pdf
 
Negotiations are not usually faster in Deal focused cultures than re.pdf
Negotiations are not usually faster in Deal focused cultures than re.pdfNegotiations are not usually faster in Deal focused cultures than re.pdf
Negotiations are not usually faster in Deal focused cultures than re.pdf
 
9. The tort of assault and the tort of battery A) can occur independe.pdf
9. The tort of assault and the tort of battery A) can occur independe.pdf9. The tort of assault and the tort of battery A) can occur independe.pdf
9. The tort of assault and the tort of battery A) can occur independe.pdf
 
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdfJj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf
Jj_numjnamecityj1SorterParisj2PunchRomej3Reade.pdf
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Change the driver file (the main .cpp) so that it asks the user to e.pdf

  • 1. Change the driver file (the main .cpp) so that it asks the user to enter the values, then prints them, reverses them, and prints them again. DO NOT USE THE revprint2 function that is in the code! LinkListImp.cpp //----- List.cpp ----- #include using namespace std; #include "linkedlist.h" //-- Definition of the class constructor List::List() : first(0), mySize(0) { } //-- Definition of the copy constructor List::List(const List & origList) { mySize = origList.mySize; first = 0; if (mySize == 0) return; List::NodePointer origPtr, lastPtr; first = new Node(origList.first->data); // copy first node lastPtr = first; origPtr = origList.first->next; while (origPtr != 0) { lastPtr->next = new Node(origPtr->data); origPtr = origPtr->next; lastPtr = lastPtr->next; } } //-- Definition of the destructor inline List::~List() { List::NodePointer prev = first, ptr; while (prev != 0) {
  • 2. ptr = prev->next; delete prev; prev = ptr; } } // Definition of empty() bool List::empty() { return mySize == 0; } //-- Definition of the assignment operator const List & List::operator=(const List & rightSide) { mySize = rightSide.mySize; first = 0; if (mySize == 0) return *this; if (this != &rightSide) { this->~List(); List::NodePointer origPtr, lastPtr; first = new Node(rightSide.first->data); // copy first node lastPtr = first; origPtr = rightSide.first->next; while (origPtr != 0) { lastPtr->next = new Node(origPtr->data); origPtr = origPtr->next; lastPtr = lastPtr->next; } } return *this; } //-- Definition of insert() void List::insert(ElementType dataVal, int index) { if (index < 0 || index > mySize)
  • 3. { cerr << "Illegal location to insert -- " << index << endl; return; } mySize++; List::NodePointer newPtr = new Node(dataVal), predPtr = first; if (index == 0) { newPtr->next = first; first = newPtr; } else { for (int i = 1; i < index; i++) predPtr = predPtr->next; newPtr->next = predPtr->next; predPtr->next = newPtr; } } //-- Definition of erase() void List::erase(int index) { if (index < 0 || index >= mySize) { cerr << "Illegal location to delete -- " << index << endl; return; } mySize--; List::NodePointer ptr, predPtr = first; if (index == 0) { ptr = first; first = ptr->next; delete ptr;
  • 4. } else { for (int i = 1; i < index; i++) predPtr = predPtr->next; ptr = predPtr->next; predPtr->next = ptr->next; delete ptr; } } //-- Definition of search() int List::search(ElementType dataVal) { int loc; List::NodePointer tempP = first; for (loc = 0; loc < mySize; loc++) if (tempP->data == dataVal) return loc; else tempP = tempP->next; return -1; } //-- Definition of display() void List::display(ostream & out) const { NodePointer ptr = first; while (ptr != 0) { out << ptr->data << " "; ptr = ptr->next; } } void List::revPrint(ostream & out) { reverse();
  • 5. display(out); reverse(); } void List::revPrint2(ostream &out, NodePointer ptr) { out << "Inside revPrint2 "; static List::NodePointer point = first; out << point->data << endl; if (point != NULL) { out << "Inside if statement "; revPrint2(out, point->next); out << point->data; } } //-- Definition of the output operator ostream & operator<<(ostream & out, const List & aList) { aList.display(out); return out; } //-- Definition of nodeCount() int List::nodeCount() { // or simply, { return mySize; } int count = 0; List::NodePointer ptr = first; while (ptr != 0) { count++; ptr = ptr->next; } return count; } //-- Definition of reverse() void List::reverse() {
  • 6. } //-- Definition of ascendingOrder() bool List::ascendingOrder() { if (mySize <= 1) //empty or one element list return true; //else NodePointer prevP = first, tempP = first->next; while (tempP != 0 && prevP->data <= tempP->data) { prevP = tempP; tempP = tempP->next; } if (tempP != 0) return false; // else return true; } linkedlist.cpp #include using namespace std; #include "linkedlist.h" int main() { List mylist; for (int i = 0; i < 6; i++) mylist.insert(i * 2, i); mylist.display(cout); cout << endl; mylist.revPrint(cout); cout << endl; cout << mylist;
  • 7. } linkedlist.h //----- List.h ----- #ifndef LINKEDLIST #define LINKEDLIST #include using namespace std; typedef int ElementType; class List { private: class Node { public: ElementType data; Node * next; Node() : next(0) { } Node(ElementType dataValue) : data(dataValue), next(0) { } }; //--- end of Node class typedef Node * NodePointer; public: //------ List OPERATIONS List(); List(const List & origList); ~List(); const List & operator=(const List & rightSide); bool List::empty(); void insert(ElementType dataVal, int index); void erase(int index); int search(ElementType dataVal);
  • 8. void display(ostream & out) const; void revPrint(ostream & out); void revPrint2(ostream &out, NodePointer ptr = NULL); int nodeCount(); void reverse(); bool ascendingOrder(); private: //------ DATA MEMBERS NodePointer first ; int mySize; }; //--- end of List class ostream & operator<<(ostream & out, const List & aList); //istream & operator>>(istream & in, List & aList); #endif Solution ////----- List.cpp ----- //added reverse functionality #include using namespace std; #include "linkedlist.h" //-- Definition of the class constructor List::List() : first(0), mySize(0) { } //-- Definition of the copy constructor List::List(const List & origList) { mySize = origList.mySize; first = 0; if (mySize == 0) return; List::NodePointer origPtr, lastPtr; first = new Node(origList.first->data); // copy first node lastPtr = first; origPtr = origList.first->next;
  • 9. while (origPtr != 0) { lastPtr->next = new Node(origPtr->data); origPtr = origPtr->next; lastPtr = lastPtr->next; } } //-- Definition of the destructor inline List::~List() { List::NodePointer prev = first, ptr; while (prev != 0) { ptr = prev->next; delete prev; prev = ptr; } } // Definition of empty() bool List::empty() { return mySize == 0; } //-- Definition of the assignment operator const List & List::operator=(const List & rightSide) { mySize = rightSide.mySize; first = 0; if (mySize == 0) return *this; if (this != &rightSide) { this->~List(); List::NodePointer origPtr, lastPtr; first = new Node(rightSide.first->data); // copy first node lastPtr = first;
  • 10. origPtr = rightSide.first->next; while (origPtr != 0) { lastPtr->next = new Node(origPtr->data); origPtr = origPtr->next; lastPtr = lastPtr->next; } } return *this; } //-- Definition of insert() void List::insert(ElementType dataVal, int index) { if (index < 0 || index > mySize) { cerr << "Illegal location to insert -- " << index << endl; return; } mySize++; List::NodePointer newPtr = new Node(dataVal), predPtr = first; if (index == 0) { newPtr->next = first; first = newPtr; } else { for (int i = 1; i < index; i++) predPtr = predPtr->next; newPtr->next = predPtr->next; predPtr->next = newPtr; } } //-- Definition of erase() void List::erase(int index)
  • 11. { if (index < 0 || index >= mySize) { cerr << "Illegal location to delete -- " << index << endl; return; } mySize--; List::NodePointer ptr, predPtr = first; if (index == 0) { ptr = first; first = ptr->next; delete ptr; } else { for (int i = 1; i < index; i++) predPtr = predPtr->next; ptr = predPtr->next; predPtr->next = ptr->next; delete ptr; } } //-- Definition of search() int List::search(ElementType dataVal) { int loc; List::NodePointer tempP = first; for (loc = 0; loc < mySize; loc++) if (tempP->data == dataVal) return loc; else tempP = tempP->next; return -1; }
  • 12. //-- Definition of display() void List::display(ostream & out) const { NodePointer ptr = first; while (ptr != 0) { out << ptr->data << " "; ptr = ptr->next; } } void List::revPrint(ostream & out) { reverse(); display(out); reverse(); } void List::revPrint2(ostream &out, NodePointer ptr) { out << "Inside revPrint2 "; static List::NodePointer point = first; out << point->data << endl; if (point != NULL) { out << "Inside if statement "; revPrint2(out, point->next); out << point->data; } } //-- Definition of the output operator ostream & operator<<(ostream & out, const List & aList) { aList.display(out); return out; } //-- Definition of nodeCount() int List::nodeCount()
  • 13. { // or simply, { return mySize; } int count = 0; List::NodePointer ptr = first; while (ptr != 0) { count++; ptr = ptr->next; } return count; } //-- Definition of reverse() //added by chegg EA void List::reverse() { static List::NodePointer current,next,prev; prev = NULL; current = first; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } first= prev; } //-- Definition of ascendingOrder() bool List::ascendingOrder() { if (mySize <= 1) //empty or one element list return true; //else NodePointer prevP = first, tempP = first->next;
  • 14. while (tempP != 0 && prevP->data <= tempP->data) { prevP = tempP; tempP = tempP->next; } if (tempP != 0) return false; // else return true; } ------------------------------------------------------------------------ // List.h this file unchanged --------------------------------------------------------------- //main.cpp #include using namespace std; #include "linkedlist.h" int main() { List mylist; int num; for (int i = 0; i < 6; i++) { //mylist.insert(i * 2, i); //added by chegg EA,ask for user input cin >> num; mylist.insert(num, i); } cout << "mylist: "; mylist.display(cout); cout << endl; //mylist.revPrint(cout); //added by chegg EA,reverse the list mylist.reverse(); //added by chegg EA, print the reversed list
  • 15. cout << endl; cout << "After reversing the list: "<