list.h

#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

#include <stdio.h&gt;
#include <stdlib.h&gt;
#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-&gt;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

#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(&amp;movies);
    if(ListIsFull(&amp;movies))
    {
        fprintf(stderr, "No memory available! Bye!n");
        exit(1);
    }

    puts("Enter first movie title: ");
    while(s_gets(temp.title, TSIZE) != NULL &amp;&amp; temp.title[0] != '')
    {
        puts("Enter your rating <0-10>: ");
        scanf("%d", &amp;temp.rating);
        while(getchar() != 'n')
        {
            continue;
        }

        if(AddItem(temp, &movies) == false)
        {
            fprintf(stderr, "Problem allocating memoryn");
            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: %dn", 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 = '';
        }
        else
        {
            while(getchar() != 'n') continue;
        }
    }
    return ret_val;
}

原文地址:https://blog.csdn.net/Haku_yyf/article/details/134603805

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_20774.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注