[comp.os.minix] insque

tholm@uvicctr.UUCP (Terrence W. Holm) (09/21/88)

EFTH MINIX report #48  - September 1988 -  insque(3) & remque(3)


There follows an implementation of insque(3) and remque(3)
for MINIX. Please consider this public domain software.
A "man" page is included.

NOTE: These subroutines are not available on all UNIX systems.

----------------------------------------------------------
echo x - insque.3
gres '^X' '' > insque.3 << '/'
XSUBROUTINES
X    insque(3)		- queue manipulation
X
XINVOCATION
X    struct queue
X      {
X      struct queue *q_next;
X      struct queue *q_last;
X      . . .
X      };
X
X    insque( element, where )
X      struct queue *element;
X      struct queue *where;
X
X    remque( element )
X      struct queue *element;
X
XEXPLANATION
X    Insque(3) and remque(3) are used to insert and remove elements
X    from a circular doubly linked list. <element> must point to a
X    structure which contains room for two pointers, as in the
X    "struct queue" prototype.
X
X    Insque(3) inserts <element> after the entry <where>, remque(3)
X    removes the <element>. A list starts with a header which has
X    both q_next and q_last pointing to itself.
X
XEXAMPLE
X    struct word
X      {
X      struct word *q_next, *q_last;
X      char *message;
X      };
X
X    main()
X      {
X      struct word *header, *first_element, *last_element;
X
X      /*  Construct a queue header.  */
X
X      header = (struct word *) malloc( sizeof(struct word) );
X      header->q_next = header->q_last = header;
X      header->message = "Header";
X
X      /*  Insert an element at the front of the queue.  */
X
X      first_element = (struct word *) malloc( sizeof(struct word) );
X      first_element->message = "Front";
X
X      insque( first_element, header );
X
X      /*  Insert an element at the tail of the queue.  */
X
X      last_element = (struct word *) malloc( sizeof(struct word) );
X      last_element->message = "Tail";
X
X      insque( last_element, header->q_last );
X
X      /*  Remove an element.  */
X
X      remque( first_element );
X
X      /*  The queue is now  "Header" <=> "Tail".  */
X      }
X
XREFERENCES
X    VAX Architecture Handbook
/
echo x - insque.c
gres '^X' '' > insque.c << '/'
X/*  insque(3) and remque(3)
X *
X *  Author: Terrence W. Holm          Sep. 1988
X */
X
Xstruct queue
X  {
X  struct queue *q_next;
X  struct queue *q_last;
X  };
X
X
Xinsque( element, where )
X  register struct queue *element;
X  register struct queue *where;
X
X  {
X  register struct queue *next = where->q_next;
X
X  element->q_next = next;
X  element->q_last = where;
X  next->q_last    = element;
X  where->q_next   = element;
X  }
X
X
Xremque( element )
X  register struct queue *element;
X
X  {
X  register struct queue *next = element->q_next;
X  register struct queue *last = element->q_last;
X
X  last->q_next = next;
X  next->q_last = last;
X  }
/
----------------------------------------------------------

		Edwin L. Froese
		  uw-beaver!ubc-cs!mprg!handel!froese

		Terrence W. Holm
		  uw-beaver!uvicctr!tholm