• ---------第五周--------总结


    #include "stdafx.h"
    #include <stdio.h>
    #include <stdio.h>
    #include <malloc.h>
    /***********单个结构体练习*************/
    //1. 定义一个日期结构体,包含年与月成员
    struct Date 
    {
        int year,month;
    };
    /**********结构体嵌套练习**************/
    //2.定义一个学生结构体,包含学号与出生日期
    struct student{
        int num;
        struct Date birth;
    
    };
    /**********结构体中嵌套结构指针练习**************/
    //1.用typedef语句定义一个学生结构体,包含学号与出生日期
    typedef struct stu2 
    {
        int num;
        struct Date *birth;    //(要求出生日期为日期结构体指针变量)        
    }s;
    
    
    
    void main()
    {/*********************************************/
        printf("** *** 第一题 
    ");
        struct Date date,*pdate;//2. 在main主函数内设置日期结构体变量与指针变量
        //3. 对结构变量中的成员赋值并输出
        date.year=1998;date.month=6;
        printf("变量输出 %d年%d月
    ",date.year,date.month);
    
        pdate=&date;//指针初始化
        pdate->year=1999;pdate->month=12;
        printf("指针变量输出 %d年%d月
    ",pdate->year,pdate->month);
    
    /*********************************************/
    
    
        
    
    /*********************************************/
        printf("** *** 第二题 
    ");
        struct student str,*ps; //2. 在main主函数内设置学生结构体变量与指针变量
         //3. 对结构变量中的成员赋值并输出
        str.num=12346; str.birth.year=1846; str.birth.month=3;
        printf("变量输出 %d学号%d年%d月
    ",str.num,str.birth.year,str.birth.month);
        ps=&str;
        ps->num=12345;ps->birth.year=1845;ps->birth.month=6;
        printf("指针变量输出 %d学号%d年%d月
    ",    ps->num,ps->birth.year,ps->birth.month);
    /*********************************************/
    
    
        
    /*********************************************/
    //3. 对结构变量中的成员赋值并输出
        printf("** *** 第三题 
    ");
        s stu,*s1;    //2. 在main主函数内设置学生结构体变量与指针变量
        stu.birth=(struct Date *)malloc(sizeof(struct Date));
        stu.num=456;stu.birth->year=2013;stu.birth->month=4;
        printf("变量输出 %d学号%d年%d月
    ",stu.num, stu.birth->year,stu.birth->month);
    
        s1=&stu;
        s1->num=789;s1->birth->year=2011;s1->birth->month=2;
        printf("指针变量输出 %d学号%d年%d月
    ",    s1->num=789,s1->birth->year,s1->birth->month);
    
        /*********************************************/
    
    }

    在这周里 我了解到指针是要先初始化,不然就会出错。然后呢,指针结构体变量要用-> ,而结构体要用.  。以前一直没有分清楚,很感谢老师帮我们再一次的复习,让我温故而新。还有因为stu.birth=(struct Date *)malloc(sizeof(struct Date))实现不了,上网搜索了下要增加头文件#include <malloc.h>就运行出来了,不过下一道题又不需要增加头文件了,不懂(⊙_⊙)?

    #include "stdafx.h"
    #include"stdio.h"
    #include"stdlib.h"
    /***********循环双链表练习************/
    //1. 按照课本中双链表的定义,定义其节点结构体类型(数据域与两个指针域).
    typedef int Datatype;
    typedef struct Table
    {
        Datatype data;
        Table *prior;
        Table *next;
    }Table,*LinkList;
    
    
    int main(int argc, char* argv[])
    {
        printf("** *** 循环双链表练习");
        //2.在main函数中设置带头节点双链表指针 
        LinkList L2,S2;
        L2=(LinkList)malloc(sizeof(Table));//指针初始化
        L2->next=L2;
        L2->prior=L2;
    
        //    3 在此双链表的链表头分别添两个数据。(参考双链表课件的15页)
    /*    //(1)
        S2=(LinkList)malloc(sizeof(Table));
        S2->data=1997;//设置节点数据
        S2->prior=L2;//在链表头插入
        L2->next=S2; 
        S2->next=L2;
        //(2)
           S2=(LinkList)malloc(sizeof(Table));
        S2->data=1998;
        S2->prior=L2;
        L2->next=S2; 
        S2->next=L2;*/
        //(1)
        S2=(LinkList)malloc(sizeof(Table));
        S2->data=1997;//设置节点数据
        S2->prior=L2;//在链表头插入
        S2->next=L2->next;
        L2->next->prior=S2;
        L2->next=S2;
        
        //(2)
           S2=(LinkList)malloc(sizeof(Table));
        S2->data=1998;
        S2->prior=L2;//在链表头插入
        S2->next=L2->next;
        L2->next->prior=S2;
        L2->next=S2;
        
    printf("
    ");
    //4正向输出此双链表数据
        S2=L2->next;
        printf("正向输出 第一节点数据=%d",S2->data);
        S2=S2->next;
        printf(" 第二节点数据=%d
    ",S2->data);
    //反向输出此双链表数据
        S2=L2->prior;
        printf("反向输出 第一节点数据=%d",S2->data);
        S2=S2->prior;
        printf("第二节点数据=%d
    ",S2->data);
    
        return 0;
    }
  • 相关阅读:
    Tree MapByFold
    Tree DepthByFold
    Tree SizeByFold
    Tree MaximumByFold
    Tree Fold
    Tree Map
    Tree Depth
    Tree Maximum
    Tree Size
    Tree
  • 原文地址:https://www.cnblogs.com/chiroudazuozhan/p/5339366.html
Copyright © 2020-2023  润新知