ex14-1.c
/* code: ex14-1.c (v1.20.00) */
#include<stdio.h>
#include<stdlib.h>
#define STACK_UNDERFLOW (-1)
#define DATA_SIZE 6
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
void stack_push (NODE_TYPE ** head, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
/* ------------------------------------------- */
int stack_pop (NODE_TYPE ** head) {
int data;
NODE_TYPE *temp;
if (*head == NULL) {
return STACK_UNDERFLOW;
}
data = (*head)->data;
temp = (*head);
*head = (*head)->next;
free (temp);
return data;
}
/* ------------------------------------------- */
void stack_print (NODE_TYPE * head) {
if (head == NULL) {
printf ("stack is empty.\n");
return;
}
printf ("stack [ ");
while (NULL != head) {
printf ("%02d ", head->data);
head = head->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *stack;
int i, data1;
stack = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("push: ");
printf ("%02d\n", data1);
stack_push (&stack, data1);
}
stack_print (stack);
for (i = 0; i < DATA_SIZE / 2; i++) {
printf ("pop: ");
data1 = stack_pop (&stack);
printf ("%02d\n", data1);
}
stack_print (stack);
return 0;
}
ex14-2.c
/* code: ex14-2.c (v1.20.00) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DATA_SIZE 6
#define QUEUE_EMPTY (-1)
struct node {
int data;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
void q_enque (NODE_TYPE ** front, NODE_TYPE ** rear, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->next = NULL;
if (*rear == NULL) {
*front = *rear = new_node;
}
else {
(*rear)->next = new_node;
*rear = new_node;
}
}
/* ------------------------------------------- */
int q_dequeue (NODE_TYPE ** front, NODE_TYPE ** rear) {
int data;
NODE_TYPE *temp;
if (*front == NULL) {
return QUEUE_EMPTY;
}
temp = *front;
data = (*front)->data;
if (*front == *rear) {
*front = *rear = NULL;
}
else {
*front = (*front)->next;
}
free (temp);
return data;
}
/* ------------------------------------------- */
void q_print (NODE_TYPE * front) {
printf ("queue [ ");
while (front != NULL) {
printf ("%02d ", front->data);
front = front->next;
}
printf ("]\n");
}
/* ------------------------------------------- */
int main () {
int i, data1;
NODE_TYPE *front, *rear;
front = NULL;
rear = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data1 = (int) rand () % 100;
printf ("enqueue: ");
printf ("%02d\n", data1);
q_enque (&front, &rear, data1);
}
q_print (front);
for (i = 0; i < DATA_SIZE / 2; i++) {
printf ("dequeue: ");
data1 = q_dequeue (&front, &rear);
printf ("%02d\n", data1);
}
q_print (front);
return 0;
}
q14-1.c
/* code: q14-1.c (v1.20.00) */
#include<stdio.h>
#include<stdlib.h>
#define DATA_SIZE 6
/* doubly linked list */
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
void dll_print_head (NODE_TYPE * head) {
NODE_TYPE *temp;
temp = head;
if (temp == NULL) {
printf ("List is empty\n");
return;
}
printf ("print (head): ");
while (temp->next != NULL) {
printf ("%d ", temp->data);
temp = temp->next;
}
printf ("%d \n", temp->data);
}
/* ------------------------------------------- */
void dll_print_tail (NODE_TYPE * tail) {
NODE_TYPE *temp;
temp = tail;
if (temp == NULL) {
printf ("List is empty\n");
return;
}
printf ("print (tail): ");
while (temp->prev != NULL) {
printf ("%d ", temp->data);
temp = temp->prev;
}
printf ("%d \n", temp->data);
}
/* ------------------------------------------- */
void dll_insert_head (NODE_TYPE ** head, NODE_TYPE ** tail, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->prev = NULL;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
*tail = *head;
}
else {
new_node->next = *head;
(*head)->prev = new_node;
*head = new_node;
}
}
/* ------------------------------------------- */
void dll_insert_tail (NODE_TYPE ** head, NODE_TYPE ** tail, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->prev = NULL;
new_node->next = NULL;
if (*head == NULL) {
*head = new_node;
new_node = *head;
}
else {
(*tail)->next = new_node;
new_node->prev = *tail;
*tail = new_node;
}
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
NODE_TYPE *tail;
int i, data;
head = NULL;
tail = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data = (int) rand () % 100;
printf ("adding (head): ");
printf ("%02d\n", data);
dll_insert_head (&head, &tail, data);
}
dll_print_head (head);
dll_print_tail (tail);
for (i = 0; i < DATA_SIZE; i++) {
data = (int) rand () % 100;
printf ("adding (tail): ");
printf ("%02d\n", data);
dll_insert_tail (&head, &tail, data);
}
dll_print_head (head);
dll_print_tail (tail);
return 0;
}
q14-2.c
/* code: q14-2.c (v1.20.00) */
#include<stdio.h>
#include<stdlib.h>
#define DATA_SIZE 6
/* circular doubly linked list */
struct node {
int data;
struct node *prev;
struct node *next;
};
typedef struct node NODE_TYPE;
/* ------------------------------------------- */
void cdll_print_head (NODE_TYPE * head, NODE_TYPE * tail) {
if ((head == tail) && (head == NULL)) {
printf ("List is empty\n");
return;
}
printf ("print (head): ");
while (head->next != tail->next) {
printf ("%d ", head->data);
head = head->next;
}
printf ("%d \n", head->data);
}
/* ------------------------------------------- */
void cdll_print_tail (NODE_TYPE * head, NODE_TYPE * tail) {
if ((head == tail) && (head == NULL)) {
printf ("List is empty\n");
return;
}
printf ("print (tail): ");
while (head->prev != tail->prev) {
printf ("%d ", tail->data);
tail = tail->prev;
}
printf ("%d \n", tail->data);
}
/* ------------------------------------------- */
void cdll_insert_head (NODE_TYPE ** head, NODE_TYPE ** tail, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->prev = NULL;
new_node->next = NULL;
if ((*head == *tail) && (*head == NULL)) {
*head = new_node;
*tail = new_node;
(*head)->prev = NULL;
(*head)->next = NULL;
(*tail)->prev = NULL;
(*tail)->next = NULL;
}
else {
new_node->next = *head;
(*head)->prev = new_node;
*head = new_node;
(*head)->prev = *tail;
(*tail)->next = *head;
}
}
/* ------------------------------------------- */
void cdll_insert_tail (NODE_TYPE ** head, NODE_TYPE ** tail, int data) {
NODE_TYPE *new_node;
new_node = malloc (sizeof (NODE_TYPE));
new_node->data = data;
new_node->prev = NULL;
new_node->next = NULL;
if ((*head == *tail) && (*head == NULL)) {
*head = new_node;
*tail = new_node;
(*head)->prev = NULL;
(*head)->next = NULL;
(*tail)->prev = NULL;
(*tail)->next = NULL;
}
else {
(*tail)->next = new_node;
new_node->prev = *tail;
*tail = new_node;
(*head)->prev = *tail;
(*tail)->next = *head;
}
}
/* ------------------------------------------- */
int main () {
NODE_TYPE *head;
NODE_TYPE *tail;
int i, data;
head = NULL;
tail = NULL;
for (i = 0; i < DATA_SIZE; i++) {
data = (int) rand () % 100;
printf ("adding (head): ");
printf ("%02d\n", data);
cdll_insert_head (&head, &tail, data);
}
cdll_print_head (head, tail);
cdll_print_tail (head, tail);
for (i = 0; i < DATA_SIZE; i++) {
data = (int) rand () % 100;
printf ("adding (tail): ");
printf ("%02d\n", data);
cdll_insert_tail (&head, &tail, data);
}
cdll_print_head (head, tail);
cdll_print_tail (head, tail);
return 0;
}
EOF