Let us code the following three source files in C++ : string List.h, string list
ID: 3915118 • Letter: L
Question
Let us code the following three source files in C++: string List.h, string list.cpp, hw5.cpp
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
(1)StringList.h
#include<iostream>
using namespace std;
class StringList{
public:
struct node{
string s;
node *next;
};
private:
struct node*head;
public:
StringList();
~StringList();
void insertfront();
void insertback();
void deletefront();
void deleteback();
void display();
};
(2)StringList.cpp
#include"StringList.h"
StringList::StringList(){
head=NULL;
}
StringList::~StringList(){
struct node *temp=head,*current;
while(temp){
current=temp;
temp=temp->next;
delete current;
}
}
void StringList::deletefront(){
if(!head)return;
struct node*temp=head;
head=head->next;
cout<<temp->s<<" is deleted ";
delete temp;
}
void StringList::deleteback(){
struct node *temp=head,*pre,*current;
if(!head)return;
else if(head->next==NULL){
current=head;head=NULL;}
else{
while(temp->next){
pre=temp;
current=temp->next;
temp=current;
}
pre->next=current->next;
}
cout<<current->s<<" is deleted ";
delete current;
}
void StringList::insertfront(){
struct node *newnode=new struct node;
string str;
cout<<"Enter string : ";
cin>>str;
newnode->s=str;
struct node *temp=head;
head=newnode;
newnode->next=temp;
}
void StringList::insertback(){
struct node *newnode=new struct node;
string str;
cout<<"Enter string : ";
cin>>str;
newnode->s=str;
newnode->next=NULL;
struct node *temp=head;
while(temp->next){
temp=temp->next;
}
temp->next=newnode;
}
void StringList::display(){
struct node*temp=head;
while(temp){
cout<<temp->s<<" ";
temp=temp->next;
}
}
(3)Driver function(main.cpp)
#include<iostream>
#include"stringList.cpp"
using namespace std;
int main(){
StringList ob;
int choice;
do{
cout<<"1:insertfront 2:insertback 3:deletefront 4:deleteback 5:display 6:quit ";
cin>>choice;
switch(choice){
case 1:{
ob.insertfront();
break;
}
case 2:{
ob.insertback();
break;
}
case 3:{
ob.deletefront();
break;
}
case 4:{
ob.deleteback();
break;
}
case 5:{
ob.display();
break;
}
}
}while(choice!=6);
return 0;
}
for any query please comment.
please upvote if find it helpful.
Thank you!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.