// Linked List 0 - Using "struct" #include // Provides size_t #include #include struct Node { typedef double Item; Item data; Node *link; }; size_t list_length(Node* head_ptr) { Node *cursor; size_t answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link) answer++; return answer; } void list_display(Node* head_ptr) { Node *cursor; cout << "( "; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link) cout << cursor->data << " "; cout << "), " << "Size = " << list_length(head_ptr) << endl << endl; } void list_head_insert(Node*& head_ptr, const Node::Item& entry) { Node *insert_ptr = new Node; insert_ptr->data = entry; insert_ptr->link = head_ptr; head_ptr = insert_ptr; } void list_append(Node*& head_ptr, const Node::Item& entry) // insert to the rear { Node *cursor, *append_ptr; for (cursor = head_ptr; cursor->link != NULL; cursor = cursor->link); append_ptr = new Node; append_ptr->data = entry; append_ptr->link = NULL; cursor->link = append_ptr; } void main() { Node *head = NULL; // NULL from stdlib.h Node::Item in; cout << "Enter numbers to create a linked list (-1 at the end): "; cin >> in; while (in > 0.0) { list_head_insert(head, in); cin >> in; } list_display(head); cout << "Enter a number to be inserted at the head of the linked list: "; cin >> in; list_head_insert(head, in); list_display(head); cout << "Enter a number to be appended to the rear of the linked list: "; cin >> in; list_append(head, in); list_display(head); }