C语言链表实现-雏形

本文介绍了C语言中链表的基础实现,探讨了链表的数据结构和基本操作。
摘要由CSDN通过智能技术生成

C语言链表实现-雏形

第一次写文章,“不要使用默认标题”,刚开始一直没发现,代码高亮也找了一会,好神奇。
下面通过一个简单的数据储存来实现C的链表雏形--蒟蒻,大佬不要嘲笑,欢迎批评。
其实不完全是我的,主要来源《C Prime Plus》,我也想点转载,好像很麻烦,这里说明下。
//the headfile 
//尝试下用英语写注释
//今天还学到了多文件编译
#pragma once//make sure that the headfile will be complied once
#ifndef LIST_H_// if not def

#define LIST_H_
#include <stdbool.h>

#define TSIZE 45

struct film {	//the type of item the list store
	char title[TSIZE]; //the max number of the movie's name
	int rating; //the rank of movie
};

typedef struct film Item; //for convenience of changing the type

typedef struct node {  //the part of list
	Item item;
	struct node* next;
}Node;

typedef Node* List; //this means create a list,not a part

void InitializeList(List* plist); // initialize the list

bool ListIsEmpty(const List* plist); // is the list empty or not?

bool ListIsFull(const List* plist); //is it full?

unsigned int ListItemCount(const List* plist); //count the number of part

bool AddItem(Item item, List* plist); //add a item 

void Traverse(const List* plist, void(*pfun)(Item item)); //a function operated on list

void EmptyTheList(List* plist); // to clear the list

#endif

```c
//the c_file includes main()
#include <stdio.h>
#include <stdlib.h>
#include "headfile.h"
void showmovies(Item item);// to display the item of list
char* s_gets(char* st, int n); // the improved gets
int main(void)
{
	List movies;	//define a list
	Item temp;		//define a part

	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_s("%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 have entered %d movies.\n", ListItemCount(&movies));

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

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

char* s_gets(char* st, int n) {
	char* val, * find;
	val = fgets(st, n, stdin);
	if (val) {
		find = strchr(st, '\n');
		if (find)
			*find = '\0';
		else
			while (getchar() != '\n')
				continue;
	}
	return val;
}
//the c_file realizes functions
#include <stdio.h>
#include <stdlib.h>
#include "headfile.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));//does the compiler would like to give me a memory?
	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=NULL;
	while (*plist != NULL) {
		psave = (*plist)->next;
		free(*plist);
		*plist = psave;
	}
}

static void CopyToNode(Item item, Node* pnode) {
	pnode->item = item;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值