#include <iostream>
using namespace std;
struct node
{
public:
int data;
node *next;
};
node *createlist(int a[], int n)
{
node *h = NULL;
//node *r = NULL;
node *p = new node;
h = p;
for (int i = 0; i < n; i++)
{
p->data = a[i];
if (i == n - 1)
{
p->next = NULL;
break;
}
p->next= new node;
p = p->next;
}
return h;
}
void showlist(node *&p)
{
while(p!= NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void freelist(node *&p)
{
node *t = NULL;
while (NULL!=p)
{
t = p;
p = p->next;
delete t;
}
t = NULL;
//delete p;
p = NULL;
}
void main()
{
int a[4] = { 1, 2, 3, 4};
node *p=createlist(a, 4);
showlist(p);
//system("pause");
freelist(p);
showlist(p);
system("pause");
}
using namespace std;
struct node
{
public:
int data;
node *next;
};
node *createlist(int a[], int n)
{
node *h = NULL;
//node *r = NULL;
node *p = new node;
h = p;
for (int i = 0; i < n; i++)
{
p->data = a[i];
if (i == n - 1)
{
p->next = NULL;
break;
}
p->next= new node;
p = p->next;
}
return h;
}
void showlist(node *&p)
{
while(p!= NULL)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void freelist(node *&p)
{
node *t = NULL;
while (NULL!=p)
{
t = p;
p = p->next;
delete t;
}
t = NULL;
//delete p;
p = NULL;
}
void main()
{
int a[4] = { 1, 2, 3, 4};
node *p=createlist(a, 4);
showlist(p);
//system("pause");
freelist(p);
showlist(p);
system("pause");
}