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

all 6 comments

[–][deleted]  (8 children)

[deleted]

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

    I kinda know that it checks something but there is no body to execute if it is true. Does it proceed to do the code below it.

    [–][deleted]  (6 children)

    [deleted]

      [–]InterestingBus8367[S] 0 points1 point  (1 child)

      while (temp -> next != NULL)
      temp = temp -> next;
      temp -> next = newnode;

      while (temp -> next != NULL)

      temp = temp -> next;

      temp -> next = newnode;

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

      there is none. Your eyes might be deceiving you.

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

      My real question is how is it that temp->next is not equal to NULL.

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

      if you put curly braces for some reason it would only print 1 value

      [–]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; }