Let us code the following three source files in C++: • StringList.h – In this fi
ID: 3914329 • Letter: L
Question
Let us code the following three source files in C++:
• StringList.h
– In this file, you declare a class named StringList.
– StringList is a modified version of NumberList class (Chapter 17), that is designed to store C++ strings in a linked list.
– Therefore, each node must store a string, not a number.
– The node structure must be declared within class, and a private attribute head points to the starting node.
– The class provides a default constructor that creates an empty linked list, and a destructor that releases all the nodes.
– The class also provides public member functions for inserting and deleting a node (see below for details).
– It also provides a public member function for displaying the list.
• StringList.cpp
– In this file, you provide definitions for the default constructor and the destructor for StringList.
– Make sure the destructor visits every node in the list and deletes every one of them from the heap.
– Define insertFront function to insert a new node into the front of the list. Therefore, the new node will be the new first node in the list. This function takes a string as a parameter.
– Define insertBack function to insert a new node into the back of the list. Therefore, the new node will be the new last node in the list. This function takes a string as a parameter.
– Define deleteFront function to delete the first node from the list. This function takes no parameter.
– Define deleteBack function to delete the last node from the list. This function takes no parameter.
– Define display function that displays the current contents (strings) of the list (display strings in a single line, separated by a space). This function takes no parameter.
• hw5.cpp
– In this file, you define main function that tests StringList class.
– You must first create a StringList object. 1
– Then start inserting new nodes, one at a time. Alternate between inserting into the front and into the back. Make sure to add at least 6 nodes.
– Then start deleting nodes, one at a time. Alternate between deleting from the front and from the back.
– Make sure all the nodes are deleted before terminating the program.
– After each insertion or deletion, call display member function to display the updated list.
– Make sure all the member functions are tested and shown to work properly, without missing any of them.
– Note that the display function must be called pretty much every time a node is inserted or deleted. Otherwise the respective functionality will not be properly demonstrated and could lead to loss of points.
– Also note that each member function should properly work regardless of the current list configuration (empty list, one-node list, or multiple-node list).
Explanation / Answer
YOU CAN UNCOMMENT THE PRINT STATEMENT LINES TO UNDERSTAND THE FLOW OF PROGRAM
(En =Enter Ex=Exit)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
StringList.h
class StringList
{
private:
struct node
{
char string_val[20];
struct node *next;
};
struct node *head;
struct node *tail;
void display()
{
//cout<<" ....En display.... ";
struct node *trav=head;
while(trav!=NULL)
{
cout<<" "<<trav->string_val;
trav=trav->next;
}
if(head==NULL)
{
cout<<" List is Empty ";
}
cout<<endl;
//cout<<" ....Ex display.... ";
}
public:
StringList();
void delete_front();
void delete_last();
void insert_front(struct node *new1);
void insert_last(struct node *new1);
struct node *new_node()
{
//cout<<" ....En new_node.... ";
struct node *new1=(struct node *)malloc(sizeof(struct node));
cout<<" Enter string for new node:";
cin>>new1->string_val;
new1->next=NULL;
//cout<<" ....Ex new_node.... ";
return new1;
}
~StringList();
};
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
StringList.cpp
StringList::StringList()
{
head=tail=NULL;
}
void StringList::delete_front()
{
//cout<<" ....En delete_front.... ";
if(head)
{
struct node *temp=head;
head=head->next;
free(temp);
display();
}
else
{
cout<<" List is empty..!!!";
}
//cout<<" ....Ex delete_front.... ";
}
void StringList::delete_last()
{
//cout<<" ....En delete_last.... ";
if(head)
{
struct node *sec_last=head;
if(head->next!=NULL)
{
while(sec_last->next->next!=NULL)
{
sec_last=sec_last->next;
}
}
if(sec_last->next==NULL)//if only one node in the list
{
free(head);
tail=head=NULL;
}
else
{
struct node *temp=sec_last->next;
sec_last->next=NULL;
free(temp);
}
display();
}
else
{
cout<<" List empty..!!!";
}
//cout<<" ....Ex delete_last.... ";
}
void StringList::insert_front(struct node *new1)
{
//cout<<" ....En insert_front.... ";
if(head)
{
new1->next=head;
head=new1;
}
else
{
head=tail=new1;
}
display();
//cout<<" ....Ex insert_front.... ";
}
void StringList::insert_last(struct node *new1)
{
//cout<<" ....En insert_last.... ";
if(head==NULL)
{
head=tail=new1;
}
else
{
tail->next=new1;
tail=new1;
}
display();
//cout<<" ....Ex insert_last.... ";
}
StringList::~StringList()
{
//cout<<" ....En ~StringList.... ";
while(head!=NULL)
{
cout<<" delete ";
delete_front();
}
//cout<<" ....Ex ~StringList.... ";
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
hw5.cpp
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#include"StringList.h"
#include"StringList.cpp"
int main()
{
StringList object;
cout<<" ----------------------------------- | Insertion Start | ----------------------------------- ";
object.insert_front(object.new_node());
object.insert_last(object.new_node());
object.insert_front(object.new_node());
object.insert_last(object.new_node());
object.insert_front(object.new_node());
object.insert_last(object.new_node());
cout<<" ----------------------------------- | Deletion Start | ----------------------------------- ";
object.delete_front();
object.delete_last();
object.delete_front();
object.delete_last();
object.delete_front();
object.delete_last();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.