Hỏi đáp

Does C Have A ” Foreach C# Là Gì, Does C Have A Foreach Loop Construct – viettingame


We can also use macros for making code easier to read and write. For example we can implement macros for implementing the foreach construct in C for some data structures lượt thích singly- and doubly-linked lists, queues, etc.

Đang xem: Foreach c# là gì

Here is a small example.

#include #include struct LinkedListNode{ int data; struct LinkedListNode *next;};#define FOREACH_LIST(node, list) for (node=list; node; node=node->next)/* Usage */int main(void){ struct LinkedListNode *list, **plist = &list, *node; int i; for (i=0; idata = i; (*plist)->next = NULL; plist = &(*plist)->next; } /* printing the elements here */ FOREACH_LIST(node, list) { printf(“%d
“, node->data); }}You can make a standard interface for such data-structures and write a generic implementation of FOREACH as:

#include #include typedef struct CollectionItem_{ int data; struct CollectionItem_ *next;} CollectionItem;typedef struct Collection_{ /* interface functions */ void* (*first)(void *coll); void* (*last) (void *coll); void* (*next) (void *coll, CollectionItem *currItem); CollectionItem *collectionHead; /* Other fields */} Collection;/* must implement */void *first(void *coll){ return ((Collection*)coll)->collectionHead;}/* must implement */void *last(void *coll){ return NULL;}/* must implement */void *next(void *coll, CollectionItem *curr){ return curr->next;}CollectionItem *new_CollectionItem(int data){ CollectionItem *item = malloc(sizeof(CollectionItem)); item->data = data; item->next = NULL; return item;}void Add_Collection(Collection *coll, int data){ CollectionItem **item = &coll->collectionHead; while(*item) item = &(*item)->next; (*item) = new_CollectionItem(data);}Collection *new_Collection(){ Collection *nc = malloc(sizeof(Collection)); nc->first = first; nc->last = last; nc->next = next; return nc;}/* generic implementation */#define FOREACH(node, collection) for (node = (collection)->first(collection); node != (collection)->last(collection); node = (collection)->next(collection, node))int main(void){ Collection *coll = new_Collection(); CollectionItem *node; int i; for(i=0; idata); }}To use this generic implementation just implement these functions for your data structure.

Xem thêm: Review Phần Mềm Simple Uid Miễn Phí Quét Uid Trên Facebook, Phần Mềm Facebook Miễn Phí Quét Uid


Xem thêm: #1: Cách Sửa Lỗi Etd Control Center Là Gì, 【Phải Xem】Etd Control Center Là Gì

void* (*first)(void *coll);2. void* (*last) (void *coll);3. void* (*next) (void *coll, CollectionItem *currItem);


PDF – Tải về C Language for không lấy phí
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0

Về Viettingame.com

Viettingame.com - Chuyên trang web tổng hợp những thông tin hữu ích trên internet như thông tin về game, tin tổng hợp
Xem tất cả các bài viết của Viettingame.com →

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *