一个简单的链表 程序运行不出
#include<iostream.h>struct data
{
int value;
data *next;
};
class Memory
{
public:
data *head;
Memory(){head = head->next = NULL;}
data* mallo(int n);
void fre(data *h);
void print(data *h,int n);
};
data* Memory::mallo (int n) //实现malloc函数
{
int i = 0;
data *t = NULL;
if(n<0)cout<<"error"<<endl;
while(i<n)
{
if(head==NULL)
{
head = new data;
t = head;
head->value = i;
i++;
}
else
{
head->next = new data;
head = head->next;
head->value = i;
i++;
}
}
head->next = NULL;
return t;
}
void Memory::print(data *h,int n)
{
int i;
for(i = 0;i < n-1;i++)
{
h = h->next ;
}
cout<<h->value<<endl;
}
void Memory::fre (data *h)
{
data *t = h;
while(h)
{
t = h->next ;
delete h;
h = t ;
}
}
int main()
{
Memory a;
data *t;
t = a.mallo (5);
a.print(t,4);
return 0;
}