C数据结构-链表

博客提及了list.h、list.c、film.c文件,采用C11进行编译,并给出了运行结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. list.h 

/* 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;

/*操作:初始化一个链表*/
/*前提条件: plist指向一个链表*/
/*后置条件: 链表初始化为空*/
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_H_

2. list.c 

/*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;//空链表,所以把pnew放在链表的开头
    }
    else
    {
        while(scan->next != NULL)
        {
            scan = scan->next;//找到链表的末尾
        }
        scan->next = pnew;//把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;
}

3. film.c

/*film.c --使用抽象数据类型(ADT)风格的链表*/
/*与list.c一起编译*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

void show_movies(Item item);
char* s_gets(char *st, int n);

int main()
{
    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, show_movies);
    }
    printf("You entered %d movies.\n", ListItemCount(&movies));

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

    return 0;
}

void show_movies(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) //如果地址不是NUNLL,在此处放置一个空字符
        {
            *find = '\0';
        }
        else
        {
            while(getchar() != '\n')
            {
                continue;//处理输入行的剩余内容
            }
        }
    }

    return ret_val;
}

 

C11编译,运行结果:

Enter first movie title:
before you
Enter your rating <0-10>:
8
Enter next movie title(empty line to stop):
the pursuit to happiness
Enter your rating <0-10>:
9
Enter next movie title(empty line to stop):
pride and prejudice
Enter your rating <0-10>:
9
Enter next movie title(empty line to stop):

Here is the movie list:
Movie: before you Rating: 8
Movie: the pursuit to happiness Rating: 9
Movie: pride and prejudice Rating: 9
You entered 3 movies.
Bye!

 

面向对象程序设计课程作业 1. 请创建一个数据类型为T的链表类模板List,实现以下成员函数: 1) 默认构造函数List(),将该链表初始化为一个空链表(10分) 2) 拷贝构造函数List(const List& list),根据一个给定的链表构造当前链表(10分) 3) 析构函数~List(),释放链表中的所有节点(10分) 4) Push_back(T e)函数,往链表最末尾插入一个元素为e的节点(10分) 5) operator<<()友元函数,将链表的所有元素按顺序输出(10分) 6) operator=()函数,实现两个链表的赋值操作(10分) 7) operator+()函数,实现两个链表的连接,A=B+C(10分) 2. 请编写main函数,测试该类模板的正确性: 1) 用List模板定义一个List类型的模板类对象int_listB,从键盘读入m个整数,调用Push_back函数将这m个整数依次插入到该链表中;(4分) 2) 用List模板定义一个List类型的模板类对象int_listC,从键盘读入n个整数,调用Push_back函数将这n个整数依次插入到该链表中;(4分) 3) 用List模板定义一个List类型的模板类对象int_listA,调用List的成员函数实现A = B + C;(4分) 4) 用cout直接输出int_listA的所有元素(3分) 5) 用List模板定义List类型的模板类对象double_listA, double_listB, double_listC,重复上述操作。(15分) 3. 输入输出样例: 1) 输入样例 4 12 23 34 45 3 56 67 78 3 1.2 2.3 3.4 4 4.5 5.6 6.7 7.8 2) 输出样例 12 23 34 45 56 67 78 1.2 2.3 3.4 4.5 5.6 6.7 7.8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值