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\n",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: \n",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] != '\0') {
        puts("enter your rating <0-10> :");
        scanf("%d",&temp.rating);
        while (getchar() != '\n') {
            continue;
        }
        if (AddItem(temp,&movies) == false) {
            fprintf(stderr, "Error allocating momory \n");
            break;
        }
        if (ListIsFull(&movies)) {
            puts("The list is full \n");
            break;
        }
        puts("enter next move title");
    }
    // 输出
    if (ListIsEmpty(&movies)) {
        puts("No data entered.");
    }else{
        printf("Here is the movie list : \n");
        Traverse(&movies, showmoview);
    }
    printf("You entered %d movies. \n",ListItemCount(&movies));
    // 释放
    EmptyTheList(&movies);
    printf("Bye! \n");
      return 0;
}

void showmoview(Item item)
{
    printf("Movie: %s Rating: %d \n",item.title,item.rating);
}

posted @ 2017-05-14 16:43  张狂莫怪  Views(188)  Comments(0Edit  收藏  举报