焚琴煮鹤 发表于 2015-6-9 22:43:38

链表可以没有名字?

RT,程序来自鱼哥的教学视频,按我的理解,链表在内存中就是一段数据,在这个程序中没有用到每个结构体的结构体名,而是用指针交叉地定义赋值,最后也是用的指针来读取的,我理解是指针就是一个地址数据,就是说可以靠对地址的读取来定义和读取链表?
另外,链表其实就是一种结构体?我的理解不错吧?
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define LEN sizeof(struct student)                //student结构的大小

struct student
{
        int num;
        float score;
        struct student *next;
};

struct student *creatic();                        //创建链表
void print(struct student *head);           //打印链表, 可以为 void print()

int n;

int main()
{
        struct student *stu;

        stu = creatic();
        print(stu);

        printf("\n\n");
        system("pause");

        return 0;
}

struct student *creatic()
{
        struct student *head;
        struct student *p1, *p2;

        p1 = p2 = (struct student *)malloc(LEN);                //LEN是student结构的大小

        printf("Please enter the num:        ");
        scanf("%d", &p1->num);
        printf("Please enter the score:        ");
        scanf("%f", &p1->score);

        head = NULL;
        n = 0;

        while(p1->num)                                //即:while(p1-> num != 0)
        {
                n++;
                if(1 == n)
                {
                        head = p1;
                }
                else
                {
                        p2->next = p1;
                }

                p2 = p1;
                p1 = (struct student *)malloc(LEN);

                printf("\nPlease enter the num:        ");
                scanf("%d", &p1->num);
                if(!p1->num)
      {
            break;
      }
                printf("Please enter the score:        ");
                scanf("%f", &p1->score);
        }

        p2->next = NULL;

        return head;
}

void print(struct student *head)
{
        struct student *p;
        printf("\nThere are %d records!\n\n", n);

        p = head;
        if(head)                                // NULL != head;
        {
                do
                {
                        printf("The student whose number is %d score is :%f\n", p->num, p->score);
                        p = p->next;
                }while(p);                        //NULL != p;
        }
}

w120699 发表于 2015-6-10 11:27:36

我只是来打酱油的。。。

焚琴煮鹤 发表于 2015-6-10 16:43:05

w120699 发表于 2015-6-10 11:27
我只是来打酱油的。。。

请,附送一罐老干妈。八百年,老干妈,好品质

~风介~ 发表于 2015-6-11 21:40:49

虽然代码和中文我都看得懂,但是不理解你要表达什么?{:7_140:}
建议你去百度“函数指针和指针函数”的区别~

焚琴煮鹤 发表于 2015-6-12 19:55:48

~风介~ 发表于 2015-6-11 21:40
虽然代码和中文我都看得懂,但是不理解你要表达什么?
建议你去百度“函数指针和指针函数”的区 ...

谢谢,不过函数指针和指针函数这个我还是能理解的,但我想问的是链表,就我的理解其实就是分配了一段存储空间然后确定好指向该段存储空间的指针,这样,只要读取这个指针就能够指向这个存储空间而不必为每个存储空间命名。不知我这样想是否正确。谁能召唤一下鱼哥,毕竟我是从他的教程视频上学的链表。

~风介~ 发表于 2015-6-12 20:02:58

焚琴煮鹤 发表于 2015-6-12 19:55
谢谢,不过函数指针和指针函数这个我还是能理解的,但我想问的是链表,就我的理解其实就是分配了一段存储 ...

{:7_140:}@小甲鱼

焚琴煮鹤 发表于 2015-6-14 11:00:09

@小甲鱼

怡静 发表于 2015-6-15 16:55:37

小甲鱼云游去了,不知什么时才回来!

焚琴煮鹤 发表于 2015-6-15 21:05:53

怡静 发表于 2015-6-15 16:55
小甲鱼云游去了,不知什么时才回来!

鱼哥是汕头的吗?我也是汕头的,如果可以很想见见他呢

仰望天上的光 发表于 2015-6-19 14:11:04

你的理解没错。其实任何变量都是保存在内存中的,都可以通过地址来访问它。

对于有名字的变量,是编译器在管理它的生存周期,所以编译器可以将a,b,c这些变量名在编译时转换为对应的地址。

对于无名变量,就像你这里的链表malloc后,天晓得你什么时候free它,这时,编译器就不可能再管理它的生命周期,就要靠你来管理它的生命周期(就是说你应该在合适的时候free它)。所以编译器就没有必要给这类变量起名字。。。所以你一般就只能自己通过地址来访问它们。

焚琴煮鹤 发表于 2015-6-19 20:03:45

仰望天上的光 发表于 2015-6-19 14:11
你的理解没错。其实任何变量都是保存在内存中的,都可以通过地址来访问它。

对于有名字的变量,是编译器 ...

谢谢,解释的很清晰
页: [1]
查看完整版本: 链表可以没有名字?