Question: LinkList C++ - Help with pointer, traverse, output. Sorry, not so good with pointer code. I am hoping to see the exact cout on the
LinkList C++ - Help with pointer, traverse, output. Sorry, not so good with pointer code.
I am hoping to see the exact cout on the lhs toString() print on the rhs. My code below;
//Main.cpp
#include#include "LLNode.h" #include "LinkedList.h" //#include "Stack.h" using namespace std; int main() { //LLNode<:string> n1("abc"); LLNode n1(9); int x = n1.getData(); cout n2("aString"); cout l1; cout (5)); cout (8)); cout (7)); cout
//LLNode.h
#ifndef LLNODE_H #define LLNODE_H #includeusing std::cout; using std::endl; #include using std::string; #include using std::ostringstream; template struct node { T data; node *next; }; template class LinkedList; template class LLNode { friend class LinkedList ; //make LinkedList a friend to access private data public: explicit LLNode (const T&); void setData(const T& val); T getData()const; void setNext(LLNode *); LLNode * getNext()const { return nextPtr; } string toString()const; private: T data; LLNode * nextPtr; };//end function LLNode template LLNode ::LLNode(const T& val) : data(val), nextPtr(NULL) { } //constructor template void LLNode ::setData(const T& val) { value = val; } template T LLNode ::getData() const { return data; } template void LLNode ::setNext(LLNode * np) { nextPtr = np; } template string LLNode ::toString()const { ostringstream convert; convert
//LinkedList.h
#ifndef LINKEDLIST_H #define LINKEDLIST_H #include "LLNode.h" #includeusing std::cout; using std::endl; #include #include using std::string; template class LinkedList { public: LinkedList (); bool isEmpty() const; void insertFirst(LLNode * np); void deleteFirst(); T getFirst(); string toString()const; private: LLNode * first; LLNode data; }; template LinkedList ::LinkedList() :first(NULL), data(0) { //empty } template bool LinkedList ::isEmpty() const { return first->nextPtr = nullptr; } template void LinkedList ::insertFirst(LLNode * np) { first = np; if (isEmpty()) { //first = first->nextPtr; first = NULL; } else { //tempPtr->nextPtr = first->nextPtr; first->nextPtr = np->nextPtr; //first = tempPtr; } } template void LinkedList ::deleteFirst() { LLNode *tempPtr = first; first = first->nextPtr; delete tempPtr; } template T LinkedList ::getFirst() { return first->data; } template string LinkedList ::toString() const { LLNode * p; string s; p = first; while (p != NULL) //get element data { s = s + p->toString(); p = p->getNext(); if (p != NULL) s = s + " "; }//end while return s; } #endif // !LINKEDLIST_H Output
n1:9 9 n2:aString: aString 11:empty: 11:5: 5 11:8 5: 8 11:7 8 5: 7 Press any key to continue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts

