zoukankan      html  css  js  c++  java
  • 创建一个链表实例

    <span style="font-family:KaiTi_GB2312;font-size:18px;">#include <stdio.h>
    #include <stdlib.h>             /* 提供malloc()原型 */
    #include <string.h>             //提供strcpy原型
    
    #define TSIZE  45
    struct film
    {
        char title[TSIZE];
        int rating;
        struct film * next;             //指向链表的下一个结构
    };
    
    int main(void)
    {
        struct film * head = NULL;
        struct film * prev, * current;
        char input[TSIZE];
    
    //收集并存储信息
        puts("Enter first movie title:");
        while(gets(input) != NULL && input[0] != '')
        {
            current = (struct film *)malloc(sizeof(struct film));
            if(head == NULL)
            head = current;
            else
            prev->next = current;
    
            strcpy(current->title,input);
            puts("enter your rating<0-10>:");
            scanf("%d",¤t->rating);
    
            while(getchar()!='
    ')
                continue;
            puts("Enter next movie title(empty line to stop):");
            prev = current;
        }
    
        if(head == NULL)
        {
            printf("no data entered!");
        }
        else
        printf("Here is the movie list:
    ");
    
        current = head;
    
        while(current != NULL)
        {
             printf("Movie :%s Rating : %d
    ",current->title,current->rating);
            current = current->next;
        }
    
        printf("Bye!!
    ");
    
        return 0;
    
    
    }
    </span>
    <span style="font-family:KaiTi_GB2312;font-size:18px;">
    </span>

    创建链表包含三个步骤:

    1、使用malloc()函数为一个结构分配足够的空间

    2、存储这个结构的地址

    3、把正确的信息拷贝到这个结构中


  • 相关阅读:
    DIV3E 基环树
    Codeforces Round #663 (Div. 2) D.505
    统计2进制中1的数量
    bitset 用法笔记
    扩展欧几里得
    KM算法(二分图最大权匹配)
    C1. Errich-Tac-Toe (Easy Version) 米奇妙妙屋
    求逆元
    python——标识符及其命名规则
    python基础——python对象概述
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6971751.html
Copyright © 2011-2022 走看看