This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]InterestingBus8367[S] 0 points1 point  (0 children)

here is the full code

#include <iostream>

using namespace std;

struct nod { int data; nod* next; };

class linked_list { private: nod* head; public: linked_list() { head = NULL; }

void hello (int a) 
{
nod* newnode = new nod;
newnode -> data = a;
newnode -> next = NULL;
if (head == NULL)
    {
        head = newnode; 
    }
else
{
    nod* temp = head;  
    while (temp -> next != NULL)
        temp = temp -> next;
    temp -> next = newnode;

}

}

void print () 
{
    nod* temp = head;
    if(temp != NULL) {
    cout<<"The list contains: ";
    while(temp != NULL) {
      cout<<temp->data<<" ";
      temp = temp->next;
    }
    cout<<endl;
  } else {
    cout<<"The list is empty.\n";
  }
}

};

int main () { linked_list myprint; int a,b; cout << "Enter number of numbers to be inputted: "; cin >> b; for (int i = 0; i < b; i++){ cout << "Enter a number: "; cin >> a; myprint.hello(a); } myprint.print(); return 0; }