285571052 发表于 2011-8-18 18:58:59

自己写的一个链表,望大牛来指点

本帖最后由 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--;
}
}

prince2008csfk 发表于 2011-8-18 22:44:37

好啊 谢谢了呵呵

285571052 发表于 2011-8-19 10:37:52

丫的,忘记写删除那块了

□_谁_□_枫_□ 发表于 2013-4-1 11:33:54

好啊 谢谢了呵呵

为梦而生 发表于 2013-4-1 12:11:26

我也在学习,也是到这了
都是新人顶你下

Kitty喜欢小鱼干 发表于 2018-8-17 12:19:44

来学习学习。
页: [1]
查看完整版本: 自己写的一个链表,望大牛来指点