Populate a linked list

struct nodeType { int data; nodeType *link; }; int main() { // // Init the list // nodeType *first; nodeType *last; nodeType *newNode; int num = 3; first = NULL; last = NULL; // // Build the list the forward approach // for (int i=0; i<3; i++) { newNode = new nodeType; // Create the new node newNode->data = num; // and assign its data value newNode->link = NULL; // make its link point to nothing if (first == NULL) // If there is nothing in the list, then make the { // newNode the first item and the last item first = newNode; last = newNode; } else // Else if first already has a value { // make the last item link to the newNode last->link = newNode; // and make newNode the last item last = newNode; } } cout << first->data; cout << last->data; return(0); }

1 Response

Yo

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.