Question

In: Computer Science

C++ Modify the class unorderedList to include a recursive forward print and a recursive reverse print...

C++

Modify the class unorderedList to include a recursive forward print and a recursive reverse print

Make your unorderedList a list of characters instead of integers

Insert ten characters into the list and print it out both ways

#include <iostream>

#include <string>

#include <cstdlib>

using namespace std;

struct node

{

int info;

node* next;

};

class unorderedList

{

private:

int length;

node* listPtr;

public:

unorderedList() {length = 0; listPtr = NULL;}

void makeEmpty();

void insertItem(int item);

void printList();

bool isFull() {node* testNode = new node;

   delete testNode;

   return 0;}

void deleteItem(int item);

~unorderedList(){makeEmpty();}

};

void unorderedList::makeEmpty()

{

int i;

node* temp;

for (i = 1; i <= length; i++)

{

temp = listPtr;

listPtr = listPtr->next;

delete temp;

}

length = 0;

cout << endl << "List is Empty" << endl;

}

void unorderedList::insertItem(int item)

{

node* temp = new node;

temp->info = item;

if (length == 0)

temp->next = NULL;

else

temp->next = listPtr;

listPtr = temp;

length++;

cout << "Added item " << item << " and length is " << length << endl;

}

void unorderedList::deleteItem(int item)

{

   node* ptr1;

   node* ptr2;

   node* temp;

   if (listPtr->info == item)

   {

   temp = listPtr;

   listPtr = listPtr->next;

   delete temp;

   }

   else

   {

   ptr1 = listPtr->next;

   ptr2 = listPtr;

   while(ptr1->info != item)

   {

   ptr2 = ptr1;

   ptr1 = ptr1->next;

   }

   temp = ptr1;

   ptr2->next = ptr1->next;

   delete temp;

   cout << endl << item << " deleted." << endl;

   }

}

void unorderedList::printList()

{

node* temp;

int i;

temp = listPtr;

cout << endl;

while (temp != NULL)

//for (i = 1; i <= length; i++)

{

cout << temp->info << " ";

temp = temp->next;

}

}

int main(){

unorderedList A;

A.insertItem(9);

A.insertItem( 8);

A.insertItem( 7);

A.insertItem(6);

A.insertItem( 5);

A.insertItem(4);

A.printList();

A.deleteItem(7);

//A.makeEmpty();

if (A.isFull())

cout << "List is full." << endl;

else

cout << "List is not full." << endl;

}

Solutions

Expert Solution

Solution - Code for the solution is given below. First 10 elements are inserted and then printed in both forward and backward directions using recursive approach. recursiveForwardPrint() method prints list in forward direction, recursiveBackwardPrint( ) method prints list in Backward direction.

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
struct node
{
  char info;
  node* next;
};

class unorderedList
{
  private:
    int length;
    node* listPtr;
  public:
    unorderedList(){
      length = 0; 
      listPtr = NULL;
    }
  void makeEmpty();
  void insertItem(char item);
  void printList();
  bool isFull(){
    node* testNode = new node;
    delete testNode;
    return 0;
    }
  void deleteItem(char item);
  node *getListpt(){
    return listPtr;
  }
  void recursiveForwardPrint(node *ptr);
  void recursiveBackwardPrint(node *ptr);
  ~unorderedList(){
    makeEmpty();
  }
};

// To print the list in Forward direction using Recursive approach
void unorderedList::recursiveForwardPrint(node * ptr){
  if(ptr==NULL)
    return;
  cout<< ptr->info <<" ";
  recursiveForwardPrint(ptr->next); 
   
}
// To print the list in Bckward direction using Recursive approach
void unorderedList::recursiveBackwardPrint(node * ptr){
  if(ptr==NULL)
    return;
  recursiveBackwardPrint(ptr->next);  
  cout<< ptr->info <<" ";
}
void unorderedList::makeEmpty(){
  int i;
  node* temp;
  for (i = 1; i < length; i++){
    temp = listPtr;
    listPtr = listPtr->next;
    delete temp;
  }
  delete listPtr;
  length = 0;
  cout << endl << "List is Empty" << endl;
}

void unorderedList::insertItem(char item){
  node* temp = new node;
  temp->info = item;
  if (length == 0)
    temp->next = NULL;
  else
    temp->next = listPtr;
  listPtr = temp;
  length++;
  cout << "Added item " << item << " and length is " << length << endl;
}

void unorderedList::deleteItem(char item){
   node* ptr1;
   node* ptr2;
   node* temp;
   if (listPtr->info == item){
    temp = listPtr;
    listPtr = listPtr->next;
    delete temp;
   }
   else{
    ptr1 = listPtr->next;
    ptr2 = listPtr;
    while(ptr1->info != item){
    ptr2 = ptr1;
    ptr1 = ptr1->next;
   }
   temp = ptr1;
   ptr2->next = ptr1->next;
   delete temp;
   cout << endl << item << " deleted." << endl;
   }
}
void unorderedList::printList(){
  node* temp;
  temp = listPtr;
  cout << endl;
  //for (i = 1; i <= length; i++)
  while (temp != NULL){
    cout << temp->info << " ";
    temp = temp->next;
  }
}

int main(){
  unorderedList A;
  A.insertItem('9');
  A.insertItem('8');
  A.insertItem('7');
  A.insertItem('6');
  A.insertItem('5');
  A.insertItem('4');
  A.printList();
  A.deleteItem('7');
  A.insertItem('0');
  A.insertItem('1');
  A.insertItem('2');
  A.insertItem('3');
  //A.makeEmpty();
  if (A.isFull())
    cout << "List is full."<< endl;
  else
    cout << "List is not full." << endl;
  node *ptr = A.getListpt();
  cout<<"List in Forward direction:"<<endl;
  A.recursiveForwardPrint(ptr);
  cout<<endl<<"List in Backward direction:"<<endl;
  ptr = A.getListpt();
  
  A.recursiveBackwardPrint(ptr);
}

Sample output is as following:-

Note - earlier there was an Segmentation error in the code given, that has been resolved.


Related Solutions

Implement a non-recursive reverse print of linked list using stack and the main function to test:...
Implement a non-recursive reverse print of linked list using stack and the main function to test: You will need to finish the printReversed_nonrecursive method in ch04.LinkedStack2 class, and the ch04.UseStack2 is the main function to test. public class LinkedStack2<T> extends LinkedStack<T> { private void revPrint(LLNode<T> listRef) { if (listRef != null) { revPrint(listRef.getLink()); System.out.println(" " + listRef.getInfo()); } } public void printReversed() { revPrint(top); } /* use stack to implement non-recursive reverse print */ public void printReversed_nonrecursive() { } public...
JAVA- Modify the LinkedList1 class presented in this chapter by adding sort() and reverse() methods. The...
JAVA- Modify the LinkedList1 class presented in this chapter by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. The class should use recursion to implement the sort and reverse operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods. LinkedList1: class...
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of...
Modify StudentLinkedList class by adding the following methods: printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student with the minimum...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and...
Problem 3: Modify StudentLinkedList class by adding the following methods:  printStudentList: print by calling and printing “toString” of every object in the linkedList. Every student object to be printed in a separate line.  deleteStudentByID(long id): delete student object from the list whose ID is matching with the passed parameter.  sortListByID(): sort the linkedlist according to students IDs.  findMarksAverage(): find the average of all marks for all students in the list.  findMinMark(int markIndex): find the student...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha...
**** IN C++ ***** 1.Given the class alpha and the main function, modify the class alpha so the main function is working properly. #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class alpha { private: int data; public: //YOUR CODE }; //////////////////////////////////////////////////////////////// int main() { alpha a1(37); alpha a2; a2 = a1; cout << "\na2="; a2.display(); //display a2 alpha a3(a1); //invoke copy constructor cout << "\na3="; a3.display(); //display a3 alpha a4 = a1; cout << "\na4="; a4.display(); cout << endl; return 0;...
Exercise 1 Modify the List class of Figure 21.3 in the textbook to include a method...
Exercise 1 Modify the List class of Figure 21.3 in the textbook to include a method that recursively searches a linked-list for a specified value. Ensure that the name of your method includes your last name. The method must return a reference to the value if it is found; otherwise, it must return null. Use your method in a test program that creates a list of integers. The program must prompt the user for a value to locate in the...
Exercise 1 Modify the List<T> class of Figure 21.3 in the textbook to include a method...
Exercise 1 Modify the List<T> class of Figure 21.3 in the textbook to include a method that recursively searches a linked-list for a specified value. Ensure that the name of your method includes your last name. The method must return a reference to the value if it is found; otherwise, it must return null. Use your method in a test program that creates a list of integers. The program must prompt the user for a value to locate in the...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
At equilibrium, the rate of the forward reaction is equal to the rate of the reverse...
At equilibrium, the rate of the forward reaction is equal to the rate of the reverse reaction. Both the forward and the reverse reactions are occurring, but because they are occurring at the same rate, no change is observed in the appearance of the solution. For this analysis, think about the affect that the addition or removal of reactant will have on the rate of the forward reaction relative to the rate of the reverse reaction at the instant that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT