马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 285571052 于 2011-8-18 19:04 编辑
我自己写的一个链表。
不足之处,还有画蛇添足之处望大牛们指出!
代码如下:
设置成空格复制之后还是这样,将就下吧!#include <stdio.h>
#include <malloc.h>
int n=0;//记录个数,后面打印的时候使用
struct student
{
int num;
struct student *next;
};
void main()
{
void print(struct student *p);//打印出链表
struct student *cr();//插入结点
struct student *head;//不解释这个了
head = cr();
print(head);
}
struct student *cr()
{
struct student *a,*b,*head;
while (1)
{
if (n==0)
{
head = a = malloc(sizeof(struct student));
scanf("%d",&a->num);
}
else
{ b = malloc(sizeof(struct student));
scanf("%d",&b->num);
a->next = b;
a = a->next ;
}
if (a->num ==-1)//当输入1的时候,跳出循环,
{
break;
}
n++;
a->next = NULL;
}
return head;
}
void print(struct student *p)
{
while (n)
{
printf("%d\n",p->num);
p = p->next;
n--;
}
}
|