inserthead(3C)
_________________________________________________________________
inserthead function
Insert an element at the head of a queue.
_________________________________________________________________
Calling Sequence
#include <queue.h>
struct queue que;
struct element *ptr;
ptr = inserthead(&que, ptr);
Description
The insert_head function inserts an element into the head of a
doubly-linked list. You must define your element in the program.
The first two double words of the element are reserved to hold
the forward and backward pointers. For example:
struct element1 {
int *forwardptr;
int *backwardptr;
char dataitem;
} *ptr;
The pointer that you pass to the insert_head function is the
pointer to the element that you define and declare in the
program.
The insert_head function, defined in the include file queue.h,
uses the hardware representation for queues. For more detail,
consult The ECLIPSE 32-Bit Systems Principles of Operation.
Returns
The insert_head function returns the pointer inserted.
Related Functions
See also the insert_tail, insert_before, insert_after, and
remove_head functions.
Example
/* Program to demonstrate insert_head and remove_head */
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
inserthead(3C)
/* functions. This program reads lines from standard */
/* input and inserts them at the head of the queue. */
/* After EOF, the program then removes elements from */
/* the head and prints them, effectively reversing */
/* the input lines. */
#include <stdio.h>
#include <queue.h>
char *malloc();
#define LSIZE 256 /* line size */
struct line {
struct element links; /* forward/backward links */
char buffer[LSIZE]; /* line buffer */
} *p_line;
struct queue head = {QNULL, QNULL}; /* head/tail of queue */
main() {
for(;;) {
p_line = (struct line *)malloc(sizeof(struct line));
if (p_line == (struct line *)0) {
printf("Out of memory.\n");
exit(1);
}
if(dg_fgets(p_line -> buffer, LSIZE, stdin) == (char)0)
break; /* end of file found */
(void) insert_head(&head, p_line); /* add to head */
}
free((char *)p_line);
/* Now remove elements from head and print each element */
while((p_line = remove_head(&head)) != (struct line *)0) {
fputs(p_line -> buffer, stdout);
free((char *) p_line);
}
exit(0);
}
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)