Better design for a generic doubly linked list around which I plan to make different wrappers?

I am currently writing a C code for a doubly linked list(dll) around which I want to write wrapers for implementing stack, queues etc. instead of writing separate codes for all of them.

I’ll be using void * for making it possible for the doubly linked list to work with any data type. The idea is basically the same as this implementation of stack using pointers in my github.

I am thinking of 2 different designs

  • Placing the size of elements alongwith the maintenance data. That would make coding easier but any instance of dll would be able to use only a particular data type.
  • Placing the size of element in the nodes. That would make it more general but I have concerns whether this approach would be easier to use?

This is the confusion that I wanted to clear before implementing any particular one. If there is any confusion regarding what I am trying to implement then please ask and I’ll clarify.

7

Let me start by answering your question:

Storing the element size per-node or per-list isn’t going to make the code any more or less complex one way or the other. The only real difference will be that anyone calling to add a node will have to pass a size once or each time, and the amount of storage required for each node may grow by an extra size_t‘s worth of bytes. The tradeoff between using less memory and having less flexibility versus using more memory and having more flexibility is something you’d have to evaluate on a case-by-case basis.

Let me finish by suggesting an alternative:

If you’re implementing this to be general-purpose tools along the lines of the STL or Java’s generic containers, I would suggest taking a completely different tack and not trying to do anything with the pointers you’re given. Just store them in your list and return them as it’s traversed.

Your allocate-and-copy approach goes on the assumption that what’s being stored are simple structures, which has a couple of pitfalls:

  • My structure may have additional pointers to allocated memory. If I’m using your list as primary storage for my data and you take it upon yourself to free() it when I call your cleanup() function, there’s no opportunity for me to free any of the additional memory unless I traverse the entire thing beforehand and do my own cleaning up. If that’s going to be the case, I might as well just do a traversal and remove the nodes one at a time. You could work around this by providing a disposal hook, which you’d call anytime you wanted to de-allocate a node.

  • The pointer I hand you may not be a pointer to a structure. For example, I may have a large block of memory and want to store a list of place markers or something which involves simply pointing at various places in the block. Your implementation requires that I give you a place to copy data from/to during push, pop and peek operations. I’d have to define another structure with a single member to hold my pointer value. That’s awkward and inconvenient. (You could, however, write a wrapper that does allocate-and-copy and uses the pointers-only implementation.)

8

Perhaps you should take a look at the intrusive linked list implementation used in the Linux kernel. The source can be browsed here.

An intrusive design has the advantage that any type can be placed into a list just by including a list element in the structure definition.

Disadvantages include slightly more cumbersome syntax (a matter of personal preference, of course) and the requirement to include multiple members of the same type when your data needs to go into more than one list.

Intrusive lists are mostly useful when you only need a small number of lists and especially when your module/app are focused on adding and removing nodes from one list. A different design might be more suitable if your code needs to deal with local or disposable lists. The former consideration is true for most uses of lists in the kernel.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật