zoukankan      html  css  js  c++  java
  • C

    结构数组

    #include <stdio.h>
    #include <stdlib.h>
    
    #define TSIZE 45
    #define FMAX 5
    
    struct film{
        char title[TSIZE];
        int rating ;
    };
    int main(int argc, const char * argv[]) {
        struct film movies[FMAX];       // 静态分配内存
        printf("%lu
    ",sizeof(movies));
        
        int  n = 5;
        struct film * movies2  = (struct film *) malloc(n * sizeof( struct film)); // 确定大小情况下,动态分配内存
        
        free(movies2);
        
        return 0;
    }
    
    

    链表

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define TSIZE 45
    
    struct film{
        char title[TSIZE];
        int rating ;
        struct film * next ; // 下一节点
    };
    void foreach(const struct film * fp);
    // 使用链表存储数据
    int main(int argc, const char * argv[]) {
        struct film * header = NULL ;
        struct film * prev , * current;
        
        current = (struct film *) malloc(sizeof(struct film));
        if (NULL == header) header = current ;
        
        strcpy(header -> title, "node0");
        header -> rating = 1;
        current = (struct film *) malloc(sizeof(struct film));
        header -> next = current ; // 指定下一个节点
        prev = header ;
        
        strcpy(current -> title, "node1");
        current -> rating = 2;
        current -> next = NULL;
        foreach(header);
        
        current = header ;  // 释放链表
        while (current != NULL) {
            free(current);
            current = current->next;
        }
        return 0;
    }
    
    // 遍历节点
    void foreach(const struct film * fp)
    {
        printf("film title: %s , rating: %d next: 
    ",fp->title , fp->rating);
        if (fp->next != NULL) {
            foreach(fp->next);
        }
    }
    

    链表实例

    list.h

    #ifndef list_h
    #define list_h
    
    
    #include <stdbool.h>
    
    #define TSIZE 50
    struct film{
        char title[TSIZE] ;
        int rating ;
    };
    
    typedef struct film Item;
    
    typedef struct node{
        Item item ;
        struct node * next ;
    } Node;     // 将结构封装为节点
    
    typedef  Node * List ; // 链表地址即为首节点地址
    
    // 初始化列表
    void InitializeList(List * plist);
    
    // 列表是否为空
    bool ListIsEmpty(const List * plist);
    
    // 列表是否已满
    bool ListIsFull(const List * plist);
    
    // 获取总数
    unsigned int ListItemCount(const List * plist);
    
    // 添加
    bool AddItem(Item item , List * plist);
    
    // 将函数作用于每个项目
    void Traverse(const List * plist , void (* fp)(Item item));
                  
    // 释放链表
    void EmptyTheList(List * plist);
    
    
    #endif /* list_h */
    
    

    list.c

    #include <stdio.h>
    #include <stdlib.h>
    #include "list.h"
    
    static void CopyToNode(Item item , Node * pnode);
    
    // 初始化列表
    void InitializeList(List * plist)
    {
        * plist = NULL;
    }
    
    // 列表是否为空
    bool ListIsEmpty(const List * plist)
    {
        if(* plist == NULL) return true;
        else return false;
    }
    
    // 列表是否已满,就是分配试试看行不行
    bool ListIsFull(const List * plist)
    {
        Node * pt ;
        bool full ;
        
        pt = (Node *) malloc(sizeof(Node));
        if (pt == NULL) full = true;
        else full = false;
        free(pt);
        return full;
    }
    
    // 获取总数
    unsigned int ListItemCount(const List * plist)
    {
        unsigned int count = 0 ;
        Node * pnode = * plist ;
        
        while (pnode != NULL) {
            ++count;
            pnode = pnode -> next ;
        }
        return count;
    }
    
    // 添加
    bool AddItem(Item item , List * plist){
        Node * pnew ;
        Node * scan = *plist;
        
        pnew = (Node *) malloc(sizeof(Node));
        if (pnew == NULL) {
            return false;
        }
        CopyToNode(item, pnew);
        pnew -> next = NULL;
        if (scan == NULL) {
            * plist = pnew ;
        }else{
            while (scan -> next != NULL) {
                scan = scan -> next ;
            }
            scan -> next = pnew ;
        }
        
        return true;
        
    }
    
    // 将函数作用于每个项目
    void Traverse(const List * plist , void (* fp)(Item item)){
        Node * pnode = * plist ;
        while (pnode != NULL) {
            (* fp)(pnode -> item);
            pnode = pnode -> next ;
        }
    }
    
    // 释放链表
    void EmptyTheList(List * plist){
        Node * psave ;
        while (* plist != NULL) {
            psave = (*plist) -> next ;
            free(*plist);
            *plist = psave ;
        }
    }
    
    static void CopyToNode(Item item , Node * pnode)
    {
        pnode -> item = item ;
    }
    
    

    mian.c

    #include <stdio.h>
    #include <stdlib.h>
    #include "list.h"
    void showmoview(Item item);
    
    // 使用链表存储数据
    int main(int argc, const char * argv[]) {
        List movies ;
        Item temp ;
        // 初始化
        InitializeList(&movies);
        if (ListIsFull(&movies)) {
            fprintf(stderr, "list is full , bye!");
            exit(1);
        }
        // 录入
        puts("enter movie title:");
        while (gets(temp.title) != NULL && temp.title[0] != '') {
            puts("enter your rating <0-10> :");
            scanf("%d",&temp.rating);
            while (getchar() != '
    ') {
                continue;
            }
            if (AddItem(temp,&movies) == false) {
                fprintf(stderr, "Error allocating momory 
    ");
                break;
            }
            if (ListIsFull(&movies)) {
                puts("The list is full 
    ");
                break;
            }
            puts("enter next move title");
        }
        // 输出
        if (ListIsEmpty(&movies)) {
            puts("No data entered.");
        }else{
            printf("Here is the movie list : 
    ");
            Traverse(&movies, showmoview);
        }
        printf("You entered %d movies. 
    ",ListItemCount(&movies));
        // 释放
        EmptyTheList(&movies);
        printf("Bye! 
    ");
          return 0;
    }
    
    void showmoview(Item item)
    {
        printf("Movie: %s Rating: %d 
    ",item.title,item.rating);
    }
    
    
  • 相关阅读:
    给网站添加icon图标
    jquery动态改变元素内容
    jQuery判断checkbox是否选中的3种方法
    jquery datatable设置垂直滚动后,表头(th)错位问题
    网页弹出[Object HTMLDivElement],怎么取值?
    JS获取元素属性和自定义属性
    Thymeleaf显示Map集合数据
    带搜索框的select下拉框
    JQuery获取浏览器窗口的高度和宽度
    鼠标悬停在一个标签上时,显示一段文字
  • 原文地址:https://www.cnblogs.com/hiqianqian/p/6852921.html
Copyright © 2011-2022 走看看