第一次写文章,“不要使用默认标题”,刚开始一直没发现,代码高亮也找了一会,好神奇。
下面通过一个简单的数据储存来实现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 45struct film {//the type of item the list storechar title[TSIZE];//the max number of the movie's nameint rating;//the rank of movie};typedefstruct film Item;//for convenience of changing the typetypedefstruct node {//the part of list
Item item;struct node* next;}Node;typedef Node* List;//this means create a list,not a partvoidInitializeList(List* plist);// initialize the list
bool ListIsEmpty(const List* plist);// is the list empty or not?
bool ListIsFull(const List* plist);//is it full?unsignedintListItemCount(const List* plist);//count the number of part
bool AddItem(Item item, List* plist);//add a item voidTraverse(const List* plist,void(*pfun)(Item item));//a function operated on listvoidEmptyTheList(List* plist);// to clear the list#endif
```c
//the c_file includes main()#include<stdio.h>#include<stdlib.h>#include"headfile.h"voidshowmovies(Item item);// to display the item of listchar*s_gets(char* st,int n);// the improved getsintmain(void){
List movies;//define a list
Item temp;//define a partInitializeList(&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");return0;}voidshowmovies(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';elsewhile(getchar()!='\n')continue;}return val;}
C语言链表实现-雏形第一次写文章,“不要使用默认标题”,刚开始一直没发现,代码高亮也找了一会,好神奇。下面通过一个简单的数据储存来实现C的链表雏形--蒟蒻,大佬不要嘲笑,欢迎批评。//the headfile //尝试下用英语写注释//今天还学到了多文件编译#pragma once//make sure that the headfile will be complied once...