Question

Submit a source.cpp file with the following: struct node { int data; node* next; } a...

Submit a source.cpp file with the following:

  • struct node
    {
    int data;
    node* next;
    }
  • a for loop that creates a singly linked list of nodes with the values 0-9 stored in them.
  • a for loop that prints off the data of the singly linked list, each one on its own line

******************************************************************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

// node structure 
struct NODE
{
    int data;
    NODE *next;
};

int main() {
  int i;
  int j = 0;

  // creating pointers of NODE type
  NODE *start = NULL; // set start to null
  NODE *ptr;
  NODE *temp;

  // for loop to insert values in linked list
  for (i = 0; i < 10; i++) {
    ptr = new NODE; // create a new node
    ptr->data = i;  // put the value of i to data part of node ptr
    ptr->next = NULL; // set next part of node ptr to null

    // if start is null
    if(start == NULL) {
        start = ptr;  // start points to ptr
    }

    else {
      // temp points to start
      temp = start;

      // while next part not become null
      // which means while we not reach the last node
      while(temp->next != NULL) {

        // pointing to next node
        temp = temp->next;
      }
      
      // set temp next to point to new node created 
      // ptr is the node we created here so last node points to ptr
      temp->next = ptr;
    }
  }

  // for loop to display data stored in nodes
  // * next is a NODE type pointer which initially points to start or head of linked list
  // iterates until next become null
  // point to next node after ach iteration
  // and in each iteration display the data part in a single line
  for (NODE *next = start; next; next = next->next) {
    cout << next->data << endl;
  }   

  return 0;
}

For help please comment.

Thank You.

Add a comment
Know the answer?
Add Answer to:
Submit a source.cpp file with the following: struct node { int data; node* next; } a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT