/*
* Элемент двухсвязного циклического списка содержит указатель на строку.
* Строки упорядоченны по возрастанию. Вставить строку в список с сохранением
* упорядоченности. В список помещается копия входной строки в динамической памяти.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
const char *list_content[] =
{
"Copyright (c) The Regents of the University of California.",
"All rights reserved.",
"",
"Redistribution and use in source and binary forms, with or without",
"modification, are permitted provided that the following conditions",
"are met:",
"1. Redistributions of source code must retain the above copyright",
" notice, this list of conditions and the following disclaimer.",
"2. Redistributions in binary form must reproduce the above copyright",
" notice, this list of conditions and the following disclaimer in the",
" documentation and/or other materials provided with the distribution.",
"3. Neither the name of the University nor the names of its contributors",
" may be used to endorse or promote products derived from this software",
" without specific prior written permission.",
"",
"THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND",
"ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE",
"IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE",
"ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE",
"FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL",
"DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS",
"OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)",
"HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT",
"LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY",
"OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF",
"SUCH DAMAGE."
};
typedef struct list_node list_node_t;
struct list_node {
char *str;
list_node_t *prev, *next;
};
/* создаёт новый узел списка */
list_node_t *node_alloc(const char *str) {
list_node_t *res = (list_node_t *)malloc(sizeof(list_node_t));
if (!res)
return NULL;
memset(res, 0, sizeof(list_node_t));
res->str = strdup(str);
if (!res->str) {
free(res);
res = NULL;
}
return res;
}
/* вставляет nn до current */
void insert_before(list_node_t *current, list_node_t *nn) {
if (!(current && nn))
return;
nn->prev = current->prev;
nn->next = current;
if (current->prev) {
current->prev->next = nn;
}
current->prev = nn;
}
/* вставляет nn после current */
void insert_after(list_node_t *current, list_node_t *nn) {
if (!(current && nn))
return;
nn->next = current->next;
nn->prev = current;
if (current->next) {
current->next->prev = nn;
}
current->next = nn;
}
/* освобождает занятую элементом пямять, оставляет список связанным */
void free_node(list_node_t *node) {
if (!node)
return;
free(node->str);
if (node->next)
node->next->prev = node->prev;
if (node->prev)
node->prev->next = node->next;
free(node);
}
/* удаляет элемены от текущего элемента к хвосту*/
void free_forward(list_node_t *node) {
while (node) {
list_node_t *next = node->next;
free_node(node);
node = next;
}
}
/* удаляет элементы от текущего элемента к голове */
void free_revers(list_node_t *node) {
while (node) {
list_node_t *prev = node->prev;
free_node(node);
node = prev;
}
}
/* удаляет список */
void free_list(list_node_t *node) {
if (!node)
return;
free_forward(node->next);
free_revers(node->prev);
free_node(node);
}
int main()
{
list_node_t *hdr = NULL, /* голова */
*current, /* курсор */
*tail; /* хвост */
size_t i;
for (i = 0; i < sizeof(list_content)/sizeof(const char *); ++i) {
list_node_t * nn = node_alloc(list_content[i]);
if (!hdr) {
hdr = tail = nn;
} else {
nn->prev = tail;
tail->next = nn;
tail = nn;
}
}
hdr->prev = tail;
tail->next = hdr;
/* вперёд */
current = hdr;
while (current->next != hdr) {
printf("%s\n", current->str);
current = current->next;
}
/* назад */
current = tail;
while (current->prev != tail) {
printf("%s\n", current->str);
current = current->prev;
}
/* вставим строчки после пустых строчек */
current = hdr;
while (current->next != hdr) {
if (strlen(current->str) == 0) {
list_node_t * nn = node_alloc("после пустой строки");
if (nn) {
insert_after(current, nn);
/* перешагиваем через только что созданный элемент */
current = nn;
}
}
current = current->next;
}
/* вперёд */
current = hdr;
while (current->next != hdr) {
printf("%s\n", current->str);
current = current->next;
}
/* назад */
current = tail;
while (current->prev != tail) {
printf("%s\n", current->str);
current = current->prev;
}
hdr->prev = NULL;
tail->next = NULL;
free_list(hdr);
return 0;
}