insque(3C)
_________________________________________________________________
insque function
Add an element to a circular queue.
_________________________________________________________________
Calling Sequence
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.
Related Functions
See also the remque function.
Example
This program reads lines from standard input, creates a linked
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
insque(3C)
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 */
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;
}
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)