zoukankan      html  css  js  c++  java
  • 结构体与函数,链表的定义

    结构体与函数,链表的定义 
    【实验二】结构体与动态数据结构(结构体与函数,链表的定义)  【要求】  
     
    掌握结构体指针作为函数参数的用法。 
     掌握链表结点定义,会用结点连接成链表,并输出链表内容。 
    结构体指针作为函数参数 
    定义 CARD 结构体,分别编写函数实现 CARD 的输入和打印,然后在主程序中调用。  
    (1)定义 CARD 结构:  // CARD 结构定义 struct CARD { 
        char suit[10];     char face[10]; }; 
    (2)编写函数输入一个 CARD 结构体变量:  
    // 输入一张扑克牌 
    void InputCard ( struct CARD *pCard ) { 
        // TODO: 分别输入 pCard->suit 和 pCard->face } 
    (3)编写函数打印一个 CARD 结构体变量。  
    // 打印一张扑克牌 
    void PrintCard ( struct CARD *pCard ) { 
        // TODO: 分别打印 pCard->suit 和 pCard->face } 
    (4)在主函数中调用:  int main() { 
        struct CARD card; 


    var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

      
        InputCard( &card );     PrintCard( &card );   
        return 0; } 
    链表的定义 
    用结点连接成链表,并输出链表中的数据。  
     
    (1)首先定义链表结点,其数据域存放一个整数。 
    struct Link { 
        int data;  // 数据域 
        struct Link *next;  // 指向下一个结点的指针域 }; 
     
    (2)然后,分别定义 5 个结点,依次存放 1、2、3、4、5。 
    int main() { 
        // 定义 5 个结点 
        struct Link n1, n2, n3, n4, n5;   
        // 依次存放 1、2、3、4、5     n1.data = 1;     n2.data = 2;     n3.data = 3;     ...   
        return 0; } 
     
    (3)给每个结点的指针域适当赋值,使其构成一个链表,且链表中的数据依次为 1、2、3、4、5。 
        // 连成一个链表     n1.next = &n2;     n2.next = &n3;     ... 
        n5.next = NULL; 


    var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);

     
    (4)定义一个指针,使其指向链表中的第一个结点,作为链表的头指针。 
        // 定义 5 个结点 
        struct Link n1, n2, n3, n4, n5;     // 定义头指针 
        struct Link *head = &n1; 
     
    (5)定义一个指针,从链表头指针开始,循环遍历整个链表,并输出其中的数据。 
        // 定义 5 个结点 
        struct Link n1, n2, n3, n4, n5;     // 定义头指针 
        struct Link *head = &n1;     // 循环遍历链表用的指针     struct Link *p;   
        // 依次存放 1、2、3、4、5     ...   
        // 连成一个链表     ...   
        // 循环遍历整个链表 
        p = head;  //指针指向第一个结点     while ( p != NULL )     { 
            printf ( "%4d", p->data ); // 输出结点中的数据         p = p->next; // 使指针指向下一个结点     }

  • 相关阅读:
    Single Number II
    Pascal's Triangle
    Remove Duplicates from Sorted Array
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Unique Paths
    Sort Colors
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Climbing Stairs
  • 原文地址:https://www.cnblogs.com/ransn/p/5132574.html
Copyright © 2011-2022 走看看