Singly-linked tail queues

A singly-linked tail queue is headed by a structure defined by the STAILQ_HEAD macro. This structure contains a pair of pointers, one to the first element in the tail queue and the other to the last element in the tail queue. The elements are singly linked for minimum space and pointer manipulation overhead at the expense of 0(n) removal for arbitrary elements. New elements can be added to the tail queue after an existing element, at the head of the tail queue, or at the end of the tail queue. A STAILQ_HEAD structure is declared as follows:

    STAILQ_HEAD(HEADNAME, TYPE) head;

where HEADNAME is the name of the structure to be defined, and TYPE is the type of the elements to be linked into the tail queu. A pointer to the head of the tail queue can be later be declared as follows:

    struct HEADNAME *headp;

SINGLY-LINKED TAIL QUEUE EXAMPLE

    STAILQ_HEAD(stailhead, entry) head = STAILQ_HEAD_INITIALIZER(head);

struct stailhead *headp;    /* Singly-linked tail queue head*/

struct entry {

    ...

    STAILQ_ENTRY(entry); entries;    /*Tail queue.*/

    ...

} *n1, *n2, *n3, *np;

STAILQ_INIT(&head);    /*Initialize the queue*/

 

n1 = malloc(sizeof(struct entry));   /*Insert at the head*/

STAILQ_INSERT_HEAD(&head, n1, entries);

 

n1 = malloc(sizeof(struct entry));

STAILQ_INSERT_TAIL(&head, n1, entries);

 

n2 = malloc(sizeof(struct entry));

STAILQ_INSERT_AFTER(&head, n1, n2, entries);   /*Insert after ...*/

STAILQ_REMOVE(&head, n2, entry, entries);

free(n2);

 

     n3 = STAILQ_FIRST(&head);
     STAILQ_REMOVE_HEAD(&head, entries);
     free(n3);
                                             /* Forward traversal. */
     STAILQ_FOREACH(np, &head, entries)
             np-> ...
                                             /* Safe forward traversal. */
     STAILQ_FOREACH_SAFE(np, &head, entries, np_temp) {
             np->do_stuff();
             ...
             STAILQ_REMOVE(&head, np, entry, entries);
             free(np);
     }
                                             /* TailQ Deletion. */
     while (!STAILQ_EMPTY(&head)) {
             n1 = STAILQ_FIRST(&head);
             STAILQ_REMOVE_HEAD(&head, entries);
             free(n1);
     }
                                             /* Faster TailQ Deletion. */
     n1 = STAILQ_FIRST(&head);
     while (n1 != NULL) {
             n2 = STAILQ_NEXT(n1, entries);
             free(n1);
             n1 = n2;
     }
     STAILQ_INIT(&head);

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值