T1 //编写一个函数 creat , 用来建立一个动态链表。所谓建立动态链表是指在程序执行过程中从无到有地建立起一个链表,即一个一个地开辟结点和输入个结点数据,并建立起前后相连的关系。各结点的数据由键盘输入。
#include <iostream>
using namespace std;
typedef struct Node
{
int data;
struct Node *next;
}Node;
int main()
{
Node *head, *tail;
int n, data;
head = new Node;
Node *p;
cout << "请输入链表的长度:\n";
cin >> n;
cout << "\n请依次输入链表各结点的值:\n";
for (int i = 0; i < n; i ++)
{
p = new Node;
cin >> data;
p -> data = data;
tail -> next = p;
tail = p;
}
tail -> next = NULL;
system ("pause");
return 0;
}