#include <iostream> using namespace std; struct Node { int data; Node *next; }; Node *createList (Node *head) { struct Node *p, *s; int ch; cout << "Input a number,then press Enter. press Ctrl + Z to end input:" <<endl; while ((cin >> ch) && ch != EOF) { s = new Node; s->data = ch; if (head == NULL) { head = s; p = s; } else p->next = s; s -> next = NULL; p = s; } cin.clear(); //一次创建完成后,须清空输入缓冲区.这样做之后,多次调用该函数,创建多个链表时才不会出错 return head; } void showList (Node *head) { do { cout << head->data << '/n'; head = head->next; }while (head != NULL); } int main() { Node *head = NULL; head = createList(head); showList(head); return 0; }