I do not know is the code I got is correct, and how to tie it together overall,
ID: 3671195 • Letter: I
Question
I do not know is the code I got is correct, and how to tie it together overall, I put the function apart from the code.
(C++)
void append(const StringLinkedList& other);
{
//create new node and store
//point to newNode
ListNode* newNode, *nodePtr;
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
if(!head)
//if no heads then it makes newNode 1st
head = newNode;
//else insert newNode at end
else{
nodePtr = head;
while(nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
#include <iostream>
#include <string>
#include <cstdlib>
//a node in a list of strings
class StringNode{
private:
string elem; //element value
StringNode* next; //next item in the list
friend class StringLinked List; //provide StringLinkedList access
};
//a linked list of strings
class StringLinked List{
public:
StringLinked List(); //empty list constructor
~StringLinked List(); //destructor
bool empty() const; //is it empty ?
void addFont(const string& e); //add to front of list
void removeFront(); //remove front item list
const string& front() const;
private:
StringNode* head; //pointer to the head of list
};
void Print() const;
//prints all of the strings in the list (1 per line)
//test bed
//add 3 strings and use this to print them.
Print ()
StringLinkedList::StringLinkedList()
:head(NULL) {}
StringLinkedList::~StringLinkedList()
{
while (!empty()) removeFront();
}
bool StringLinkedList::empty() const
{
return head==NULL;
}
const string& StringLinkedList::front() const
{
return head->elem;
}
//add to front of list
void StringLinkedList::addFront(const string& e){
StringNode* v = new StringNode;
v->elem=e; //store data
v->next = head; //head now follows v
head=v; //v is now the head
}
void StringLinkedList::removeFront(){
StringNode* old=head;
head = old->next;
delete old;
}
int main () {
StringLinkedList list; // construct an instance
list.addFront("Hello"); // Add "Hello"
list.addFront("Cool"); // Add "Cool"
list.addFront("bye"); // Add "bye"
list.removeFront(); // Remove "bar"
// List is automatically deleted now
}
Explanation / Answer
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
//a node in a list of strings
class StringNode{
private:
string elem; //element value
StringNode* next; //next item in the list
friend class StringLinkedList; //provide StringLinkedList access
};
//a linked list of strings
class StringLinkedList{
public:
StringLinkedList(); //empty list constructor
~StringLinkedList(); //destructor
bool empty() const; //is it empty ?
void addFront(const string& e); //add to front of list
void removeFront(); //remove front item list
void append(const StringLinkedList& other);
void print(); //printing list
const string& front() const;
private:
StringNode* head; //pointer to the head of list
};
StringLinkedList::StringLinkedList():head(NULL) {}
StringLinkedList::~StringLinkedList()
{
while (!empty()) removeFront();
}
void StringLinkedList::append(const StringLinkedList& other)
{
StringNode *nodePtr,*otherPtr;
otherPtr = other.head;
if( head==NULL && otherPtr!=NULL) //if no heads then it makes newNode 1st
{
StringNode* v = new StringNode;
v->elem = otherPtr->elem; //store data
v->next = NULL; //head now follows v
head = v;
otherPtr = otherPtr->next;
}else if( head==NULL && otherPtr==NULL)
return;
else{
nodePtr = head;
while(nodePtr->next!=NULL)
nodePtr = nodePtr->next;
while(otherPtr !=NULL)
{
StringNode* v = new StringNode;
v->elem = otherPtr->elem; //store data
v->next = NULL; //head now follows v
nodePtr->next = v;
nodePtr = nodePtr->next;
otherPtr = otherPtr->next;
}
}
}
bool StringLinkedList::empty() const
{
return head==NULL;
}
const string& StringLinkedList::front() const
{
return head->elem;
}
//add to front of list
void StringLinkedList::addFront(const string& e){
StringNode* v = new StringNode;
v->elem = e; //store data
v->next = head; //head now follows v
head=v; //v is now the head
}
void StringLinkedList::removeFront(){
StringNode* old=head;
head = old->next;
delete old;
}
void StringLinkedList::print()
{
StringNode *nodePtr = head;
while(nodePtr!=NULL)
{
cout<<nodePtr->elem<<endl;
nodePtr = nodePtr->next;
}
cout<<endl<<endl;
}
int main () {
StringLinkedList list1; // construct an instance
list1.addFront("Hello"); // Add "Hello"
list1.addFront("Cool"); // Add "Cool"
list1.addFront("bye"); // Add "bye"
StringLinkedList list2; // construct an instance
list2.addFront("Appended_Hello"); // Add "Appended_Hello"
list2.addFront("Appended_Cool"); // Add "Appended_Cool"
list2.addFront("Appended_bye"); // Add "Appended_bye"
cout<<" Before appending list1 looks like ";
list1.print();
cout<<"list2 looks like ";
list2.print();
cout<<" After appending with list2, list1 looks like ";
list1.append(list2);
list1.print();
cout<<" And list2 still looks like ";
list2.print();
// List is automatically deleted now
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.