Need help fixing the second line. I compile it, and it works fine, but the number is squished together below the first row #include "nodes_LLoLL.h" #include "cnPtrQueue.h" #include using namespace std; namespace CS3358_SP2023_A5P2 { // do breadth-first (level) traversal and print data void ShowAll_BF(PNode* pListHead, ostream& outs) { cnPtrQueue q; CNode *cursor = 0; while (pListHead != 0) { if (pListHead->data != 0) { q.push(pListHead->data); } pListHead = pListHead->link; } while (!q.empty()) { cursor = q.front(); q.pop(); outs << cursor->data; if (cursor->link != 0) { q.push(cursor->link); } } } void Destroy_cList(CNode*& cListHead) { int count = 0; CNode* cNodePtr = cListHead; while (cListHead != 0) { cListHead = cListHead->link; delete cNodePtr; cNodePtr = cListHead; ++count; } cout << "Dynamic memory for " << count << " CNodes freed" << endl; } void Destroy_pList(PNode*& pListHead) { int count = 0; PNode* pNodePtr = pListHead; while (pListHead != 0) { pListHead = pListHead->link; Destroy_cList(pNodePtr->data); delete pNodePtr; pNodePtr = pListHead; ++count; } cout << "Dynamic memory for " << count << " PNodes freed" << endl; } // do depth-first traversal and print data void ShowAll_DF(PNode* pListHead, ostream& outs) { while (pListHead != 0) { CNode* cListHead = pListHead->data; while (cListHead != 0) { outs << cListHead->data << " "; cListHead = cListHead->link; } pListHead = pListHead->link; } } } #include "cnPtrQueue.h" #include using namespace std; namespace CS3358_SP2023_A5P2 { cnPtrQueue::cnPtrQueue() { numItems = 0; } bool cnPtrQueue::empty() const { return outStack.empty() && inStack.empty(); } cnPtrQueue::size_type cnPtrQueue::size() const { return numItems; } CNode *cnPtrQueue::front() { assert(!inStack.empty() || !outStack.empty()); if (outStack.empty()) { while (!inStack.empty()) { outStack.push(inStack.top()); inStack.pop(); } } return outStack.top(); } void cnPtrQueue::push(CNode *cnPtr) { inStack.push(cnPtr); numItems++; } void cnPtrQueue::pop() { assert(!inStack.empty() || !outStack.empty()); if (outStack.empty()) { while (!inStack.empty()) { outStack.push(inStack.top()); inStack.pop(); } } outStack.pop(); numItems--; } } #include "nodes_LLoLL.h" #include using namespace std; using namespace CS3358_SP2023_A5P2; void Build_cList(int argc, CNode*& cListHead); void Build_pList(int argc, PNode*& pListHead); int main(int argc, char* argv[]) { PNode* head_LLoLL = 0; char reply; do { Build_pList(argc, head_LLoLL); ShowAll_DF(head_LLoLL, cout); cout << endl; ShowAll_BF(head_LLoLL, cout); cout << endl; Destroy_pList(head_LLoLL); if (argc < 2) cout << "Another? (n = no, others = yes) "; cin >> reply; } while (reply != 'n' && reply != 'N'); return EXIT_SUCCESS; } void Build_cList(int argc, CNode*& cListHead) { int oneInt; char reply; CNode* cListTail = cListHead; if (argc < 2) cout << "Add cList data? (n = no, others = yes) "; cin >> reply; while (reply != 'n' && reply != 'N') { if (argc < 2) cout << "Enter cList data (int): "; cin >> oneInt; CNode* cNodePtr = new CNode; cNodePtr->data = oneInt; cNodePtr->link = 0; if (cListTail == 0) cListHead = cListTail = cNodePtr; else { cListTail->link = cNodePtr; cListTail = cNodePtr; } if (argc < 2) cout << "Add more cList data? (n = no, others = yes) "; cin >> reply; } } void Build_pList(int argc, PNode*& pListHead) { char reply; PNode* pListTail = pListHead; if (argc < 2) cout << "Add cList to pList? (n = no, others = yes) "; cin >> reply; while (reply != 'n' && reply != 'N') { CNode* cListHead = 0; Build_cList(argc, cListHead); PNode* pNodePtr = new PNode; pNodePtr->data = cListHead; pNodePtr->link = 0; if (pListTail == 0) pListHead = pListTail = pNodePtr; else { pListTail->link = pNodePtr; pListTail = pNodePtr; } if (argc < 2) cout << "Add more cList to pList? (n = no, others = yes) "; cin >> reply; } }
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
Need help fixing the second line. I compile it, and it works fine, but the number is squished together below the first row
#include "nodes_LLoLL.h"
#include "cnPtrQueue.h"
#include <iostream>
using namespace std;
namespace CS3358_SP2023_A5P2
{
// do breadth-first (level) traversal and print data
void ShowAll_BF(PNode* pListHead, ostream& outs)
{
cnPtrQueue q;
CNode *cursor = 0;
while (pListHead != 0) {
if (pListHead->data != 0) {
q.push(pListHead->data);
}
pListHead = pListHead->link;
}
while (!q.empty()) {
cursor = q.front();
q.pop();
outs << cursor->data;
if (cursor->link != 0) {
q.push(cursor->link);
}
}
}
void Destroy_cList(CNode*& cListHead)
{
int count = 0;
CNode* cNodePtr = cListHead;
while (cListHead != 0)
{
cListHead = cListHead->link;
delete cNodePtr;
cNodePtr = cListHead;
++count;
}
cout << "Dynamic memory for " << count << " CNodes freed"
<< endl;
}
void Destroy_pList(PNode*& pListHead)
{
int count = 0;
PNode* pNodePtr = pListHead;
while (pListHead != 0)
{
pListHead = pListHead->link;
Destroy_cList(pNodePtr->data);
delete pNodePtr;
pNodePtr = pListHead;
++count;
}
cout << "Dynamic memory for " << count << " PNodes freed"
<< endl;
}
// do depth-first traversal and print data
void ShowAll_DF(PNode* pListHead, ostream& outs)
{
while (pListHead != 0)
{
CNode* cListHead = pListHead->data;
while (cListHead != 0)
{
outs << cListHead->data << " ";
cListHead = cListHead->link;
}
pListHead = pListHead->link;
}
}
}
#include "cnPtrQueue.h"
#include <cassert>
using namespace std;
namespace CS3358_SP2023_A5P2 {
cnPtrQueue::cnPtrQueue()
{
numItems = 0;
}
bool cnPtrQueue::empty() const { return outStack.empty() && inStack.empty(); }
cnPtrQueue::size_type cnPtrQueue::size() const { return numItems; }
CNode *cnPtrQueue::front() {
assert(!inStack.empty() || !outStack.empty());
if (outStack.empty()) {
while (!inStack.empty()) {
outStack.push(inStack.top());
inStack.pop();
}
}
return outStack.top();
}
void cnPtrQueue::push(CNode *cnPtr) {
inStack.push(cnPtr);
numItems++;
}
void cnPtrQueue::pop() {
assert(!inStack.empty() || !outStack.empty());
if (outStack.empty()) {
while (!inStack.empty()) {
outStack.push(inStack.top());
inStack.pop();
}
}
outStack.pop();
numItems--;
}
}
#include "nodes_LLoLL.h"
#include <cstdlib>
using namespace std;
using namespace CS3358_SP2023_A5P2;
void Build_cList(int argc, CNode*& cListHead);
void Build_pList(int argc, PNode*& pListHead);
int main(int argc, char* argv[])
{
PNode* head_LLoLL = 0;
char reply;
do
{
Build_pList(argc, head_LLoLL);
ShowAll_DF(head_LLoLL, cout);
cout << endl;
ShowAll_BF(head_LLoLL, cout);
cout << endl;
Destroy_pList(head_LLoLL);
if (argc < 2)
cout << "Another? (n = no, others = yes) ";
cin >> reply;
}
while (reply != 'n' && reply != 'N');
return EXIT_SUCCESS;
}
void Build_cList(int argc, CNode*& cListHead)
{
int oneInt;
char reply;
CNode* cListTail = cListHead;
if (argc < 2)
cout << "Add cList data? (n = no, others = yes) ";
cin >> reply;
while (reply != 'n' && reply != 'N')
{
if (argc < 2)
cout << "Enter cList data (int): ";
cin >> oneInt;
CNode* cNodePtr = new CNode;
cNodePtr->data = oneInt;
cNodePtr->link = 0;
if (cListTail == 0)
cListHead = cListTail = cNodePtr;
else
{
cListTail->link = cNodePtr;
cListTail = cNodePtr;
}
if (argc < 2)
cout << "Add more cList data? (n = no, others = yes) ";
cin >> reply;
}
}
void Build_pList(int argc, PNode*& pListHead)
{
char reply;
PNode* pListTail = pListHead;
if (argc < 2)
cout << "Add cList to pList? (n = no, others = yes) ";
cin >> reply;
while (reply != 'n' && reply != 'N')
{
CNode* cListHead = 0;
Build_cList(argc, cListHead);
PNode* pNodePtr = new PNode;
pNodePtr->data = cListHead;
pNodePtr->link = 0;
if (pListTail == 0)
pListHead = pListTail = pNodePtr;
else
{
pListTail->link = pNodePtr;
pListTail = pNodePtr;
}
if (argc < 2)
cout << "Add more cList to pList? (n = no, others = yes) ";
cin >> reply;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps