Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ insque(3C) — DG/UX 4.30

Media Vault

Software Library

Restoration Projects

Artifacts Sought



     insque(3C)                 DG/UX 4.30                  insque(3C)



     NAME
          insque - Add an element to a circular queue.

     SYNOPSIS
          struct qelem {
           struct qelem *qforw;
           struct qelem *qback;
          /* User data follows */
          };

          int insque();
          struct qelem *elem, *pred;
          insque(elem, pred);


          where   elem is a pointer to the structure to add to a
                  linked list.
                  pred is a pointer to an element structure that the
                  pointer elem is added immediately after.


     DESCRIPTION
          The insque function inserts an element into a circular
          linked list; this function comes from the University of
          California Berkeley UNIX (BSD) system.

          The structures in the linked list must reserve the first two
          double words for use as the forward and backward pointers.
          Since the linked list is a circular list, the initial
          element must have forward and backward links to itself.

     RETURNS
          The insque function does not return a value.

     SEE ALSO
          See also the remque function.

     EXAMPLE
          This program reads lines from standard input, creates a
          linked list with insque, and prints out the elements.
          /* Program test for the insque() function */

          #include <stdio.h>

          #define LSIZE 256       /* line size */

          extern char  *malloc();
          extern int   insque(), remque();
          extern void  free();

          struct qelem {
              struct qelem *q_forw;       /* forward link */



     Licensed material--property of copyright holder(s)         Page 1





     insque(3C)                 DG/UX 4.30                  insque(3C)



              struct qelem *q_back;       /* backward link */
              char buffer[LSIZE];         /* line buffer */
          };

          struct qelem head = {&head, &head}; /* head of queue */
          struct qelem *p_last = &head;   /* last item */
          struct qelem *p_line;           /* ptr to walk list */
          struct qelem *p_next;           /* next item in list */

          main() {
              for (;;) {
                  p_line = (struct qelem *)
                           malloc(sizeof(struct qelem));
                  if (p_line == (struct qelem *)0) {
                      printf("Out of memory.\n");
                      exit(1);
                  }
                  if (!fgets(p_line -> buffer, LSIZE, stdin))
                      break;      /* End of file found */
                  (void) insque(p_line, p_last);
                  p_last = p_line;
              }
              free((char *)p_line);

              /* Now walk list and print elements */

              for (p_line = head.q_forw; p_line != &head;
                   p_line = p_next) {
                  p_next = p_line -> q_forw;
                  (void) remque(p_line);
                  fputs(p_line -> buffer, stdout);
                  free((char *)p_line);
              }
              return 0;
          }




















     Licensed material--property of copyright holder(s)         Page 2



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026