struct Node {
int data;
Node*next;
};
Node *createList(Node**root)
{
Node*head = (Node*)calloc(1, sizeof(Node));
*root = head;
head->data = -1;
head->next = NULL;
int n;
while(cin >> n)
{
if (n == -1) break;
Node*pNew = (Node*)calloc(1, sizeof(Node));
pNew->data = n;
pNew->next = NULL;
head->next = pNew;
head = pNew;
}
return *root;
}
void freeList(Node*root)
{
if (root == NULL) return;
Node*p = NULL;
while (p=root->next)
{
free(root);
root = p;
}
free(root);
root = NULL;
}
void myprint(Node*root)
{
while (root->next != NULL)
{
cout << root->data << " ";
root = root->next;
}
cout << root->data << " ";
}
int main()
{
Node *root = NULL;
createList(&root);
myprint(root);
freeList(root);
return 0;
}