C 语言 实现 List

list.h

cpp 复制代码
#ifndef LIST_H_
#define LIST_H_

#include <stdbool.h>

#define TSIZE 45

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(*pfun)(Item item));
void EmptyTheList(List *plist);




#endif

list.c

cpp 复制代码
#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(*pfun)(Item item))
{
    Node *pnode = *plist;
    while(pnode != NULL)
    {
        (*pfun)(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;
}

main.c

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
#include <string.h>
void showmovies(Item item);
char *s_gets(char *st, int n);

int main(void)
{
    List movies;
    Item temp;

    InitializeList(&movies);
    if(ListIsFull(&movies))
    {
        fprintf(stderr, "No memory available! Bye!\n");
        exit(1);
    }

    puts("Enter first movie title: ");
    while(s_gets(temp.title, TSIZE) != 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, "Problem allocating memory\n");
            break;
        }

        if(ListIsFull(&movies))
        {
            puts("The list is now full.");
            break;
        }
        puts("Enter next movie title (empty line to stop): ");
    }

    if(ListIsEmpty(&movies))
    {
        printf("No data entered. ");
    }
    else
    {
        printf("Here is the movie list: \n");
        Traverse(&movies, showmovies);
    }

    printf("You entered %d movies.\n", ListItemCount(&movies));

    EmptyTheList(&movies);
    printf("Bye!\n");

    return 0;
}

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

char *s_gets(char *st, int n)
{
    char *ret_val;
    char *find;
    ret_val = fgets(st, n, stdin);
    if(ret_val)
    {
        find = strchr(st, '\n');
        if(find)
        {
            *find = '\0';
        }
        else
        {
            while(getchar() != '\n') continue;
        }
    }
    return ret_val;
}
相关推荐
西猫雷婶8 分钟前
python学opencv|读取图像(四十三)使用cv2.bitwise_and()函数实现图像按位与运算
开发语言·python·opencv
C++小厨神13 分钟前
C#语言的函数实现
开发语言·后端·golang
qwe35263314 分钟前
自定义数据集使用scikit-learn中的包实现线性回归方法对其进行拟合
开发语言·python
S-X-S23 分钟前
OpenAI模块重构
开发语言·重构·openai
计算机-秋大田36 分钟前
基于JAVA的微信点餐小程序设计与实现(LW+源码+讲解)
java·开发语言·后端·微信·小程序·课程设计
llp111041 分钟前
基于java线程池和EasyExcel实现数据异步导入
java·开发语言
四念处茫茫1 小时前
【C语言系列】深入理解指针(3)
c语言·开发语言·visual studio
漫漫进阶路6 小时前
VS C++ 配置OPENCV环境
开发语言·c++·opencv
BinaryBardC7 小时前
Swift语言的网络编程
开发语言·后端·golang
code_shenbing8 小时前
基于 WPF 平台使用纯 C# 制作流体动画
开发语言·c#·wpf