In: Computer Science
I am just learning C++ and need to convert my previous code to a new linked list method. Please help.
My current LL program:
#include
#include
using namespace std;
struct LLnode{
string theData;
LLnode * fwdPtr;
};
void push_front(LLnode * &llh, string newData{
if(!llh){
LLnode * newNode = new LLnode;
newNode -> theData = newData;
newNode -> fwdPtr = nullptr;
llh = newNode;
}
else{
LLnode * trav = new LLnode;
trav = llh;
LLnode * newNode = new LLnode;
newNode -> theData = newData;
newNode -> fwdPtr = trav;
llh = newNode;
}
}
void push_back(LLnode * &llh, string newData){
LLnode * trav = new LLnode;
if(!llh){
llh = new LLnode;
llh -> theData = newData;
llh -> fwdPtr = nullptr;
}
else{
LLnode * newNode = new LLnode;
newNode -> theData = newData;
newNode -> fwdPtr = nullptr;
trav = llh;
while(trav -> fwdPtr){
trav = trav -> fwdPtr;
}
trav -> fwdPtr = newNode;
}
}
int list_length(LLnode * &llh){
int count = 0;
LLnode * trav = new LLnode;
trav = llh;
if(!llh){
return 0;
}
else{
while(trav){
count ++;
trav = trav -> fwdPtr;
}
return count;
}
}
string retrieve_front (LLnode * &llh){
if(!llh)
throw string ("Exception at retrieve back");
return (llh -> theData);
}
string retrieve_back (LLnode * &llh){
LLnode * trav = new LLnode;
trav = llh;
if(!llh)
throw string ("Exception at retrieve back");
while(trav -> fwdPtr){
trav = trav -> fwdPtr;
}
return trav -> theData;
}
void display_nodes(LLnode * &llh) {
LLnode * trav = new LLnode;
trav = llh;
if(!llh){
cout << "No nodes to display " << endl;
}
else{
cout << "Displaying nodes: " << endl;
while(trav){
cout << trav -> theData << ", ";
trav = trav -> fwdPtr;
}
}
cout << endl;
}
int main(){
LLnode * theLLHeader1 = nullptr;
cout << "Main: number of nodes in empty list " <<
list_length(theLLHeader1) << endl;
display_nodes(theLLHeader1);
push_front(theLLHeader1, "aaaaa");
push_back(theLLHeader1, "bbbbb");
push_front(theLLHeader1, "before aaaaa");
push_back(theLLHeader1, "after bbbbb");
cout << "Main: number of nodes after 4 pushed: " <<
list_length(theLLHeader1) << endl;
display_nodes(theLLHeader1);
cout << "Main: retrieve front: " << retrieve_front
(theLLHeader1) << endl;
cout << "Main: retrieve back: " << retrieve_back
(theLLHeader1) << endl;
cout << endl;
LLnode * theLLHeader2 = nullptr;
push_front(theLLHeader2, "33333");
push_front(theLLHeader2, "22222");
push_front(theLLHeader2, "11111");
push_back(theLLHeader2, "44444");
push_back(theLLHeader2, "55555");
push_back(theLLHeader2, "66666");
display_nodes (theLLHeader2);
return 0;
}
What I need to do:
Step 1
Convert LLnode struct into a header file. Any code module that refers to LLnode will need to contain a #include for this header file.
Create a linked list class named LL. Put into the same header file as struct. It will contain private data member: a linked list header, which is just a pointer variable of type LLnode for LLnode variables
I have already coded six linked list processing functions – push_front, push_back, list_length, retrieve_front, retrieve_back, and display_list. I need to convert these functions into public member functions of the class. The constructor for the LL class should set the linked list header to nullptr. There is no destructor. I also need to Templatize the class, and a requirement for templatized class functions is that they be coded in-line in the class definition. So, code the member functions in-line within the header file. You’ll have only two course files: the header file, and the .cpp for main.
I would like evidence of Couts so I can better understand the program. This would be much appreciated.
Step 2
Using a class template, I need to change struct and class to use any variable type. Main # 2 has been set up for debugging this step.
Step 3
Add member functions that perform the following functions:
void destroy_list ()
deletes each node in the list, and resets the header to nullptr
bool search_list (key value)
searches the list for a node with the given key. Returns true if
found, false if not.
bool delete_node (key value)
deletes the node which contains the given key. If there is more
than one node with the same key, delete_node deletes the first
occurrence. Returns true if delete successful, false if the node
was not found.
Main # 3 contains a test program for testing step 3.
Here are the main classes (1,2,3)::
//MAIN 1
int main() {
LL ll1;
cout << "length of empty list - " << ll1.list_length()
<< endl;
ll1.display_list();
ll1.push_front("aaaaa");
ll1.push_back("bbbbb");
ll1.push_front("before aaaaa");
ll1.push_back("after bbbbb");
cout << "length of list after 4 pushes - " <<
ll1.list_length() << endl;
ll1.display_list();
cout << endl;
LL ll2;
ll2.push_front("33333");
ll2.push_front("22222");
ll2.push_front("11111");
ll2.push_back("44444");
ll2.push_back("55555");
ll2.push_back("66666");
ll2.display_list();
return 0;
}
//MAIN 2
int main() {
LL ll1;
cout << "main: length of empty list - " <<
ll1.list_length() << endl;
cout << "main: trying to display empty list 1" <<
endl;
ll1.display_list();
cout << "main: trying to display initial size of ll1 - "
<< ll1.list_length() << endl;
ll1.push_front("aaaaa");
ll1.push_back("bbbbb");
ll1.push_front("before aaaaa");
ll1.push_back("after bbbbb");
cout << "main: length of ll1 after 4 pushes - " <<
ll1.list_length() << endl;
cout << "main: now trying to display ll1 after 4 push's"
<< endl;
ll1.display_list();
cout << "main: displaying final size of ll1 - " <<
ll1.list_length() << endl;
cout << endl;
LL ll2;
ll2.push_front("33333");
ll2.push_front("22222");
ll2.push_front("11111");
ll2.push_back("44444");
ll2.push_back("55555");
ll2.push_back("66666");
cout << "main: now trying to display ll2 after 6 push's"
<< endl;
ll2.display_list();
return 0;
}
//MAIN 3
int main() {
LL ll1;
cout << "main: length of empty list - " <<
ll1.list_length() << endl;
cout << "main: trying to display empty list 1" <<
endl;
ll1.display_list();
cout << "main: trying to display initial size of ll1 - "
<< ll1.list_length() << endl;
ll1.push_front("aaaaa");
ll1.push_back("bbbbb");
ll1.push_front("before aaaaa");
ll1.push_back("after bbbbb");
cout << "main: length of ll1 after 4 pushes - " <<
ll1.list_length() << endl;
cout << "main: now trying to display ll1 after 4 push's"
<< endl;
ll1.display_list();
cout << "main: displaying final size of ll1 - " <<
ll1.list_length() << endl;
ll1.destroy_list();
cout << "main: displaying size of list 1 after destroy - "
<< ll1.list_length() << endl;
cout << endl;
LL ll2;
ll2.push_front("33333");
ll2.push_front("22222");
ll2.push_front("11111");
ll2.push_back("44444");
ll2.push_back("55555");
ll2.push_back("66666");
cout << "main: now trying to display ll2 after 6 push's"
<< endl;
ll2.display_list();
cout << "main: now searching for node 44444" <<
endl;
if (ll2.search_list("44444"))
{
cout <<"main: found node 44444" << endl;
}
else
{
cout << "main: did not find node 44444" << endl;
}
cout << "main: now searching for node 44445" <<
endl;
if (ll2.search_list("44445"))
{
cout <<"main: found node 44445" << endl;
}
else
{
cout << "main: did not find node 44445" << endl;
}
cout << "main: now trying to delete node 44444" <<
endl;
if (ll2.delete_node("44444"))
{
cout <<"main: node 44444 deleted" << endl;
}
else
{
cout << "main: did not find 44444 for delete" <<
endl;
}
if (ll2.search_list("44444"))
{
cout <<"main: searched for 44444 after delete, found"
<< endl;
}
else
{
cout << "main: searched for 44444 after delete, not found"
<< endl;
}
cout << "main: displaying whole list after delete 44444"
<< endl;
ll2.display_list();
cout << "main: now trying to delete node 11111" <<
endl;
if (ll2.delete_node("11111"))
{
cout <<"main: node 11111 deleted" << endl;
}
else
{
cout << "main: did not find node 11111 for delete" <<
endl;
}
cout << "main displaying whole list after delete 11111"
<< endl;
ll2.display_list();
ll2.destroy_list();
return 0;
}
I will much appreciate any help. Thank you.
Hi, I created the header file and cpp for the both string and template type, which will accept any type of data. Your push_front needed some change. I updated it also some variable names. other than that no changes are required to your code. Here is your solution, let me know if you need any clarification or if you face any issues.
|
#ifndef LINKEDLIST_H_ #include <iostream> using namespace std;
// declare class of template type #endif /* LINKEDLIST_H_*/
|
|
#include "LinkedList.h" LinkedList ::LinkedList() { void LinkedList ::push_front(string newData) { void LinkedList ::push_back(string newData) { int LinkedList ::size() { } string LinkedList ::retrieve_front() { string LinkedList ::retrieve_back() { void LinkedList ::display_nodes() { LinkedList list2; // list2.push_front(33333); return 0; ![]() ![]() ![]() ![]() ![]() |
![]() |
|
#ifndef LINKEDLISTTEMPLATE_H_ #include <iostream> using namespace std; // define struct of template // declare class of template type #endif /* LINKEDLISTTEMPLATE_H_ */ |
|
} LinkedList<int>list2; list2.push_front(33333); return 0;
|
![]() |