How do I get rid of this 0 in the output? I think the problem is with the function called printReceipt.
#include <iostream>
using namespace std;
struct Node
{
string name;
int price;
Node *link;
};
// Insert Node with value new_name at the end of the linked list
// whose first node is head_ref
void appendName(Node** head_ref, string new_name)
{
Node* newNode = new Node();
Node *last = *head_ref;
newNode->name = new_name;
newNode->link = NULL;
if (*head_ref == NULL)
{
*head_ref = newNode;
return;
}
while (last->link != NULL)
{
last = last->link;
}
last->link = newNode;
return;
}
// Insert Node with price new_price at the end of the linked list
// whose first node is head_ref
void appendPrice(Node** head_ref, int new_price)
{
Node* newNode = new Node();
Node *last = *head_ref;
newNode->price = new_price;
newNode->link = NULL;
if (*head_ref == NULL)
{
*head_ref = newNode;
return;
}
while (last->link != NULL)
{
last = last->link;
}
last->link = newNode;
return;
}
// Print the linked list
void printReceipt(Node *node)
{
while (node != NULL)
{
cout<<" "<<node->name<<" "<<node->price;
node = node->link;
}
}
int main()
{
Node* r1 = NULL;
appendName(&r1, "Item#1");
appendPrice(&r1, 23);
cout<<"Receipt\n";
printReceipt(r1);
return 0;
}
Program output:
Receipt
Item#1 0 23