In: Computer Science
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;
}
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.