123
Enter numbers ending with -999 45 62 34 98 123 -999
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
#include<iostream> #include<list> #include<cassert> using namespace std; struct Node { int data; Node *link; }; typedef Node *NodePtr, *head, *tmp; class linkedList { public: void head_insert(NodePtr& head, int the_number); //Precondition: The pointer variable head points to //the head of a linked list. //Postcondition: A new node containing the_number //has been added at the head of the linked list. bool empty() const; void print(); }; void linkedList::head_insert(NodePtr& head, int the_number) { NodePtr temp_ptr; temp_ptr = new Node; temp_ptr->data = the_number; temp_ptr->link = head; head = temp_ptr; } //void linkedList::print() //{ //Display the nodes //NodePtr tmp; // tmp = head; // while(tmp) // { //cout << tmp->data << " " <<endl; // tmp = tmp->link; //} //} //Function Definition int main() { linkedList list; NodePtr head, tmp, first; int num; cout<<"Enter numbers ending with -999\n"; cin >> num; while(num != -999) { list.head_insert(head, num); } cin >> num; //Display the nodes //NodePtr tmp; tmp = head; while(tmp) { cout << tmp->data << " " <<endl; tmp = tmp->link; } return 0; }
Enter numbers ending with -999 45 78 98 12 34 1732 -999 1732 34 12 98 78 45
head
NodePtr head = nullptr, tmp, first;