Implementation of ADT List

Source: Internet
Author: User

Header file List.h

#include <stdbool.h>#ifndef List_h_#defineList_h_#defineT_size 41structfilm{CharName[t_size]; intratting;}; typedefstructfilm Item;structnode{Item data; structNode *next;}; typedefstructNode Node;typedef node*List;voidInitlist (List *plist);BOOLListisempty (ConstList *plist);BOOLListisfull (ConstList *plist); unsignedintListitemcount (ConstList *plist);BOOLAddItem (item item, List *plist);//void Traverse (const List * plist, Void (*pfun) (item item));BOOLEmptythelist (List *plist);#endif

Implementation of List list.c file

#include <stdio.h>#include<stdlib.h>#include"list.h"voidInitlist (List *plist) {    *plist =NULL;}BOOLListisempty (ConstList *plist) {    if(*plist = =NULL)return true; Else        return false;}BOOLListisfull (ConstList *plist) {Node*pt; BOOLFull ; PT= (Node *)malloc(sizeof(Node)); if(pt = =NULL) full=true; Else Full=false;  Free(PT); returnFull ;} unsignedintListitemcount (ConstList *plist) {unsignedintCount =0; Node*temp; Temp= *plist;  while(Temp! =NULL) {Count++; Temp= TempNext; }    returncount;}BOOLAddItem (item item, List *plist) {    BOOLresult =false; Node*temp; Node*F; Temp= (Node *)malloc(sizeof(Node)); if(temp = =NULL) {         Free(temp); return false; } temp-Data =item; TempNext =NULL; if(*plist = =NULL)*plist =temp; Else{f= *plist;  while(F-Next! =NULL) F= fNext; FNext =temp; } f=NULL; Temp=NULL; return true;}BOOLEmptythelist (List *plist) {Node*F; Node*p; P= *plist; F= P-Next;  Free(P);  while(F! =NULL) {P=F; F= fNext;  Free(P); } P=NULL; F=NULL; *plist =NULL; return true;}

MAIN.C file:

#include <stdio.h>#include<string.h>#include"list.h"#defineCOUNT 10intMainvoid) {List L;    Item item; unsignedintCount =0; inti; Initlist (&l); strcpy (Item.name,"Su7"); Item.ratting=8;  for(i =0; i < COUNT; i++) AddItem (item,&l); Count= Listitemcount (&l); printf ("Count is%u.\n", Count); return 0;}

Implementation of ADT List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.