📄 int_list.c
字号:
/* Copyright (C) 2004,2005,2006 Bull S.A. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#if HAVE_CONFIG_H#include <config.h>#endif#include <stdlib.h>#include "modules.h"struct my_int_list { int value; struct my_int_list *next;};static struct my_int_list *my_int_list;static int counter = 0;int int_insert (struct my_int_list *prev, struct my_int_list *next, int value) { /* precondition : prev is not NULL */ struct my_int_list *new; new = (struct my_int_list *) malloc (sizeof (struct my_int_list)); if (new == NULL) { fprintf(stderr, "Error with malloc\n"); return -1; } new->value = value; new->next = next; prev->next = new; counter++; return 0;}static int init () { struct my_int_list *new; new = (struct my_int_list *) malloc (sizeof (struct my_int_list)); if (new == NULL) { fprintf(stderr, "Error with malloc\n"); return -1; } new->value = -1; new->next = NULL; my_int_list = new; return 0;}static int sorted_insert (int value) { /* sorted insertion */ /* precondition : the list has at least one element */ struct my_int_list *curr = my_int_list; struct my_int_list *prev = my_int_list; while (curr != NULL) { if (value == curr->value) return 0; /* value is already in the list */ if (value > curr->value) { prev = curr; curr = curr->next; } else return int_insert (prev, curr, value); /* insertion */ } /* last element of the list */ return int_insert (prev, curr, value);}static void display () { /* skip fictive element */ struct my_int_list *curr = my_int_list->next; printf (" %d : (", counter); while (curr != NULL) { printf(" %d", curr->value); curr = curr->next; } printf (" )\n"); }static void close () { struct my_int_list *curr = my_int_list; struct my_int_list *next; while (curr != NULL) { next = curr->next; free (curr); curr = next; } }struct int_list sorted_int_list = { .init = init, .insert = sorted_insert, .display = display, .close = close};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -