6.3w+
社区成员
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
typedef struct node
{
char name[20];
struct node *link;
}stud;
stud *create(int n)
{
stud *p,*h,*s;
h=(stud*)malloc(sizeof(stud));
//h->name[0]='\0';
h->link=NULL;
p=h;
for (int i=0;i<n;i++)
{
s=(stud*)malloc(sizeof(stud));
cout<<"Please input student's name:"<<endl;
cin>>s->name;
p->link=s;
p=s;
}
p->name[0]='\0';
return h;
}
bool output(stud *head)
{
stud *p;
p=head->link;
while(1)
{
if('\0'==p->name[0])
break;
cout<<p->name<<endl;
p=p->link;
}
return 1;
}
void main()
{
int number;
cout<<"Please input student's number:"<<endl;
cin>>number;
stud *head=create(number);
output(head);
}
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
typedef struct node
{
char name[20];
struct node *link;
}stud;
class Link
{
public:
Link(int n);
~Link();
void output();
private:
stud *head;
};
Link::Link(int n)
{
head = (stud*)malloc(sizeof(stud));
head->link = NULL;
stud *s,*p;
p = head;
for(int i=0; i<n; i++)
{
s = (stud*)malloc(sizeof(stud));
cout<<"Please input student's name:"<<endl;
cin>>s->name;
p->link = s;
p = s;
}
p->link = NULL;
}
Link::~Link()
{
stud *p;
while(head != NULL)
{
p = head;
head = head->link;
free(p);
}
}
void Link:: output()
{
stud *p;
p = head->link;
while(p != NULL)
{
cout<<p->name<<endl;
p = p->link;
}
}
void main()
{
int number;
cout<<"Please input student's number:"<<endl;
cin>>number;
Link students(number);
students.output();
}
void strdestroy(stud *head)
{
stud *p,*s;
p=head;
s=p;
while(p)
{
p=p->link;
free(s);
s=p;
}
}
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
typedef struct node
{
char name[20];
struct node *link;
}stud;
stud *create(int n)
{
stud *p,*h,*s;
h=(stud*)malloc(sizeof(stud));
//h->name[0]='\0';
h->link=NULL;
p=h;
for (int i=0;i<n;i++)
{
s=(stud*)malloc(sizeof(stud));
cout<<"Please input student's name:"<<endl;
cin>>s->name;
p->link=s;
p=s;
}
p->link = 0;
return h;
}
bool output(stud *head)
{
stud *p;
p=head->link;
while(p)
{
cout<<p->name<<endl;
p=p->link;
}
return 1;
}
void main()
{
int number;
cout<<"Please input student's number:"<<endl;
cin>>number;
stud *head=create(number);
output(head);
}