C language-container implementation.

Source: Internet
Author: User

* Please refer to this document for reference from blog.csdn.net/wtz1985

I believe everyone is familiar with the container, especially Java or C #. It is just an intermediate interface, that is, a pure virtual class. Its implementation can be achieved through a linked list or array structure. Its appearance is to allow other function bodies to freely choose their own implementation structure. For example, hash tables, stacks, and other structures can be implemented by using linked lists or dynamic arrays. If there is no intermediate interface and you want to implement it separately, this is a waste of time and space.

1. interface definition.

  1. /* ----- Container. h -------*/
  2. # Ifndef _ container_h
  3. # DEFINE _ container_h
  4. Struct _ container;
  5. Typedef struct _ container;
  6. Yptedef void (* foreachfunc) (void * data, void * CTX );
  7. Typedef void * (* containerappendfunc) (container * thiz, void * data );
  8. Typedef void * (* containerinsertfunc) (container * thiz, void * data, size * index );
  9. Typedef void * (* containerdeletefunc) (container * thiz, size_t index );
  10. Typedef void (* containerdestroyfunc) (container * thiz );
  11. Typedef void (* containerforeachfunc) (container * thiz, foreachfunc print, void * CTX );
  12. Struct _ container
  13. {
  14. Containerappendfunc container_append;
  15. Containerinsertfunc container_insert;
  16. Containerdeletefunc container_delete;
  17. Containerdestroyfunc container_func;
  18. Containrforeachfunc container_foreach;
  19. Char priv [0];
  20. };
  21. Static inline void * container_append (container * thiz, void * Data)
  22. {
  23. Assert (thiz! = NULL & thiz-> container_append! = NULL );
  24. Return thiz-> container_append (thiz, data );
  25. }
  26. Static inline void * container_insert (container * thiz, void * data, size_t index)
  27. {
  28. Assert (thiz! = NULL & thiz-> container_insert! = NULL );
  29. Return thiz-> container_insert (thiz, Data, index );
  30. }
  31. Static inline void * container_delete (container * thiz, size_t index)
  32. {
  33. Assert (thiz! = NULL & thiz-> container_delete! = NULL );
  34. Return thiz-> container_delete (thiz, index );
  35. }
  36. Static inline void container_destroy (container * thiz)
  37. {
  38. Assert (thiz! = NULL & thiz-> container_destroy! = NULL );
  39. Return thiz-> container_destroy (thiz );
  40. }
  41. Static inline void container_foreach (container * thiz, foreachfunc print, void * CTX)
  42. {
  43. Assert (thiz! = NULL & thiz-> container_foreach! = NULL );
  44. Return thiz-> container_foreach (thiz, print, CTX );
  45. }
  46. # Endif/_ container_h/

2. Different interface implementations:

Linked List:

  1. /* ------- Container_list.h ---------*/
  2. # Include "container. H"
  3. # Include "Dlist. H"
  4. # Ifndef _ container_list_h
  5. # DEFINE _ container_list_h
  6. Container * container_list_create (Dlist * List );
  7. # Endif/* _ container_h */

Dynamic Array:

  1. /* --------- Container_vector.h -----------*/
  2. # Include "vector. H"
  3. # Include "container. H"
  4. # Ifndef _ container_vector_h
  5. # DEFINE _ container_vector_h
  6. Container * container_vector_create (vector * thiz );
  7. # Endif/* container_vector_h */

With these interfaces, you don't have to write any more for specific implementation. I believe everyone should know about them. If there is something bad to write, please advise.

 

~~ End ~~

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.