|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
struct test{
int data;
struct test *next;
};
void Creatlist(struct test *head, int a[], int n);
void Printelem(struct test *head);
void Free(struct test *head);
int main()
{
struct test *head;
int i = 0,j=1;
int a[40];
for(i;i<40;i++,j+=3)
{
a[i]=j;
}
Creatlist(head,a,10);
Printelem(head);
Free(head);
}
void Creatlist(struct test *head, int a[], int n)
{
struct test *p,*s;
int i;
head = (struct test*)malloc(sizeof(struct test));
head->next = NULL;
head->data = n;
p = head;
for(i=0;i<n;i++){
s = (struct test*)malloc(sizeof(struct test));
s->data = a[i];
p->next = s;
p = p->next;
printf("%d\n",p->data);
}
p->next = NULL;
printf("创建完成\n");
}
void Printelem(struct test *head)
{
struct test *p = head;
int j = 1;
while(p!=NULL){
printf("%d\t%d\n", j, p->data);
p = p->next;
j++;
}
printf("输出完成;\n");
}
void Free(struct test *head)
{
struct test *p = NULL, *ps;
ps = head;
while(ps){
p = ps;
ps = head->next;
free(p);
}
printf("释放完成\n");
} |
|